Skip to content

Commit 788f49d

Browse files
authored
Add Kubernetes check and ulid (#260)
1 parent 05b55a6 commit 788f49d

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

server/src/banner.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn print(config: &Config, meta: StorageMetadata) {
2727
let scheme = config.parseable.get_scheme();
2828
status_info(config, &scheme, meta.deployment_id);
2929
storage_info(config);
30-
about::print();
30+
about::print(meta);
3131
println!();
3232
}
3333

@@ -95,6 +95,7 @@ pub mod about {
9595
use crossterm::style::Stylize;
9696
use std::fmt;
9797

98+
use crate::storage::StorageMetadata;
9899
use crate::utils::update;
99100

100101
pub enum ParseableVersion {
@@ -111,7 +112,11 @@ pub mod about {
111112
}
112113
}
113114

114-
pub fn print_about(current_version: semver::Version, commit_hash: String) {
115+
pub fn print_about(
116+
current_version: semver::Version,
117+
commit_hash: String,
118+
meta: StorageMetadata,
119+
) {
115120
eprint!(
116121
"
117122
{}
@@ -120,7 +125,7 @@ pub mod about {
120125
current_version,
121126
);
122127

123-
if let Ok(latest_release) = update::get_latest() {
128+
if let Ok(latest_release) = update::get_latest(meta) {
124129
if latest_release.version > current_version {
125130
print_latest_release(latest_release);
126131
}
@@ -144,13 +149,13 @@ pub mod about {
144149
eprint!("{}", fmt_latest_version.red());
145150
}
146151

147-
pub fn print() {
152+
pub fn print(meta: StorageMetadata) {
148153
// print current version
149154
let current = current();
150155

151156
match current.0 {
152157
ParseableVersion::Version(current_version) => {
153-
print_about(current_version, current.1);
158+
print_about(current_version, current.1, meta);
154159
}
155160
ParseableVersion::Prerelease(current_prerelease) => {
156161
eprintln!(

server/src/utils.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,16 @@ pub mod uid {
134134

135135
pub mod update {
136136
use crate::banner::about::current;
137+
use std::env;
137138
use std::{path::Path, time::Duration};
138139

139140
use anyhow::anyhow;
140141
use chrono::{DateTime, Utc};
142+
use ulid::Ulid;
143+
144+
use crate::storage::StorageMetadata;
145+
146+
static K8S_ENV_TO_CHECK: &str = "KUBERNETES_SERVICE_HOST";
141147

142148
pub struct LatestRelease {
143149
pub version: semver::Version,
@@ -151,22 +157,38 @@ pub mod update {
151157
"Native".to_string()
152158
}
153159

160+
fn is_k8s() -> String {
161+
if env::var(K8S_ENV_TO_CHECK).is_ok() {
162+
return "Kubernetes".to_string();
163+
}
164+
"".to_string()
165+
}
166+
167+
fn platform() -> String {
168+
let mut platform = is_k8s();
169+
if platform.is_empty() {
170+
platform = is_docker();
171+
}
172+
platform
173+
}
174+
154175
// User Agent for Download API call
155-
// Format: Parseable/<version>/<commit_hash> (OS; Platform)
156-
fn user_agent() -> String {
176+
// Format: Parseable/<UID>/<version>/<commit_hash> (<OS>; <Platform>)
177+
fn user_agent(uid: Ulid) -> String {
157178
let info = os_info::get();
158179
format!(
159-
"Parseable/{}/{} ({}; {})",
180+
"Parseable/{}/{}/{} ({}; {})",
181+
uid,
160182
current().0,
161183
current().1,
162184
info.os_type(),
163-
is_docker()
185+
platform()
164186
)
165187
}
166188

167-
pub fn get_latest() -> Result<LatestRelease, anyhow::Error> {
189+
pub fn get_latest(meta: StorageMetadata) -> Result<LatestRelease, anyhow::Error> {
168190
let agent = ureq::builder()
169-
.user_agent(user_agent().as_str())
191+
.user_agent(user_agent(meta.deployment_id).as_str())
170192
.timeout(Duration::from_secs(8))
171193
.build();
172194

0 commit comments

Comments
 (0)