depl 2.4.3

Toolkit for a bunch of local and remote CI/CD actions
Documentation
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
//! Pipelines module.
//!
//! Pipeline is a list of actions.

use anyhow::{anyhow, bail};
use colored::Colorize;
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::exit;

use crate::actions::{Action, DefinedAction, UsedAction};
use crate::bmap;
use crate::cmd::{CatPipelineArgs, CheckProjectPipelineArgs, EditPipelineArgs, NewPipelineArgs, WithPipelineArgs};
use crate::cmd::{ExportPipelineArgs, RemovePipelineArgs};
use crate::entities::ansible_opts::AnsibleOpts;
use crate::entities::compose_opts::ComposeOpts;
use crate::entities::containered_opts::ContaineredOpts;
use crate::entities::custom_command::CustomCommand;
use crate::entities::driver::PipelineDriver;
use crate::entities::environment::RunEnvironment;
use crate::entities::github_cicd_opts::GitHubOpts;
use crate::entities::gitlab_cicd_opts::GitLabOpts;
use crate::entities::info::StrToInfo;
use crate::entities::info::{PipelineInfo, info2str, str2info};
use crate::entities::placements::Placement;
use crate::entities::requirements::Requirement;
use crate::entities::systemd_opts::SystemdOpts;
use crate::globals::DeployerGlobalConfig;
use crate::project::DeployerProjectOptions;
use crate::run::{prepare_artifacts_folder, sync_run_folder};
use crate::rw::read_checked;
use crate::tui::setup::specify_pipeline_short_name;

/// Described pipeline.
#[derive(Deserialize, Serialize, Eq, Clone)]
pub struct DescribedPipeline {
  /// Pipeline name.
  ///
  /// When you're using project's assigned pipelines, make sure that
  /// title is simple enough to use it as argument to `depl run name1,name2,name3,..`.
  pub title: String,

  /// Pipeline description.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub desc: Option<String>,

  /// Short name and version.
  #[serde(serialize_with = "info2str", deserialize_with = "str2info")]
  pub info: PipelineInfo,

  /// List of tags (to use with `grep` when searching through `depl ls pipelines`).
  #[serde(default, skip_serializing_if = "Vec::is_empty")]
  pub tags: Vec<String>,

  /// This field is used only in projects.
  ///
  /// If set to `true`, and no pipeline specified to `depl run`, runs automatically.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub default: Option<bool>,

  /// Copy only these files instead of all project files.
  ///
  /// Ideal for copying artifacts or a limited number of project files.
  #[serde(default, skip_serializing_if = "Vec::is_empty")]
  pub copy_only: Vec<String>,

  /// Used actions with execution order.
  #[serde(default, skip_serializing_if = "Vec::is_empty")]
  pub actions: Vec<UsedAction>,

  /// Relative artifacts sources inside build folder and destinations inside
  /// `artifacts` folder in project's directory.
  #[serde(default, skip_serializing_if = "Vec::is_empty")]
  pub artifacts: Vec<Placement>,

  /// Specify exclusive execution tag to save unique cache.
  ///
  /// If set to any string, Deployer will perform this pipeline only in special build folder.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub exclusive_exec_tag: Option<String>,

  /// Pipeline driver.
  ///
  /// Defines who will run the pipeline: Deployer on itself, or shell.
  ///
  /// Defaults to Deployer.
  #[serde(default, skip_serializing_if = "PipelineDriver::is_deployer")]
  pub driver: PipelineDriver,

  /// Indicates that this pipeline should be executed inside containered environment.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub containered_opts: Option<ContaineredOpts>,

  /// Indicates that this pipeline should be executed with Docker Compose.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub compose_opts: Option<ComposeOpts>,

  /// Indicates that this pipeline should be executed with Ansible.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub ansible_opts: Option<AnsibleOpts>,

  /// Additional options for exporting this pipeline as systemd service job.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub systemd_opts: Option<SystemdOpts>,

  /// Additional options for exporting this pipeline as GitHub Actions.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub gh_opts: Option<GitHubOpts>,

  /// Additional options for exporting this pipeline as GitLab CI/CD.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub gl_opts: Option<GitLabOpts>,
}

impl PartialEq for DescribedPipeline {
  fn eq(&self, other: &Self) -> bool {
    self.info.eq(&other.info)
  }
}

impl Ord for DescribedPipeline {
  fn cmp(&self, other: &Self) -> std::cmp::Ordering {
    self.info.cmp(&other.info)
  }
}

#[allow(clippy::non_canonical_partial_ord_impl)]
impl PartialOrd for DescribedPipeline {
  fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
    Some(self.info.cmp(&other.info))
  }
}

