Updated with logic to detect if MacOS agent service definition needs to be fixed

Bryan Roe committed May 13, 2019 at 14:27 UTC 9ddc55a0eea3a7a0d97948d38c5e64aa4cc25f2e
1 file changed +75
agents/meshcore.js
+75
@@ -39,6 +39,81 @@ var MESHRIGHT_LIMITEDINPUT = 4096;
39 function createMeshCore(agent) {
40 var obj = {};
41
42 + if (process.platform == 'darwin' && !process.versions) {
43 + // This is an older MacOS Agent, so we'll need to check the service definition so that Auto-Update will function correctly
44 + var child = require('child_process').execFile('/bin/sh', ['sh']);
45 + child.stdout.str = '';
46 + child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
47 + child.stdin.write("cat /Library/LaunchDaemons/meshagent_osx64_LaunchDaemon.plist | tr '\n' '\.' | awk '{split($0, a, \"<key>KeepAlive</key>\"); split(a[2], b, \"<\"); split(b[2], c, \">\"); ");
48 + child.stdin.write(" if(c[1]==\"dict\"){ split(a[2], d, \"</dict>\"); if(split(d[1], truval, \"<true/>\")>1) { split(truval[1], kn1, \"<key>\"); split(kn1[2], kn2, \"</key>\"); print kn2[1]; } }");
49 + child.stdin.write(" else { split(c[1], ka, \"/\"); if(ka[1]==\"true\") {print \"ALWAYS\";} } }'\nexit\n");
50 + child.waitExit();
51 + if (child.stdout.str.trim() == 'Crashed') {
52 + child = require('child_process').execFile('/bin/sh', ['sh']);
53 + child.stdout.str = '';
54 + child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
55 + child.stdin.write("launchctl list | grep 'meshagent' | awk '{ if($3==\"meshagent\"){print $1;}}'\nexit\n");
56 + child.waitExit();
57 +
58 + if (parseInt(child.stdout.str.trim()) == process.pid) {
59 + // The currently running MeshAgent is us, so we can continue with the update
60 + var plist = require('fs').readFileSync('/Library/LaunchDaemons/meshagent_osx64_LaunchDaemon.plist').toString();
61 + var tokens = plist.split('<key>KeepAlive</key>');
62 + if (tokens[1].split('>')[0].split('<')[1] == 'dict') {
63 + var tmp = tokens[1].split('</dict>');
64 + tmp.shift();
65 + tokens[1] = '\n <true/>' + tmp.join('</dict>');
66 + tokens = tokens.join('<key>KeepAlive</key>');
67 +
68 + require('fs').writeFileSync('/Library/LaunchDaemons/meshagent_osx64_LaunchDaemon.plist', tokens);
69 +
70 + var fix = '';
71 + fix += ("function macosRepair()\n");
72 + fix += ("{\n");
73 + fix += (" var child = require('child_process').execFile('/bin/sh', ['sh']);\n");
74 + fix += (" child.stdout.str = '';\n");
75 + fix += (" child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });\n");
76 + fix += (" child.stderr.on('data', function (chunk) { });\n");
77 + fix += (" child.stdin.write('launchctl unload /Library/LaunchDaemons/meshagent_osx64_LaunchDaemon.plist\\n');\n");
78 + fix += (" child.stdin.write('launchctl load /Library/LaunchDaemons/meshagent_osx64_LaunchDaemon.plist\\n');\n");
79 + fix += (" child.stdin.write('rm /Library/LaunchDaemons/meshagentRepair.plist\\n');\n");
80 + fix += (" child.stdin.write('rm " + process.cwd() + "/macosRepair.js/\\n');\n");
81 + fix += (" child.stdin.write('launchctl stop meshagentRepair\\nexit\\n');\n");
82 + fix += (" child.waitExit();\n");
83 + fix += ("}\n");
84 + fix += ("macosRepair();\n");
85 + fix += ("process.exit();\n");
86 + require('fs').writeFileSync(process.cwd() + '/macosRepair.js', fix);
87 +
88 + var plist = '<?xml version="1.0" encoding="UTF-8"?>\n';
89 + plist += '<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n';
90 + plist += '<plist version="1.0">\n';
91 + plist += ' <dict>\n';
92 + plist += ' <key>Label</key>\n';
93 + plist += (' <string>meshagentRepair</string>\n');
94 + plist += ' <key>ProgramArguments</key>\n';
95 + plist += ' <array>\n';
96 + plist += (' <string>' + process.execPath + '</string>\n');
97 + plist += ' <string>macosRepair.js</string>\n';
98 + plist += ' </array>\n';
99 + plist += ' <key>WorkingDirectory</key>\n';
100 + plist += (' <string>' + process.cwd() + '</string>\n');
101 + plist += ' <key>RunAtLoad</key>\n';
102 + plist += ' <true/>\n';
103 + plist += ' </dict>\n';
104 + plist += '</plist>';
105 + require('fs').writeFileSync('/Library/LaunchDaemons/meshagentRepair.plist', plist);
106 +
107 + child = require('child_process').execFile('/bin/sh', ['sh']);
108 + child.stdout.str = '';
109 + child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
110 + child.stdin.write("launchctl load /Library/LaunchDaemons/meshagentRepair.plist\nexit\n");
111 + child.waitExit();
112 + }
113 + }
114 + }
115 + }
116 +
117 /*
118 function borderController() {
119 this.container = null;