/
index.js
executable file
·200 lines (163 loc) · 4.5 KB
/
index.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
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
#!/usr/bin/env node
'use strict'
/**
* operations-orchestration-backup
* @module operations-orchestration-backup
*/
var OO = require('operations-orchestration-api');
var commandLineArgs = require('command-line-args');
var commandLineUsage = require('command-line-usage');
var jsonfile = require('jsonfile');
var chalk = require('chalk');
var options = {
username: 'admin',
password: 'admin',
baseUrl: 'http://localhost:8050'
};
/**
* get module package information
*
* @method getPackageInfo
* @return {string} package information as a sring
*/
function getPackageInfo() {
var pkg = require('./package.json');
var str = '';
str += pkg.name + "\n";
str += 'version: ' + pkg.version + ' by ' + pkg.author.name + "\n";
return str;
}
/**
* prints out command line usage information and exit with an error code
*
* @method cliShowUsage
* @param {string} command line arguments usage help
* @param {string} the error message to print to the screen
*/
function cliShowUsage(cliUsage, msg) {
console.log(getPackageInfo());
console.log(chalk.red(Error(msg)));
console.error(cliUsage);
process.exit(1);
}
/**
* prints out an error message
*
* @method cliExitError
* @param {string} the error message to print on the screen
*/
function cliExitError(msg) {
console.log(chalk.red(msg));
process.exit(1);
}
/**
* prints out a success message
*
* @method cliExitClean
* @param {string} a success message text to print to the screen
*/
function cliExitClean(msg) {
console.log(chalk.green(msg));
process.exit(0);
}
/**
* parse command line arguments to set the options for connecting to OO
*
* @method cliCheck
* @return {object} return the object with the passed parameters to the command line
*/
function cliCheck() {
var cli = [
{ name: 'username', alias: 'u', type: String, description: 'Username for Operations Orchestration that is allowed to query the API' },
{ name: 'password', alias: 'p', type: String, description: 'Password for the Username provided' },
{ name: 'url', type: String, description: 'The URL where Operations Orchestration API is available. Example: http://localhost:8050' },
{ name: 'import', type: String, description: 'Provide a JSON file name to import all the data from into an OO install, expecting an array of objects' },
{ name: 'export', type: String, description: 'Provide a file name to export all the data to as JSON object' }
];
var cliOptions = commandLineArgs(cli);
var cliUsageStruct = [
{
header: 'Options',
optionList: cli
}
];
var cliUsage = commandLineUsage(cliUsageStruct);
if (!cliOptions.url) {
cliShowUsage(cliUsage, "must provide url for the OO REST API server");
return false;
} else {
options.baseUrl = cliOptions.url + '/oo/rest/v1';
}
if (cliOptions.username) {
options.username = cliOptions.username;
}
if (cliOptions.password) {
options.password = cliOptions.password;
}
if (!cliOptions.import && !cliOptions.export) {
cliShowUsage(cliUsage, "must provide at least one option of --export or --import options available");
return false;
}
// set OO API settings
OO.setClient(options);
return cliOptions;
}
/**
* imports configuration from a JSON file into an OO install
*
* @method importConfig
* @param {object} the command line arguments options object
*/
function importConfig(options) {
jsonfile.readFile(options.import, async function (err, obj) {
if (err) {
cliExitError(err);
}
var item = '';
for (var configItem of obj) {
try {
await OO.config.setItem(configItem)
process.stdout.write(chalk.green('+'));
} catch (err) {
process.stdout.write(chalk.red('-'));
}
}
});
}
/**
* exports an OO install configuration to a JSON file
*
* @method exportConfig
* @param {object} the command line arguments options object
*/
async function exportConfig(options) {
if (!options.export) {
cliExitError(Error("must provide export file name"));
return false;
}
try {
const body = await OO.config.getAllItems();
if (body) {
jsonfile.writeFile(options.export, body, { spaces: 2 }, function (err) {
if (err) {
cliExitError(err);
}
cliExitClean("Successfully export OO configuration to: " + options.export);
return true;
});
}
} catch (err) {
cliExitError(err);
}
}
var cliOptions = cliCheck();
if (!cliOptions) {
cliExitError();
}
console.log(getPackageInfo());
if (cliOptions.export) {
return exportConfig(cliOptions).then(() => cliExitClean());
}
if (cliOptions.import) {
return importConfig(cliOptions).then(() => cliExitClean());
}