/// Places pipeline artifacts in the `artifacts` folder inside project's directory.
///
/// `panic_when_not_found` is set to `false` on all function's usages now.
pub fn place_artifacts(
  env: &RunEnvironment,
  placements: Vec<(String, String)>,
  panic_when_not_found: bool,
) -> anyhow::Result<()> {
  for (from, to) in placements {
    let artifact_path = env.run_dir.join(from);
    if !std::fs::exists(artifact_path.clone())? {
      if panic_when_not_found {
        panic!("There is no such artifact: {artifact_path:?}!");
      }
    } else if artifact_path.as_path().is_dir() || artifact_path.as_path().is_file() {
      crate::rw::copy_all(
        artifact_path.as_path(),
        artifact_path.as_path(),
        env.artifacts_dir.join(to).as_path(),
        &[] as &[&str],
      )?;
    }
  }

  Ok(())
}

impl DescribedPipeline {
  /// Returns all custom commands as `bash`-like commands in this pipeline.
  pub fn return_all_cmds(&self, definitions: &BTreeSet<DefinedAction>) -> anyhow::Result<Vec<String>> {
    let mut cmds = vec![];

    for action in &self.actions {
      match &action.definition(definitions)?.action {
        Action::Interrupt => {}
        Action::SyncToRemote { .. } => cmds.push("<sync-to-remote>".to_string()),
        Action::SyncFromRemote { .. } => cmds.push("<sync-from-remote>".to_string()),
        Action::Custom(cmd) => cmds.push(cmd.cmd.to_owned()),
        Action::Test(check) => cmds.push(format!("<test> {}", check.command.cmd)),
        Action::Staged(cmd) => cmds.push(cmd.command.cmd.to_string()),
        Action::Observe(a) => cmds.push(format!("<observe> {}", a.command.cmd)),
        Action::UseFromStorage { .. } => cmds.push("<use-from-storage>".to_string()),
        Action::AddToStorage(_) => cmds.push("<add-to-storage>".to_string()),
        Action::Patch(p) => cmds.push(format!("<patch-with-file> {:?}", p.patch)),
      }
    }

    Ok(cmds)
  }

  /// Tries to satisfy all requirements. Returns true on success and number of checked requirements.
  pub async fn satisfy_all_requirements(
    &self,
    config: &DeployerProjectOptions,
    env: &RunEnvironment<'_>,
  ) -> anyhow::Result<(bool, usize)> {
    use crate::bset;
    use crate::entities::requirements::SatisfyErr;

    #[allow(clippy::mutable_key_type)]
    let mut requirements = bset!();
    let mut unsatisfied = false;
    for action in self.actions.iter() {
      let action = action.definition(&config.actions)?;
      for action_req in &action.requirements {
        requirements.insert(action_req.clone());
      }
    }
    for requirement in requirements.iter() {
      if let Err(e) = requirement.satisfy(env).await {
        unsatisfied = true;
        match e {
          SatisfyErr::Exists(path) => println!("Due to: path `{}` is not exist.", path.to_string_lossy()),
          SatisfyErr::ExistsAny(paths) => {
            let paths = paths
              .iter()
              .map(|p| p.to_string_lossy().to_string())
              .collect::<Vec<_>>();
            println!("Due to: no one of paths `{}` is not exist.", &paths.join("`, `"));
          }
          SatisfyErr::NoBinary(e) => println!("Due to: such binary `{e}` is unavailable."),
          SatisfyErr::Check(output) => {
            println!("Due to: requirement (check) is not satisfied, output:");
            for line in output {
              println!("- {line}");
            }
          }
          SatisfyErr::Remote(e) => println!("Due to: such remote `{e}` is unavailable."),
        }
      }
    }

    Ok((!unsatisfied, requirements.len()))
  }

