opencode_cloud_core/docker/
mod.rs1mod client;
16pub mod container;
17mod dockerfile;
18mod error;
19pub mod exec;
20mod health;
21pub mod image;
22pub mod mount;
23pub mod progress;
24pub mod state;
25pub mod update;
26pub mod users;
27mod version;
28pub mod volume;
29
30pub use client::DockerClient;
32pub use error::DockerError;
33pub use progress::ProgressReporter;
34
35pub use health::{
37 ExtendedHealthResponse, HealthError, HealthResponse, check_health, check_health_extended,
38};
39
40pub use dockerfile::{DOCKERFILE, IMAGE_NAME_DOCKERHUB, IMAGE_NAME_GHCR, IMAGE_TAG_DEFAULT};
42
43pub use image::{build_image, image_exists, pull_image};
45
46pub use update::{UpdateResult, has_previous_image, rollback_image, update_image};
48
49pub use version::{VERSION_LABEL, get_cli_version, get_image_version, versions_compatible};
51
52pub use exec::{exec_command, exec_command_exit_code, exec_command_with_stdin};
54
55pub use users::{
57 UserInfo, create_user, delete_user, list_users, lock_user, persist_user, remove_persisted_user,
58 restore_persisted_users, set_user_password, unlock_user, user_exists,
59};
60
61pub use volume::{
63 MOUNT_CACHE, MOUNT_CONFIG, MOUNT_PROJECTS, MOUNT_SESSION, MOUNT_STATE, MOUNT_USERS,
64 VOLUME_CACHE, VOLUME_CONFIG, VOLUME_NAMES, VOLUME_PROJECTS, VOLUME_SESSION, VOLUME_STATE,
65 VOLUME_USERS, ensure_volumes_exist, remove_all_volumes, remove_volume, volume_exists,
66};
67
68pub use mount::{MountError, ParsedMount, check_container_path_warning, validate_mount_path};
70
71pub use container::{
73 CONTAINER_NAME, ContainerBindMount, ContainerPorts, OPENCODE_WEB_PORT, container_exists,
74 container_is_running, container_state, create_container, get_container_bind_mounts,
75 get_container_ports, remove_container, start_container, stop_container,
76};
77
78pub use state::{ImageState, clear_state, get_state_path, load_state, save_state};
80
81pub async fn setup_and_start(
95 client: &DockerClient,
96 opencode_web_port: Option<u16>,
97 env_vars: Option<Vec<String>>,
98 bind_address: Option<&str>,
99 cockpit_port: Option<u16>,
100 cockpit_enabled: Option<bool>,
101 bind_mounts: Option<Vec<mount::ParsedMount>>,
102) -> Result<String, DockerError> {
103 volume::ensure_volumes_exist(client).await?;
105
106 let container_id = if container::container_exists(client, container::CONTAINER_NAME).await? {
108 let info = client
110 .inner()
111 .inspect_container(container::CONTAINER_NAME, None)
112 .await
113 .map_err(|e| {
114 DockerError::Container(format!("Failed to inspect existing container: {e}"))
115 })?;
116 info.id
117 .unwrap_or_else(|| container::CONTAINER_NAME.to_string())
118 } else {
119 container::create_container(
121 client,
122 None,
123 None,
124 opencode_web_port,
125 env_vars,
126 bind_address,
127 cockpit_port,
128 cockpit_enabled,
129 bind_mounts,
130 )
131 .await?
132 };
133
134 if !container::container_is_running(client, container::CONTAINER_NAME).await? {
136 container::start_container(client, container::CONTAINER_NAME).await?;
137 }
138
139 users::restore_persisted_users(client, container::CONTAINER_NAME).await?;
141
142 Ok(container_id)
143}
144
145pub const DEFAULT_STOP_TIMEOUT_SECS: i64 = 30;
147
148pub async fn stop_service(
155 client: &DockerClient,
156 remove: bool,
157 timeout_secs: Option<i64>,
158) -> Result<(), DockerError> {
159 let name = container::CONTAINER_NAME;
160 let timeout = timeout_secs.unwrap_or(DEFAULT_STOP_TIMEOUT_SECS);
161
162 if !container::container_exists(client, name).await? {
164 return Err(DockerError::Container(format!(
165 "Container '{name}' does not exist"
166 )));
167 }
168
169 if container::container_is_running(client, name).await? {
171 container::stop_container(client, name, Some(timeout)).await?;
172 }
173
174 if remove {
176 container::remove_container(client, name, false).await?;
177 }
178
179 Ok(())
180}