1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
use crate::{config::Config, docker_client::DockerClient, git_client::GitClient};
use serde_yaml::Value;
use std::path::Path;
pub struct DeploymentManager {
docker: DockerClient,
git: GitClient,
config: Config,
}
impl DeploymentManager {
pub fn new(config: Config) -> Self {
Self {
docker: DockerClient::new(config.socket_path.clone()),
git: GitClient,
config,
}
}
/// Robustly extract the service name from a container.
/// Prefer the Docker Compose label if present, otherwise parse the container name.
fn extract_service_name(container: &crate::types::Container) -> String {
// Try Docker Compose label first
if let Some(labels) = &container.labels {
if let Some(service) = labels.get("com.docker.compose.service") {
return service.clone();
}
}
// Fallback: parse from container name
if let Some(name) = container.names.get(0) {
let name = name.trim_start_matches('/');
// Try underscore split (compose v2 default: <project>_<service>_<index>)
let underscore_parts: Vec<&str> = name.split('_').collect();
if underscore_parts.len() >= 3 {
return underscore_parts[underscore_parts.len() - 2].to_string();
}
// Try dash split (older compose: <something>-<service>-<index>)
let dash_parts: Vec<&str> = name.split('-').collect();
if dash_parts.len() >= 2 {
// If last part is a number, use the one before it
if dash_parts.last().unwrap().parse::<u32>().is_ok() {
return dash_parts[dash_parts.len() - 2].to_string();
}
}
// Fallback: just return the name
return name.to_string();
}
// If all else fails, empty string
String::new()
}
fn update_compose_file_volume_source(
compose_file: &str,
symlink_path: &str,
mount_path: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let content = std::fs::read_to_string(compose_file)?;
let mut doc: Value = serde_yaml::from_str(&content)?;
let mut replaced = false;
if let Some(services) = doc.get_mut("services").and_then(Value::as_mapping_mut) {
for (_svc_name, svc) in services.iter_mut() {
if let Some(vols) = svc.get_mut("volumes").and_then(Value::as_sequence_mut) {
// Try to find and update an existing mapping
for vol in vols.iter_mut() {
// Handle string form: "host:container[:mode]"
if let Some(s) = vol.as_str() {
let parts: Vec<&str> = s.split(':').collect();
if parts.len() >= 2 {
let target = parts[1];
if target == mount_path && !replaced {
// preserve mode if present
let mut new_vol = format!("{}:{}", symlink_path, mount_path);
if parts.len() > 2 {
new_vol.push(':');
new_vol.push_str(parts[2]);
}
*vol = Value::String(new_vol);
replaced = true;
}
}
}
// Handle map form (YAML 1.2): {type: bind, source: ..., target: ...}
else if let Some(map) = vol.as_mapping_mut() {
if let Some(target) = map
.get(&Value::String("target".to_string()))
.and_then(Value::as_str)
{
if target == mount_path && !replaced {
map.insert(
Value::String("source".to_string()),
Value::String(symlink_path.to_string()),
);
replaced = true;
}
}
}
if replaced {
break;
}
}
// If not found, add a new mapping
if !replaced {
// Default to rw mode
let new_vol = format!("{}:{}:rw", symlink_path, mount_path);
vols.push(Value::String(new_vol));
replaced = true;
}
}
if replaced {
break;
}
}
}
if replaced {
let updated = serde_yaml::to_string(&doc)?;
std::fs::write(compose_file, updated)?;
}
Ok(())
}
pub async fn rolling_deploy(
&self,
tag: &str,
swarm: bool,
) -> Result<(), Box<dyn std::error::Error>> {
let config = &self.config;
println!(
"Starting rolling deployment for project '{}' with tag '{}'",
config.name, tag
);
// 1. Clone the new configuration to a versioned directory
let symlink_path = self
.git
.clone_repository_to_versioned_path(&config.repo_url, tag, &config.clone_path)
.await?;
// 1.5. Update the compose file to use the new config path as the volume source
// NOTE: You must add serde_yaml = "*" to Cargo.toml
Self::update_compose_file_volume_source(
&config.compose_file,
&symlink_path,
&config.mount_path,
)?;
if swarm {
let service = &config.name;
println!(
"Swarm mode: updating service '{}' mount to new config path.",
service
);
let add_arg = format!("type=bind,src={},dst={}", symlink_path, config.mount_path);
let status = std::process::Command::new("docker")
.args([
"service",
"update",
"--mount-rm",
&config.mount_path,
"--mount-add",
&add_arg,
service,
])
.status()?;
if !status.success() {
return Err(format!("docker service update failed for service {}", service).into());
}
println!("Successfully updated service '{}' in Swarm mode.", service);
} else {
// 2. Find running Traefik containers for this project
let running_containers = self
.docker
.get_running_containers_by_image_substring(&config.name)
.await?;
if running_containers.is_empty() {
return Err(
format!("No running containers found for project '{}'", config.name).into(),
);
}
println!(
"Found {} running Traefik containers",
running_containers.len()
);
// 3. For each running container, recreate the service
for (_index, container) in running_containers.iter().enumerate() {
let service_name = Self::extract_service_name(container);
println!("Rolling service: {}", service_name);
// Determine the absolute path to the compose file
let compose_file_abs = std::fs::canonicalize(&config.compose_file)?;
let compose_dir = compose_file_abs.parent().unwrap_or_else(|| Path::new("."));
// Check if the directory exists
if !compose_dir.exists() {
return Err(format!(
"Compose directory does not exist: {}",
compose_dir.display()
)
.into());
}
// Run docker compose up -d --force-recreate <service> in the compose file's directory
let status = std::process::Command::new("docker")
.args([
"compose",
"-f",
compose_file_abs.to_str().unwrap(),
"up",
"-d",
"--force-recreate",
service_name.as_str(),
])
.current_dir(compose_dir)
.status()?;
if !status.success() {
return Err(
format!("docker compose up failed for service {}", service_name).into(),
);
}
println!("Successfully rolled {} to new version", service_name);
}
}
// 4. Clean up old config directories (keep last 3 versions)
self.cleanup_old_configs(&config.clone_path, 3).await?;
println!("Rolling deployment completed successfully!");
Ok(())
}
async fn cleanup_old_configs(
&self,
base_path: &str,
keep_versions: usize,
) -> Result<(), Box<dyn std::error::Error>> {
let mut config_dirs = Vec::new();
if let Ok(entries) = std::fs::read_dir(base_path) {
for entry in entries {
if let Ok(entry) = entry {
let path = entry.path();
if path.is_dir() {
if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
if name.starts_with("traefik-config-") {
config_dirs.push(path);
}
}
}
}
}
}
// Sort by creation time (newest first)
config_dirs.sort_by_key(|path| {
std::fs::metadata(path)
.and_then(|m| m.created())
.unwrap_or(std::time::SystemTime::UNIX_EPOCH)
});
config_dirs.reverse();
// Remove old versions beyond the keep limit
for old_config in config_dirs.iter().skip(keep_versions) {
println!("Cleaning up old config: {:?}", old_config);
if let Err(e) = std::fs::remove_dir_all(old_config) {
eprintln!("Failed to remove old config {:?}: {}", old_config, e);
}
}
Ok(())
}
pub async fn rollback(
&self,
project_name: &str,
tag: &str,
config: &Config,
swarm: bool,
) -> Result<(), Box<dyn std::error::Error>> {
println!(
"Starting rollback of project '{}' to tag '{}'",
project_name, tag
);
// Check if the target version already exists
let target_config_path = format!("{}/traefik-config-{}", config.clone_path, tag);
if !std::path::Path::new(&target_config_path).exists() {
// If the config doesn't exist locally, clone it
println!("Target config not found locally, cloning...");
self.git
.clone_repository_to_versioned_path(&config.repo_url, tag, &config.clone_path)
.await?;
} else {
println!("Using existing config at {}", target_config_path);
}
// Perform rolling deployment to the target tag
self.rolling_deploy(tag, swarm).await?;
println!("Rollback completed successfully!");
Ok(())
}
}