diff --git a/.circleci/config.yml b/.circleci/config.yml index 518f58682..d7a27e46f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -24,7 +24,7 @@ defaults: jobs: audit: docker: - - image: rust:latest + - image: rust:1.70 auth: username: $DOCKER_USER password: $DOCKER_PASS diff --git a/Cargo.lock b/Cargo.lock index faf6ae6a7..87f5c2fbd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -787,6 +787,7 @@ dependencies = [ "futures 0.3.28", "futures-backoff", "futures-util", + "gethostname", "hex", "httparse", "hyper 0.14.26", @@ -794,7 +795,6 @@ dependencies = [ "log", "mockall", "mockito", - "mozsvc-common", "openssl", "rand 0.8.5", "regex", @@ -1900,6 +1900,16 @@ dependencies = [ "version_check", ] +[[package]] +name = "gethostname" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0176e0459c2e4a1fe232f984bca6890e681076abb9934f6cea7c326f3fc47818" +dependencies = [ + "libc", + "windows-targets 0.48.0", +] + [[package]] name = "getrandom" version = "0.1.16" diff --git a/Dockerfile b/Dockerfile index f4377df6e..a8c5fc848 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,5 @@ -FROM rust:1.68-buster as builder +# NOTE: Ensure builder's Rust version matches CI's in .circleci/config.yml +FROM rust:1.70-buster as builder ARG CRATE ADD . /app diff --git a/autopush-common/Cargo.toml b/autopush-common/Cargo.toml index d3f3730e0..53c890a22 100644 --- a/autopush-common/Cargo.toml +++ b/autopush-common/Cargo.toml @@ -55,8 +55,8 @@ url.workspace = true again = "0.1" async-trait = "0.1" +gethostname = "0.4" futures-backoff = "0.1.0" -mozsvc-common = "0.2" woothee = "0.13" [dev-dependencies] diff --git a/autopush-common/src/logging.rs b/autopush-common/src/logging.rs index 76de676d3..11f18719d 100644 --- a/autopush-common/src/logging.rs +++ b/autopush-common/src/logging.rs @@ -1,21 +1,19 @@ -use std::io; +use std::{io, sync::OnceLock, time::Duration}; -use mozsvc_common::{aws::get_ec2_instance_id, get_hostname}; +use gethostname::gethostname; use slog::{self, Drain}; use slog_mozlog_json::MozLogJson; use crate::errors::Result; +static EC2_INSTANCE_ID: OnceLock> = OnceLock::new(); + pub fn init_logging(json: bool) -> Result<()> { let logger = if json { - let hostname = match get_ec2_instance_id() { - Some(v) => v.to_owned(), - None => match get_hostname() { - Ok(v) => v.to_string_lossy().to_string(), - Err(e) => return Err(e.into()), - }, - }; - + let ec2_instance_id = EC2_INSTANCE_ID.get_or_init(|| get_ec2_instance_id().ok()); + let hostname = ec2_instance_id + .clone() + .unwrap_or_else(|| gethostname().to_string_lossy().to_string()); let drain = MozLogJson::new(io::stdout()) .logger_name(format!( "{}-{}", @@ -59,3 +57,17 @@ pub fn init_test_logging() { slog_scope::set_global_logger(logger).cancel_reset(); slog_stdlog::init().ok(); } + +/// Fetch the EC2 instance-id +/// +/// NOTE: This issues a blocking web request +fn get_ec2_instance_id() -> reqwest::Result { + let client = reqwest::blocking::Client::builder() + .timeout(Duration::from_secs(1)) + .build()?; + client + .get("http://169.254.169.254/latest/meta-data/instance-id") + .send()? + .error_for_status()? + .text() +}