Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Self configure fixes #453

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/http/routers/v1/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub async fn handle_v1_docker_container_action(
DockerAction::Remove => format!("container remove --volumes {}", post.container),
DockerAction::Stop => format!("container stop {}", post.container),
};
let (output, _) = docker.command_execute(&docker_command, gcx.clone(), true).await
let (output, _) = docker.command_execute(&docker_command, gcx.clone(), true, false).await
.map_err(|e| ScratchError::new(StatusCode::INTERNAL_SERVER_ERROR, format!("Command {} failed: {}", docker_command, e)))?;

Ok(Response::builder().status(StatusCode::OK).body(Body::from(
Expand All @@ -87,7 +87,7 @@ pub async fn handle_v1_docker_container_list(
None => "container list --all --no-trunc --format json".to_string(),
};

let (unparsed_output, _) = docker.command_execute(&docker_command, gcx.clone(), true).await
let (unparsed_output, _) = docker.command_execute(&docker_command, gcx.clone(), true, false).await
.map_err(|e| ScratchError::new(StatusCode::INTERNAL_SERVER_ERROR, format!("Command {} failed: {}", docker_command, e)))?;

let mut output: Vec<Value> = unparsed_output.lines().map(|line| serde_json::from_str(line)).collect::<Result<Vec<_>, _>>()
Expand All @@ -113,7 +113,7 @@ pub async fn handle_v1_docker_container_list(
}

let inspect_command = format!("container inspect --format json {}", container_ids.join(" "));
let (inspect_unparsed_output, _) = docker.command_execute(&inspect_command, gcx.clone(), true).await
let (inspect_unparsed_output, _) = docker.command_execute(&inspect_command, gcx.clone(), true, false).await
.map_err(|e| ScratchError::new(StatusCode::INTERNAL_SERVER_ERROR, format!("Command {} failed: {}", inspect_command, e)))?;

let inspect_output = serde_json::from_str::<Vec<serde_json::Value>>(&inspect_unparsed_output)
Expand Down
45 changes: 15 additions & 30 deletions src/integrations/docker/docker_container_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,6 @@ pub struct Port {
pub target: String,
}

impl<'de> Deserialize<'de> for Port {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let s = String::deserialize(deserializer)?;
let (published, target) = s.split_once(':')
.ok_or_else(|| serde::de::Error::custom("expected format '8080:3000'"))?;
Ok(Port { published: published.to_string(), target: target.to_string() })
}
}

impl Serialize for Port {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&format!("{}:{}", self.published, self.target))
}
}