  /// Run pipeline with Deployer executor.
  pub async fn exec_with_deployer_driver(
    &self,
    config: &DeployerProjectOptions,
    env: &mut RunEnvironment<'_>,
  ) -> anyhow::Result<bool> {
    use crate::rw::{build_log, generate_build_log_filepath};
    use std::time::{Duration, Instant};

    let mut total_time = Duration::from_secs(0);
    let log_file = generate_build_log_filepath(&config.project_name, &self.title, env.cache_dir);

    println!("Starting the `{}` pipeline...", &self.title);
    build_log(&log_file, &[format!("Starting the `{}` pipeline...", &self.title)])?;

    let run_path = env.run_dir.canonicalize()?;
    let run_path = run_path.to_str().expect("Can't convert `Path` to string!");
    println!("Run path: {run_path}");
    build_log(&log_file, &[format!("Run path: {run_path}")])?;

    let log_file_path = log_file.canonicalize()?;
    let log_file_path = log_file_path.to_str().expect("Can't convert `Path` to string!");
    println!("Log file: {log_file_path}");
    build_log(&log_file, &[format!("Log file: {log_file_path}")])?;

    let now = Instant::now();

    let (satisfied, checked) = self.satisfy_all_requirements(config, env).await?;
    if !satisfied {
      return Ok(false);
    }
    if checked > 0 {
      let elapsed = now.elapsed();
      println!("Requirements checked in {}.", format!("{elapsed:.2?}").green());
      build_log(&log_file, &[format!("Requirements checked in {:.2?}.", elapsed)])?;
      total_time += elapsed;
    }

    let mut success = true;
    let mut cntr = 1usize;

    let mut to_sync = false;
    let real_run_dir = env.run_dir;

    let mut sd_notifier = None;

    let total = self.actions.len();
    for used in self.actions.iter() {
      let action = used.definition(&config.actions)?;

      if env.skipper.skip().await {
        cntr += 1;
        continue;
      }

      let mut env = if action.exec_in_project_dir.is_some_and(|v| v)
        && let Some(project_dir) = env.project_dir
      {
        if action.skip_sync.is_none_or(|v| !v) {
          to_sync = true;
        }
        RunEnvironment {
          run_dir: project_dir,
          daemons: env.daemons.clone(),
          skipper: env.skipper.clone(),
          restart_requested: env.restart_requested.clone(),
          ..(*env)
        }
      } else {
        RunEnvironment {
          daemons: env.daemons.clone(),
          skipper: env.skipper.clone(),
          restart_requested: env.restart_requested.clone(),
          ..(*env)
        }
      };

      let start_msg = format!(
        "[{}/{}] Action `{}`...",
        cntr,
        total,
        used
          .title
          .as_deref()
          .unwrap_or(action.info.to_str().as_str())
          .blue()
          .italic()
      );
      if !matches!(&action.action, Action::Observe(_)) {
        print!("{start_msg}");
        std::io::stdout().flush()?;
      } else {
        println!("{start_msg}");
      }
      build_log(&log_file, &[start_msg])?;

      if cntr == total
        && let Ok("1") = std::env::var("DEPLOYER_NOTIFY_SYSTEMD").as_deref()
      {
        let skip_time_sec = std::env::var("DEPLOYER_NOTIFY_SYSTEMD_AFTER_SECS").unwrap_or("1".to_string());
        let skip_time_sec: u64 = skip_time_sec.parse().unwrap_or(1);
        sd_notifier = Some(std::thread::spawn(move || {
          std::thread::sleep(std::time::Duration::from_secs(skip_time_sec));
          let _ = sd_notify::notify(true, &[sd_notify::NotifyState::Ready]);
        }));
      }

      let now = Instant::now();

      let (status, output) = Box::pin(action.action.run_with(config, self, &mut env, &used.with)).await?;

      if to_sync {
        sync_run_folder(
          config,
          self,
          real_run_dir,
          env
            .project_dir
            .expect("No project directory specified on pipeline run!"),
          false,
          false,
        )?;
        to_sync = false;
      }

      let elapsed = now.elapsed();
      if !matches!(&action.action, Action::Observe(_)) {
        total_time += elapsed;
      }

      let ending = format!(
        "{} ({}).",
        if status {
          " done".to_string()
        } else {
          " got an error!".red().bold().to_string()
        },
        format!("{elapsed:.2?}").green(),
      );
      let end_msg = format!(
        "[{}/{}] Action `{}` -{}",
        cntr,
        total,
        used
          .title
          .as_deref()
          .unwrap_or(action.info.to_str().as_str())
          .blue()
          .italic(),
        ending,
      );

      if status
        && (!action.is_always_piped()
          || (!action.is_observer()
            && (output.is_empty() || (output.len() == 1 && output.first().is_some_and(|l| l.as_str().eq("\n"))))))
      {
        println!("{ending}");
      } else {
        println!("{end_msg}");
      }

      if !status && !action.is_always_piped() {
        for line in &output {
          println!("{line}");
        }
      }

      build_log(&log_file, &output)?;
      build_log(&log_file, &[end_msg])?;

      cntr += 1;

      success &= status;
      if !status {
        break;
      }

      if let Some(restart_signal) = &env.restart_requested
        && restart_signal.load(std::sync::atomic::Ordering::Relaxed)
      {
        let is_last_action = cntr > total;

        if !is_last_action {
          crate::rw::log("Restart requested, stopping pipeline after current action...");
          println!("{}", "Files changed, restarting pipeline...".yellow());
          break;
        }
      }
    }

    env.daemons.shutdown().await;
    if let Some(sd_notifier) = sd_notifier {
      let _ = sd_notifier.join();
    }

    let placements = self.collect_artifacts_placements(config, env).await?;
    place_artifacts(env, placements, false)?;

    println!("Done in {}.", format!("{total_time:.2?}").green());
    build_log(&log_file, &[format!("Done in {:.2?}.", total_time)])?;

    Ok(success)
  }

