-
Notifications
You must be signed in to change notification settings - Fork 1
/
ansibrest.js
74 lines (65 loc) · 2.23 KB
/
ansibrest.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
#!/usr/bin/env node
process.title = "ansibrest";
const http = require("http");
const commander = require("commander");
const Fs = require("fs-extra");
const Path = require("path");
const yaml = require("js-yaml");
const log4js = require("log4js");
const Ansibrest = require("./lib/ansibrest");
const cnfPath = Path.join(process.cwd(), ".ansibrest");
if(!Fs.existsSync(cnfPath)) Fs.writeFileSync(cnfPath, "ansiblePath: ansible\ninventoryPath: inventories");
const config = yaml.safeLoad(Fs.readFileSync(cnfPath, "utf8")) || {};
const program = commander
.option("-p,--port <PORT>", "PORT", Number, config.port || 2400)
.option("--base-path <BASE_PATH>", "BASE_PATH", String, config.base_path || "")
.option("--ansible-path <ANSIBLE_PATH>", "ANSIBLE_PATH", String, config.ansible_path || "ansible")
.option("--inventory-path <INVENTORY_PATH>", "INVENTORY_PATH", String, config.inventory_path || "inventories")
.option("--log-dir <LOG_DIR>", "LOG_DIR", String, config.log_dir)
.parse(process.argv);
const getLogger = ()=>{
const getLogConfig = ()=>{
if(!program.logDir || program.logDir === ""){
return {
category: "ansibrest",
type: "console"
};
}else{
const logDir = program.logDir.indexOf("/") === 0 ?
program.logDir : Path.join(__dirname, program.logDir);
try{
Fs.statSync(logDir);
}catch(err){
Fs.mkdirsSync(logDir);
}
return {
category: "ansibrest",
type: "dateFile",
filename: `${program.logDir}/ansibrest.log`,
pattern: ".yyyyMMdd"
};
}
};
log4js.configure({appenders: [getLogConfig()]});
return log4js.getLogger("ansibrest");
};
const logger = program.logger = getLogger();
const ansibrest = new Ansibrest(program);
const server = http.createServer(ansibrest.app());
ansibrest.socket(server);
server.listen(program.port);
server.on("listening", ()=>{
logger.info(`ansibrest start on ${program.port}`);
});
server.on("error", (err)=>{
logger.error(err.stack);
process.exit(-1);
});
process.on("uncaughtException", (err)=>{
logger.error(err.stack);
process.exit(-1);
});
process.on("unhandledRejection", (err)=>{
logger.error(err.stack);
process.exit(-1);
});