Skip to content

Commit 652ac6e

Browse files
committed
Windows support for diagnostic collection
TODO: 1. rewrite - move all to node without external calls. 2. cleanup - currently very chatty log due to the hour and instability.
1 parent 496995c commit 652ac6e

File tree

6 files changed

+98
-19
lines changed

6 files changed

+98
-19
lines changed

config.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,16 @@ config.on_premise = {
3535
/*
3636
Central Stats Collection & Diagnostics
3737
*/
38+
39+
var is_windows = (process.platform === "win32");
40+
41+
if (!is_windows){
42+
process.env.ProgramData = '/tmp';
43+
}
3844
config.central_stats = {
3945
send_stats: true,
4046
central_listener: '127.0.0.1',
41-
previous_diag_packs_dir: '/tmp/prev_diags',
47+
previous_diag_packs_dir: process.env.ProgramData+'/prev_diags',
4248
previous_diag_packs_count: 3 //TODO: We might want to split between agent and server
4349
};
4450

@@ -66,4 +72,4 @@ config.SUPERVISOR_DEFAULTS = {
6672
DIRECTORY: '/root/node_modules/noobaa-core'
6773
};
6874

69-
module.exports = config;
75+
module.exports = config;

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "noobaa-core",
3-
"version": "0.4.5",
3+
"version": "0.4.4",
44
"private": true,
55
"description": "noobaa-core ===========",
66
"engines": {

src/agent/agent.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,9 @@ Agent.prototype.self_test_peer = function(req) {
696696

697697
Agent.prototype.collect_diagnostics = function(req) {
698698
dbg.log1('Recieved diag req', req);
699-
var inner_path = '/tmp/agent_diag.tgz';
699+
var is_windows = (process.platform === "win32");
700+
var inner_path = is_windows ? process.env.ProgramData + '/agent_diag.tgz' : '/tmp/agent_diag.tgz';
701+
700702
return P.fcall(function() {
701703
return diag.collect_agent_diagnostics();
702704
})

src/util/base_diagnostics.js

+34-12
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,29 @@ var promise_utils = require('../util/promise_utils');
1919
var os_utils = require('../util/os_util');
2020
var config = require('../../config.js');
2121

22-
var TMP_WORK_DIR = '/tmp/diag';
22+
var is_windows = (process.platform === "win32");
23+
24+
var TMP_WORK_DIR = is_windows ? process.env.ProgramData+'/diag' : '/tmp/diag';
2325

2426
function collect_basic_diagnostics(limit_logs_size) {
2527
return P.fcall(function() {
26-
return promise_utils.promised_spawn('rm', ['-rf', TMP_WORK_DIR], process.cwd(), true);
28+
return promise_utils.folder_delete(TMP_WORK_DIR);
29+
//return promise_utils.promised_spawn('rm', ['-rf', TMP_WORK_DIR], process.cwd(), true);
2730
})
2831
.then(function() {
29-
return promise_utils.promised_spawn('rm', ['-rf', process.cwd() + '/build/public/diagnose.tgz'], process.cwd(), true);
32+
return promise_utils.file_delete(process.cwd() + '/build/public/diagnose.tgz');
33+
//return promise_utils.promised_spawn('rm', ['-rf', process.cwd() + '/build/public/diagnose.tgz'], process.cwd(), true);
3034
})
3135
.then(function() {
36+
console.log('creating ',TMP_WORK_DIR);
3237
return mkdirp.sync(TMP_WORK_DIR);
3338
})
3439
.then(function() {
3540
if (fs.existsSync(process.cwd() + '/logs')) {
36-
if (limit_logs_size){
41+
if (limit_logs_size) {
3742
//will take only noobaa.log and the first 9 zipped logs
38-
return promise_utils.full_dir_copy(process.cwd() + '/logs', TMP_WORK_DIR,'noobaa?[1-9][0-9].log.gz');
39-
}else{
43+
return promise_utils.full_dir_copy(process.cwd() + '/logs', TMP_WORK_DIR, 'noobaa?[1-9][0-9].log.gz');
44+
} else {
4045
return promise_utils.full_dir_copy(process.cwd() + '/logs', TMP_WORK_DIR);
4146
}
4247
} else {
@@ -45,13 +50,15 @@ function collect_basic_diagnostics(limit_logs_size) {
4550
})
4651
.then(function() {
4752
if (fs.existsSync('/var/log/noobaa_local_service.log')) {
48-
return promise_utils.promised_spawn('cp', ['-f', '/var/log/noobaa_local_service.log', TMP_WORK_DIR], process.cwd());
53+
return promise_utils.file_copy('/var/log/noobaa_local_service.log', TMP_WORK_DIR);
54+
//return promise_utils.promised_spawn('cp', ['-f', '/var/log/noobaa_local_service.log', TMP_WORK_DIR], process.cwd());
4955
} else {
5056
return;
5157
}
5258
})
5359
.then(function() {
54-
return promise_utils.promised_spawn('cp', ['-f', process.cwd() + '/package.json', TMP_WORK_DIR], process.cwd());
60+
return promise_utils.file_copy( process.cwd() + '/package.json', TMP_WORK_DIR);
61+
//return promise_utils.promised_spawn('cp', ['-f', process.cwd() + '/package.json', TMP_WORK_DIR], process.cwd());
5562
})
5663
.then(function() {
5764
return os_utils.netstat_single(TMP_WORK_DIR + '/netstat.out');
@@ -71,7 +78,11 @@ function write_agent_diag_file(data) {
7178

7279
function pack_diagnostics(dst) {
7380
return P.fcall(function() {
74-
return promise_utils.promised_exec('tar -zcvf ' + dst + ' ' + TMP_WORK_DIR + '/*');
81+
return promise_utils.file_delete(dst);
82+
}).then(function(){
83+
console.log('pack - ',dst);
84+
return promise_utils.pack(dst,TMP_WORK_DIR);
85+
// return promise_utils.promised_exec('tar -zcvf ' + dst + ' ' + TMP_WORK_DIR + '/*');
7586
})
7687
.then(function() {
7788
return archive_diagnostics_pack(dst);
@@ -80,7 +91,8 @@ function pack_diagnostics(dst) {
8091
//The reason is that every distribution has its own issues.
8192
//We had a case where it failed due to file change during the file.
8293
//This flag can help, but not all the distributions support it
83-
console.error("failed to tar, an attempt to ignore file changes");
94+
//This is not valid for windows where we have our own executable
95+
console.error("failed to tar, an attempt to ignore file changes",err);
8496
return P.fcall(function() {
8597
return promise_utils.promised_exec('tar --warning=no-file-changed -zcvf ' + dst + ' ' + TMP_WORK_DIR + '/*');
8698
})
@@ -101,27 +113,37 @@ function pack_diagnostics(dst) {
101113
//Archive the current diagnostics pack, save to
102114
function archive_diagnostics_pack(dst) {
103115
return P.fcall(function() {
116+
console.log('archive_diagnostics_pack1');
104117
return mkdirp.sync(config.central_stats.previous_diag_packs_dir);
105118
})
106119
.then(function() {
120+
console.log('archive_diagnostics_pack2');
107121
return P.nfcall(fs.readdir, config.central_stats.previous_diag_packs_dir);
108122
})
109123
.then(function(files) {
124+
console.log('archive_diagnostics_pack3');
125+
110126
//Check if current number of archived packs exceeds the max
111127
if (files.length === config.central_stats.previous_diag_packs_count) {
112128
//Delete the oldest pack
129+
console.log('archive_diagnostics_pack4');
130+
113131
var sorted_files = _.orderBy(files);
114132
return P.nfcall(fs.unlink, config.central_stats.previous_diag_packs_dir + '/' + sorted_files[0]);
115133
} else {
134+
console.log('archive_diagnostics_pack5');
135+
116136
return;
117137
}
118138
})
119139
.then(function() {
140+
console.log('archive_diagnostics_pack6');
141+
120142
//Archive the current pack
121143
var now = new Date();
122144
var tail = now.getDate() + '-' + (now.getMonth() + 1) + '_' + now.getHours() + '-' + now.getMinutes();
123-
124-
return promise_utils.promised_exec('cp ' + dst + ' ' + config.central_stats.previous_diag_packs_dir + '/DiagPack_' + tail + '.tgz');
145+
return promise_utils.file_copy(dst,config.central_stats.previous_diag_packs_dir + '/DiagPack_' + tail + '.tgz');
146+
//return promise_utils.file_copy('cp ' + dst + ' ' + config.central_stats.previous_diag_packs_dir + '/DiagPack_' + tail + '.tgz');
125147
})
126148
.then(null, function(err) {
127149
console.error('Error in archive_diagnostics_pack', err, err.stack);

src/util/os_util.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,11 @@ function top_single(dst) {
201201

202202
function netstat_single(dst) {
203203
var file_redirect = dst ? ' &> ' + dst : '';
204-
if (os.type() === 'Darwin' || os.type() === 'Windows_NT') {
204+
if (os.type() === 'Darwin' ){
205205
return promise_utils.promised_exec('netstat -na' + file_redirect);
206-
} else if (os.type() === 'Linux') {
206+
} else if (os.type() === 'Windows_NT') {
207+
return promise_utils.promised_exec('netstat -na >'+dst);
208+
}else if (os.type() === 'Linux') {
207209
return promise_utils.promised_exec('netstat -nap' + file_redirect);
208210
} else {
209211
throw new Error('netstat_single ' + os.type + ' not supported');

src/util/promise_utils.js

+48-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ var P = require('./promise');
66
var child_process = require('child_process');
77
require('setimmediate');
88
var ncp = require('ncp').ncp;
9+
var fs = require('fs');
910
var dbg = require('../util/debug_module')(__filename);
1011

12+
var is_windows = (process.platform === "win32");
1113

1214
module.exports = {
1315
join: join,
@@ -21,6 +23,10 @@ module.exports = {
2123
promised_spawn: promised_spawn,
2224
promised_exec: promised_exec,
2325
full_dir_copy: full_dir_copy,
26+
file_copy: file_copy,
27+
file_delete: file_delete,
28+
folder_delete: folder_delete,
29+
pack: pack,
2430
wait_for_event: wait_for_event,
2531
pwhile: pwhile,
2632
auto: auto,
@@ -201,7 +207,7 @@ function set_immediate() {
201207
TODO: The two following should be removed once we push to node 12 which has the spawnSync and execSync
202208
*/
203209
function promised_spawn(command, args, cwd, ignore_rc) {
204-
dbg.log2('promise spawn', command, args, cwd, ignore_rc);
210+
dbg.log0('promise spawn', command, args, cwd, ignore_rc);
205211
if (!command || !cwd) {
206212
return P.reject(new Error('Both command and working directory must be given'));
207213
}
@@ -218,6 +224,7 @@ function promised_spawn(command, args, cwd, ignore_rc) {
218224
dbg.log2('on stdout', data);
219225
});
220226

227+
221228
proc.on('error', function(error) {
222229
if ((typeof ignore_rc !== 'undefined') && ignore_rc) {
223230
dbg.warn(command + " " + args.join(" ") + " in " + cwd + " exited with error:" + error.message + " and ignored");
@@ -268,6 +275,46 @@ function promised_exec(command, ignore_rc, return_stdout) {
268275
return deferred.promise;
269276
}
270277

278+
function pack(tar_file_name, source) {
279+
console.log('pack windows?', is_windows);
280+
if (is_windows) {
281+
console.log('in windows','7za.exe a -ttar -so tmp.tar ' + source.replace(/\//g, '\\') + '| 7za.exe a -si ' + tar_file_name.replace(/\//g, '\\'));
282+
return promised_exec('7za.exe a -ttar -so tmp.tar ' + source.replace(/\//g, '\\') + '| 7za.exe a -si ' + tar_file_name.replace(/\//g, '\\'));
283+
} else {
284+
console.log('not windows?', is_windows);
285+
return promised_exec('tar -zcvf ' + tar_file_name + ' ' + source + '/*');
286+
}
287+
}
288+
289+
function file_copy(src, dst) {
290+
if (is_windows) {
291+
console.log('file copy ' + src.replace(/\//g, '\\')+' '+dst.replace(/\//g, '\\'));
292+
return promised_exec('copy /Y "'+src.replace(/\//g, '\\') +'" "'+ dst.replace(/\//g, '\\')+'"');
293+
} else {
294+
return promised_spawn('cp', ['-f', src, dst], process.cwd());
295+
}
296+
}
297+
298+
function folder_delete(path) {
299+
if( fs.existsSync(path) ) {
300+
fs.readdirSync(path).forEach(function(file,index){
301+
var curPath = path + "/" + file;
302+
if(fs.lstatSync(curPath).isDirectory()) { // recurse
303+
folder_delete(curPath);
304+
} else { // delete file
305+
fs.unlinkSync(curPath);
306+
}
307+
});
308+
fs.rmdirSync(path);
309+
}
310+
}
311+
312+
function file_delete(file_name) {
313+
if( fs.existsSync(file_name) ) {
314+
return fs.unlinkAsync(file_name);
315+
}
316+
}
317+
271318
function full_dir_copy(src, dst, filter_regex) {
272319
ncp.limit = 10;
273320
let ncp_options = {};

0 commit comments

Comments
 (0)