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
//! Docker-Compose functionality
//!
//! A separate app to examine and run docker compose

const APP_NAME: &str = "Docker Compose";
const MODULE_VERSION: &'static str = env!("CARGO_PKG_VERSION");

use anyhow::{Context, Result};
use schemars::JsonSchema;
use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;
use std::rc::Rc;

use super::schema::*;
use super::DockerContainer;
use super::FoundryError;
use super::{ActionTrait, AppInstance, AppQuery, AppTrait, ContainerTrait, Message};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DockerCompose {
  /// We want to put the shell/parent container here
  #[serde(skip)]
  parent: Option<Rc<dyn ContainerTrait>>,
  // docker: Docker,
  instance: AppInstance,
  config: Option<Schema>,
  containers: HashMap<String, DockerContainer>,
}

impl AppTrait for DockerCompose {
  fn get_name(&self) -> String {
    match &self.instance.version {
      Some(ver) => format!("{} ({})", APP_NAME, ver),
      None => format!("{} (Unknown Version)", APP_NAME),
    }
  }

  fn build(instance: AppInstance, parent: Option<Rc<dyn ContainerTrait>>) -> Result<DockerCompose> {
    Ok(DockerCompose {
      parent,
      instance: AppInstance {
        module_version: Some(DockerCompose::get_module_version()?),
        ..instance.clone()
      },
      config: None,
      containers: Default::default(),
    })
  }

  // /// Knows how to get the version number of the AppTrait
  // /// Figures out how to call the cli using the given container
  // fn set_cli(
  //   &self,
  //   _instance: AppInstance,
  //   _container: Rc<dyn ContainerTrait>,
  // ) -> Result<AppInstance> {
  //   unimplemented!()
  // }

  /// Knows how to get the version number of the installed app (not the module version)
  fn set_version(&self, _instance: AppInstance) -> Result<AppInstance> {
    unimplemented!()
  }
}

impl ContainerTrait for DockerCompose {
  /// This will find a list of apps with configurations that the container knows about
  fn find(&self, query: AppQuery) -> Result<Vec<AppInstance>> {
    let conf = self.get_conf()?;
    Ok(
      conf
        .list_service_names()
        .into_iter()
        .filter_map(|item| match item.to_lowercase() == query.name {
          false => None,
          true => Some(AppInstance::new(item)),
        })
        .collect(),
    )
  }

  /// List the known items in the app cache
  fn cached_apps(&self) -> Result<Vec<AppInstance>> {
    unimplemented!("No App Cache for Bash Yet")
  }

  /// Send the message to a child item
  fn forward(&self, to: AppInstance, message: Message) -> Result<String> {
    match message {
      Message::Command(cmd) => {
        let exec = ExecOptions {
          service_name: to.name,
          user: cmd.run_as.clone(),
          command: cmd.command,
          args: cmd.args,
          ..Default::default()
        };
        match exec.run(self.instance.clone())? {
          ActionResult::Exec(val) => Ok(val),
          err => Err(FoundryError::UnexpectedValue).context(format!(
            "Running DockerCompose::ExecOptions did not return an ExecResult:\n{:#?}",
            err
          )),
        }
      }
      _ => Err(FoundryError::UnexpectedValue)
        .context("Docker Compose tried to forward a non-command to a container"),
    }
  }

  /// Get the name/version of the container, usually for use in logging/errors.
  fn get_name(&self) -> String {
    self.get_name()
  }
}

impl DockerCompose {
  fn get_module_version() -> Result<semver::Version> {
    Ok({
      semver::Version::parse(MODULE_VERSION).context(format!(
        "{} has an invalid version number '{}' Cargo.toml",
        APP_NAME, MODULE_VERSION
      ))
    }?)
  }

  fn get_name(&self) -> String {
    match &self.instance.version {
      Some(ver) => format!("{} ({})", APP_NAME, ver),
      None => format!("{} (Unknown Version)", APP_NAME),
    }
  }