  /// Run pipeline with shell driver.
  pub async fn exec_with_shell(
    &self,
    config: &DeployerProjectOptions,
    env: &RunEnvironment<'_>,
  ) -> anyhow::Result<bool> {
    let content = self.to_shell_script(config, env).await?;
    let pipeline_filename = format!(".pipe.{}.sh", env.master_pipeline);
    std::fs::write(env.run_dir.join(pipeline_filename.as_str()), content)?;
    let pipeline_filename = CustomCommand::escape_with_spaces(pipeline_filename);
    CustomCommand::run_simple_observer(env, format!("chmod +x {pipeline_filename}")).await?;
    CustomCommand::run_simple_observer(env, format!("./{pipeline_filename}"))
      .await
      .map(|_| true)
  }

  /// Converts pipeline into ready-to-run shell script.
  pub async fn to_shell_script(
    &self,
    config: &DeployerProjectOptions,
    env: &RunEnvironment<'_>,
  ) -> anyhow::Result<String> {
    let mut cmds = vec!["#!/bin/sh".to_string(), "".to_string(), "set -e".to_string()];
    if !env.ansible_run && !env.containered_build && !env.containered_run {
      cmds.push("clear".to_string());
    }
    cmds.extend_from_slice(&[
      "".to_string(),
      "BLUE='\\033[34;3m'".to_string(),
      "GREEN='\\033[32m'".to_string(),
      "RESET='\\033[0m'".to_string(),
      "".to_string(),
    ]);

    cmds.extend_from_slice(self.actions_to_shell(config, env).await?.as_slice());

    if !self.artifacts.is_empty() && !env.containered_build {
      cmds.push("mkdir -p artifacts".to_string());
      for (from, to) in self.collect_artifacts_placements(config, env).await? {
        cmds.push(format!("cp -rf {} artifacts/{} || true", from, to,));
      }
    }
    cmds.push("".to_string());

    if let Ok("1") = std::env::var("DEPLOYER_NOTIFY_SYSTEMD").as_deref() {
      cmds.extend_from_slice(&["systemd-notify --ready".to_string(), "".to_string()]);
    }

    let content = cmds.join("\n");
    Ok(content)
  }

  /// Converts pipeline actions to shell commands.
  pub async fn actions_to_shell(
    &self,
    config: &DeployerProjectOptions,
    env: &RunEnvironment<'_>,
  ) -> anyhow::Result<Vec<String>> {
    let mut cmds = vec![];
    let mut cntr = 1usize;

    let total = self.actions.len();

    for used in self.actions.iter() {
      let with = config.variables_for(&used.with)?;
      let action = used.definition(&config.actions)?;
      let info = if !env.ansible_run {
        format!(
          "printf '[{}/{}] Action `%b{}%b`...\\n' \"$BLUE\" \"$RESET\"",
          cntr,
          total,
          CustomCommand::escape(used.title.as_deref().unwrap_or(action.info.to_str().as_str()))
        )
      } else {
        format!(
          "printf '[{}/{}] Action `{}`...\\n'",
          cntr,
          total,
          CustomCommand::escape(used.title.as_deref().unwrap_or(action.info.to_str().as_str()))
        )
      };

      cmds.push(info);
      match action.to_shell(env, &with).await {
        Ok(v) => cmds.extend_from_slice(&v),
        Err(e) => {
          eprintln!("{e:?}");
          println!("Skip action during translation into shell script...");
          cmds.push("Skip action during translation into shell script...".to_string());
        }
      }
      cmds.push(String::new());
      cntr += 1;
    }

    Ok(cmds)
  }

  /// Collect needed actions (only those which are found) for running this pipeline.
  pub fn collect_actions(&self, definitions: &BTreeSet<DefinedAction>) -> BTreeSet<DefinedAction> {
    self
      .actions
      .iter()
      .filter_map(|action| action.definition(definitions).ok())
      .cloned()
      .collect::<BTreeSet<_>>()
  }

  /// Collect needed artifact placements with given variable for running this pipeline.
  pub async fn collect_artifacts_placements(
    &self,
    config: &DeployerProjectOptions,
    env: &RunEnvironment<'_>,
  ) -> anyhow::Result<Vec<(String, String)>> {
    let mut resolved_placements = Vec::with_capacity(self.artifacts.len());

    for placement in &self.artifacts {
      if placement.with.is_empty() {
        resolved_placements.push((placement.from.clone(), placement.to.clone()));
      } else {
        let vars = config.variables_for(&placement.with)?;
        let vars = {
          let mut _v = BTreeMap::new();
          for (k, v) in vars {
            _v.insert(k, v.get_value(env).await?);
          }
          _v
        };

        let mut from = placement.from.clone();
        for (k, v) in &vars {
          from = from.replace(k, v.as_str());
        }

        let mut to = placement.to.clone();
        for (k, v) in &vars {
          to = to.replace(k, v.as_str());
        }
      }
    }

    Ok(resolved_placements)
  }
}

