rash_core 2.20.0

Declarative shell scripting using Rust native bindings
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
/// ANCHOR: module
/// # docker_exec
///
/// Execute commands inside running Docker containers.
///
/// ## Attributes
///
/// ```yaml
/// check_mode:
///   support: none
/// ```
/// ANCHOR_END: module
/// ANCHOR: examples
/// ## Example
///
/// ```yaml
/// - name: Run a simple command in a container
///   docker_exec:
///     container: myapp
///     command: ls /app
///
/// - name: Run script in container as specific user
///   docker_exec:
///     container: myapp
///     command: /scripts/update.sh
///     user: appuser
///     workdir: /app
///
/// - name: Run command with environment variables
///   docker_exec:
///     container: webapp
///     command: env
///     env:
///       DEBUG: "true"
///       LOG_LEVEL: info
///
/// - name: Run interactive command with TTY
///   docker_exec:
///     container: myapp
///     command: /bin/bash
///     tty: true
///     stdin: true
///
/// - name: Run command in background
///   docker_exec:
///     container: myapp
///     command: /scripts/background_task.sh
///     detach: true
/// ```
/// ANCHOR_END: examples
use crate::context::GlobalParams;
use crate::error::{Error, ErrorKind, Result};
use crate::modules::{Module, ModuleResult, parse_params};

#[cfg(feature = "docs")]
use rash_derive::DocJsonSchema;

use log::trace;
use std::process::{Command, Output};

use minijinja::Value;
#[cfg(feature = "docs")]
use schemars::{JsonSchema, Schema};
use serde::Deserialize;
use serde_json;
use serde_norway::Value as YamlValue;
use serde_norway::value;

#[derive(Debug, PartialEq, Deserialize)]
#[cfg_attr(feature = "docs", derive(JsonSchema, DocJsonSchema))]
#[serde(deny_unknown_fields)]
pub struct Params {
    /// Container name or ID.
    container: String,
    /// Command to execute inside the container.
    command: String,
    /// User to run the command as (e.g., "user", "uid", "user:group", "uid:gid").
    #[serde(default)]
    user: Option<String>,
    /// Working directory inside the container.
    #[serde(default)]
    workdir: Option<String>,
    /// Environment variables as a dictionary.
    #[serde(default)]
    env: Option<serde_json::Map<String, serde_json::Value>>,
    /// Run command in the background (detached mode).
    #[serde(default)]
    detach: bool,
    /// Allocate a pseudo-TTY.
    #[serde(default)]
    tty: bool,
    /// Keep STDIN open even if not attached.
    #[serde(default)]
    stdin: bool,
}

#[derive(Debug)]
pub struct DockerExec;

impl Module for DockerExec {
    fn get_name(&self) -> &str {
        "docker_exec"
    }

    fn exec(
        &self,
        _: &GlobalParams,
        optional_params: YamlValue,
        _vars: &Value,
        check_mode: bool,
    ) -> Result<(ModuleResult, Option<Value>)> {
        Ok((
            docker_exec(parse_params(optional_params)?, check_mode)?,
            None,
        ))
    }

    fn force_string_on_params(&self) -> bool {
        false
    }

    #[cfg(feature = "docs")]
    fn get_json_schema(&self) -> Option<Schema> {
        Some(Params::get_json_schema())
    }
}

struct DockerClient;

impl DockerClient {
    fn new() -> Self {
        DockerClient
    }

    fn exec_cmd(&self, args: &[&str], check_success: bool) -> Result<Output> {
        let output = Command::new("docker")
            .args(args)
            .output()
            .map_err(|e| Error::new(ErrorKind::SubprocessFail, e))?;
        trace!("command: `docker {:?}`", args);
        trace!("{output:?}");

        if check_success && !output.status.success() {
            return Err(Error::new(
                ErrorKind::SubprocessFail,
                format!(
                    "Error executing docker: {}",
                    String::from_utf8_lossy(&output.stderr)
                ),
            ));
        }
        Ok(output)
    }

    fn container_exists(&self, name: &str) -> Result<bool> {
        let output = self.exec_cmd(
            &[
                "ps",
                "-a",
                "--filter",
                &format!("name=^{}$", name),
                "--format",
                "{{.Names}}",
            ],
            false,
        )?;
        let stdout = String::from_utf8_lossy(&output.stdout);
        Ok(stdout.lines().any(|line| line.trim() == name))
    }
}

fn validate_container_name(name: &str) -> Result<()> {
    if name.is_empty() {
        return Err(Error::new(
            ErrorKind::InvalidData,
            "Container name cannot be empty",
        ));
    }
    Ok(())
}