  fn get_conf(&self) -> Result<Schema> {
    match self.config.clone() {
      Some(conf) => Ok(conf),
      None => Err(FoundryError::ConfigurationError).context(
        "Docker Compose does not have a loaded configuration. Make sure DockerCompose::load is used first"
      )?,
    }
  }

  /// Load the configuration from an existing yaml file
  pub fn load(&self, config_file: String) -> Result<DockerCompose> {
    log::debug!("reading the docker compose schema");

    log::info!("Parsing the docker compose file at {}", config_file);
    let contents = std::fs::read_to_string(config_file.clone()).context(format!(
      "Failed to open docker-compose file at {}",
      config_file
    ))?;

    let conf = Schema {
      source: Some(config_file.clone()),
      ..serde_yaml::from_str(&contents).context(format!(
        "Failed to parse the docker-compose file at {}",
        config_file
      ))?
    };

    log::debug!("Successfully parsed the schema at {}", config_file,);

    log::debug!("Getting the docker containers");
    let containers = HashMap::new();
    let mut new_compose = DockerCompose {
      config: Some(conf.clone()),
      containers,
      instance: AppInstance {
        config_file: Some(config_file.clone()),
        ..self.instance.clone()
      },
      ..self.clone()
    };

    for name in conf.list_service_names() {
      new_compose
        .containers
        .insert(name.clone(), new_compose.define_container(name)?);
    }

    log::debug!("Returning the compose");
    Ok(new_compose)
  }

  /// Private function used to build a container from the schema
  /// TODO: Add in result from "docker inspect" if running
  /// TODO: If status is "Up", we want to get/set shell
  fn define_container(&self, name: String) -> Result<DockerContainer> {
    let instance = AppInstance::new(name.clone());
    DockerContainer::build(instance, Some(Rc::new(self.clone())))
  }

  /// Set the status of all the services that this instance knows about
  fn _update_status(&mut self, _name: String) -> Result<()> {
    Ok(())
  }

  pub fn run_action(&self, action: Action) -> Result<ActionResult> {
    action.run(self.clone())
  }

  pub fn get_container(&self, name: String) -> Result<DockerContainer> {
    match self.containers.get(&name) {
      Some(container) => Ok(container.clone()),
      None => {
        let conf = self
          .get_conf()
          .context("Failed to run DockerCompose::get_container")?;
        Err(FoundryError::NotFound).context(format!(
          "Docker Compose does not have a service named '{}' in conf at '{}'. Possible choices are: {:#?} ",
          name,
          conf.get_source(),
          conf.list_service_names(),
        ))
      }
    }
  }

  // Cli functions will go here
}

// Let examine messages for the foundry for communicating rather than directly returning values
#[derive(Clone, Debug, JsonSchema, Serialize, Deserialize)]
pub enum Event {
  UpComplete,
  DownComplete,
  RestartedService,
  BuildComplete,
}

/// The actions registered to the system.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum Action {
  Find(FindApp),
  // Run the exec command against a running container
  Exec(ExecOptions),
  /// Dump the configuration to the given file location. Useful for adding volumes/ports on the fly
  Export,
}

/// The responses from the various actions.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ActionResult {
  // Run(RunResult),
  FindResult(Vec<AppInstance>),
  ListServices(Vec<String>),
  Exec(String),
}

impl Action {
  fn run(&self, compose: DockerCompose) -> Result<ActionResult> {
    match self {
      Action::Export => unimplemented!("Next up, Export"),
      Action::Find(query) => match query.0.find_all {
        true => Ok(ActionResult::FindResult(compose.find(query.0.clone())?)),
        false => Ok(ActionResult::FindResult(vec![
          compose.find_one(query.0.clone())?
        ])),
      },
      Action::Exec(opts) => opts.run(compose.instance.clone()),
    }
  }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FindApp(AppQuery);

impl ActionTrait for FindApp {
  type RESPONSE = ActionResult;

