ya_client_model/activity/
exe_script_command.rs

1/*
2 * Yagna Activity API
3 *
4 * It conforms with capability level 1 of the [Activity API specification](https://golem-network.gitbook.io/golem-internal-documentation-test/golem-activity-protocol/golem-activity-api).
5 *
6 * The version of the OpenAPI document: v1
7 *
8 *
9 */
10
11use crate::activity::ExeScriptCommandState;
12use serde::{Deserialize, Serialize};
13use std::collections::HashMap;
14
15#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
16#[serde(rename_all = "camelCase")]
17pub enum ExeScriptCommand {
18    Sign {},
19    Deploy {
20        #[serde(default)]
21        net: Vec<Network>,
22        #[serde(default)]
23        hosts: HashMap<String, String>, // hostname -> IP
24    },
25    Start {
26        #[serde(default)]
27        args: Vec<String>,
28    },
29    Run {
30        entry_point: String,
31        #[serde(default)]
32        args: Vec<String>,
33        #[serde(skip_serializing_if = "Option::is_none")]
34        #[serde(default)]
35        capture: Option<Capture>,
36    },
37    Transfer {
38        from: String,
39        to: String,
40        #[serde(flatten)]
41        args: TransferArgs,
42    },
43    Terminate {},
44}
45
46#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
47#[serde(rename_all = "camelCase")]
48pub struct Network {
49    pub id: String,
50    pub ip: String,
51    pub mask: Option<String>,
52    pub gateway: Option<String>,
53    pub node_ip: String,
54    #[serde(default)]
55    pub nodes: HashMap<String, String>, // IP -> NodeId
56}
57
58#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Default)]
59#[serde(rename_all = "camelCase")]
60pub struct Capture {
61    #[serde(skip_serializing_if = "Option::is_none")]
62    pub stdout: Option<CaptureMode>,
63    #[serde(skip_serializing_if = "Option::is_none")]
64    pub stderr: Option<CaptureMode>,
65}
66
67#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
68#[serde(rename_all = "camelCase")]
69pub enum CaptureMode {
70    AtEnd {
71        #[serde(skip_serializing_if = "Option::is_none")]
72        #[serde(flatten)]
73        part: Option<CapturePart>,
74        #[serde(skip_serializing_if = "Option::is_none")]
75        format: Option<CaptureFormat>,
76    },
77    Stream {
78        #[serde(skip_serializing_if = "Option::is_none")]
79        limit: Option<usize>,
80        #[serde(skip_serializing_if = "Option::is_none")]
81        format: Option<CaptureFormat>,
82    },
83}
84
85#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
86#[serde(rename_all = "lowercase")]
87pub enum CaptureFormat {
88    #[default]
89    #[serde(alias = "string")]
90    Str,
91    #[serde(alias = "binary")]
92    Bin,
93}
94
95#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
96#[serde(rename_all = "camelCase")]
97pub enum CapturePart {
98    Head(usize),
99    Tail(usize),
100    HeadTail(usize),
101}
102
103#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Default)]
104pub struct TransferArgs {
105    pub format: Option<String>,
106    pub depth: Option<usize>,
107    pub fileset: Option<FileSet>,
108}
109
110#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
111#[serde(untagged)]
112pub enum FileSet {
113    Pattern(SetEntry<String>),
114    Object(SetEntry<SetObject>),
115}
116
117#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Default)]
118pub struct SetObject {
119    pub desc: Option<String>,
120    pub includes: Option<SetEntry<String>>,
121    pub excludes: Option<SetEntry<String>>,
122}
123
124#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
125#[serde(untagged)]
126pub enum SetEntry<T> {
127    Single(T),
128    Multiple(Vec<T>),
129}
130
131impl From<ExeScriptCommand> for ExeScriptCommandState {
132    fn from(cmd: ExeScriptCommand) -> Self {
133        match cmd {
134            ExeScriptCommand::Sign { .. } => ExeScriptCommandState {
135                command: "Sign".to_string(),
136                progress: None,
137                params: None,
138            },
139            ExeScriptCommand::Deploy { .. } => ExeScriptCommandState {
140                command: "Deploy".to_string(),
141                progress: None,
142                params: None,
143            },
144            ExeScriptCommand::Start { args } => ExeScriptCommandState {
145                command: "Start".to_string(),
146                progress: None,
147                params: Some(args),
148            },
149            ExeScriptCommand::Run {
150                entry_point,
151                mut args,
152                capture: _,
153            } => ExeScriptCommandState {
154                command: "Run".to_string(),
155                progress: None,
156                params: Some({
157                    args.insert(0, entry_point);
158                    args
159                }),
160            },
161            ExeScriptCommand::Transfer { from, to, .. } => ExeScriptCommandState {
162                command: "Transfer".to_string(),
163                progress: None,
164                params: Some(vec![from, to]),
165            },
166            ExeScriptCommand::Terminate {} => ExeScriptCommandState {
167                command: "Terminate".to_string(),
168                progress: None,
169                params: None,
170            },
171        }
172    }
173}
174
175#[cfg(test)]
176mod test {
177
178    #[test]
179    fn test_transfers_parsing() {
180        let command = r#"
181        [ {"transfer": {
182            "from": "http://some-site/data.zip",
183            "to": "container:/app//in/data.zip"
184          } },
185          {"transfer": {
186            "from": "http://some-site/data.zip",
187            "to": "container:/app//in/",
188            "format": "zip"
189           } },
190          {"transfer": {
191            "from": "http://some-site/data.zip",
192            "to": "container:/app//in/",
193            "depth": 0,
194            "format": "zip.0",
195            "fileset": "*.o"
196           } },
197           {"transfer": {
198            "from": "http://some-site/data.zip",
199            "to": "container:/app//in/",
200            "format": "zip.0",
201            "fileset": [
202                {"includes": "out/*",
203                 "excludes": ["*.tmp", ".gitignore"]
204                },
205                {"includes": "gen-spec/*"
206                }
207            ]
208           } }
209
210
211        ]"#;
212        let _: Vec<super::ExeScriptCommand> = serde_json::from_str(command).unwrap();
213    }
214}