summaryrefslogtreecommitdiffstats
path: root/src/job/runnable.rs
blob: 620df733f3203f0dec5787fffa4f103c0d31d539 (plain)
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
//
// Copyright (c) 2020-2021 science+computing ag and other contributors
//
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//

use std::collections::HashMap;

use anyhow::anyhow;
use anyhow::Context;
use anyhow::Result;
use getset::Getters;
use log::debug;
use uuid::Uuid;

use crate::config::Configuration;
use crate::filestore::Artifact;
use crate::job::Job;
use crate::job::JobResource;
use crate::package::Package;
use crate::package::Script;
use crate::package::ScriptBuilder;
use crate::source::SourceCache;
use crate::source::SourceEntry;
use crate::util::EnvironmentVariableName;
use crate::util::docker::ImageName;

/// A job configuration that can be run. All inputs are clear here.
#[derive(Debug, Getters)]
pub struct RunnableJob {
    #[getset(get = "pub")]
    uuid: Uuid,

    #[getset(get = "pub")]
    package: Package,

    #[getset(get = "pub")]
    image: ImageName,

    #[getset(get = "pub")]
    source_cache: SourceCache,

    #[getset(get = "pub")]
    script: Script,

    #[getset(get = "pub")]
    resources: Vec<JobResource>,
}

impl RunnableJob {
    pub async fn build_from_job(
        job: &Job,
        source_cache: &SourceCache,
        config: &Configuration,
        dependencies: Vec<Artifact>,
    ) -> Result<Self> {
        // Add the environment from the original Job object to the resources
        let resources = dependencies
            .into_iter()
            .map(JobResource::from)
            .chain({
                job.resources()
                    .iter()
                    .filter(|jr| jr.env().is_some())
                    .cloned()
            })
            .collect();

        if config.containers().check_env_names() {
            debug!("Checking environment if all variables are allowed!");
            let _ = Self::env_resources(job.resources(), job.package().environment().as_ref())
                .into_iter()
                .inspect(|(name, _)| debug!("Checking: {}", name))
                .try_for_each(|(name, _)| {
                    if !config.containers().allowed_env().contains(&name) {
                        Err(anyhow!("Environment variable name not allowed: {}", name))
                    } else {
                        Ok(())
                    }
                })
                .with_context(|| {
                    anyhow!(
                        "Checking allowed variables for package {} {}",
                        job.package().name(),
                        job.package().version()
                    )
                })
                .context("Checking allowed variable names")?;
        } else {
            debug!("Environment checking disabled");
        }

        let script = ScriptBuilder::new(&job.script_shebang).build(
            &job.package,
            &job.script_phases,
            *config.strict_script_interpolation(),
        )?;

        Ok(RunnableJob {
            uuid: job.uuid.clone(),
            package: job.package.clone(),
            image: job.image.clone(),
            resources,
            source_cache: source_cache.clone(),

            script,
        })
    }

    pub fn package_sources(&self) -> Vec<SourceEntry> {
        self.source_cache.sources_for(&self.package())
    }

    pub fn environment(&self) -> Vec<(EnvironmentVariableName, String)> {
        Self::env_resources(&self.resources, self.package().environment().as_ref())
    }

    /// Helper function to collect a list of resources and the result of package.environment() into
    /// a Vec of environment variables
    fn env_resources(
        resources: &[JobResource],
        pkgenv: Option<&HashMap<EnvironmentVariableName, String>>,
    ) -> Vec<(EnvironmentVariableName, String)> {
        let iter = resources
            .iter()
            .filter_map(JobResource::env)
            .map(|(k, v)| (k.clone(), v.clone()));

        if let Some(hm) = pkgenv {
            iter.chain(hm.iter().map(|(k, v)| (k.clone(), v.clone())))
                .collect()
        } else {
            iter.collect()
        }
    }

    pub fn package_environment(&self) -> Vec<(String, String)> {
        vec![
            (
                String::from("BUTIDO_PACKAGE_NAME"),
                self.package().name().clone().to_string(),
            ),
            (
                String::from("BUTIDO_PACKAGE_VERSION"),
                self.package().version().clone().to_string(),
            ),
            (
                String::from("BUTIDO_PACKAGE_VERSION_IS_SEMVER"),
                String::from(if *self.package().version_is_semver() {
                    "true"
                } else {
                    "false"
                }),
            ),
        ]
    }

}