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;
24mod registry;
25pub mod state;
26pub mod update;
27pub mod users;
28mod version;
29pub mod volume;
30
31pub use client::DockerClient;
33pub use error::DockerError;
34pub use progress::ProgressReporter;
35
36pub use health::{
38 ExtendedHealthResponse, HealthError, HealthResponse, check_health, check_health_extended,
39};
40
41pub use dockerfile::{DOCKERFILE, IMAGE_NAME_DOCKERHUB, IMAGE_NAME_GHCR, IMAGE_TAG_DEFAULT};
43
44pub use image::{build_image, image_exists, pull_image};
46
47pub use update::{UpdateResult, has_previous_image, rollback_image, update_image};
49
50pub use version::{
52 VERSION_LABEL, get_cli_version, get_image_version, get_registry_latest_version,
53 versions_compatible,
54};
55
56pub use exec::{
58 exec_command, exec_command_exit_code, exec_command_with_status, exec_command_with_stdin,
59};
60
61pub use users::{
63 UserInfo, create_user, delete_user, list_users, lock_user, persist_user, remove_persisted_user,
64 restore_persisted_users, set_user_password, unlock_user, user_exists,
65};
66
67pub use volume::{
69 MOUNT_CACHE, MOUNT_CONFIG, MOUNT_PROJECTS, MOUNT_SESSION, MOUNT_STATE, MOUNT_USERS,
70 VOLUME_CACHE, VOLUME_CONFIG, VOLUME_NAMES, VOLUME_PROJECTS, VOLUME_SESSION, VOLUME_STATE,
71 VOLUME_USERS, ensure_volumes_exist, remove_all_volumes, remove_volume, volume_exists,
72};
73
74pub use mount::{MountError, ParsedMount, check_container_path_warning, validate_mount_path};
76
77pub use container::{
79 CONTAINER_NAME, ContainerBindMount, ContainerPorts, OPENCODE_WEB_PORT, container_exists,
80 container_is_running, container_state, create_container, get_container_bind_mounts,
81 get_container_ports, remove_container, start_container, stop_container,
82};
83
84pub use state::{ImageState, clear_state, get_state_path, load_state, save_state};
86
87pub async fn setup_and_start(
101 client: &DockerClient,
102 opencode_web_port: Option<u16>,
103 env_vars: Option<Vec<String>>,
104 bind_address: Option<&str>,
105 cockpit_port: Option<u16>,
106 cockpit_enabled: Option<bool>,
107 bind_mounts: Option<Vec<mount::ParsedMount>>,
108) -> Result<String, DockerError> {
109 volume::ensure_volumes_exist(client).await?;
111
112 let container_id = if container::container_exists(client, container::CONTAINER_NAME).await? {
114 let info = client
116 .inner()
117 .inspect_container(container::CONTAINER_NAME, None)
118 .await
119 .map_err(|e| {
120 DockerError::Container(format!("Failed to inspect existing container: {e}"))
121 })?;
122 info.id
123 .unwrap_or_else(|| container::CONTAINER_NAME.to_string())
124 } else {
125 container::create_container(
127 client,
128 None,
129 None,
130 opencode_web_port,
131 env_vars,
132 bind_address,
133 cockpit_port,
134 cockpit_enabled,
135 bind_mounts,
136 )
137 .await?
138 };
139
140 if !container::container_is_running(client, container::CONTAINER_NAME).await? {
142 container::start_container(client, container::CONTAINER_NAME).await?;
143 }
144
145 users::restore_persisted_users(client, container::CONTAINER_NAME).await?;
147
148 Ok(container_id)
149}
150
151pub const DEFAULT_STOP_TIMEOUT_SECS: i64 = 30;
153
154pub async fn stop_service(
161 client: &DockerClient,
162 remove: bool,
163 timeout_secs: Option<i64>,
164) -> Result<(), DockerError> {
165 let name = container::CONTAINER_NAME;
166 let timeout = timeout_secs.unwrap_or(DEFAULT_STOP_TIMEOUT_SECS);
167
168 if !container::container_exists(client, name).await? {
170 return Err(DockerError::Container(format!(
171 "Container '{name}' does not exist"
172 )));
173 }
174
175 if container::container_is_running(client, name).await? {
177 container::stop_container(client, name, Some(timeout)).await?;
178 }
179
180 if remove {
182 container::remove_container(client, name, false).await?;
183 }
184
185 Ok(())
186}