Skip to content

Commit

Permalink
Prefer inventory::artifact::Arch
Browse files Browse the repository at this point in the history
  • Loading branch information
runesoerensen authored and schneems committed Sep 5, 2024
1 parent 10506b3 commit b8cfa55
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 87 deletions.
10 changes: 5 additions & 5 deletions jruby_executable/src/bin/jruby_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use clap::Parser;
use fs_err::PathExt;
use gem_version::GemVersion;
use indoc::formatdoc;
use inventory::artifact::Artifact;
use inventory::artifact::{Arch, Artifact};
use jruby_executable::jruby_build_properties;
use shared::{
append_filename_with, artifact_is_different, artifact_same_url_different_checksum,
atomic_inventory_update, download_tar, sha256_from_path, source_dir, tar_dir_to_file,
untar_to_dir, ArtifactMetadata, BaseImage, CpuArch, TarDownloadPath,
untar_to_dir, ArtifactMetadata, BaseImage, TarDownloadPath,
};
use std::convert::From;
use std::error::Error;
Expand Down Expand Up @@ -123,12 +123,12 @@ fn jruby_build(args: &Args) -> Result<(), Box<dyn Error>> {
fs_err::copy(tar_file.path(), &sha_seven_path)?;

let timestamp = chrono::Utc::now();
for cpu_arch in &[CpuArch::new("amd64")?, CpuArch::new("arm64")?] {
for cpu_arch in [Arch::Amd64, Arch::Arm64] {
let distro_version = base_image.distro_version();
let artifact = Artifact {
version: GemVersion::from_str(version)?,
os: inventory::artifact::Os::Linux,
arch: cpu_arch.try_into()?,
arch: cpu_arch,
url: format!(
"{S3_BASE_URL}/{}",
sha_seven_path.strip_prefix(&volume_output_dir)?.display()
Expand Down Expand Up @@ -165,7 +165,7 @@ fn jruby_build(args: &Args) -> Result<(), Box<dyn Error>> {
}

// Can be removed once manifest file support is fully rolled out
for cpu_arch in &[CpuArch::new("amd64")?, CpuArch::new("arm64")?] {
for cpu_arch in [Arch::Amd64, Arch::Arm64] {
let dir = volume_output_dir
.join(base_image.to_string())
.join(cpu_arch.to_string());
Expand Down
5 changes: 3 additions & 2 deletions jruby_executable/src/bin/jruby_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use bullet_stream::{style, Print};
use clap::Parser;
use fun_run::CommandWithName;
use indoc::formatdoc;
use inventory::artifact::Arch;
use jruby_executable::jruby_build_properties;
use shared::{source_dir, BaseImage, CpuArch};
use shared::{source_dir, BaseImage};
use std::error::Error;
use std::io::Write;
use std::{path::PathBuf, process::Command};
Expand All @@ -13,7 +14,7 @@ static INNER_OUTPUT: &str = "/tmp/output";
#[derive(Parser, Debug)]
struct RubyArgs {
#[arg(long)]
arch: CpuArch,
arch: Arch,

#[arg(long)]
version: String,
Expand Down
9 changes: 4 additions & 5 deletions ruby_executable/src/bin/ruby_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ use fs_err::PathExt;
use fun_run::CommandWithName;
use gem_version::GemVersion;
use indoc::{formatdoc, indoc};
use inventory::artifact::Artifact;
use inventory::artifact::{Arch, Artifact};
use shared::{
append_filename_with, artifact_is_different, artifact_same_url_different_checksum,
atomic_inventory_update, download_tar, output_tar_path, sha256_from_path, source_dir,
validate_version_for_stack, ArtifactMetadata, BaseImage, CpuArch, RubyDownloadVersion,
TarDownloadPath,
validate_version_for_stack, ArtifactMetadata, BaseImage, RubyDownloadVersion, TarDownloadPath,
};
use std::{
io::Write,
Expand All @@ -25,7 +24,7 @@ static S3_BASE_URL: &str = "https://heroku-buildpack-ruby.s3.us-east-1.amazonaws
#[derive(Parser, Debug)]
struct RubyArgs {
#[arg(long)]
arch: CpuArch,
arch: Arch,

#[arg(long)]
version: RubyDownloadVersion,
Expand Down Expand Up @@ -176,7 +175,7 @@ fn ruby_build(args: &RubyArgs) -> Result<(), Box<dyn std::error::Error>> {
let artifact = Artifact {
version: GemVersion::from_str(&version.bundler_format())?,
os: inventory::artifact::Os::Linux,
arch: arch.try_into()?,
arch: *arch,
url,
checksum: format!("sha256:{sha}").parse()?,
metadata: ArtifactMetadata {
Expand Down
5 changes: 3 additions & 2 deletions ruby_executable/src/bin/ruby_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ use bullet_stream::{style, Print};
use clap::Parser;
use fun_run::CommandWithName;
use indoc::formatdoc;
use shared::{output_tar_path, source_dir, BaseImage, CpuArch, RubyDownloadVersion};
use inventory::artifact::Arch;
use shared::{output_tar_path, source_dir, BaseImage, RubyDownloadVersion};
use std::{error::Error, path::PathBuf, process::Command};

static INNER_OUTPUT: &str = "/tmp/output";

#[derive(Parser, Debug)]
struct RubyArgs {
#[arg(long)]
arch: CpuArch,
arch: Arch,

#[arg(long)]
version: RubyDownloadVersion,
Expand Down
66 changes: 0 additions & 66 deletions shared/src/base_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::str::FromStr;

use serde::{Deserialize, Serialize};

static KNOWN_ARCHITECTURES: [&str; 2] = ["amd64", "arm64"];
static KNOWN_BASE_IMAGES: &[(&str, &str)] = &[
("heroku-20", "20"),
("heroku-22", "22"),
Expand Down Expand Up @@ -68,68 +67,3 @@ impl FromStr for BaseImage {
BaseImage::new(s)
}
}

#[derive(Debug, Clone)]
pub struct CpuArch {
name: String,
}

impl Display for CpuArch {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name)
}
}

impl FromStr for CpuArch {
type Err = CpuArchError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
CpuArch::new(s)
}
}

#[derive(Debug, thiserror::Error)]
pub enum CpuArchError {
#[error("Invalid CPU architecture {0} must be one of {}", KNOWN_ARCHITECTURES.join(", "))]
Unknown(String),

#[error("CPU architecture {0} cannot be converted {1}")]
CannotConvert(String, inventory::artifact::UnsupportedArchError),
}

impl TryFrom<&CpuArch> for inventory::artifact::Arch {
type Error = CpuArchError;

fn try_from(value: &CpuArch) -> Result<Self, Self::Error> {
Self::from_str(&value.name).map_err(|e| CpuArchError::CannotConvert(value.name.clone(), e))
}
}

impl CpuArch {
pub fn new(s: &str) -> Result<Self, CpuArchError> {
KNOWN_ARCHITECTURES
.iter()
.find(|&&name| name == s)
.map(|_| Self { name: s.to_owned() })
.ok_or_else(|| CpuArchError::Unknown(s.to_owned()))
}

pub fn from_system() -> Result<Self, CpuArchError> {
let arch = if cfg!(target_arch = "aarch64") {
"arm64"
} else if cfg!(target_arch = "x86_64") {
"amd64"
} else {
"Unknown architecture"
};

Self::new(arch)
}

#[cfg(test)]
pub(crate) fn from_test_str(name: &str) -> Self {
Self {
name: name.to_string(),
}
}
}
12 changes: 5 additions & 7 deletions shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use bullet_stream::state::SubBullet;
use bullet_stream::Print;
use fs_err::{File, PathExt};
use fun_run::CommandWithName;
use inventory::artifact::Arch;
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::process::Command;
Expand All @@ -10,7 +11,7 @@ mod base_image;
mod download_ruby_version;
mod inventory_help;

pub use base_image::{BaseImage, CpuArch, CpuArchError};
pub use base_image::BaseImage;
pub use download_ruby_version::RubyDownloadVersion;
pub use inventory_help::{
artifact_is_different, artifact_same_url_different_checksum, atomic_inventory_update,
Expand Down Expand Up @@ -81,9 +82,6 @@ pub fn untar_to_dir(tar_path: &TarDownloadPath, workspace: &Path) -> Result<(),

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Error {0}")]
UnknownArchitecture(CpuArchError),

#[error("Command failed {0}")]
CmdError(fun_run::CmdError),

Expand Down Expand Up @@ -155,7 +153,7 @@ pub fn output_tar_path(
output: &Path,
version: &RubyDownloadVersion,
base_image: &BaseImage,
cpu_architecture: &CpuArch,
cpu_architecture: &Arch,
) -> PathBuf {
let directory = if base_image.is_arch_aware() {
PathBuf::from(base_image.to_string()).join(cpu_architecture.to_string())
Expand Down Expand Up @@ -307,7 +305,7 @@ mod test {
let output = PathBuf::from("/tmp");
let version = RubyDownloadVersion::from_str("2.7.3").unwrap();
let base_image = BaseImage::new("heroku-20").unwrap();
let cpu_architecture = CpuArch::from_test_str("amd64");
let cpu_architecture = Arch::Amd64;

let tar_path = output_tar_path(&output, &version, &base_image, &cpu_architecture);

Expand All @@ -320,7 +318,7 @@ mod test {
let output = PathBuf::from("/tmp");
let version = RubyDownloadVersion::from_str("2.7.3").unwrap();
let base_image = BaseImage::new("heroku-24").unwrap();
let cpu_architecture = CpuArch::from_test_str("amd64");
let cpu_architecture = Arch::Amd64;

let tar_path = output_tar_path(&output, &version, &base_image, &cpu_architecture);

Expand Down

0 comments on commit b8cfa55

Please sign in to comment.