Skip to content

Commit

Permalink
Fixed mirrord ext
Browse files Browse the repository at this point in the history
  • Loading branch information
Razz4780 committed Jan 21, 2025
1 parent 75149ca commit 5a4735b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
9 changes: 5 additions & 4 deletions mirrord/cli/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
use mirrord_analytics::{AnalyticsError, AnalyticsReporter, Reporter};
use mirrord_config::{
config::ConfigError, feature::env::mapper::EnvVarsRemapper,
internal_proxy::MIRRORD_INTPROXY_CONNECT_TCP_ENV, LayerConfig,
internal_proxy::MIRRORD_INTPROXY_CONNECT_TCP_ENV, LayerConfig, MIRRORD_RESOLVED_CONFIG_ENV,
};
use mirrord_intproxy::agent_conn::AgentConnectInfo;
use mirrord_operator::client::OperatorSession;
Expand Down Expand Up @@ -269,7 +269,7 @@ impl MirrordExecution {

let stdout = proxy_process.stdout.take().expect("stdout was piped");

let address: SocketAddr = BufReader::new(stdout)
let intproxy_address: SocketAddr = BufReader::new(stdout)
.lines()
.next_line()
.await
Expand All @@ -288,9 +288,10 @@ impl MirrordExecution {
))
})?;

// Provide details for layer to connect to agent via internal proxy
config.connect_tcp = Some(format!("127.0.0.1:{}", address.port()));
config.connect_tcp.replace(intproxy_address.to_string());
config.update_env_var()?;
let config_as_env = config.to_env_var()?;
env_vars.insert(MIRRORD_RESOLVED_CONFIG_ENV.to_string(), config_as_env);

// Fixes <https://github.com/metalbear-co/mirrord/issues/1745>
// by disabling the fork safety check in the Objective-C runtime.
Expand Down
18 changes: 5 additions & 13 deletions mirrord/cli/src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use mirrord_analytics::{AnalyticsError, AnalyticsReporter, Reporter};
use mirrord_config::{LayerConfig, MIRRORD_CONFIG_FILE_ENV};
use mirrord_progress::{JsonProgress, Progress, ProgressTracker};

use crate::{config::ExtensionExecArgs, error::CliError, execution::MirrordExecution, CliResult};
use crate::{config::ExtensionExecArgs, execution::MirrordExecution, CliResult};

/// Actually facilitate execution after all preparations were complete
async fn mirrord_exec<P>(
Expand Down Expand Up @@ -40,23 +40,15 @@ where
pub(crate) async fn extension_exec(args: ExtensionExecArgs, watch: drain::Watch) -> CliResult<()> {
let progress = ProgressTracker::try_from_env("mirrord preparing to launch")
.unwrap_or_else(|| JsonProgress::new("mirrord preparing to launch").into());
let mut env: HashMap<String, String> = HashMap::new();

// Set environment required for `LayerConfig::from_env_with_warnings`.
if let Some(config_file) = args.config_file.as_ref() {
// Set canoncialized path to config file, in case forks/children are in different
// working directories.
let full_path = std::fs::canonicalize(config_file)
.map_err(|e| CliError::CanonicalizeConfigPathFailed(config_file.into(), e))?;
std::env::set_var(MIRRORD_CONFIG_FILE_ENV, full_path.clone());
env.insert(
MIRRORD_CONFIG_FILE_ENV.into(),
full_path.to_string_lossy().into(),
);
std::env::set_var(MIRRORD_CONFIG_FILE_ENV, config_file);
}
if let Some(target) = args.target.as_ref() {
std::env::set_var("MIRRORD_IMPERSONATED_TARGET", target.clone());
env.insert("MIRRORD_IMPERSONATED_TARGET".into(), target.to_string());
}

let (config, mut context) = LayerConfig::from_env_with_warnings()?;

let mut analytics = AnalyticsReporter::only_error(config.telemetry, Default::default(), watch);
Expand All @@ -76,7 +68,7 @@ pub(crate) async fn extension_exec(args: ExtensionExecArgs, watch: drain::Watch)
)
.await;
#[cfg(not(target_os = "macos"))]
let execution_result = mirrord_exec(env, config, progress, &mut analytics).await;
let execution_result = mirrord_exec(Default::default(), config, progress, &mut analytics).await;

if execution_result.is_err() && !analytics.has_error() {
analytics.set_error(AnalyticsError::Unknown);
Expand Down
8 changes: 4 additions & 4 deletions mirrord/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,15 +345,15 @@ impl LayerConfig {

/// Given a [`LayerConfig`], serialise it and convert to base 64 so it can be
/// set into [`MIRRORD_RESOLVED_CONFIG_ENV`].
fn to_env_var(&self) -> Result<String, ConfigError> {
pub fn to_env_var(&self) -> Result<String, ConfigError> {
let serialized = serde_json::to_string(self)
.map_err(|error| ConfigError::EnvVarEncodeError(error.to_string()))?;
Ok(BASE64_STANDARD.encode(serialized))
}

/// Given the encoded config as a string, set it into [`MIRRORD_RESOLVED_CONFIG_ENV`].
/// Must be used when updating [`LayerConfig`] after creation in order for the config
/// in env to reflect the change.
/// Encode this config with [`Self::to_env_var`] and set it into
/// [`MIRRORD_RESOLVED_CONFIG_ENV`]. Must be used when updating [`LayerConfig`] after
/// creation in order for the config in env to reflect the change.
pub fn update_env_var(&self) -> Result<(), ConfigError> {
std::env::set_var(MIRRORD_RESOLVED_CONFIG_ENV, self.to_env_var()?);
Ok(())
Expand Down

0 comments on commit 5a4735b

Please sign in to comment.