-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
146 lines (127 loc) · 3.6 KB
/
main.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
var nconf = require('nconf')
fs = require('fs')
async = require('async')
log4js = require('log4js')
path = require('path')
var mainLogger = log4js.getLogger("main");
var tasks = {}
async.series(
[
loadConfig,
loadTasks,
executeTasks
]
);
function loadConfig(cb)
{
mainLogger.info("Loading config..");
nconf.file({ file: 'config.json' });
cb();
}
function loadTasks(cb)
{
mainLogger.info("Loading tasks..");
var loaderLogger = log4js.getLogger("taskLoader");
var sharedConfig = nconf.get("shared");
fs.readdir('./tasks',
function (err, files)
{
if(err)
{
loaderLogger.error("Could not load tasks: " + err);
return cb(err);
}
loaderLogger.info("Found %s tasks!", files.length);
async.each(files,
function (file, cb)
{
var task = require('./tasks/' + file);
var taskname = task.name;
var taskconfig = getTaskConfig(taskname);
if(nconf.get("backup:tasks").indexOf(taskname) == -1)
{
loaderLogger.warn("Ignoring task '%s'", taskname);
return cb();
}
async.reject(task.requiredConfig,
function (key, cb)
{
var taskVal = nconf.get("tasks:" + taskname + ":" + key);
var sharedVal = nconf.get("shared:" + key);
var isValid = isValidConfig(taskVal) || isValidConfig(sharedVal);
cb(isValid);
},
function (results)
{
var validConfig = results.length == 0;
if(validConfig)
{
tasks[taskname] = task;
loaderLogger.info("Task '%s' successfully loaded", taskname);
}
else
{
loaderLogger.error(
"The following config-entries for task '%s' were not found: %s",
taskname,
results.join(', ')
);
}
cb(validConfig ? null : "missing-config");
}
);
}, cb);
}
);
}
function isValidConfig(val)
{
return (val && val.length != 0);
}
function executeTasks(cb)
{
mainLogger.info("Executing %s tasks..", Object.keys(tasks).length);
var executeLogger = log4js.getLogger("taskExecutor");
var mainDestination = path.resolve(nconf.get("backup:destination"), "backup-" + Date.now());
createDirIfNotExists(mainDestination);
async.eachSeries(Object.keys(tasks),
function (taskname, cb)
{
var task = tasks[taskname];
var config = getTaskConfig(taskname);
var destination = path.join(mainDestination, taskname);
var taskLogger = log4js.getLogger(taskname);
createDirIfNotExists(destination);
executeLogger.info("Executing Task '%s'..", taskname);
task.execute(config, destination, taskLogger,
function (err)
{
if(!err)
executeLogger.info("Task '%s' was executed successfully!", taskname);
else
executeLogger.error("Task '%s' failed to execute: %s", taskname, err);
cb();
});
},
function ()
{
executeLogger.info("All tasks executed.");
cb();
}
)
}
function getTaskConfig(taskname)
{
var config = nconf.get("tasks:" + taskname);
var sharedConfig = nconf.get("shared");
for (var attr in sharedConfig)
config[attr] = sharedConfig[attr];
return config;
}
function createDirIfNotExists(dir)
{
var exists = fs.existsSync(dir);
mainLogger.debug("Creating dir, exists=%s, %s", exists, dir);
if (!exists)
fs.mkdirSync(dir);
}