From 2a2e743f6ae99fa9c1744f03dc23afa9b776d932 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Fri, 16 Apr 2021 17:34:52 +0200 Subject: [PATCH] Allow the configuration of some default values at compile time using environment variables --- CHANGELOG.md | 6 ++++++ README.md | 8 ++++++++ acmed/build.rs | 20 ++++++++++++++++++++ acmed/src/main.rs | 10 +++++----- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index efd9f15..f6481dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Added +- Allow the configuration of some default values at compile time using environment variables. + + ## [0.16.0] - 2020-11-11 ### Added diff --git a/README.md b/README.md index 66c987c..4ab4b18 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,14 @@ For example, you can build statically linked binaries using the `openssl_vendore make FEATURES="openssl_vendored" TARGET="x86_64-unknown-linux-musl" ``` +The following environment variables can be used to change default values at compile time: + +- `ACMED_DEFAULT_ACCOUNTS_DIR`: directory where account files are stored (default to `/etc/acmed/accounts`) +- `ACMED_DEFAULT_CERT_DIR`: directory where certificates and private keys are stored (default to `/etc/acmed/certs`) +- `ACMED_DEFAULT_CERT_FORMAT`: format for certificates and private keys files names (default to `{{name}}_{{key_type}}.{{file_type}}.{{ext}}`) +- `ACMED_DEFAULT_CONFIG_FILE`: main configuration file (default to `/etc/acmed/acmed.toml`) +- `ACMED_DEFAULT_PID_FILE`: main process PID file (default to `/var/run/acmed.pid`) + ### Packaging Most of the time, when packaging, you want to install the program in a dedicated directory. This is possible using the `DESTDIR` variable. diff --git a/acmed/build.rs b/acmed/build.rs index fa66262..157b511 100644 --- a/acmed/build.rs +++ b/acmed/build.rs @@ -7,6 +7,14 @@ use std::fs::File; use std::io::prelude::*; use std::path::PathBuf; +macro_rules! set_env_var_if_absent { + ($name: expr, $default_value: expr) => {{ + if let Err(_) = env::var($name) { + set_rustc_env_var!($name, $default_value); + } + }}; +} + macro_rules! set_rustc_env_var { ($name: expr, $value: expr) => {{ println!("cargo:rustc-env={}={}", $name, $value); @@ -73,7 +81,19 @@ fn set_target() { }; } +fn set_default_values() { + set_env_var_if_absent!("ACMED_DEFAULT_ACCOUNTS_DIR", "/etc/acmed/accounts"); + set_env_var_if_absent!("ACMED_DEFAULT_CERT_DIR", "/etc/acmed/certs"); + set_env_var_if_absent!( + "ACMED_DEFAULT_CERT_FORMAT", + "{{name}}_{{key_type}}.{{file_type}}.{{ext}}" + ); + set_env_var_if_absent!("ACMED_DEFAULT_CONFIG_FILE", "/etc/acmed/acmed.toml"); + set_env_var_if_absent!("ACMED_DEFAULT_PID_FILE", "/var/run/acmed.pid"); +} + fn main() { set_target(); set_lock(); + set_default_values(); } diff --git a/acmed/src/main.rs b/acmed/src/main.rs index a1276e5..a06bea3 100644 --- a/acmed/src/main.rs +++ b/acmed/src/main.rs @@ -23,11 +23,11 @@ mod storage; pub const APP_NAME: &str = "ACMEd"; pub const APP_VERSION: &str = env!("CARGO_PKG_VERSION"); -pub const DEFAULT_PID_FILE: &str = "/var/run/acmed.pid"; -pub const DEFAULT_CONFIG_FILE: &str = "/etc/acmed/acmed.toml"; -pub const DEFAULT_ACCOUNTS_DIR: &str = "/etc/acmed/accounts"; -pub const DEFAULT_CERT_DIR: &str = "/etc/acmed/certs"; -pub const DEFAULT_CERT_FORMAT: &str = "{{name}}_{{key_type}}.{{file_type}}.{{ext}}"; +pub const DEFAULT_ACCOUNTS_DIR: &str = env!("ACMED_DEFAULT_ACCOUNTS_DIR"); +pub const DEFAULT_CERT_DIR: &str = env!("ACMED_DEFAULT_CERT_DIR"); +pub const DEFAULT_CERT_FORMAT: &str = env!("ACMED_DEFAULT_CERT_FORMAT"); +pub const DEFAULT_CONFIG_FILE: &str = env!("ACMED_DEFAULT_CONFIG_FILE"); +pub const DEFAULT_PID_FILE: &str = env!("ACMED_DEFAULT_PID_FILE"); pub const DEFAULT_SLEEP_TIME: u64 = 3600; pub const DEFAULT_POOL_TIME: u64 = 5000; pub const DEFAULT_CSR_DIGEST: HashFunction = HashFunction::Sha256;