fn docker_exec(params: Params, check_mode: bool) -> Result<ModuleResult> {
    validate_container_name(&params.container)?;

    if check_mode {
        return Ok(ModuleResult {
            changed: true,
            output: Some(format!(
                "Would execute '{}' in container '{}'",
                params.command, params.container
            )),
            extra: None,
        });
    }

    let client = DockerClient::new();

    if !client.container_exists(&params.container)? {
        return Err(Error::new(
            ErrorKind::InvalidData,
            format!("Container '{}' does not exist", params.container),
        ));
    }

    let mut args: Vec<String> = vec!["exec".to_string()];

    if params.detach {
        args.push("-d".to_string());
    }

    if params.tty {
        args.push("-t".to_string());
    }

    if params.stdin {
        args.push("-i".to_string());
    }

    if let Some(ref user) = params.user {
        args.push("-u".to_string());
        args.push(user.clone());
    }

    if let Some(ref workdir) = params.workdir {
        args.push("-w".to_string());
        args.push(workdir.clone());
    }

    if let Some(ref env_dict) = params.env {
        for (key, value) in env_dict {
            let env_str = match value {
                serde_json::Value::String(s) => format!("{}={}", key, s),
                serde_json::Value::Number(n) => format!("{}={}", key, n),
                serde_json::Value::Bool(b) => format!("{}={}", key, b),
                _ => format!("{}={}", key, value),
            };
            args.push("-e".to_string());
            args.push(env_str);
        }
    }

    args.push(params.container.clone());

    args.push("/bin/sh".to_string());
    args.push("-c".to_string());
    args.push(params.command.clone());

    let args_refs: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
    let output = client.exec_cmd(&args_refs, false)?;

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    let rc = output.status.code();

    let extra = Some(value::to_value(serde_json::json!({
        "stdout": stdout.to_string(),
        "stderr": stderr.to_string(),
        "rc": rc,
        "container": params.container,
    }))?);

    let output_str = if stdout.is_empty() && !stderr.is_empty() {
        Some(stderr.into_owned())
    } else {
        Some(stdout.into_owned())
    };

    Ok(ModuleResult {
        changed: true,
        output: output_str,
        extra,
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_params_minimal() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            container: myapp
            command: ls /app
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        assert_eq!(params.container, "myapp");
        assert_eq!(params.command, "ls /app");
        assert_eq!(params.user, None);
        assert_eq!(params.workdir, None);
        assert_eq!(params.env, None);
        assert!(!params.detach);
        assert!(!params.tty);
        assert!(!params.stdin);
    }

    #[test]
    fn test_parse_params_full() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            container: myapp
            command: /scripts/update.sh
            user: appuser
            workdir: /app
            env:
              DEBUG: "true"
              LOG_LEVEL: info
            detach: true
            tty: true
            stdin: true
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        assert_eq!(params.container, "myapp");
        assert_eq!(params.command, "/scripts/update.sh");
        assert_eq!(params.user, Some("appuser".to_string()));
        assert_eq!(params.workdir, Some("/app".to_string()));
        assert!(params.detach);
        assert!(params.tty);
        assert!(params.stdin);

        let env = params.env.unwrap();
        assert_eq!(
            env.get("DEBUG").unwrap(),
            &serde_json::Value::String("true".to_string())
        );
        assert_eq!(
            env.get("LOG_LEVEL").unwrap(),
            &serde_json::Value::String("info".to_string())
        );
    }

    #[test]
    fn test_parse_params_env_with_numbers() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            container: myapp
            command: env
            env:
              PORT: 8080
              ENABLED: true
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        let env = params.env.unwrap();
        assert_eq!(env.get("PORT").unwrap(), &serde_json::json!(8080));
        assert_eq!(env.get("ENABLED").unwrap(), &serde_json::Value::Bool(true));
    }

    #[test]
    fn test_parse_params_missing_container() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            command: ls
            "#,
        )
        .unwrap();
        let error = parse_params::<Params>(yaml).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::InvalidData);
    }

    #[test]
    fn test_parse_params_missing_command() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            container: myapp
            "#,
        )
        .unwrap();
        let error = parse_params::<Params>(yaml).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::InvalidData);
    }

    #[test]
    fn test_parse_params_random_field() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            container: myapp
            command: ls
            invalid_field: value
            "#,
        )
        .unwrap();
        let error = parse_params::<Params>(yaml).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::InvalidData);
    }

    #[test]
    fn test_validate_container_name() {
        assert!(validate_container_name("myapp").is_ok());
        assert!(validate_container_name("my-app").is_ok());
        assert!(validate_container_name("my_app").is_ok());
        assert!(validate_container_name("my.app").is_ok());
        assert!(validate_container_name("myapp123").is_ok());
        assert!(validate_container_name("MyApp").is_ok());
        assert!(validate_container_name("a1b2c3d4e5f6").is_ok());
        assert!(validate_container_name("").is_err());
    }

    #[test]
    fn test_check_mode() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            container: myapp
            command: ls /app
            "#,
        )
        .unwrap();
        let result = docker_exec(parse_params(yaml).unwrap(), true);
        assert!(result.is_ok());
        let result = result.unwrap();
        assert!(result.changed);
        assert!(result.output.unwrap().contains("Would execute"));
    }
}