-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoJobSSH.yaml
More file actions
228 lines (210 loc) · 10.7 KB
/
oJobSSH.yaml
File metadata and controls
228 lines (210 loc) · 10.7 KB
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
# Define the jobs
# Copyright 2023 Nuno Aguiar
jobs:
## -----------------
## SSH functionality
## -----------------
# Load a hosts yaml file into a channel
- name : SSH Load hosts
help : >
Loads a list of hosts (in YAML) to a channel. The expected arguments are:
- args.chHosts (String) A channel to contain the hosts data (will be created if it doesn't exist)
- args.file (String) The YAML file containing the hosts definition
- args.hosts (Array) If you don't want to provide a file you can configure the Array
The YAML file or array should contain:
- name (String) The name of the host (optional)
- host (String) The SSH host
- port (Number) The SSH port (defaults to 22)
- login (String) The SSH login
- pass (String) The SSH password
- key (String) The path to a SSH key file (optional)
exec : >
if (isUnDef(args.chHosts)) args.chHosts = "hosts";
$ch(args.chHosts).create();
if (isDef(args.file)) {
log("Loading from file '" + args.file + "'");
$ch(args.chHosts).setAll(['host', 'port', 'login'], io.readFileYAML(args.file));
}
if (isArray(args.hosts)) {
$ch(args.chHosts).setAll(['host', 'port', 'login'], args.hosts);
}
print("Hosts:");
print(af.toYAML($from($ch(args.chHosts).getAll()).select(function(r) {
return (isDef(r.name) ? r.name : r.host + ":" + (isDef(r.port) ? r.port : 22) );
})));
# Exec commands over SSH
- name : SSH Exec
help : >
Executes commands on a SSH connection. The expected arguments are:
- args.cmd (String) A SSH command-line to execute or and array of it (keep in mind that this isn't bash)
- args.stdin (String) An optional SSH stdin for the command-line to execute
- args.chHosts (String) A channel with hosts configurations to use instead of individual config
- args.host (String) The SSH host
- args.port (Number) The SSH port (defaults to 22)
- args.login (String) The SSH login
- args.pass (String) The SSH pass
- args.key (String) The path to a SSH key file (optional)
- args.exitcode (Number) Determines what exitcode should be consider success (default is 0)
- args.sudo (String) Sudo's to the corresponding user to executing the command-line
- args.quiet (Boolean) Determines if no output of the command(s) execution should be provided (default to false)
- args.prefix (Boolean) Should use a prefix for each output line if quiet is false (default is false)
- args.prefixLog (Boolean) Determines that the prefix will use log instead of print (default is false)
- args.parallel (Boolean) Executes over chHosts in parallel (default is false)
exec : >
plugin("SSH");
var iio = !args.quiet;
if (isUnDef(args.host) && isUnDef(args.chHosts) && $ch().list().indexOf("hosts")) args.chHosts = "hosts";
if (isUnDef(args.parallel)) args.parallel = false;
if (args.prefix && !args.quiet) {
iio = false;
ow.loadFormat();
if (args.prefixLog)
cmdf = ow.format.streamSHLog((f) => { return args.prefix + " | " + f; });
else
cmdf = ow.format.streamSH((f) => { return args.prefix + " | " + f; });
}
if (isDef(args.chHosts)) {
args.stdout = "";
args.stderr = "";
var tfunc = function(r) {
var cmdf;
if (args.prefix && !args.quiet) {
iio = false;
ow.loadFormat();
if (args.prefixLog)
cmdf = ow.format.streamSHLog((f) => { return (isDef(r.name) ? r.name : r.host + ":" + r.port) + " | " + f; });
else
cmdf = ow.format.streamSH((f) => { return (isDef(r.name) ? r.name : r.host + ":" + r.port) + " | " + f; });
}
if (isUnDef(r.port)) r.port = 22;
if (isUnDef(r.stdin)) r.stdin = "";
if (isUnDef(args.exitcode)) args.exitcode = 0;
var s = new SSH(r.host, r.port, r.login, r.pass, r.key, true);
if (iio) log("[" + (isDef(r.name) ? r.name : r.host + ":" + r.port) + "]");
if (isArray(args.cmd)) {
for(var i in args.cmd) {
var res;
if (!args.sudo)
res = s.exec(templify(args.cmd[i], args), args.stdin, iio, void 0, true, cmdf);
else
res = s.execSudo(templify(args.cmd[i], args), args.sudo, args.stdin, iio, void 0, true, cmdf);
args.stdout += res.stdout;
args.stderr += res.stderr;
if (String(res.exitcode) != String(args.exitcode)) throw "Exit code (for command '" + args.cmd[i] + "'): " + res.exitcode;
}
} else {
var res;
if (!args.sudo)
res = s.exec(templify(args.cmd, args), args.stdin, iio, void 0, true, cmdf);
else
res = s.execSudo(templify(args.cmd, args), args.sudo, args.stdin, iio, void 0, true, cmdf);
args.stdout += res.stdout;
args.stderr += res.stderr;
if (String(res.exitcode) != String(args.exitcode)) throw "Exit code (for command '" + args.cmd + "'): " + res.exitcode;
}
s.close();
}
if (args.parallel)
$from($ch(args.chHosts).getAll()).pselect(tfunc);
else
$from($ch(args.chHosts).getAll()).select(tfunc);
} else {
var cmdf;
if (args.prefix && !args.quiet) {
iio = false;
ow.loadFormat();
if (args.prefixLog)
cmdf = ow.format.streamSHLog((f) => { return (isDef(args.name) ? args.name : args.host + ":" + args.port) + " | " + f; });
else
cmdf = ow.format.streamSH((f) => { return (isDef(args.name) ? args.name : args.host + ":" + args.port) + " | " + f; });
}
if (isUnDef(args.port)) args.port = 22;
if (isUnDef(args.stdin)) args.stdin = "";
if (isUnDef(args.exitcode)) args.exitcode = 0;
var s = new SSH(args.host, args.port, args.login, args.pass, args.key, true);
if (iio) log("[" + (isDef(args.name) ? args.name : args.host + ":" + args.port) + "]");
args.stdout = "";
args.stderr = "";
if (isArray(args.cmd)) {
for (var i in args.cmd) {
var res;
if (!args.sudo)
res = s.exec(templify(args.cmd[i], args), args.stdin, iio, void 0, true, cmdf);
else
res = s.execSudo(templify(args.cmd[i], args), args.sudo, args.stdin, iio, void 0, true, cmdf);
args.stdout += res.stdout;
args.stderr += res.stderr;
if (String(res.exitcode) != String(args.exitcode)) throw "Exit code (for command '" + args.cmd[i] + "'): " + res.exitcode;
}
} else {
var res;
if (!args.sudo)
res = s.exec(templify(args.cmd, args), args.stdin, iio, void 0, true, cmdf);
else
res = s.execSudo(templify(args.cmd, args), args.sudo, args.stdin, iio, void 0, true, cmdf);
args.stdout += res.stdout;
args.stderr += res.stderr;
if (String(res.exitcode) != String(args.exitcode)) throw "Exit code (for command '" + args.cmd + "'): " + res.exitcode;
}
s.close();
}
# Copies files over SSH
- name : SSH Send file
help : >
Sends a file over a SSH connection. The expected arguments are:
- args.source (String) The source file to copy
- args.target (String) The target filepath
- args.chHosts (String) A channel with hosts configurations to use instead of individual config
- args.host (String) The SSH host
- args.port (Number) The SSH port (defaults to 22)
- args.login (String) The SSH login
- args.pass (String) The SSH pass
- args.key (String) The path to a SSH key file
exec : >
plugin("SSH");
if (isUnDef(args.host) && isUnDef(args.chHosts) && $ch().list().indexOf("hosts")) args.chHosts = "hosts";
if (isDef(args.chHosts)) {
$from($ch(args.chHosts).getAll()).select(function(r) {
if (isUnDef(r.port)) r.port = 22;
var s = new SSH(r.host, r.port, r.login, r.pass, r.key, true);
log("[" + (isDef(r.name) ? r.name : r.host + ":" + r.port) + "] Copying '" + args.source + "' to '" + args.target + "'");
s.put(args.source, args.target);
s.close();
})
} else {
if (isUnDef(args.port)) args.port = 22;
var s = new SSH(args.host, args.port, args.login, args.pass, args.key, true);
log("[" + (isDef(args.name) ? args.name : args.host + ":" + args.port) + "] Copying '" + args.source + "' to '" + args.target + "'");
s.put(args.source, args.target);
s.close();
}
# Retrieves remote files over SSH
- name : SSH Get file
help : >
Gets a file over a SSH connection. The expected arguments are:
- args.source (String) The remote source file to copy
- args.target (String) The local target filepath
- args.chHosts (String) A channel with hosts configurations to use instead of individual config
- args.host (String) The SSH host
- args.port (Number) The SSH port (defaults to 22)
- args.login (String) The SSH login
- args.pass (String) The SSH pass
- args.key (String) The path to a SSH key file
exec : >
plugin("SSH");
if (isUnDef(args.host) && isUnDef(args.chHosts) && $ch().list().indexOf("hosts")) args.chHosts = "hosts";
if (isDef(args.chHosts)) {
$from($ch(args.chHosts).getAll()).select(function(r) {
if (isUnDef(r.port)) r.port = 22;
var s = new SSH(r.host, r.port, r.login, r.pass, r.key, true);
log("[" + (isDef(r.name) ? r.name : r.host + ":" + r.port) + "] Getting '" + args.source + "' to '" + args.target + "'");
s.get(args.source, args.target);
s.close();
})
} else {
if (isUnDef(args.port)) args.port = 22;
var s = new SSH(args.host, args.port, args.login, args.pass, args.key, true);
log("[" + (isDef(args.name) ? args.name : args.host + ":" + args.port) + "] Getting '" + args.source + "' to '" + args.target + "'");
s.get(args.source, args.target);
s.close();
}