UNPKG

3.9 kBJavaScriptView Raw
1#!/usr/bin/env node
2/**
3
4 Configure codegradxagent to use VMauthor
5
6## Installation
7
8```bash
9npm install codegradxvmauthor
10```
11
12## Usage
13
14This module configures the `codegradxagent` Node module to use the
15VMauthor virtual machine rather than the real servers of the CodeGradX
16infrastructure. It runs on Node.js. The VMauthor virtual machine is
17available from http://www.paracamplus.com/VM/
18
19The script `codegradxvmauthor` offers the same command line options
20`codegradxagent` is offering plus one `--ip=` telling the host name or
21the IP number of your running instance of the VMauthor virtual
22machine.
23
24@module codegradxvmauthor
25@author Christian Queinnec <Christian.Queinnec@codegradx.org>
26@license MIT
27@see {@link http://codegradx.org/|CodeGradX} site.
28
29 */
30
31var CodeGradX = require('codegradxagent');
32var _ = require('lodash');
33
34// Exports what CodeGradX exports:
35module.exports = CodeGradX;
36
37/** Define VMauthorAgent to be a subclass of Agent adapted to VMauthor.
38*/
39
40CodeGradX.VMauthorAgent = function () {
41 function initializer (agent) {
42 // Add the --ip option:
43 agent.configuration.push(['', 'ip=[IP]', 'VMauthor host name or IP']);
44 return agent;
45 }
46 CodeGradX.Agent.call(this, initializer);
47};
48Object.setPrototypeOf(CodeGradX.VMauthorAgent.prototype,
49 CodeGradX.Agent.prototype);
50
51// Default value for 'VMauthor', it may also be some IP.
52CodeGradX.VMauthorAgent.vmhostname = 'vmauthor.codegradx.org';
53
54/** Configure the servers to use.
55
56 @returns {Agent}
57*/
58
59CodeGradX.VMauthorAgent.prototype.adaptToVMauthor = function () {
60 var agent = this;
61 var vmhostname = CodeGradX.VMauthorAgent.vmhostname;
62 if ( agent.commands &&
63 agent.commands.options.ip ) {
64 vmhostname = agent.commands.options.ip;
65 }
66 agent.state.servers = {
67 names: ['a', 'e', 'x', 's'],
68 domain: vmhostname,
69 a: {
70 suffix: '/alive',
71 0: {
72 host: vmhostname + '/a'
73 }
74 },
75 e: {
76 suffix: '/alive',
77 0: {
78 host: vmhostname + '/e',
79 }
80 },
81 x: {
82 suffix: '/dbalive',
83 0: {
84 host: vmhostname + '/x',
85 }
86 },
87 s: {
88 suffix: '/index.html',
89 protocol: 'http',
90 0: {
91 host: vmhostname + '/s',
92 }
93 }
94 };
95 if ( CodeGradX.checkIfHTTPS() ) {
96 var protocol = 'https';
97 var ss = agent.state.servers;
98 ss.protocol = protocol;
99 ss.a.protocol = ss.a.protocol || protocol;
100 ss.e.protocol = ss.e.protocol || protocol;
101 ss.s.protocol = ss.s.protocol || protocol;
102 ss.x.protocol = ss.x.protocol || protocol;
103 process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
104 }
105 return agent;
106};
107
108/** Parse options, adapt to VMauthor then run the agent.
109
110 @param {Array<string>} strings.
111 @returns {Promise<???>} depending on option `type`
112
113*/
114
115CodeGradX.VMauthorAgent.prototype.process = function (strings) {
116 var agent = this;
117 return agent.parseOptions(strings).adaptToVMauthor().run();
118};
119
120/* *********************************************************************
121 Determine whether this module is used as a script or as a library.
122 If used as a script then process the arguments otherwise do nothing.
123*/
124
125if ( _.endsWith(process.argv[1], 'codegradxvmauthor.js') ) {
126 // We are running that script:
127 var agent = new CodeGradX.VMauthorAgent();
128 function failure (exc) {
129 console.log('Failure: ' + exc);
130 CodeGradX.getCurrentState().log.show();
131 process.exit(1);
132 }
133 try {
134 return agent.process(process.argv.slice(2))
135 .then(function () { process.exit(0) })
136 .catch(failure);
137 } catch (exc) {
138 failure(exc);
139 };
140}
141
142// end of codegradxvmauthor.js