|
| 1 | +use anyhow::anyhow; |
| 2 | +use clap::Subcommand; |
| 3 | +use std::fs; |
| 4 | +use std::process::{Command, Stdio}; |
| 5 | + |
| 6 | +#[derive(Subcommand, Debug)] |
| 7 | +pub enum ImageCommand { |
| 8 | + /// Build bitcoind and bitcoin-cli from <repo>/<branch> as <registry>:<tag>. |
| 9 | + /// Optionally deploy to remote registry using --action=push, otherwise image is loaded to local registry. |
| 10 | + Build { |
| 11 | + #[arg(long)] |
| 12 | + repo: String, |
| 13 | + #[arg(long)] |
| 14 | + branch: String, |
| 15 | + #[arg(long)] |
| 16 | + registry: String, |
| 17 | + #[arg(long)] |
| 18 | + tag: String, |
| 19 | + #[arg(long)] |
| 20 | + build_args: Option<String>, |
| 21 | + #[arg(long)] |
| 22 | + arches: Option<String>, |
| 23 | + #[arg(long)] |
| 24 | + action: Option<String>, |
| 25 | + }, |
| 26 | +} |
| 27 | + |
| 28 | +const ARCHES: [&str; 3] = ["amd64", "arm64", "armv7"]; |
| 29 | + |
| 30 | +fn run_command(command: &str) -> anyhow::Result<bool> { |
| 31 | + println!("Executing: {}", command); |
| 32 | + let mut child = Command::new("bash") |
| 33 | + .arg("-c") |
| 34 | + .arg(command) |
| 35 | + .stdout(Stdio::inherit()) |
| 36 | + .stderr(Stdio::inherit()) |
| 37 | + .spawn()?; |
| 38 | + |
| 39 | + let output = child.wait()?; |
| 40 | + |
| 41 | + if output.success() { |
| 42 | + Ok(true) |
| 43 | + } else { |
| 44 | + Err(anyhow!("Command failed")) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +fn build_image( |
| 49 | + repo: &String, |
| 50 | + branch: &String, |
| 51 | + docker_registry: &String, |
| 52 | + tag: &String, |
| 53 | + build_args: &Option<String>, |
| 54 | + arches: &Option<String>, |
| 55 | + action: &Option<String>, |
| 56 | +) -> anyhow::Result<()> { |
| 57 | + let build_args = match build_args { |
| 58 | + Some(args) => format!("\"{}\"", args), |
| 59 | + None => "\"--disable-tests --without-gui --disable-bench --disable-fuzz-binary --enable-suppress-external-warnings \"".to_string(), |
| 60 | + }; |
| 61 | + |
| 62 | + let mut build_arches = vec![]; |
| 63 | + match arches { |
| 64 | + Some(a) => build_arches.extend(a.split(',').map(String::from)), |
| 65 | + None => build_arches.push("amd64".to_string()), |
| 66 | + } |
| 67 | + |
| 68 | + for arch in &build_arches { |
| 69 | + if !ARCHES.contains(&arch.as_str()) { |
| 70 | + println!("Error: {} is not a supported architecture", arch); |
| 71 | + return Err(anyhow!("Unsupported architecture: {}", arch)); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + println!("repo={}", repo); |
| 76 | + println!("branch={}", branch); |
| 77 | + println!("docker_registry={}", docker_registry); |
| 78 | + println!("tag={}", tag); |
| 79 | + println!("build_args={}", build_args); |
| 80 | + println!("build_arches={:?}", build_arches); |
| 81 | + |
| 82 | + if !fs::metadata("src/templates") |
| 83 | + .map(|m| m.is_dir()) |
| 84 | + .unwrap_or(false) |
| 85 | + { |
| 86 | + println!("Directory src/templates does not exist."); |
| 87 | + println!("Please run this script from the project root."); |
| 88 | + return Err(anyhow!("src/templates directory not found")); |
| 89 | + } |
| 90 | + |
| 91 | + let builder_name = "bitcoind-builder"; |
| 92 | + let create_builder_cmd = format!("docker buildx create --name {} --use", builder_name); |
| 93 | + let creat_builder_res = run_command(&create_builder_cmd); |
| 94 | + if creat_builder_res.is_err() { |
| 95 | + let use_builder_cmd = format!("docker buildx use {}", builder_name); |
| 96 | + run_command(&use_builder_cmd)?; |
| 97 | + } |
| 98 | + |
| 99 | + let image_full_name = format!("{}:{}", docker_registry, tag); |
| 100 | + println!("image_full_name={}", image_full_name); |
| 101 | + |
| 102 | + let platforms = build_arches |
| 103 | + .iter() |
| 104 | + .map(|arch| format!("linux/{}", arch)) |
| 105 | + .collect::<Vec<_>>() |
| 106 | + .join(","); |
| 107 | + |
| 108 | + let action = match action { |
| 109 | + Some(action) => action, |
| 110 | + None => "load", |
| 111 | + }; |
| 112 | + let build_command = format!( |
| 113 | + "docker buildx build --platform {} --build-arg REPO={} --build-arg BRANCH={} --build-arg BUILD_ARGS={} --tag {} --file src/templates/Dockerfile . --{}", |
| 114 | + platforms, repo, branch, build_args, image_full_name, action |
| 115 | + ); |
| 116 | + |
| 117 | + println!("Using build_command={}", build_command); |
| 118 | + |
| 119 | + let res = run_command(&build_command); |
| 120 | + if res.is_ok() { |
| 121 | + println!("Build completed"); |
| 122 | + } else { |
| 123 | + println!("Build failed."); |
| 124 | + } |
| 125 | + |
| 126 | + let cleanup_builder_cmd = format!("docker buildx rm {}", builder_name); |
| 127 | + let cleanup_res = run_command(&cleanup_builder_cmd); |
| 128 | + if cleanup_res.is_ok() { |
| 129 | + println!("Buildx builder removed successfully."); |
| 130 | + } else { |
| 131 | + println!("Warning: Failed to remove the buildx builder."); |
| 132 | + } |
| 133 | + |
| 134 | + match res { |
| 135 | + Ok(true) => Ok(()), |
| 136 | + Ok(false) => Err(anyhow!( |
| 137 | + "Build command failed, but no specific error was provided." |
| 138 | + )), |
| 139 | + Err(e) => Err(anyhow!("Build command failed with error: {}", e)), |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +pub async fn handle_image_command(command: &ImageCommand) -> anyhow::Result<()> { |
| 144 | + match command { |
| 145 | + ImageCommand::Build { |
| 146 | + repo, |
| 147 | + branch, |
| 148 | + registry, |
| 149 | + tag, |
| 150 | + build_args, |
| 151 | + arches, |
| 152 | + action, |
| 153 | + } => build_image(repo, branch, registry, tag, build_args, arches, action), |
| 154 | + } |
| 155 | +} |
0 commit comments