-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathactions.js
116 lines (102 loc) · 4.3 KB
/
actions.js
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
module.exports = function (self) {
self.setActionDefinitions({
execCommand: {
name: 'Execute SSH Command',
options: [
{
id: 'cmd',
type: 'textinput',
label: 'Command',
tooltip:
'Executes command(s) on the remove server. You can execute multiple commands asynchronously if you separate them with \\n.\nSee help page for more info.',
useVariables: true,
},
],
callback: async (event) => {
// make sure that we start our execution status as OK
self.setVariableValues({ [self.getConstants().CMD_ERROR_VAR_NAME]: false })
self.checkFeedbacks(self.getConstants().CMD_ERROR_FEEDBACK_NAME)
let cmd = event.options.cmd
//parse the command for any variables
let cmdParsed = await self.parseVariablesInString(cmd)
// we need to check for line breaks and execute each line as a separate command
// NOTE: if you need \n to be in the string without a linefeed, you can use \\n to escape \n representing linefeed
let currentCmds = cmdParsed.split(/\\*(?<!\\)\\n/g)
currentCmds.forEach((element) => {
// make sure to replace any \\n with a regular \n
let currentCmd = element.replace(/\\\\n/g, '\\n')
self.log('debug', 'Executing command: ' + currentCmd)
self.sshClient.exec(currentCmd, (err, stream) => {
stream.stderr.on('data', (data) => {
// here is where a STDERR happened
self.setVariableValues({ [self.getConstants().CMD_ERROR_VAR_NAME]: true })
self.checkFeedbacks(self.getConstants().CMD_ERROR_FEEDBACK_NAME)
self.log('error', 'Command: ' + currentCmd + ' wrote to STDERR: ')
})
stream.on('exit', (code) => {
if (code != 0) {
// we have an error code that is not 0 coming back, show error status
self.setVariableValues({ [self.getConstants().CMD_ERROR_VAR_NAME]: true })
self.checkFeedbacks(self.getConstants().CMD_ERROR_FEEDBACK_NAME)
self.log('error', 'Command: ' + currentCmd + ' exited with error code: ' + code)
}
})
stream.on('data', (data) => {
self.log('debug', data.toString())
})
})
})
},
},
shellCommand: {
name: 'Execute SSH Command in Shell Session (Advanced)',
options: [
{
id: 'cmd',
type: 'textinput',
label: 'Command',
tooltip:
'Execute command(s) on the remote server using a shell session. You can execute multiple commands sequentially if you separate them with \\n.\nSee help page for more info.',
useVariables: true,
},
],
callback: async (event) => {
// make sure that we start our execution status as OK
self.setVariableValues({ [self.getConstants().CMD_ERROR_VAR_NAME]: false })
self.checkFeedbacks(self.getConstants().CMD_ERROR_FEEDBACK_NAME)
let cmd = event.options.cmd
//parse the command for any variables
let cmdParsed = await self.parseVariablesInString(cmd)
self.log('debug', 'Executing advanced command: ' + cmdParsed);
self.sshClient.shell((err, stream) => {
stream
.on('close', (code) => {
self.log('debug', 'stream closed with code: ' + code)
if (code != 0) {
self.setVariableValues({ [self.getConstants().CMD_ERROR_VAR_NAME]: true })
self.checkFeedbacks(self.getConstants().CMD_ERROR_FEEDBACK_NAME)
}
})
.on('data', (data) => {
self.log('debug', data.toString())
})
.on('error', (e) => {
self.log('error', 'Stream error: ' + e)
self.setVariableValues({ [self.getConstants().CMD_ERROR_VAR_NAME]: true })
self.checkFeedbacks(self.getConstants().CMD_ERROR_FEEDBACK_NAME)
self.sshClient.end()
})
// we need to check for line breaks and execute each line as a separate command
// NOTE: if you need \n to be in the string without a linefeed, you can use \\n to escape \n representing linefeed
let currentCmd = cmdParsed.replace(/\\*(?<!\\)\\n/g, String.fromCharCode(10))
// make sure to replace any \\n with a regular \n
currentCmd = currentCmd.replace(/\\\\n/g, '\\n')
// make sure to concat a \nexit\n at the end, so that we can end the shell session when we are done executing commands
currentCmd = currentCmd + String.fromCharCode(10) + 'exit' + String.fromCharCode(10)
self.log('debug', 'Executing command: ' + currentCmd)
stream.end(currentCmd)
})
},
},
})
}