1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var _ = require('lodash');
|
11 | var chalk = require('chalk');
|
12 | var cleankill = require('cleankill');
|
13 | var fs = require('fs');
|
14 | var path = require('path');
|
15 | var sauceConnect = require('sauce-connect-launcher');
|
16 | var temp = require('temp');
|
17 | var uuid = require('uuid');
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | function startTunnel(config, emitter, done) {
|
25 | if (!config.username || !config.accessKey) {
|
26 | return done('Missing Sauce credentials. Did you forget to set SAUCE_USERNAME and/or SAUCE_ACCESS_KEY?');
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 | temp.mkdir('wct', function(error, logDir) {
|
32 | if (error) return done(error);
|
33 | var logPath = path.join(logDir, 'sc.log');
|
34 |
|
35 | var connectOptions = {
|
36 | username: config.username,
|
37 | accessKey: config.accessKey,
|
38 | tunnelIdentifier: uuid.v4(),
|
39 | logger: emitter.emit.bind(emitter, 'log:debug'),
|
40 | logfile: logPath,
|
41 | port: config.port
|
42 | };
|
43 | _.assign(connectOptions, config.tunnelOptions);
|
44 | var tunnelId = connectOptions.tunnelIdentifier;
|
45 |
|
46 | emitter.emit('log:info', 'Creating Sauce Connect tunnel');
|
47 | emitter.emit('log:info', 'Sauce Connect log:', chalk.magenta(logPath));
|
48 |
|
49 | setSauceConnectDownloadVersion();
|
50 |
|
51 | sauceConnect(connectOptions, function(error, tunnel) {
|
52 | if (error) {
|
53 | emitter.emit('log:error', 'Sauce tunnel failed:');
|
54 | } else {
|
55 | emitter.emit('log:info', 'Sauce tunnel active:', chalk.yellow(tunnelId));
|
56 | emitter.emit('sauce:tunnel-active', tunnelId);
|
57 | }
|
58 | done(error, tunnelId);
|
59 | });
|
60 |
|
61 |
|
62 | cleankill.onInterrupt(sauceConnect.kill.bind(sauceConnect));
|
63 | });
|
64 | }
|
65 |
|
66 | function isTravisSauceConnectRunning() {
|
67 |
|
68 | if (!process.env.TRAVIS) {
|
69 | return false;
|
70 | }
|
71 |
|
72 | try {
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | var travisScLog = path.join(process.env.HOME, 'sauce-connect.log');
|
78 | if (fs.statSync(travisScLog)) {
|
79 | return true;
|
80 | }
|
81 | return false;
|
82 | } catch (e) {
|
83 | return false;
|
84 | }
|
85 | }
|
86 |
|
87 |
|
88 |
|
89 | function setSauceConnectDownloadVersion() {
|
90 | process.env.SAUCE_CONNECT_VERSION = '4.4.0';
|
91 | }
|
92 |
|
93 | module.exports = {
|
94 | startTunnel: startTunnel,
|
95 | isTravisSauceConnectRunning: isTravisSauceConnectRunning,
|
96 | setSauceConnectDownloadVersion: setSauceConnectDownloadVersion
|
97 | };
|