-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_environment_scripts.js
executable file
·154 lines (117 loc) · 4.58 KB
/
create_environment_scripts.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
#!/usr/bin/env node
///
/// Little script to copy the current UPS enviroment into a few
/// static scripts. This should be called every time you change enviromnent.
///
/// Creates the file ups_env.json
var fs = require('fs');
// Template for nodemon file
var nodemon = {
"__comment" : "This file is auto-generated from create_environment_scripts.js. Make changes there!",
verbose: true,
signal: "sighup",
ext: "js pug inc dylib so",
ignore: ["static/**", "logs/*"],
}
var pm_scripts = {
pm2: {
"__comment" : "pm2.json This file is auto-generated from create_environment_scripts.js. Make changes there!",
"apps" : [
{
"name": "argo-node",
"script": "index.js",
"watch": false,
"error_file":__dirname+"/logs/argo.err",
"out_file":__dirname+"/logs/argo.out",
"cwd":__dirname,
"env": {
"NODE_ENV": "production",
}
}
]
},
pm2_live: {
"__comment" : "pm2_live.json This file is auto-generated from create_environment_scripts.js. Make changes there!",
"apps" : [
{
name: 'argo-live-backend',
"cwd":__dirname,
script: 'libargo/run-live-backend.sh',
exec_interpreter: 'bash',
exec_mode: "fork_mode",
args: "libargo/live.config.json",
env : {
}
}
]
}
};
// node-cluster is responsible for clustering, not pm2
// "exec_mode" : "cluster",
// "instances" : 2,
// "wait_ready": true,
// "listen_timeout": 20000,
var obj = { DYLD_LIBRARY_PATH :null
, DYLD_FALLBACK_LIBRARY_PATH:null
, LD_LIBRARY_PATH :null
, ROOT_INCLUDE_PATH :null
, PYTHONPATH :null
, PYTHONHOME :null
, PATH :null
};
// Nodemon script.
nodemon.env = {};
for(v in obj) {
nodemon.env[v] = process.env[v]; // get current variable.
}
nodemon.env["DYLD_LIBRARY_PATH"] = "";
nodemon.env["DYLD_FALLBACK_LIBRARY_PATH"] = process.env["DYLD_LIBRARY_PATH"];
fs.writeFileSync(__dirname+"/nodemon.json",JSON.stringify(nodemon,null,2));
// Pm2 scripts.
if(__dirname.includes("_dev")) {
// Development version! Rename the output exec
pm_scripts.pm2.apps[0].name="argo-dev";
}
for(var scpt in pm_scripts) {
var pm2 = pm_scripts[scpt];
for(var iapp=0;iapp<pm2.apps.length;iapp++){
for(v in obj) {
pm2.apps[iapp].env[v] = process.env[v]; // get current variable.
}
pm2.apps[iapp].env["DYLD_LIBRARY_PATH"] = "";
pm2.apps[iapp].env["DYLD_FALLBACK_LIBRARY_PATH"] = process.env["DYLD_LIBRARY_PATH"];
}
fs.writeFileSync(__dirname+"/"+scpt+".json",JSON.stringify(pm2,null,2));
}
if(fs.existsSync("/etc/systemd/system"))
{
// We're running on a linux system, so we want to use PM2.
// Create an environment file usable by the PM2 startup script.
// We'll do this by creating an extra config file that systemd looks at, which is cool!
// https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/sect-Managing_Services_with_systemd-Unit_Files#brid-Managing_Services_with_systemd-Extending_Unit_Config
// The downside to this method: all scripts run under pm2 will use this enviroment! Boo!
// Build the file.
var cf = "";
cf += "# File auto-generated by Argo create_environment_scripts.js\n";
cf += "Environment=PATH="+process.env["PATH"]+"\n";
cf += "Environment=LD_LIBRARY_PATH="+process.env["LD_LIBRARY_PATH"]+"\n";
cf += "Environment=ROOT_INCLUDE_PATH="+process.env["ROOT_INCLUDE_PATH"]+"\n";
cf += "Environment=PYTHONPATH="+process.env["PYTHONPATH"]+"\n";
cf += "Environment=PYTHONHOME="+process.env["PYTHONHOME"]+"\n";
var need_to_redeploy = true;
var conf_file_path = "/etc/systemd/system/pm2-"+process.env["USER"]+".service.d/";
var conf_file_name = "argo-environment.conf";
if(fs.existsSync(conf_file_path+conf_file_name)) {
var old_cf = fs.readFileSync(conf_file_path+conf_file_name);
if(old_cf == cf) need_to_redeploy = false;
}
if(need_to_redeploy) {
fs.writeFileSync(__dirname+"/"+conf_file_name,cf);
console.log("PM2 environment is not ready. To prepare:");
if(!fs.existsSync(conf_file_path))
console.log(' ksu -a -c "mkdir -m 755 '+conf_file_path+'"');
console.log (' ksu -a -c "cp ./'+__dirname+"/"+conf_file_name+' '+conf_file_path+conf_file_name+ '" && rm '+__dirname+"/"+conf_file_name );
console.log (' ksu -a -c "systemctl daemon-reload"');
console.log (' ksu -a -c "systemctl restart pm2-'+process.env["USER"]+'.service"');
}
}