File size: 3,377 Bytes
3f268e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
 * Copyright 2017 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

"use strict";
/* eslint require-jsdoc: "off" */

var cwd = process.cwd();

var NODE_EXIT_KILL = 1;
var NODE_EXIT_UPDATE = 2;
var NODE_EXIT_MASTERDISCONNECT = 3;
var NODE_EXIT_KILLDAEMON = 128;
var NODE_EXIT_INTERNALERROR = 255;

function usage() {
  console.log('ERROR -- missing config file');
  console.log('Usage: node runnerProcess.ws.js runnerId');
  process.exit(NODE_EXIT_KILLDAEMON);
}

function usage() {
  console.log('ERROR -- missing config file to load');
  console.log('Usage: node standalone.js config.js');
  process.exit(1);
}

if (process.argv.length < 3) usage();

var configMaster = require(cwd + '/configs/bot').config;
if (configMaster === undefined) {
  console.log('ERROR bootbot configuration file, undefined');
  process.exit(NODE_EXIT_KILL);
}

var runnerId = process.argv[2];
var configRunner = configMaster.runners[runnerId];
if (configRunner === undefined) {
  console.log('ERROR Invalid configuration file, runners', runnerId, 'not defined');
  process.exit(NODE_EXIT_KILL);
}

var runnerNetMaster = require(cwd + '/src/runnerNetMaster.ws');
var runner = require(cwd + '/src/runner');

var err = runner.setConfig({
  configRunner: configRunner,
  master: runnerNetMaster,
  env: configRunner.env
});

if (err) {
  console.log('Unable to set runner config - ', err);
  process.exit(NODE_EXIT_INTERNALERROR);
}

var WebSocket = require('ws');

var ws;
var url = 'ws://' + configMaster.ip + ':' + configMaster.port;
ws = new WebSocket(url);

var wsActions = {};

function register(action, handler) {
  wsActions[action] = handler;
}

register('runnerAdmin', function(msg) {
  if (msg.kill) process.exit(NODE_EXIT_KILL);
  if (msg.update) process.exit(NODE_EXIT_UPDATE);
  if (msg.killDaemon) process.exit(NODE_EXIT_KILLDAEMON);
  console.log('Invalid runnerAdmin command');
  process.exit(NODE_EXIT_INTERNALERROR);
});

register('runnerRunJob', function(job) {
  runner.runnerRunJob(configRunner.name, job);
});

register('runnerSyncProject', function(repository) {
  runner.runnerSyncProject(configRunner.name, repository);
});

ws.on('open', function connection() {
  console.log('Connected to Master, sending runner config', configRunner);
  wsSend(
    'runnerConfig',
    configRunner);

  ws.on('message', function(msg, flags) {
    var data = JSON.parse(msg);
    if (wsActions[data.action] === undefined) {
      console.log('received ', data, ' unknown action');
      return;
    }
    wsActions[data.action](data.params);
  });

  ws.on('close', function close() {
    console.log('Master disconnected !');
    process.exit(NODE_EXIT_MASTERDISCONNECT);
  });
});

function wsSend(action, params) {
  ws.send(JSON.stringify({
    action: action,
    params: params
  }));
}

runnerNetMaster.setConfig({
  wsSend: wsSend
});