/// Lists all available pipelines.
pub fn list_pipelines(globals: &DeployerGlobalConfig) -> anyhow::Result<()> {
  println!("Available pipelines in Deployer's Registry:");

  for pipeline in globals.pipelines_registry.iter() {
    let pipeline_info = pipeline.info.to_str();
    let pipeline_title = format!("[{}]", pipeline.title);
    let tags = if pipeline.tags.is_empty() {
      String::new()
    } else {
      format!(" (tags: {})", pipeline.tags.join(", ").as_str().blue().italic())
    };
    println!(
      "• {} {}{}{}",
      pipeline_info.blue().bold(),
      pipeline_title.green().bold(),
      tags,
      if let Some(desc) = pipeline.desc.as_deref()
        && !desc.is_empty()
      {
        format!(" > {}", desc.green().italic())
      } else {
        String::new()
      }
    );
  }

  Ok(())
}

fn choose_pipeline<'a>(
  pipelines_registry: &'a BTreeSet<DescribedPipeline>,
  prompt: &str,
) -> anyhow::Result<&'a DescribedPipeline> {
  if pipelines_registry.is_empty() {
    bail!("There is no pipelines in the Registry.");
  }

  let keys = pipelines_registry
    .iter()
    .map(|pipeline| pipeline.info.to_str())
    .collect::<Vec<_>>();
  let selected = inquire_reorder::Select::new(prompt, keys).prompt()?;
  pipelines_registry
    .iter()
    .find(|pipeline| pipeline.info.to_str().eq(&selected))
    .ok_or(anyhow!("No such pipeline!"))
}

/// Creates a new pipeline.
pub fn new_pipeline(globals: &mut DeployerGlobalConfig, args: &NewPipelineArgs) -> anyhow::Result<DescribedPipeline> {
  if let Some(from_file) = &args.from {
    let pipeline = read_checked::<DescribedPipeline>(from_file)
      .map_err(|e| {
        panic!("Can't read provided Pipeline file due to: {e}");
      })
      .unwrap();
    globals.pipelines_registry.insert(pipeline.clone());
    return Ok(pipeline);
  }

  let mut updated_actions = globals.actions_registry.clone();
  let described_pipeline = DescribedPipeline::new_from_prompt(globals, &mut updated_actions)?;
  globals.actions_registry = updated_actions;

  if globals
    .pipelines_registry
    .iter()
    .any(|p| p.info == described_pipeline.info)
    && !inquire_reorder::Confirm::new(&format!(
      "Pipelines Registry already have `{}` pipeline. Do you want to override it? (y/n)",
      described_pipeline.info.to_str(),
    ))
    .prompt()?
  {
    return Ok(described_pipeline);
  }

  globals.pipelines_registry.insert(described_pipeline.clone());

  Ok(described_pipeline)
}

/// Removes a pipeline.
pub fn remove_pipeline(globals: &mut DeployerGlobalConfig, args: RemovePipelineArgs) -> anyhow::Result<()> {
  let pipeline = if let Some(info) = args.info {
    let info = info.to_info()?;
    globals
      .pipelines_registry
      .iter()
      .find(|pipeline| pipeline.info.eq(&info))
      .ok_or(anyhow!(""))?
      .clone()
  } else {
    choose_pipeline(
      &globals.pipelines_registry,
      "Select pipeline for removing from the registry:",
    )?
    .clone()
  };

  if !args.yes && !inquire_reorder::Confirm::new("Are you sure? (y/n)").prompt()? {
    return Ok(());
  }

  globals.pipelines_registry.remove(&pipeline);

  Ok(())
}

/// Prints a pipeline as YAML.
pub fn cat_pipeline(globals: &DeployerGlobalConfig, args: CatPipelineArgs) -> anyhow::Result<()> {
  let pipeline = if let Some(info) = &args.info {
    let info = info.to_info()?;
    globals
      .pipelines_registry
      .iter()
      .find(|pipeline| pipeline.info.eq(&info))
      .ok_or(anyhow!(""))?
      .clone()
  } else {
    choose_pipeline(&globals.pipelines_registry, "Select pipeline for displaying:")?.clone()
  };

  let pipeline_ser = serde_pretty_yaml::to_string_pretty(&pipeline)?;
  println!("{pipeline_ser}");

  Ok(())
}

