From d5681018ddf244490ff57a03e2e2135ac0a691c0 Mon Sep 17 00:00:00 2001 From: Aleksandr Trushkin Date: Mon, 11 Jan 2021 01:03:53 +0300 Subject: [PATCH] print app version --- .drone.jsonnet | 10 +++++++--- .drone.yml | 4 +++- Cargo.lock | 2 +- Cargo.toml | 2 +- build.rs | 41 +++++++++++++++++++++++++++++++++++++++++ src/main.rs | 25 +++++++++++++++++++++++-- 6 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 build.rs diff --git a/.drone.jsonnet b/.drone.jsonnet index ae51f5a..ccecdba 100644 --- a/.drone.jsonnet +++ b/.drone.jsonnet @@ -62,15 +62,19 @@ local host_volume(name, path) = { "build", depends=["test"], commands=["cargo build" + flags], + env={ + GIT_REVISION: "${DRONE_COMMIT:0:8}", + GIT_BRANCH: "${DRONE_COMMIT_BRANCH}", + }, ), step( "deploy", depends=["build"], commands=["sh scripts/deploy.sh"], env={ - "TARGET_DIR": "/cache/target/" + target_arch + "/release", - "SSH_PRIVATE_KEY": {"from_secret": "ssh_pk_base64"}, - "SSH_USER": {"from_secret": "SSH_USER"}, + TARGET_DIR: "/cache/target/" + target_arch + "/release", + SSH_PRIVATE_KEY: {"from_secret": "ssh_pk_base64"}, + SSH_USER: {"from_secret": "SSH_USER"}, }, ), ], diff --git a/.drone.yml b/.drone.yml index b1f7b67..a6d9694 100644 --- a/.drone.yml +++ b/.drone.yml @@ -47,6 +47,8 @@ steps: environment: APP_NAME: altherego CARGO_TARGET_DIR: /cache/target + GIT_BRANCH: ${DRONE_COMMIT_BRANCH} + GIT_REVISION: ${DRONE_COMMIT:0:8} volumes: - name: cargo path: /usr/local/cargo @@ -91,6 +93,6 @@ volumes: --- kind: signature -hmac: 9636ad34a72c9a41ad90c91729e8b711579563345ae485a4ea2ee40eb3ff46e4 +hmac: a942d89af2c38916d55ebe377709febf08145ce8e08a2b585dda8a1c251eaca0 ... diff --git a/Cargo.lock b/Cargo.lock index 4ddbf13..d153938 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,7 +11,7 @@ dependencies = [ [[package]] name = "altherego" -version = "0.1.0" +version = "0.9.5" dependencies = [ "env_logger", "envconfig", diff --git a/Cargo.toml b/Cargo.toml index 5bb499b..8d137c7 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "altherego" -version = "0.1.0" +version = "0.9.5" authors = ["Aleksandr Trushkin "] edition = "2018" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..ecb1a94 --- /dev/null +++ b/build.rs @@ -0,0 +1,41 @@ +use std::env; + +fn main() { + let rev = get_value_from_env("GIT_VERSION") + .or_else(|| get_value_from_command("git", &["rev-parse", "--short", "HEAD"])) + .unwrap_or_else(|| "unknown".to_owned()); + + let branch = get_value_from_env("GIT_BRANCH") + .or_else(|| get_value_from_command("git", &["rev-parse", "--abbrev-ref", "HEAD"])) + .unwrap_or_else(|| "unknown".to_owned()); + + println!("cargo:rustc-env=GIT_REVISION={}", rev); + println!("cargo:rustc-env=GIT_BRANCH={}", branch); + println!("cargo:rerun-if-env-changed=GIT_REVISION"); +} + +fn get_value_from_env(key: &str) -> Option { + env::var(key).map_or_else(|_| None, |v| Some(v)) +} + +fn get_value_from_command, S: AsRef>( + cmd: &str, + args: I, +) -> Option { + std::process::Command::new(cmd) + .args(args) + .output() + .map_or_else( + |_| None, + |out| { + if !out.status.success() { + return None; + } + + match std::str::from_utf8(&out.stdout) { + Ok(value) => Some(value.to_owned()), + Err(_) => None, + } + }, + ) +} diff --git a/src/main.rs b/src/main.rs index 31763bc..c5cc25d 100755 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,9 @@ use envconfig::Envconfig; use log::{debug, info, warn}; use teloxide::{prelude::*, utils::command::BotCommand}; +const VERSION: &'static str = env!("GIT_REVISION"); +const BRANCH: &'static str = env!("GIT_BRANCH"); + #[tokio::main] async fn main() { debug!("starting the application"); @@ -32,6 +35,7 @@ async fn run() { env_logger::init(); let settings = Settings::init_from_env().expect("reading config values"); + let startup = std::sync::Arc::from(std::time::SystemTime::now()); let bot = teloxide::Bot::builder() .token(&settings.telegram_token) @@ -44,8 +48,10 @@ async fn run() { let cmd: Vec<&str> = cmd.split(" ").collect(); let console_cmd = cmd.get(0).expect("getting console command").to_string(); let arg: String = cmd.get(1).unwrap_or_else(|| &"").to_string(); + // let startup = std::sync::Arc::from(*startup); + let startup = *startup; - async move { handler(cx, command, climate, console_cmd, arg).await } + async move { handler(cx, command, climate, console_cmd, arg, startup).await } }) .await; } @@ -62,6 +68,7 @@ async fn handler( dsn: String, console_command: String, console_arg: String, + startup: std::time::SystemTime, ) -> ResponseResult<()> { let request_id = uuid::Uuid::new_v4(); @@ -73,7 +80,10 @@ async fn handler( match command { Command::Help => cx.answer(Command::descriptions()).send().await?, Command::HostTemperature => { - info!("querying command {} with arg {}", console_command, console_arg); + info!( + "querying command {} with arg {}", + console_command, console_arg + ); let cmd = std::process::Command::new(&console_command) .arg(&console_arg) @@ -130,6 +140,15 @@ async fn handler( )) .await? } + Command::VersionRequest => { + cx.answer_str(format!( + "app version is {}@{}, uptime is {} second(-s)", + VERSION, + BRANCH, + startup.elapsed().unwrap().as_secs() + )) + .await? + } }; Ok(()) @@ -144,4 +163,6 @@ enum Command { RoomTemperature, #[command(description = "temperature of raspberry.")] HostTemperature, + #[command(description = "prints current version.")] + VersionRequest, }