pub struct DockerContainerSession {
container_id: String,
connection: DockerContainerConnectionEnum,
Expand Down Expand Up @@ -171,7 +156,7 @@ pub async fn docker_container_check_status_or_start(

if !docker.settings_docker.command.is_empty() {
let cmd_to_execute = format!("exec --detach {} {}", container_id, docker.settings_docker.command);
match docker.command_execute(&cmd_to_execute, gcx.clone(), false).await {
match docker.command_execute(&cmd_to_execute, gcx.clone(), false, true).await {
Ok((cmd_stdout, cmd_stderr)) => { info!("Command executed: {cmd_stdout}\n{cmd_stderr}") },
Err(e) => { error!("Command execution failed: {}", e) },
};
Expand Down Expand Up @@ -257,7 +242,7 @@ async fn docker_container_create(
);

info!("Executing docker command: {}", &run_command);
let (run_output, _) = docker.command_execute(&run_command, gcx.clone(), true).await?;
let (run_output, _) = docker.command_execute(&run_command, gcx.clone(), true, true).await?;

let container_id = run_output.trim();
if container_id.len() < 12 {
Expand All @@ -282,8 +267,8 @@ async fn docker_container_sync_yaml_configs(
let temp_dir = tempfile::Builder::new().tempdir()
.map_err(|e| format!("Error creating temporary directory: {}", e))?;
let temp_dir_path = temp_dir.path().to_string_lossy().to_string();
docker.command_execute(&format!("container cp {temp_dir_path} {container_id}:{container_home_dir}/.cache/"), gcx.clone(), true).await?;
docker.command_execute(&format!("container cp {temp_dir_path} {container_id}:{container_home_dir}/.cache/refact"), gcx.clone(), true).await?;
docker.command_execute(&format!("container cp {temp_dir_path} {container_id}:{container_home_dir}/.cache/"), gcx.clone(), true, true).await?;
docker.command_execute(&format!("container cp {temp_dir_path} {container_id}:{container_home_dir}/.cache/refact"), gcx.clone(), true, true).await?;

let config_files_to_sync = ["privacy.yaml", "integrations.yaml", "bring-your-own-key.yaml", "competency.yaml"];
let remote_integrations_path = {
Expand All @@ -297,13 +282,13 @@ async fn docker_container_sync_yaml_configs(
_ => cache_dir.join(file).to_string_lossy().to_string(),
};
let container_path = format!("{container_id}:{container_home_dir}/.cache/refact/{file}");
docker.command_execute(&format!("container cp {local_path} {container_path}"), gcx.clone(), true).await?;
docker.command_execute(&format!("container cp {local_path} {container_path}"), gcx.clone(), true, true).await?;
}

// Copying config folder
let config_dir_string = config_dir.to_string_lossy().to_string();
docker.command_execute(&format!("container cp {temp_dir_path} {container_id}:{container_home_dir}/.config/"), gcx.clone(), true).await?;
docker.command_execute(&format!("container cp {config_dir_string} {container_id}:{container_home_dir}/.config/refact"), gcx.clone(), true).await?;
docker.command_execute(&format!("container cp {temp_dir_path} {container_id}:{container_home_dir}/.config/"), gcx.clone(), true, true).await?;
docker.command_execute(&format!("container cp {config_dir_string} {container_id}:{container_home_dir}/.config/refact"), gcx.clone(), true, true).await?;

Ok(())
}
Expand All @@ -314,7 +299,7 @@ async fn docker_container_get_home_dir(
gcx: Arc<ARwLock<GlobalContext>>,
) -> Result<String, String> {
let inspect_config_command = "container inspect --format '{{json .Config}}' ".to_string() + &container_id;
let (inspect_config_output, _) = docker.command_execute(&inspect_config_command, gcx.clone(), true).await?;
let (inspect_config_output, _) = docker.command_execute(&inspect_config_command, gcx.clone(), true, true).await?;

let config_json: serde_json::Value = serde_json::from_str(&inspect_config_output)
.map_err(|e| format!("Error parsing docker config: {}", e))?;
Expand All @@ -334,13 +319,13 @@ async fn docker_container_start(
container_id: &str,
) -> Result<(), String> {
let start_command = "container start ".to_string() + &container_id;
docker.command_execute(&start_command, gcx.clone(), true).await?;
docker.command_execute(&start_command, gcx.clone(), true, true).await?;

// If docker container is not running, print last lines of logs.
let inspect_command = "container inspect --format '{{json .State.Running}}' ".to_string() + &container_id;
let (inspect_output, _) = docker.command_execute(&inspect_command, gcx.clone(), true).await?;
let (inspect_output, _) = docker.command_execute(&inspect_command, gcx.clone(), true, true).await?;
if inspect_output.trim() != "true" {
let (logs_output, _) = docker.command_execute(&format!("container logs --tail 10 {container_id}"), gcx.clone(), true).await?;
let (logs_output, _) = docker.command_execute(&format!("container logs --tail 10 {container_id}"), gcx.clone(), true, true).await?;
return Err(format!("Docker container is not running: \n{logs_output}"));
}

Expand Down Expand Up @@ -387,7 +372,7 @@ async fn docker_container_sync_workspace(
tar_builder.finish().await.map_err(|e| format!("Error finishing tar archive: {}", e))?;

let cp_command = format!("container cp {} {}:{}", temp_tar_file.to_string_lossy(), container_id, container_workspace_folder.to_string_lossy());
docker.command_execute(&cp_command, gcx.clone(), true).await?;
docker.command_execute(&cp_command, gcx.clone(), true, true).await?;

let sync_files_post = SyncFilesExtractTarPost {
tar_path: container_workspace_folder.join(&tar_file_name).to_string_lossy().to_string(),
Expand Down Expand Up @@ -439,7 +424,7 @@ async fn docker_container_get_exposed_ports(
gcx: Arc<ARwLock<GlobalContext>>,
) -> Result<Vec<Port>, String> {
let inspect_command = "inspect --format '{{json .NetworkSettings.Ports}}' ".to_string() + &container_id;
let (inspect_output, _) = docker.command_execute(&inspect_command, gcx.clone(), true).await?;
let (inspect_output, _) = docker.command_execute(&inspect_command, gcx.clone(), true, true).await?;
tracing::info!("{}:\n{}", inspect_command, inspect_output);

let inspect_data: serde_json::Value = serde_json::from_str(&inspect_output)
Expand All @@ -461,9 +446,9 @@ async fn docker_container_kill(
) -> Result<(), String> {
let docker = docker_tool_load(gcx.clone()).await?;

docker.command_execute(&format!("container stop {container_id}"), gcx.clone(), true).await?;
docker.command_execute(&format!("container stop {container_id}"), gcx.clone(), true, true).await?;
info!("Stopped docker container {container_id}.");
docker.command_execute(&format!("container remove {container_id}"), gcx.clone(), true).await?;
docker.command_execute(&format!("container remove {container_id}"), gcx.clone(), true, true).await?;
info!("Removed docker container {container_id}.");
Ok(())
}
31 changes: 26 additions & 5 deletions src/integrations/docker/integr_docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct SettingsDocker {
pub command: String,
#[serde(serialize_with = "serialize_num_to_str", deserialize_with = "deserialize_str_to_num")]
pub keep_containers_alive_for_x_minutes: u64,
#[serde(serialize_with = "serialize_ports", deserialize_with = "deserialize_ports")]
pub ports: Vec<Port>,
}

Expand All @@ -46,6 +47,21 @@ where
String::deserialize(deserializer)?.parse().map_err(serde::de::Error::custom)
}

fn serialize_ports<S: serde::Serializer>(ports: &Vec<Port>, serializer: S) -> Result<S::Ok, S::Error> {
let ports_str = ports.iter().map(|port| format!("{}:{}", port.published, port.target))
.collect::<Vec<_>>().join(",");
serializer.serialize_str(&ports_str)
}

fn deserialize_ports<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<Vec<Port>, D::Error> {
let ports_str = String::deserialize(deserializer)?;
ports_str.split(',').filter(|s| !s.is_empty()).map(|port_str| {
let (published, target) = port_str.split_once(':')
.ok_or_else(|| serde::de::Error::custom("expected format 'published:target'"))?;
Ok(Port { published: published.to_string(), target: target.to_string() })
}).collect()
}

impl SettingsDocker {
pub fn get_ssh_config(&self) -> Option<SshConfig> {
if self.remote_docker {
Expand Down Expand Up @@ -99,7 +115,7 @@ impl IntegrationTrait for ToolDocker {
}

impl ToolDocker {
pub async fn command_execute(&self, command: &str, gcx: Arc<ARwLock<GlobalContext>>, fail_if_stderr_is_not_empty: bool) -> Result<(String, String), String>
pub async fn command_execute(&self, command: &str, gcx: Arc<ARwLock<GlobalContext>>, fail_if_stderr_is_not_empty: bool, verbose_error: bool) -> Result<(String, String), String>
{
let mut command_args = split_command(&command)?;

Expand All @@ -121,7 +137,12 @@ impl ToolDocker {
let stderr = String::from_utf8_lossy(&output.stderr).to_string();

if fail_if_stderr_is_not_empty && !stderr.is_empty() {
return Err(format!("Error executing command {command}: \n{stderr}"));
let error_message = if verbose_error {
format!("Command `{}` failed: {}", command, stderr)
} else {
stderr
};
return Err(error_message);
}

Ok((stdout, stderr))
Expand Down Expand Up @@ -157,7 +178,7 @@ impl Tool for ToolDocker {
ccx_locked.global_context.clone()
};

let (stdout, _) = self.command_execute(&command, gcx.clone(), true).await?;
let (stdout, _) = self.command_execute(&command, gcx.clone(), true, false).await?;

Ok((false, vec![
ContextEnum::ChatMessage(ChatMessage {
Expand Down Expand Up @@ -334,8 +355,8 @@ fields:
f_desc: "How long to keep containers alive in minutes."
f_default: "60"
ports:
f_type: array
f_desc: "Ports to expose."
f_type: string_long
f_desc: "Comma separated published:target notation for ports to publish, example '8080:3000,5000:5432'"
available:
on_your_laptop_possible: true
when_isolated_possible: false
Expand Down
8 changes: 7 additions & 1 deletion src/integrations/integr_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,13 @@ docker:
sl_chat:
- role: "user"
content: |
🔧 Your job is to create a new section under "docker" that will define a new postgres container, inside the current config file %CURRENT_CONFIG%. Follow the system prompt.
🔧 Your job is to create a postgres container, using the image and environment from new_container_default section in the current config file: %CURRENT_CONFIG%. Follow the system prompt.
smartlinks_for_each_container:
- sl_label: "Use for integration"
sl_chat:
- role: "user"
content: |
🔧 Your job is to modify postgres connection config in the current file to match the variables from the container, use docker tool to inspect the container if needed. Current config file: %CURRENT_CONFIG%.
"#;


Expand Down
1 change: 1 addition & 0 deletions src/integrations/yaml_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub struct ISchemaDocker {
pub filter_image: String,
pub new_container_default: DockerService,
pub smartlinks: Vec<ISmartLink>,
pub smartlinks_for_each_container: Vec<ISmartLink>,
}

#[derive(Serialize, Deserialize, Debug, Default)]
Expand Down
Loading