/// Assigns and setup the pipeline from Registry.
///
/// This function also assigns all related to the pipeline actions from Registry.
pub fn assign_pipeline_to_project(
  globals: &mut DeployerGlobalConfig,
  config: &mut DeployerProjectOptions,
  args: &WithPipelineArgs,
) -> anyhow::Result<()> {
  if *config == Default::default() {
    panic!("Config is invalid! Reinit the project.");
  }

  let mut pipeline = if let Some(tag) = &args.tag {
    let info = tag.to_info()?;
    globals
      .pipelines_registry
      .iter()
      .find(|pipe| pipe.info == info)
      .ok_or_else(|| {
        anyhow::anyhow!("There is no such pipeline in Registry. See available pipelines with `depl ls pipelines`.")
      })?
      .clone()
  } else if !globals.pipelines_registry.is_empty() {
    let mut ptags = bmap!();
    let mut tags = vec![];

    globals
      .pipelines_registry
      .iter()
      .map(|pipe| {
        (
          format!("`{}` - {}", pipe.info.to_str().blue().bold(), pipe.title.green().bold()),
          pipe,
        )
      })
      .for_each(|(t, p)| {
        tags.push(t.clone());
        ptags.insert(t, p.clone());
      });
    tags.push("• Specify another pipeline".to_string());

    let selected = inquire_reorder::Select::new("Select the pipeline for this project:", tags).prompt()?;

    if selected.as_str().eq("• Specify another pipeline") {
      DescribedPipeline::new_from_prompt(globals, &mut config.actions)?
    } else {
      let pipeline = ptags.get(&selected).ok_or(anyhow::anyhow!(
        "There is no such pipeline in Registry. See available pipelines with `depl ls pipelines`."
      ))?;
      (*pipeline).clone()
    }
  } else {
    DescribedPipeline::new_from_prompt(globals, &mut config.actions)?
  };

  let needed_actions = pipeline.collect_actions(&globals.actions_registry);
  config.actions = config.actions.union(&needed_actions).cloned().collect();

  if config.variables.is_empty() {
    println!("Please, specify variables for project first!");
    let mut needed = vec![];
    for action in &pipeline.actions {
      let definition = action.definition(&config.actions)?;
      for var in definition.collect_required_variables() {
        needed.push(format!(
          "\t- variable `{var}` for pipeline `{}`, action `{}`",
          pipeline.info.to_str(),
          action.used.to_str()
        ));
      }
    }
    println!("These variables are required for specified pipeline:");
    needed.iter().for_each(|v| println!("{v}"));
    return Ok(());
  }
  for action in &mut pipeline.actions {
    *action = action.prompt_setup_for_project(&config.variables, &config.actions)?;
  }

  let short_name = if let Some(short_name) = args.r#as.as_ref() {
    short_name.to_owned()
  } else {
    inquire_reorder::Text::new("Write the pipeline's short name (only for this project):").prompt()?
  };

  pipeline.desc = Some(format!(
    r#"Got from `{}`.{}{}"#,
    pipeline.title,
    if pipeline.desc.as_ref().is_none_or(|d| d.is_empty()) {
      ""
    } else {
      " "
    },
    pipeline.desc.as_deref().unwrap_or("")
  ));
  pipeline.title = short_name.clone();

  if specify_pipeline_short_name(config, &mut pipeline.title).is_err() {
    return Ok(());
  };

  if let Some(old_default) = config.pipelines.iter_mut().find(|p| p.default.is_some_and(|v| v)) {
    if inquire_reorder::Confirm::new(&format!(
      "Pipeline `{}` is already set by default. Set this pipeline running by default instead?",
      old_default.title.as_str()
    ))
    .prompt()?
    {
      old_default.default = None;
      pipeline.default = Some(true);
    }
  } else if inquire_reorder::Confirm::new("Set this pipeline running by default? (y/n)").prompt()? {
    pipeline.default = Some(true);
  }

  if let Some(i) = config.pipelines.iter().position(|p| p.title.as_str() == short_name) {
    config.pipelines.remove(i);
  }
  config.pipelines.push(pipeline);

  println!("Pipeline is successfully set up for this project.");

  Ok(())
}

/// Edits the pipeline.
pub async fn edit_pipeline(globals: &mut DeployerGlobalConfig, args: EditPipelineArgs) -> anyhow::Result<()> {
  let mut pipeline = if let Some(info) = args.info {
    let info = info.to_info()?;
    globals
      .pipelines_registry
      .iter()
      .find(|pipeline| pipeline.info.eq(&info))
      .ok_or(anyhow!("This pipeline is not found in registry!"))?
      .clone()
  } else {
    choose_pipeline(&globals.pipelines_registry, "Select pipeline for editing:")?.clone()
  };

  pipeline
    .edit_pipeline_from_prompt(
      &mut globals.pipelines_registry,
      &globals.actions_registry,
      None,
      &globals.remote_hosts,
      true,
    )
    .await?;
  globals
    .pipelines_registry
    .retain(|in_registry| in_registry.info.ne(&pipeline.info));
  globals.pipelines_registry.insert(pipeline);

  Ok(())
}

