1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.runPipeAsUser = exports.runPipe = exports.runAsyncAsUser = exports.runAsync = exports.runSyncAsUser = exports.runSyncOk = exports.runSync = undefined;
|
7 |
|
8 | var _child_process = require('child_process');
|
9 |
|
10 | var _nightingale = require('nightingale');
|
11 |
|
12 | var _nightingale2 = _interopRequireDefault(_nightingale);
|
13 |
|
14 | var _params = require('../params');
|
15 |
|
16 | var _flowRuntime = require('flow-runtime');
|
17 |
|
18 | var _flowRuntime2 = _interopRequireDefault(_flowRuntime);
|
19 |
|
20 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
21 |
|
22 | const logger = new _nightingale2.default('app:commands:exec');
|
23 |
|
24 | const scriptDirname = `${__dirname}/../../../scripts/`;
|
25 |
|
26 | const StdioOptionValueType = _flowRuntime2.default.type('StdioOptionValueType', _flowRuntime2.default.union(_flowRuntime2.default.string('ignore'), _flowRuntime2.default.string('pipe'), _flowRuntime2.default.string('inherit')));
|
27 |
|
28 | const StdioOptionType = _flowRuntime2.default.type('StdioOptionType', _flowRuntime2.default.union(StdioOptionValueType, _flowRuntime2.default.array(_flowRuntime2.default.union(StdioOptionValueType, _flowRuntime2.default.any()))));
|
29 |
|
30 | const RunOptionsType = _flowRuntime2.default.type('RunOptionsType', _flowRuntime2.default.exactObject(_flowRuntime2.default.property('cwd', _flowRuntime2.default.nullable(_flowRuntime2.default.string()), true), _flowRuntime2.default.property('env', _flowRuntime2.default.nullable(_flowRuntime2.default.object()), true), _flowRuntime2.default.property('stdio', _flowRuntime2.default.nullable(StdioOptionType), true)));
|
31 |
|
32 | const ArgsType = _flowRuntime2.default.type('ArgsType', _flowRuntime2.default.array(_flowRuntime2.default.union(_flowRuntime2.default.string(), _flowRuntime2.default.number())));
|
33 |
|
34 | const createEnvObject = env => Object.assign({}, process.env, env, {
|
35 | BOARD_ID: _params.device.id
|
36 | });
|
37 |
|
38 | const runSync = exports.runSync = (script, args = [], _arg = {}) => {
|
39 | let _scriptType = _flowRuntime2.default.string();
|
40 |
|
41 | _flowRuntime2.default.param('script', _scriptType).assert(script);
|
42 |
|
43 | _flowRuntime2.default.param('args', ArgsType).assert(args);
|
44 |
|
45 | let { cwd, env } = RunOptionsType.assert(_arg);
|
46 |
|
47 | logger.debug('run', { script, args, cwd, env });
|
48 | const result = (0, _child_process.spawnSync)(script, args, {
|
49 | cwd: cwd || scriptDirname,
|
50 | env: createEnvObject(env)
|
51 | });
|
52 |
|
53 | if (result.error) {
|
54 | logger.error(result.error.message);
|
55 | }
|
56 |
|
57 | if (result.stderr) {
|
58 | const stderr = result.stderr.toString();
|
59 | if (stderr) {
|
60 | logger.error(stderr, { script, args, env });
|
61 | }
|
62 | }
|
63 |
|
64 | if (result.stdout) {
|
65 | const stdout = result.stdout.toString().trim();
|
66 | logger.debug(stdout, { script, args });
|
67 | return stdout;
|
68 | } else {
|
69 | return '';
|
70 | }
|
71 | };
|
72 |
|
73 | const runSyncOk = exports.runSyncOk = (script, args = [], _arg2 = {}) => {
|
74 | let _scriptType2 = _flowRuntime2.default.string();
|
75 |
|
76 | _flowRuntime2.default.param('script', _scriptType2).assert(script);
|
77 |
|
78 | _flowRuntime2.default.param('args', ArgsType).assert(args);
|
79 |
|
80 | let { cwd, env } = RunOptionsType.assert(_arg2);
|
81 |
|
82 | const result = (0, _child_process.spawnSync)(script, args, { cwd: cwd || scriptDirname, env: createEnvObject(env) });
|
83 | return !result.status;
|
84 | };
|
85 |
|
86 | const runSyncAsUser = exports.runSyncAsUser = (script, args, _arg3 = {}) => {
|
87 | let _scriptType3 = _flowRuntime2.default.string();
|
88 |
|
89 | _flowRuntime2.default.param('script', _scriptType3).assert(script);
|
90 |
|
91 | _flowRuntime2.default.param('args', ArgsType).assert(args);
|
92 |
|
93 | let { cwd, env } = RunOptionsType.assert(_arg3);
|
94 |
|
95 | if (env) throw new Error('env not supported in runSyncAsUser');
|
96 | return runSync('sudo', ['-u', _params.user, script, ...args], { cwd });
|
97 | };
|
98 |
|
99 | const runAsync = exports.runAsync = (script, args, _arg4 = {}) => {
|
100 | let _scriptType4 = _flowRuntime2.default.string();
|
101 |
|
102 | _flowRuntime2.default.param('script', _scriptType4).assert(script);
|
103 |
|
104 | _flowRuntime2.default.param('args', ArgsType).assert(args);
|
105 |
|
106 | let { cwd, env } = RunOptionsType.assert(_arg4);
|
107 |
|
108 | logger.info('exec', { script, args });
|
109 | const childProcess = (0, _child_process.spawn)(script, args, {
|
110 | cwd: cwd || scriptDirname,
|
111 | env: createEnvObject(env),
|
112 | stdio: 'pipe'
|
113 | });
|
114 | childProcess.stdout.on('data', data => logger.debug(data.toString()));
|
115 | childProcess.stderr.on('data', data => logger.error(data.toString()));
|
116 | return childProcess;
|
117 | };
|
118 |
|
119 | const runAsyncAsUser = exports.runAsyncAsUser = (script, args) => {
|
120 | let _scriptType5 = _flowRuntime2.default.string();
|
121 |
|
122 | _flowRuntime2.default.param('script', _scriptType5).assert(script);
|
123 |
|
124 | _flowRuntime2.default.param('args', ArgsType).assert(args);
|
125 |
|
126 | return runAsync('sudo', ['-u', _params.user, script, ...args]);
|
127 | };
|
128 |
|
129 | const runPipe = exports.runPipe = (script, args, _arg5 = {}) => {
|
130 | let _scriptType6 = _flowRuntime2.default.string();
|
131 |
|
132 | _flowRuntime2.default.param('script', _scriptType6).assert(script);
|
133 |
|
134 | _flowRuntime2.default.param('args', ArgsType).assert(args);
|
135 |
|
136 | let { cwd, env } = RunOptionsType.assert(_arg5);
|
137 |
|
138 | logger.debug('pipe', { script, args, cwd, env });
|
139 | const result = (0, _child_process.spawnSync)(script, args, { cwd: cwd || scriptDirname, env: createEnvObject(env) });
|
140 | if (result.error) {
|
141 | logger.error(result.error.message);
|
142 | return null;
|
143 | }
|
144 |
|
145 | if (result.stderr) {
|
146 | const stderr = result.stderr.toString();
|
147 | if (stderr) {
|
148 | logger.error(stderr, { script, args });
|
149 | return null;
|
150 | }
|
151 | }
|
152 |
|
153 | return result.stdout;
|
154 | };
|
155 |
|
156 | const runPipeAsUser = exports.runPipeAsUser = (script, args) => {
|
157 | let _scriptType7 = _flowRuntime2.default.string();
|
158 |
|
159 | _flowRuntime2.default.param('script', _scriptType7).assert(script);
|
160 |
|
161 | _flowRuntime2.default.param('args', ArgsType).assert(args);
|
162 |
|
163 | return runPipe('sudo', ['-u', _params.user, script, ...args]);
|
164 | };
|
165 |
|
\ | No newline at end of file |