  fn run(&self, _target: AppInstance) -> Result<Self::RESPONSE> {
    unimplemented!("Still haven't figured out Actions yet")
  }

  fn to_message(&self, _target: Option<AppInstance>) -> Result<Vec<Message>> {
    unimplemented!("ActionTrait not implemented for shell")
  }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ExecOptions {
  command: String,
  args: Vec<String>,
  service_name: String,
  detach: bool,
  privileged: bool,
  index: Option<u8>,
  user: Option<String>,
  env: Option<HashMap<String, String>>,
  work_dir: Option<String>,
}

impl ExecOptions {
  pub fn new(service_name: String, command: String) -> ExecOptions {
    ExecOptions {
      service_name,
      command,
      ..Default::default()
    }
  }
}

// "/usr/local/bin/docker-compose" "-f" "/home/dfogelson/Foundry/TheProcessFoundry/the_process_foundry/tests/data/postgres.docker-compose.yml" "exec" "-T" "postgres" "bash" "-c" "command -v pg_basebackup"
fn command_str(command: &std::process::Command) -> Result<String> {
  let cmd = format!("{:#?}", command);
  let regex = regex::Regex::new(r#"(^\s*")|(" ")|("\s*$)"#)?;
  Ok(regex.replace_all(&cmd, " ").to_string())
}

impl ActionTrait for ExecOptions {
  type RESPONSE = ActionResult;

  fn run(&self, compose: AppInstance) -> Result<Self::RESPONSE> {
    // TODO: change this to use the instance path
    let mut cmd = std::process::Command::new(format!("/usr/local/bin/{}", compose.name));

    // Add the option args
    match compose.config_file {
      Some(path) => cmd.arg("-f").arg(path.clone()),
      // TODO: Actually make the write function exist
      None => Err(FoundryError::ConfigurationError).context(
        "Docker Compose does not configuration file. TODO: Use DockerCompose::write to create one",
      )?,
    };
    cmd.arg("exec").arg("-T");
    match &self.user {
      None => (),
      Some(user) => {
        cmd.arg("--user");
        cmd.arg(&user.clone());
      }
    };
    cmd.arg(self.service_name.clone());

    // And add the command
    cmd.arg(self.command.clone());
    cmd.args(&self.args);
    log::debug!("Docker compose is executing a cmd:\n{}", command_str(&cmd)?);
    let result = cmd.arg("").output()?;
    match result.status.success() {
      true => Ok(ActionResult::Exec(
        String::from_utf8(result.stdout)?.trim_end().to_string(),
      )),
      false => {
        Err(FoundryError::RemoteError).context("Received an error trying to run in docker compose")
      }
    }
  }

  fn to_message(&self, _target: Option<AppInstance>) -> Result<Vec<Message>> {
    unimplemented!("ActionTrait not implemented for shell")
  }
}

pub enum CliActions {
  /* -------    Cli Actions (To be pruned to only items to be exposed)   --------*/
  /// Build or rebuild services
  Build,
  ///Validate and view the Compose file
  Config,
  /// Create services
  Create,
  /// Stop and remove containers, networks, images, and volumes
  Down,
  /// Receive real time events from containers
  Events,
  /// Execute a command in a running container
  Exec,
  /// Get help on a command
  Help,
  /// List images
  Images,
  /// Kill containers
  Kill,
  /// View output from containers
  Logs,
  /// Pause services
  Pause,
  /// Print the public port for a port binding
  Port,
  /// List containers
  Ps,
  /// Pull service images
  Pull,
  /// Push service images
  Push,
  /// Restart services
  Restart,
  /// Remove stopped containers
  Rm,
  /// Run a one-off command
  Run,
  /// Set number of containers for a service
  Scale,
  /// Start services
  Start,
  /// Stop services
  Stop,
  /// Display the running processes
  Top,
  /// Un-pause services
  UnPause,
  /// Create and start containers
  Up,
  /// Show the Docker-Compose version information
  Version,
}