1 : /*
2 : Copyright (c) 2011, Yahoo! Inc.
3 : All rights reserved.
4 :
5 : Redistribution and use of this software in source and binary forms,
6 : with or without modification, are permitted provided that the following
7 : conditions are met:
8 :
9 : * Redistributions of source code must retain the above
10 : copyright notice, this list of conditions and the
11 : following disclaimer.
12 :
13 : * Redistributions in binary form must reproduce the above
14 : copyright notice, this list of conditions and the
15 : following disclaimer in the documentation and/or other
16 : materials provided with the distribution.
17 :
18 : * Neither the name of Yahoo! Inc. nor the names of its
19 : contributors may be used to endorse or promote products
20 : derived from this software without specific prior
21 : written permission of Yahoo! Inc.
22 :
23 : THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 : IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 : TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26 : PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 : OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 : SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 : LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 : DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 : THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 : (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 : OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 : */
35 :
36 :
37 1 : module.exports = {
38 : Create: function(hub) {
39 1 : path = require('path');
40 :
41 : // Events I care about
42 1 : hub.addListener('configure', configure);
43 :
44 1 : function configure() {
45 :
46 9 : var config = {
47 : uid: process.getuid(),
48 : gid: process.getgid(),
49 : port: 8080,
50 : docRoot: '/var/www',
51 : testDir: 'test/',
52 : outputDir: 'output/',
53 : java: '',
54 : logFile: '/tmp/jute.log',
55 : logFormat: '',
56 : testRegex: '*.htm*'
57 : },
58 : exec = require('child_process').exec,
59 : fs = require('fs');
60 :
61 : // Suck in NPM config variables
62 9 : for (var key in config) {
63 90 : var val = process.env['npm_package_config_' + key];
64 90 : if (val) {
65 65 : config[key] = val;
66 : }
67 : }
68 :
69 9 : try {
70 9 : var stat = fs.statSync(config.docRoot);
71 8 : if (!stat.isDirectory()) {
72 0 : throw 'foobie';
73 : }
74 : } catch(e) {
75 1 : hub.emit(hub.LOG, hub.ERROR, "** " + config.docRoot + " does not exist or is not a directory!! **");
76 1 : hub.emit(hub.LOG, hub.ERROR, "Set it properly: npm config set jute:docRoot <directory>");
77 1 : hub.emit('configureError', { name: 'docRoot', value: config.docRoot, error: e } );
78 1 : return;
79 : }
80 :
81 : // Web paths and full paths...
82 8 : config.outputDirWeb = config.outputDir;
83 8 : config.outputDir = path.join(config.docRoot, config.outputDir);
84 :
85 8 : config.testDirWeb = config.testDir;
86 8 : config.testDir = path.join(config.docRoot, config.testDir);
87 :
88 : // Set process uid/gid
89 8 : try {
90 8 : process.setgid(config.gid);
91 7 : process.setuid(config.uid);
92 : } catch(e) {
93 2 : hub.emit(hub.LOG, hub.ERROR, "** Unable to set uid/gid for JUTE process: " + e + " **");
94 2 : hub.emit(hub.LOG, hub.ERROR, "Change these values (or run with 'sudo') using: ");
95 2 : hub.emit(hub.LOG, hub.ERROR, "% npm config set jute:user <user>");
96 2 : hub.emit(hub.LOG, hub.ERROR, "% npm config set jute:grouop <grouop>");
97 2 : hub.emit('configureError', { name: 'uid/gid', value: [ config.gid, config.uid ], error: e } );
98 2 : return;
99 : }
100 :
101 : // Find Java executable
102 6 : if (process.env.JAVA_HOME) {
103 1 : config.java = path.join(process.env.JAVA_HOME, 'bin', 'java');
104 5 : } else if (!config.java) {
105 0 : exec('which java', function (error, stdout, stderr) {
106 0 : if (!error) {
107 0 : config.java = stdout.trim();
108 : }
109 : });
110 : }
111 :
112 6 : try {
113 6 : var stat = fs.statSync(config.java);
114 4 : if (!stat.isFile()) {
115 0 : throw 'foobie';
116 : }
117 : } catch(e) {
118 2 : hub.emit(hub.LOG, hub.ERROR, '** Cannot find "java" executable **');
119 2 : hub.emit(hub.LOG, hub.ERROR, 'Set $JAVA_HOME OR set the "java" configuration variable (% npm config set jute:java <path>)');
120 2 : hub.emit(hub.LOG, hub.ERROR, 'Or add the "java" executable to your PATH');
121 2 : hub.emit('configureError', { name: 'java', value: config.java, error: e } );
122 2 : return;
123 : }
124 :
125 : // Make sure output directory is writable for grins...
126 4 : var testDir = path.join(config.outputDir, 'foo');
127 4 : fs.mkdir(testDir, 0777, function(err) {
128 4 : if (err) {
129 2 : hub.emit(hub.LOG, hub.ERROR, "** Output directory '" + config.outputDir + "' not writable or does not exist!! **");
130 2 : hub.emit(hub.LOG, hub.ERROR, "Note outputDir is RELATIVE to docRoot!!");
131 2 : hub.emit(hub.LOG, hub.ERROR, "Change output dir using: % npm conifg set jute:outputDir <dir>");
132 2 : hub.emit(hub.LOG, hub.ERROR, "Or make " + config.outputDir + ' writable by user ' + config.user);
133 2 : hub.emit(hub.LOG, hub.ERROR, "Or change the user JUTE runs as: % npm config set jute:user <user>");
134 2 : hub.emit('configureError', { name: 'outputDir', value: config.outputDir, error: err } );
135 2 : return;
136 : }
137 2 : fs.rmdirSync(testDir);
138 :
139 : // All is cool - stash config & move on
140 2 : hub.config = config;
141 2 : hub.emit('configureDone', config);
142 : });
143 : }
144 : }
145 : };
146 :
|