/// Exports given pipeline from project as shell script.
pub async fn export_project_pipeline(
  config: &DeployerProjectOptions,
  globals: &DeployerGlobalConfig,
  cache_dir: &Path,
  config_dir: &Path,
  storage_dir: &Path,
  args: ExportPipelineArgs,
) -> anyhow::Result<()> {
  let pipeline = config
    .pipelines
    .iter()
    .find(|p| p.title.as_str().eq(args.pipeline_tag.as_str()))
    .ok_or(anyhow!("Can't find chosen pipeline!"))?;

  let curr_dir = std::env::current_dir().expect("Can't get current dir!");
  let artifacts_dir = prepare_artifacts_folder(&curr_dir)?;
  let env = RunEnvironment {
    master_pipeline: &pipeline.title,
    run_dir: &curr_dir,
    cache_dir,
    config_dir,
    storage_dir,
    project_dir: Some(&curr_dir),
    artifacts_dir: &artifacts_dir,
    artifacts_placements: &pipeline.artifacts,
    new_build: false,
    remotes: &globals.remote_hosts,
    ignore: &config.ignore_files,
    log_file: None,
    containered_build: false,
    containered_run: false,
    ansible_run: false,
    daemons: Default::default(),
    observe_cli: &None,
    skipper: Default::default(),
    driver: PipelineDriver::Shell,
    restart_requested: None,
  };

  let shell_script = pipeline.to_shell_script(config, &env).await?;

  let has_cicd_export = args.gh || args.gl;
  if let Some(shell_path) = args.output.as_deref()
    && !has_cicd_export
  {
    std::fs::write(shell_path, shell_script.as_str())?;
  }
  if args.gh {
    crate::github::export(
      config,
      pipeline,
      pipeline.gh_opts.as_ref(),
      &env,
      shell_script.as_str(),
      args.output.as_deref(),
    )?;
  }
  if args.gl {
    crate::gitlab::export(
      config,
      pipeline,
      pipeline.gl_opts.as_ref(),
      &env,
      shell_script.as_str(),
      args.output.as_deref(),
    )?;
  }
  if args.systemd {
    crate::systemd::export(config, pipeline, &env)?;
  }
  if args.containerized || args.compose || args.ansible {
    let output_dir = if let Some(ref output) = args.output {
      PathBuf::from(output)
    } else {
      curr_dir.clone()
    };
    let export_env = RunEnvironment {
      master_pipeline: &pipeline.title,
      run_dir: &output_dir,
      cache_dir,
      config_dir,
      storage_dir,
      project_dir: Some(&curr_dir),
      artifacts_dir: &artifacts_dir,
      artifacts_placements: &pipeline.artifacts,
      new_build: false,
      remotes: &globals.remote_hosts,
      ignore: &config.ignore_files,
      log_file: None,
      containered_build: false,
      containered_run: false,
      ansible_run: false,
      daemons: Default::default(),
      observe_cli: &None,
      skipper: Default::default(),
      driver: pipeline.driver,
      restart_requested: None,
    };

    if args.containerized {
      if let Some(opts) = pipeline.containered_opts.as_ref() {
        let exclusive_exec_tag =
          pipeline.exclusive_exec_tag.clone().unwrap_or(String::from("default")) + "-containered";
        let vars = config.variables_for(&opts.with)?;
        crate::containered::generate_dockerfile(config, &export_env, pipeline, opts, &exclusive_exec_tag, &vars)
          .await?;
        crate::containered::generate_dockerignore(&export_env, config)?;
        if pipeline.driver.is_shell() {
          crate::containered::generate_shell_driver_scripts(config, &export_env, pipeline).await?;
        }
        println!("Exported Dockerfile to: {}", output_dir.display());
      } else {
        anyhow::bail!("Pipeline `{}` has no containerized options configured!", pipeline.title);
      }
    }
    if args.compose {
      if let Some(opts) = pipeline.compose_opts.as_ref() {
        let exclusive_exec_tag = pipeline.exclusive_exec_tag.clone().unwrap_or(String::from("default")) + "-compose";
        let vars = config.variables_for(&opts.app.with)?;
        crate::compose::generate_compose_file(config, &export_env, pipeline, opts, &exclusive_exec_tag, &vars).await?;
        crate::containered::generate_dockerignore(&export_env, config)?;
        if pipeline.driver.is_shell() {
          crate::containered::generate_shell_driver_scripts(config, &export_env, pipeline).await?;
        }
        println!("Exported docker-compose file to: {}", output_dir.display());
      } else {
        anyhow::bail!("Pipeline `{}` has no compose options configured!", pipeline.title);
      }
    }
    if args.ansible {
      if let Some(ansible_opts) = &pipeline.ansible_opts {
        crate::ansible::make_inventory(&export_env, ansible_opts, export_env.remotes)?;
        crate::ansible::make_playbook(config, &export_env, pipeline).await?;
        println!("Exported Ansible playbook and inventory to: {}", output_dir.display());
      } else {
        anyhow::bail!("Pipeline `{}` has no Ansible options configured!", pipeline.title);
      }
    }
  }

  println!("{}", "Export completed.".green());

  Ok(())
}

/// Checks requirements and some other conditions to run chosen pipeline.
pub async fn check_project_pipeline(
  config: &DeployerProjectOptions,
  globals: &DeployerGlobalConfig,
  cache_dir: &Path,
  config_dir: &Path,
  storage_dir: &Path,
  args: CheckProjectPipelineArgs,
) {
  let pipeline = if let Some(pipe) = config
    .pipelines
    .iter()
    .find(|p| p.title.as_str().eq(args.pipeline_tag.as_str()))
  {
    pipe
  } else {
    println!("{}", "Can't find chosen pipeline!".red());
    exit(1);
  };

  let curr_dir = std::env::current_dir().expect("Can't get current dir!");
  let artifacts_dir = prepare_artifacts_folder(&curr_dir).expect("Can't prepare artifacts folder!");
  let env = RunEnvironment {
    master_pipeline: &pipeline.title,
    run_dir: &curr_dir,
    cache_dir,
    config_dir,
    storage_dir,
    project_dir: Some(&curr_dir),
    artifacts_dir: &artifacts_dir,
    artifacts_placements: &pipeline.artifacts,
    new_build: false,
    remotes: &globals.remote_hosts,
    ignore: &config.ignore_files,
    log_file: None,
    containered_build: false,
    containered_run: false,
    ansible_run: false,
    daemons: Default::default(),
    observe_cli: &None,
    skipper: Default::default(),
    driver: pipeline.driver,
    restart_requested: None,
  };

  let mut errs = 0u32;

  if let Some(opts) = &pipeline.containered_opts {
    if opts.executor.is_docker() && Requirement::in_path("docker").satisfy(&env).await.is_err() {
      println!("{}", "Docker is not installed!".red());
      errs += 1;
    } else if !opts.executor.is_docker() && Requirement::in_path("podman").satisfy(&env).await.is_err() {
      println!("{}", "Podman is not installed!".red());
      errs += 1;
    }
  }

  if let Some(opts) = &pipeline.compose_opts {
    let executor = opts.effective_executor();
    if executor.is_docker() && Requirement::in_path("docker").satisfy(&env).await.is_err() {
      println!("{}", "Docker is not installed (required for Compose)!".red());
      errs += 1;
    } else if !executor.is_docker() && Requirement::in_path("podman").satisfy(&env).await.is_err() {
      println!("{}", "Podman is not installed (required for Compose)!".red());
      errs += 1;
    }
  }

  if pipeline.ansible_opts.is_some() && Requirement::in_path("ansible-playbook").satisfy(&env).await.is_err() {
    println!("{}", "Ansible is not installed!".red());
    errs += 1;
  }

  let (satisfied, _) = pipeline.satisfy_all_requirements(config, &env).await.unwrap();
  if !satisfied {
    println!("{}", "Requirements are not satisfied!".red());
    errs += 1;
  }

  for used in &pipeline.actions {
    match used.definition(&config.actions) {
      Err(e) => {
        println!(
          "{} {} due to: {e}.",
          "Can't find definition of this action:".red(),
          used.used.to_str().blue()
        );
        errs += 1;
        continue;
      }
      Ok(action) => {
        if let Action::Patch(patch) = &action.action {
          println!(
            "Checking patch {}. Run with `{}` environment variable to verbose patch output.",
            action.info.to_str().blue(),
            "SMPTCHVERBOSE=1".green()
          );
          match patch.test(&env).await {
            Err(e) => {
              println!(
                "{} {} due to: {e}.",
                "Can't test patch action:".red(),
                action.info.to_str().blue()
              );
              errs += 1;
              continue;
            }
            Ok((status, out)) => {
              if !status {
                println!(
                  "{} {} due to: {}.",
                  "Patch is invalid:".red(),
                  action.info.to_str().blue(),
                  out[0],
                );
                errs += 1;
                continue;
              }
            }
          }
        }
      }
    }
    let vars = if let Ok(vars) = config.variables_for(&used.with) {
      vars
    } else {
      println!(
        "{} {}.",
        "Can't collect all variables in this project for action:".red(),
        used.used.to_str().blue()
      );
      errs += 1;
      continue;
    };
    for (name, var) in vars {
      if var.get_value(&env).await.is_err() {
        println!(
          "{} {}.",
          "Can't get value of variable in this project with name:".red(),
          name
        );
        errs += 1;
        continue;
      }
    }
  }

  if errs > 0 {
    exit(1);
  }
}