Initial
This commit is contained in:
commit
3f60b72c3b
48 changed files with 6599 additions and 0 deletions
186
src/compose/parser.rs
Normal file
186
src/compose/parser.rs
Normal file
|
|
@ -0,0 +1,186 @@
|
|||
use crate::registry::ImageRef;
|
||||
use anyhow::Result;
|
||||
use regex::Regex;
|
||||
use std::fs;
|
||||
use tracing::{info, warn};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ServiceImage {
|
||||
pub service_name: String,
|
||||
pub image_ref: ImageRef,
|
||||
pub original_line: String,
|
||||
pub line_number: usize,
|
||||
}
|
||||
|
||||
pub struct ComposeFile {
|
||||
pub content: String,
|
||||
pub services: Vec<ServiceImage>,
|
||||
}
|
||||
|
||||
pub struct ComposeParser;
|
||||
|
||||
impl ComposeParser {
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
|
||||
pub fn parse_file(&self, file_path: &str) -> Result<ComposeFile> {
|
||||
let content = fs::read_to_string(file_path)?;
|
||||
let services = self.extract_services(&content)?;
|
||||
|
||||
Ok(ComposeFile { content, services })
|
||||
}
|
||||
|
||||
fn extract_services(&self, content: &str) -> Result<Vec<ServiceImage>> {
|
||||
let mut services = Vec::new();
|
||||
let lines: Vec<&str> = content.lines().collect();
|
||||
let mut in_services = false;
|
||||
let mut current_service: Option<String> = None;
|
||||
let image_regex = Regex::new(r#"^\s*image:\s*(?:["']([^"']+)["']|([^\s#]+))\s*(#.*)?$"#)?;
|
||||
|
||||
for (line_number, line) in lines.iter().enumerate() {
|
||||
if line.trim_start().starts_with("services:") {
|
||||
in_services = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if in_services
|
||||
&& line.chars().next().is_some_and(|c| c.is_alphabetic())
|
||||
&& !line.starts_with(" ")
|
||||
{
|
||||
in_services = false;
|
||||
current_service = None;
|
||||
continue;
|
||||
}
|
||||
|
||||
if in_services {
|
||||
if let Some(service_name) = self.extract_service_name(line) {
|
||||
current_service = Some(service_name);
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(ref service_name) = current_service {
|
||||
if let Some(image_ref) = self.extract_image_from_line(line, &image_regex)? {
|
||||
info!(
|
||||
"Found service '{}' with image '{}'",
|
||||
service_name, image_ref
|
||||
);
|
||||
services.push(ServiceImage {
|
||||
service_name: service_name.clone(),
|
||||
image_ref,
|
||||
original_line: line.to_string(),
|
||||
line_number,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(services)
|
||||
}
|
||||
|
||||
fn extract_service_name(&self, line: &str) -> Option<String> {
|
||||
let trimmed = line.trim_start();
|
||||
let indent_level = line.len() - trimmed.len();
|
||||
|
||||
if indent_level > 0 && indent_level <= 8 && trimmed.ends_with(':') && !trimmed.contains(' ')
|
||||
{
|
||||
let potential_service = trimmed.trim_end_matches(':');
|
||||
if !potential_service.is_empty()
|
||||
&& potential_service
|
||||
.chars()
|
||||
.all(|c| c.is_alphanumeric() || c == '_' || c == '-')
|
||||
{
|
||||
return Some(potential_service.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
fn extract_image_from_line(&self, line: &str, image_regex: &Regex) -> Result<Option<ImageRef>> {
|
||||
let trimmed = line.trim_start();
|
||||
let indent_level = line.len() - trimmed.len();
|
||||
|
||||
if indent_level > 2 && trimmed.starts_with("image:") {
|
||||
if let Some(captures) = image_regex.captures(line) {
|
||||
let image_str = captures
|
||||
.get(1)
|
||||
.or_else(|| captures.get(2))
|
||||
.unwrap()
|
||||
.as_str();
|
||||
|
||||
if image_str.find('@').is_some() {
|
||||
info!("Found digest in image string, skipping: {}", image_str);
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
if image_str.trim().is_empty()
|
||||
|| image_str.trim() == "\"\""
|
||||
|| image_str.trim() == "''"
|
||||
{
|
||||
info!("Empty image string found in line: {}, skipping", line);
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
match ImageRef::parse(image_str) {
|
||||
Ok(image_ref) => Ok(Some(image_ref)),
|
||||
Err(e) => {
|
||||
warn!("Failed to parse image '{}': {}", image_str, e);
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ComposeParser {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::io::Write;
|
||||
use tempfile::NamedTempFile;
|
||||
|
||||
#[test]
|
||||
fn test_parse_compose_file() {
|
||||
let compose_content = r#"
|
||||
version: '3.8'
|
||||
services:
|
||||
web:
|
||||
image: nginx:1.21.0 # Web server
|
||||
ports:
|
||||
- "80:80"
|
||||
|
||||
db:
|
||||
image: postgres:13.7
|
||||
environment:
|
||||
POSTGRES_DB: myapp
|
||||
|
||||
redis:
|
||||
image: redis:6.2-alpine
|
||||
"#;
|
||||
|
||||
let mut temp_file = NamedTempFile::new().unwrap();
|
||||
temp_file.write_all(compose_content.as_bytes()).unwrap();
|
||||
|
||||
let parser = ComposeParser::new();
|
||||
let result = parser
|
||||
.parse_file(temp_file.path().to_str().unwrap())
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.services.len(), 3);
|
||||
assert_eq!(result.services[0].service_name, "web");
|
||||
assert_eq!(result.services[0].image_ref.name, "nginx");
|
||||
assert_eq!(result.services[0].image_ref.tag, "1.21.0");
|
||||
}
|
||||
}
|
||||
293
src/compose/updater.rs
Normal file
293
src/compose/updater.rs
Normal file
|
|
@ -0,0 +1,293 @@
|
|||
use super::parser::{ComposeFile, ComposeParser, ServiceImage};
|
||||
use crate::config::Config;
|
||||
use crate::registry::Client as RegistryClient;
|
||||
use crate::strategy::create_selector;
|
||||
use crate::version::parse_version_tag;
|
||||
use anyhow::{anyhow, Result};
|
||||
use regex::Regex;
|
||||
use std::collections::HashSet;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use tracing::{debug, info, warn};
|
||||
|
||||
pub struct ComposeUpdater {
|
||||
config: Config,
|
||||
registry_client: RegistryClient,
|
||||
parser: ComposeParser,
|
||||
}
|
||||
|
||||
impl ComposeUpdater {
|
||||
pub fn new(config: Config) -> Self {
|
||||
let registry_client = RegistryClient::new(config.clone());
|
||||
let parser = ComposeParser::new();
|
||||
|
||||
Self {
|
||||
config,
|
||||
registry_client,
|
||||
parser,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update_all_compose_files(&self) -> Result<Vec<String>> {
|
||||
let mut updated_files = Vec::new();
|
||||
|
||||
for compose_path in &self.config.compose_paths {
|
||||
let compose_files = self.find_compose_files(compose_path)?;
|
||||
|
||||
for file_path in compose_files {
|
||||
let updated = self.update_compose_file(&file_path).await?;
|
||||
if updated {
|
||||
updated_files.push(file_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(updated_files)
|
||||
}
|
||||
|
||||
pub fn parse_compose_file(&self, file_path: &str) -> Result<ComposeFile> {
|
||||
self.parser.parse_file(file_path)
|
||||
}
|
||||
|
||||
async fn update_compose_file(&self, file_path: &str) -> Result<bool> {
|
||||
info!("Processing compose file: {}", file_path);
|
||||
|
||||
let compose_file = self.parse_compose_file(file_path)?;
|
||||
let mut updated = false;
|
||||
let mut new_content = compose_file.content.clone();
|
||||
|
||||
for service in &compose_file.services {
|
||||
if self.config.is_image_ignored(&service.image_ref.to_string()) {
|
||||
info!("Skipping ignored image: {}", service.image_ref.to_string());
|
||||
continue;
|
||||
}
|
||||
|
||||
match self.update_service_image(service).await {
|
||||
Ok(Some(new_image)) => {
|
||||
new_content =
|
||||
self.replace_image_in_content(&new_content, service, &new_image)?;
|
||||
updated = true;
|
||||
info!(
|
||||
"Updated {}: {} -> {}",
|
||||
service.service_name,
|
||||
service.image_ref.to_string(),
|
||||
new_image
|
||||
);
|
||||
}
|
||||
Ok(None) => {
|
||||
debug!(
|
||||
"No update needed for {}: {}",
|
||||
service.service_name,
|
||||
service.image_ref.to_string()
|
||||
);
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(
|
||||
e.context(format!("Failed to update service {}", service.service_name))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if updated {
|
||||
self.write_updated_content(file_path, new_content)?;
|
||||
}
|
||||
|
||||
Ok(updated)
|
||||
}
|
||||
|
||||
fn write_updated_content(&self, file_path: &str, content: String) -> Result<()> {
|
||||
if !self.config.dry_run {
|
||||
fs::write(file_path, content)?;
|
||||
info!("Updated compose file: {}", file_path);
|
||||
} else {
|
||||
info!("Dry run: Would update compose file: {}", file_path);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn update_service_image(&self, service: &ServiceImage) -> Result<Option<String>> {
|
||||
info!(
|
||||
"Checking for updates for service: {} (current image: {})",
|
||||
service.service_name,
|
||||
service.image_ref.to_string()
|
||||
);
|
||||
let (current_version, current_prefix, current_suffix, _) =
|
||||
parse_version_tag(&service.image_ref.tag);
|
||||
|
||||
let available_versions = self
|
||||
.registry_client
|
||||
.get_available_versions(&service.image_ref)
|
||||
.await?;
|
||||
|
||||
if available_versions.is_empty() {
|
||||
warn!("No versions available for selection");
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let selector = create_selector(&self.config.update_strategy);
|
||||
if let Some(target_version_info) =
|
||||
selector.select_target_version(&available_versions, current_prefix, current_suffix)
|
||||
{
|
||||
// Only update if the target version is different AND higher than the current version
|
||||
// This prevents downgrades
|
||||
if Some(target_version_info.version.clone()) != current_version {
|
||||
if let Some(ref current_ver) = current_version {
|
||||
if target_version_info.version < *current_ver {
|
||||
info!(
|
||||
"Skipping downgrade from {} to {}",
|
||||
current_ver, target_version_info.version
|
||||
);
|
||||
return Ok(None);
|
||||
}
|
||||
}
|
||||
let mut new_image_ref = service.image_ref.clone();
|
||||
new_image_ref.tag = target_version_info.to_string();
|
||||
return Ok(Some(new_image_ref.to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub fn replace_image_in_content(
|
||||
&self,
|
||||
content: &str,
|
||||
service: &ServiceImage,
|
||||
new_image: &str,
|
||||
) -> Result<String> {
|
||||
let image_regex =
|
||||
Regex::new(r#"^(\s*image:\s*)(?:["']([^"']+)["']|([^\s#]+))(\s*(?:#.*)?)$"#)?;
|
||||
|
||||
if let Some(captures) = image_regex.captures(&service.original_line) {
|
||||
let prefix = captures.get(1).unwrap().as_str();
|
||||
let _old_image = captures
|
||||
.get(2)
|
||||
.or_else(|| captures.get(3))
|
||||
.unwrap()
|
||||
.as_str();
|
||||
let suffix = captures.get(4).unwrap().as_str();
|
||||
|
||||
let image_part = if captures.get(2).is_some() {
|
||||
format!("\"{new_image}\"")
|
||||
} else {
|
||||
new_image.to_string()
|
||||
};
|
||||
|
||||
let new_line = format!("{prefix}{image_part}{suffix}");
|
||||
|
||||
let lines: Vec<&str> = content.lines().collect();
|
||||
if service.line_number < lines.len()
|
||||
&& lines[service.line_number] == service.original_line
|
||||
{
|
||||
let mut result_lines = lines;
|
||||
result_lines[service.line_number] = &new_line;
|
||||
let mut result = result_lines.join("\n");
|
||||
|
||||
if content.ends_with('\n') {
|
||||
result.push('\n');
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
} else {
|
||||
Err(anyhow!(
|
||||
"Line number mismatch or content changed for service: {}",
|
||||
service.service_name
|
||||
))
|
||||
}
|
||||
} else {
|
||||
Err(anyhow!(
|
||||
"Could not parse image line: {}",
|
||||
service.original_line
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn find_compose_files(&self, path: &Path) -> Result<Vec<String>> {
|
||||
let mut visited = HashSet::new();
|
||||
self.find_compose_files_recursive(path, &mut visited)
|
||||
}
|
||||
|
||||
fn find_compose_files_recursive(
|
||||
&self,
|
||||
path: &Path,
|
||||
visited: &mut HashSet<PathBuf>,
|
||||
) -> Result<Vec<String>> {
|
||||
let canonical_path = match path.canonicalize() {
|
||||
Ok(p) => p,
|
||||
Err(e) => {
|
||||
warn!("Failed to canonicalize path {}: {}", path.display(), e);
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
};
|
||||
|
||||
if !visited.insert(canonical_path.clone()) {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
|
||||
let mut compose_files = Vec::new();
|
||||
|
||||
if path.is_file() {
|
||||
if self.is_compose_file(path)? {
|
||||
compose_files.push(path.to_string_lossy().to_string());
|
||||
}
|
||||
} else if path.is_dir() {
|
||||
for entry in fs::read_dir(path)? {
|
||||
let entry = entry?;
|
||||
let entry_path = entry.path();
|
||||
|
||||
if entry_path.is_file() && self.is_compose_file(&entry_path)? {
|
||||
compose_files.push(entry_path.to_string_lossy().to_string());
|
||||
} else if entry_path.is_dir() {
|
||||
compose_files.extend(self.find_compose_files_recursive(&entry_path, visited)?);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(compose_files)
|
||||
}
|
||||
|
||||
fn is_compose_file(&self, path: &Path) -> Result<bool> {
|
||||
let filename = path
|
||||
.file_name()
|
||||
.ok_or_else(|| anyhow!("Invalid file path: {:?}", path))?
|
||||
.to_string_lossy();
|
||||
Ok(filename.ends_with(".yml") || filename.ends_with(".yaml"))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::config::{Config, UpdateStrategy};
|
||||
use std::io::Write;
|
||||
use tempfile::NamedTempFile;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_prevents_downgrade() {
|
||||
let mut config = Config::default();
|
||||
config.update_strategy = UpdateStrategy::Latest;
|
||||
config.dry_run = true;
|
||||
|
||||
let updater = ComposeUpdater::new(config);
|
||||
|
||||
// Create a temporary compose file with a higher version
|
||||
let mut temp_file = NamedTempFile::new().unwrap();
|
||||
writeln!(temp_file, "services:\n web:\n image: nginx:1.25.0").unwrap();
|
||||
|
||||
let file_path = temp_file.path().to_str().unwrap();
|
||||
let compose_file = updater.parse_compose_file(file_path).unwrap();
|
||||
|
||||
assert_eq!(compose_file.services.len(), 1);
|
||||
let service = &compose_file.services[0];
|
||||
|
||||
// Mock a scenario where the strategy selects a lower version
|
||||
// This would happen if available versions only include older versions
|
||||
let (current_version, _, _, _) = parse_version_tag(&service.image_ref.tag);
|
||||
assert!(current_version.is_some());
|
||||
|
||||
// The actual test would need mocked registry responses, but we can verify
|
||||
// the logic by checking that current version is properly extracted
|
||||
assert_eq!(current_version.unwrap().to_string(), "1.25.0");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue