-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmod.rs
More file actions
415 lines (335 loc) · 15.6 KB
/
mod.rs
File metadata and controls
415 lines (335 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use std::{env, path::Path, sync::Arc};
use clap::{Parser, Subcommand, ValueEnum};
use directories::ProjectDirs;
use snafu::{OptionExt, ResultExt, Snafu};
use stackable_cockpit::{
constants::{HELM_REPO_NAME_DEV, HELM_REPO_NAME_STABLE, HELM_REPO_NAME_TEST},
helm,
platform::operator::{
ChartSourceType, listener_operator::determine_and_store_listener_class_preset,
},
utils::path::{
IntoPathOrUrl, IntoPathsOrUrls, ParsePathsOrUrls, PathOrUrl, PathOrUrlParseError,
},
xfer::{self, cache::Settings},
};
use tracing::{Level, instrument};
use tracing_indicatif::indicatif_eprintln;
use crate::{
args::{CommonFileArgs, CommonOperatorConfigsArgs, CommonRepoArgs},
cmds::{cache, completions, debug, demo, operator, release, stack, stacklet, version},
config::UserConfig,
constants::{
DEMOS_REPOSITORY_DEMOS_SUBPATH, DEMOS_REPOSITORY_STACKS_SUBPATH, DEMOS_REPOSITORY_URL_BASE,
ENV_KEY_DEMO_FILES, ENV_KEY_RELEASE_FILES, ENV_KEY_STACK_FILES, REMOTE_RELEASE_FILE,
USER_DIR_APPLICATION_NAME, USER_DIR_ORGANIZATION_NAME, USER_DIR_QUALIFIER,
},
output::{ErrorContext, Output, ResultContext},
release_check,
};
#[derive(Debug, Snafu)]
pub enum Error {
#[snafu(display("failed to execute operator (sub)command"))]
Operator { source: operator::CmdError },
#[snafu(display("failed to execute release (sub)command"))]
Release { source: release::CmdError },
#[snafu(display("failed to execute stack (sub)command"))]
Stack { source: stack::CmdError },
#[snafu(display("failed to execute stacklet (sub)command"))]
Stacklet { source: stacklet::CmdError },
#[snafu(display("failed to execute demo (sub)command"))]
Demo { source: demo::CmdError },
#[snafu(display("failed to execute completions (sub)command"))]
Completions { source: completions::CmdError },
#[snafu(display("failed to execute cache (sub)command"))]
Cache { source: cache::CmdError },
#[snafu(display("failed to execute debug (sub)command"))]
Debug { source: debug::CmdError },
#[snafu(display("failed to execute version (sub)command"))]
Version { source: version::CmdError },
#[snafu(display("failed to add Helm repositories"))]
AddHelmRepos { source: helm::Error },
#[snafu(display("failed to retrieve cache settings"))]
RetrieveCacheSettings { source: CacheSettingsError },
#[snafu(display("failed to auto-purge cache"))]
AutoPurgeCache { source: xfer::Error },
#[snafu(display("failed to initialize transfer client"))]
InitializeTransferClient { source: xfer::Error },
#[snafu(display("failed to retrieve XDG directories"))]
RetrieveXdgDirectories,
}
#[derive(Debug, Snafu)]
#[snafu(module)]
pub enum CacheSettingsError {
#[snafu(display("unable to resolve user directories"))]
UserDir,
}
#[derive(Debug, Parser)]
#[command(author, version, about, propagate_version = true)]
pub struct Cli {
/// Log level this application uses
#[arg(short, long, global = true)]
pub log_level: Option<Level>,
/// Do not cache the remote (default) demo, stack and release files
#[arg(
long,
global = true,
long_help = "Do not cache the remote (default) demo, stack and release files
Cached files are saved at '$XDG_CACHE_HOME/stackablectl', which is usually
'$HOME/.cache/stackablectl' when not explicitly set."
)]
pub no_cache: bool,
#[command(flatten)]
pub files: CommonFileArgs,
#[command(flatten)]
pub repos: CommonRepoArgs,
#[command(flatten)]
pub operator_configs: CommonOperatorConfigsArgs,
#[command(subcommand)]
pub subcommand: Command,
}
impl Cli {
/// Returns a list of demo files, consisting of entries which are either a path or URL. The list of files combines
/// the default demo file URL constructed from [`DEMOS_REPOSITORY_URL_BASE`] and the provided branch, files provided
/// by the ENV variable [`ENV_KEY_DEMO_FILES`], and lastly, files provided by the CLI argument `--demo-file`.
pub fn get_demo_files(&self, branch: &str) -> Result<Vec<PathOrUrl>, PathOrUrlParseError> {
let branch_url =
format!("{DEMOS_REPOSITORY_URL_BASE}/{branch}/{DEMOS_REPOSITORY_DEMOS_SUBPATH}");
let mut files = get_files(&branch_url, ENV_KEY_DEMO_FILES)?;
let arg_files = self.files.demo_files.clone().into_paths_or_urls()?;
files.extend(arg_files);
Ok(files)
}
/// Returns a list of stack files, consisting of entries which are either a path or URL. The list of files combines
/// the default stack file URL constructed from [`DEMOS_REPOSITORY_URL_BASE`] and the provided branch, files provided
/// by the ENV variable [`ENV_KEY_STACK_FILES`], and lastly, files provided by the CLI argument `--stack-file`.
pub fn get_stack_files(&self, branch: &str) -> Result<Vec<PathOrUrl>, PathOrUrlParseError> {
let branch_url =
format!("{DEMOS_REPOSITORY_URL_BASE}/{branch}/{DEMOS_REPOSITORY_STACKS_SUBPATH}");
let mut files = get_files(&branch_url, ENV_KEY_STACK_FILES)?;
let arg_files = self.files.stack_files.clone().into_paths_or_urls()?;
files.extend(arg_files);
Ok(files)
}
/// Returns a list of release files, consisting of entries which are either a path or URL. The list of files
/// combines the default demo file URL, [`REMOTE_RELEASE_FILE`], files provided by the ENV variable
/// [`ENV_KEY_RELEASE_FILES`], and lastly, files provided by the CLI argument `--release-file`.
pub fn get_release_files(&self) -> Result<Vec<PathOrUrl>, PathOrUrlParseError> {
let mut files = get_files(REMOTE_RELEASE_FILE, ENV_KEY_RELEASE_FILES)?;
let arg_files = self.files.release_files.clone().into_paths_or_urls()?;
files.extend(arg_files);
Ok(files)
}
/// Adds the default (or custom) Helm repository URLs. Internally this calls the Helm SDK written in Go through the
/// `go-helm-wrapper`.
pub fn add_helm_repos(&self) -> Result<(), helm::Error> {
tracing::info!("Add Helm repos");
// Stable repository
helm::add_repo(HELM_REPO_NAME_STABLE, &self.repos.helm_repo_stable)?;
// Test repository
helm::add_repo(HELM_REPO_NAME_TEST, &self.repos.helm_repo_test)?;
// Dev repository
helm::add_repo(HELM_REPO_NAME_DEV, &self.repos.helm_repo_dev)?;
Ok(())
}
fn cache_settings(&self, cache_directory: &Path) -> Result<Settings, CacheSettingsError> {
if self.no_cache {
tracing::debug!("Cache disabled");
Ok(Settings::disabled())
} else {
tracing::debug!(
cache_directory = %cache_directory.to_string_lossy(),
"Setting cache directory"
);
Ok(Settings::disk(cache_directory))
}
}
#[allow(clippy::result_large_err)]
fn xdg_directories() -> Result<ProjectDirs, Error> {
ProjectDirs::from(
USER_DIR_QUALIFIER,
USER_DIR_ORGANIZATION_NAME,
USER_DIR_APPLICATION_NAME,
)
.context(RetrieveXdgDirectoriesSnafu)
}
#[instrument(skip_all)]
pub async fn run(self) -> Result<String, Error> {
// FIXME (Techassi): There might be a better way to handle this with
// the match later in this function.
// Add Helm repos only when required
match &self.subcommand {
Command::Completions(_) => (),
Command::Cache(_) => (),
_ => self.add_helm_repos().context(AddHelmReposSnafu)?,
}
let xdg_directories = Cli::xdg_directories()?;
// TODO (@Techassi): Move this file name to a constant
let user_config_path = xdg_directories.config_dir().join("config.toml");
let user_config = UserConfig::from_file_or_default(user_config_path).unwrap();
let cache_settings = self
.cache_settings(xdg_directories.cache_dir())
.context(RetrieveCacheSettingsSnafu)?;
let transfer_client = xfer::Client::new(cache_settings)
.await
.context(InitializeTransferClientSnafu)?;
// Only run the cache auto-purge when the user executes ANY command other than the cache
// commands.
if !matches!(self.subcommand, Command::Cache(_)) {
transfer_client
.auto_purge()
.await
.context(AutoPurgeCacheSnafu)?;
}
// Make transfer client sharable across multiple futures/threads
let transfer_client = Arc::new(transfer_client);
determine_and_store_listener_class_preset(
self.operator_configs.listener_class_preset.as_ref(),
)
.await;
// Only run the version check in the background if the user runs ANY other command than
// the version command and the check isn't disabled via the user config. Also only report
// if the current version is outdated.
let check_version =
!matches!(self.subcommand, Command::Version(_)) && user_config.version.check_enabled;
let release_check_future =
release_check::version_notice_output(transfer_client.clone(), check_version, true);
let release_check_future =
tokio::time::timeout(std::time::Duration::from_secs(2), release_check_future);
#[rustfmt::skip]
let command_future = async move {
match self.subcommand {
Command::Operator(ref args) => args.run(&self).await.context(OperatorSnafu),
Command::Release(ref args) => args.run(&self, transfer_client).await.context(ReleaseSnafu),
Command::Stack(ref args) => args.run(&self, transfer_client).await.context(StackSnafu),
Command::Stacklet(ref args) => args.run().await.context(StackletSnafu),
Command::Demo(ref args) => args.run(&self, transfer_client).await.context(DemoSnafu),
Command::Completions(ref args) => args.run().context(CompletionsSnafu),
Command::Cache(ref args) => args.run(transfer_client).await.context(CacheSnafu),
Command::ExperimentalDebug(ref args) => args.run().await.context(DebugSnafu),
Command::Version(ref args) => args.run(transfer_client).await.context(VersionSnafu),
}
};
// Run the version check and the actual command in parallel and not sequentially. This is
// done to not abort/stall execution when the version check couldn't be performed (because
// of network issues for example). We also optimistically run the version check and don't
// hard-error below when the check failed.
let (release_check_result, command_result) =
tokio::join!(release_check_future, command_future);
// NOTE (@Techassi): This is freaking ugly (I'm sorry) but there seems to be no other better
// way to achieve what we want without reworking the entire output handling/rendering
// mechanism.
// FIXME (@Techassi): This currently messes up any structured output. This is also not
// trivially solved as explained above.
match command_result {
Ok(command_output) => {
let output = if let Ok(Ok(Some(release_check_output))) = release_check_result {
format!("{command_output}\n\n{release_check_output}")
} else {
command_output
};
Ok(output)
}
Err(err) => {
if let Ok(Ok(Some(release_check_output))) = release_check_result {
indicatif_eprintln!("{release_check_output}\n");
}
Err(err)
}
}
}
// Output utility functions
pub fn result() -> Output<ResultContext> {
Output::new(ResultContext::default(), true).expect("Failed to create output renderer")
}
pub fn error() -> Output<ErrorContext> {
Output::new(ErrorContext::default(), true).expect("Failed to create output renderer")
}
pub fn chart_type(&self) -> ChartSourceTypeArg {
self.repos.chart_source.clone()
}
}
#[derive(Debug, Subcommand)]
pub enum Command {
/// Interact with single operator instead of the full platform
#[command(alias("op"))]
Operator(operator::OperatorArgs),
/// Interact with all operators of the platform which are released together
#[command(alias("re"))]
Release(release::ReleaseArgs),
/// Interact with stacks, which are ready-to-use product combinations
#[command(alias("st"))]
Stack(stack::StackArgs),
/// Interact with deployed stacklets, which are bundles of resources and
/// containers required to run the product.
#[command(aliases(["stl", "sl"]))]
#[command(
long_about = "Interact with deployed stacklets, which are bundles of resources and containers
required to run the product.
Each stacklet consists of init containers, app containers, sidecar containers
and additional Kubernetes resources like StatefulSets, ConfigMaps, Services and
CRDs."
)]
Stacklet(stacklet::StackletArgs),
/// Interact with demos, which are end-to-end usage demonstrations of the Stackable data platform
Demo(demo::DemoArgs),
/// Generate shell completions for this tool
#[command(alias("comp"))]
Completions(completions::CompletionsArgs),
/// Interact with locally cached files
Cache(cache::CacheArgs),
/// EXPERIMENTAL: Launch a debug container for a Pod
#[command(long_about = "EXPERIMENTAL: Launch a debug container for a Pod.
This container will have access to the same data volumes as the primary container.")]
ExperimentalDebug(debug::DebugArgs),
/// Retrieve version data of the stackablectl installation
Version(version::VersionArguments),
}
#[derive(Clone, Debug, Default, ValueEnum)]
pub enum OutputType {
/// Print output formatted as plain text
Plain,
/// Print output formatted as a table
#[default]
Table,
/// Print output formatted as JSON
Json,
/// Print output formatted as YAML
Yaml,
}
/// Returns a list of paths or urls based on the default (remote) file and
/// files provided via the env variable.
fn get_files(default_file: &str, env_key: &str) -> Result<Vec<PathOrUrl>, PathOrUrlParseError> {
let mut files: Vec<PathOrUrl> = vec![default_file.into_path_or_url()?];
let env_files = match env::var(env_key) {
Ok(env_files) => env_files.parse_paths_or_urls()?,
Err(_) => vec![],
};
files.extend(env_files);
Ok(files)
}
/// Enum used for resolving the argument for chart source type. This will be
/// mapped to ChartSourceType (see below): the reason why we don't have one
/// enum is to avoid having to add clap dependencies to stackable-cockpit
/// for the ValueEnum macro.
#[derive(Clone, Debug, Default, ValueEnum)]
pub enum ChartSourceTypeArg {
/// OCI registry
#[default]
OCI,
/// index.yaml-based repositories: resolution (dev, test, stable) is based on the version and thus will be operator-specific
Repo,
}
impl From<ChartSourceTypeArg> for ChartSourceType {
/// Resolves the enum used by clap/arg-resolution to the core type used in
/// stackable-cockpit. For the (index.yaml-based) repo case this core type cannot be
/// decorated with meaningful information as that would be operator-specific
/// i.e. we cannot resolve *which* (index.yaml-based) repo to use until we have inspected
/// the operator version. Hence just a simple mapping.
fn from(cli_enum: ChartSourceTypeArg) -> Self {
match cli_enum {
ChartSourceTypeArg::OCI => ChartSourceType::OCI,
ChartSourceTypeArg::Repo => ChartSourceType::Repo,
}
}
}