Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Release ID from file /etc/heroku/release_id #17

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions buildpacks/release-phase/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ This command must output release artifacts into `/workspace/static-artifacts/`.

## Configuration: runtime environment vars

### `RELEASE_ID`
### `/etc/heroku/release_id` or `RELEASE_ID`

**Required.** Should be provided by the runtime environment, such as a UUID or version number.
**Required.** Should be provided by the runtime environment, such as a UUID or version number, either set in the file `/etc/heroku/release_id`, or as the environment variable `RELEASE_ID`.

Artifacts are stored at the `STATIC_ARTIFACTS_URL` with the name `release-<RELEASE_ID>.tgz`.

Expand Down
11 changes: 3 additions & 8 deletions buildpacks/release-phase/src/bin/load-release-artifacts.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
// Required due to: https://github.com/rust-lang/rust/issues/95513
#![allow(unused_crate_dependencies)]

use std::{collections::HashMap, env, path::Path};
use std::{collections::HashMap, path::Path};

use libcnb::data::exec_d::ExecDProgramOutputKey;
use libcnb::data::exec_d_program_output_key;
use libcnb::exec_d::write_exec_d_program_output;

use release_artifacts::load;
use release_artifacts::{capture_env, load};

#[tokio::main]
async fn main() {
let source_dir = Path::new("static-artifacts");

let mut env = HashMap::new();
for (key, value) in env::vars() {
if key.starts_with("STATIC_ARTIFACTS_") || key == "RELEASE_ID" {
env.insert(key, value);
}
}
let env = capture_env(Path::new("/etc/heroku"));

match load(&env, source_dir).await {
Ok(loaded_key) => {
Expand Down
11 changes: 3 additions & 8 deletions buildpacks/release-phase/src/bin/save-release-artifacts.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Required due to: https://github.com/rust-lang/rust/issues/95513
#![allow(unused_crate_dependencies)]

use std::{collections::HashMap, env, path::Path};
use std::{env, path::Path};

use release_artifacts::save;
use release_artifacts::{capture_env, save};

#[tokio::main]
async fn main() {
Expand All @@ -14,12 +14,7 @@ async fn main() {
}
let source_dir = Path::new(&args[1]);

let mut env = HashMap::new();
for (key, value) in env::vars() {
if key.starts_with("STATIC_ARTIFACTS_") || key == "RELEASE_ID" {
env.insert(key, value);
}
}
let env = capture_env(Path::new("/etc/heroku"));

match save(&env, source_dir).await {
Ok(()) => {
Expand Down
70 changes: 63 additions & 7 deletions common/release_artifacts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use flate2::{read::GzDecoder, Compression, GzBuilder};
use regex::Regex;
use std::{
collections::HashMap,
env,
fs::{self, File},
hash::BuildHasher,
io::Write,
io::{Read, Write},
path::{Path, PathBuf},
};
use tar::Archive;
Expand All @@ -20,6 +21,28 @@ use url::Url;
use tokio as _;
use uuid::{self as _, Uuid};

#[must_use]
pub fn capture_env(dyno_metadata_dir: &Path) -> HashMap<String, String> {
let mut env = HashMap::new();
for (key, value) in env::vars() {
if key.starts_with("STATIC_ARTIFACTS_") || key == "RELEASE_ID" {
env.insert(key, value);
}
}
// Override RELEASE_ID with value from the dyno filesystem, when present.
File::open(dyno_metadata_dir.join("release_id"))
.map_or(None, |mut file| {
let mut buffer = String::new();
if file.read_to_string(&mut buffer).is_ok() {
buffer = buffer.trim().to_string();
return Some(buffer);
}
None
})
.map(|dyno_release_id| env.insert("RELEASE_ID".to_owned(), dyno_release_id));
env
}

pub async fn save<S: BuildHasher>(
env: &HashMap<String, String, S>,
dir: &Path,
Expand Down Expand Up @@ -440,7 +463,7 @@ mod tests {
collections::HashMap,
env,
fs::{self, File},
io::Read,
io::{Read, Write},
path::Path,
};

Expand All @@ -453,13 +476,46 @@ mod tests {
use aws_smithy_types::body::SdkBody;

use crate::{
create_archive, detect_storage_scheme, download_specific_or_latest_with_client,
download_with_client, errors::ReleaseArtifactsError, extract_archive,
find_latest_with_client, generate_archive_name, generate_file_storage_location,
generate_s3_client, generate_s3_storage_location, guard_file, guard_s3, load,
make_s3_test_credentials, parse_s3_url, save, upload_with_client,
capture_env, create_archive, detect_storage_scheme,
download_specific_or_latest_with_client, download_with_client,
errors::ReleaseArtifactsError, extract_archive, find_latest_with_client,
generate_archive_name, generate_file_storage_location, generate_s3_client,
generate_s3_storage_location, guard_file, guard_s3, load, make_s3_test_credentials,
parse_s3_url, save, upload_with_client,
};

#[test]
fn capture_env_succeeds() {
env::set_var("RELEASE_ID", "test-release-id");
let result = capture_env(Path::new("does-not-exist"));
env::remove_var("RELEASE_ID");
assert_eq!(
result.get("RELEASE_ID"),
Some(&"test-release-id".to_string())
);
}

#[test]
fn capture_env_with_metadata_file_succeeds() {
let unique = Uuid::new_v4();
let dyno_metadata_dir = format!("dyno-metadata-for-test-{unique}");
let dyno_metadata_path = Path::new(&dyno_metadata_dir);
fs::create_dir_all(dyno_metadata_path).expect("dyno metadata dir should be created");
let release_id_path = dyno_metadata_path.join("release_id");
File::create(&release_id_path)
.and_then(|mut file| file.write_all(b"test-release-id-from-file"))
.expect("dyno metadata file shoud be written");

env::set_var("RELEASE_ID", "test-release-id-from-env");
let result = capture_env(dyno_metadata_path);
env::remove_var("RELEASE_ID");
assert_eq!(
result.get("RELEASE_ID"),
Some(&"test-release-id-from-file".to_string())
);
fs::remove_dir_all(dyno_metadata_path).unwrap_or_default();
}

#[tokio::test]
async fn save_file_url_succeeds() {
let unique = Uuid::new_v4();
Expand Down
Loading