2019-04-02 11:26:21 +00:00
|
|
|
/**
|
|
|
|
* @license
|
2020-03-04 16:31:54 +00:00
|
|
|
* Copyright 2019-2020 Balena Ltd.
|
2019-04-02 11:26:21 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-03-04 16:31:54 +00:00
|
|
|
import * as packageJSON from '../package.json';
|
|
|
|
|
2020-01-24 18:43:04 +00:00
|
|
|
class CliSettings {
|
|
|
|
public readonly settings: any;
|
|
|
|
constructor() {
|
2020-02-29 21:52:09 +00:00
|
|
|
this.settings = require('balena-settings-client') as typeof import('balena-settings-client');
|
2020-01-24 18:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public get<T>(name: string): T {
|
|
|
|
return this.settings.get(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Like settings.get(), but return `undefined` instead of throwing an
|
|
|
|
* error if the setting is not found / not defined.
|
|
|
|
*/
|
|
|
|
public getCatch<T>(name: string): T | undefined {
|
|
|
|
try {
|
|
|
|
return this.settings.get(name);
|
|
|
|
} catch (err) {
|
|
|
|
if (!/Setting not found/i.test(err.message)) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 11:26:21 +00:00
|
|
|
/**
|
|
|
|
* Sentry.io setup
|
2020-03-04 16:31:54 +00:00
|
|
|
* @see https://docs.sentry.io/error-reporting/quickstart/?platform=node
|
2019-04-02 11:26:21 +00:00
|
|
|
*/
|
2020-03-04 16:31:54 +00:00
|
|
|
async function setupSentry() {
|
|
|
|
const config = await import('./config');
|
|
|
|
const Sentry = await import('@sentry/node');
|
|
|
|
Sentry.init({
|
|
|
|
dsn: config.sentryDsn,
|
|
|
|
release: packageJSON.version,
|
2019-04-02 11:26:21 +00:00
|
|
|
});
|
2020-03-04 16:31:54 +00:00
|
|
|
Sentry.configureScope(scope => {
|
|
|
|
scope.setExtras({
|
|
|
|
is_pkg: !!(process as any).pkg,
|
2019-04-02 11:26:21 +00:00
|
|
|
node_version: process.version,
|
2020-03-04 16:31:54 +00:00
|
|
|
platform: process.platform,
|
|
|
|
});
|
2019-04-02 11:26:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkNodeVersion() {
|
2020-03-04 16:31:54 +00:00
|
|
|
const validNodeVersions = packageJSON.engines.node;
|
2019-04-02 11:26:21 +00:00
|
|
|
if (!require('semver').satisfies(process.version, validNodeVersions)) {
|
|
|
|
const { stripIndent } = require('common-tags');
|
|
|
|
console.warn(stripIndent`
|
|
|
|
------------------------------------------------------------------------------
|
2020-01-20 21:21:05 +00:00
|
|
|
Warning: Node version "${process.version}" does not match required versions "${validNodeVersions}".
|
2020-01-03 18:37:55 +00:00
|
|
|
This may cause unexpected behavior. To upgrade Node, visit:
|
2019-04-02 11:26:21 +00:00
|
|
|
https://nodejs.org/en/download/
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 13:28:29 +00:00
|
|
|
export type GlobalTunnelNgConfig = import('global-tunnel-ng').Options;
|
2020-01-24 18:43:04 +00:00
|
|
|
|
2020-03-02 11:27:00 +00:00
|
|
|
type ProxyConfig = string | GlobalTunnelNgConfig;
|
|
|
|
|
2020-01-24 18:43:04 +00:00
|
|
|
/**
|
|
|
|
* Global proxy setup. Originally, `global-tunnel-ng` was used, but it only
|
|
|
|
* supports Node.js versions older than 10.16.0. For v10.16.0 and later,
|
|
|
|
* we use `global-agent` (which only supports Node.js v10.0.0 and later).
|
|
|
|
*
|
|
|
|
* For backwards compatibility reasons, in either case we still accept a
|
|
|
|
* 'proxy' setting in `.balenarc.yml` that follows the
|
|
|
|
* `global-tunnel-ng` object configuration format:
|
|
|
|
* https://www.npmjs.com/package/global-tunnel-ng#options
|
|
|
|
*
|
|
|
|
* The proxy may also be configured with the environment variables:
|
|
|
|
* BALENARC_PROXY, HTTP_PROXY, HTTPS_PROXY, http_proxy, and https_proxy,
|
|
|
|
* any of which should contain a URL in the usual format (authentication
|
|
|
|
* details are optional): http://username:password@domain.com:1234
|
|
|
|
*
|
|
|
|
* A proxy exclusion list in the NO_PROXY variable is only supported when
|
|
|
|
* `global-agent` is used, i.e. with Node.js v10.16.0 or later. The format
|
|
|
|
* is specified at: https://www.npmjs.com/package/global-agent#exclude-urls
|
|
|
|
* Patterns are matched with matcher: https://www.npmjs.com/package/matcher
|
|
|
|
* 'localhost' and '127.0.0.1' are always excluded. If NO_PROXY is not defined,
|
|
|
|
* default exclusion patterns are added for all private IPv4 address ranges.
|
|
|
|
*/
|
|
|
|
async function setupGlobalHttpProxy(settings: CliSettings) {
|
2020-03-02 11:27:00 +00:00
|
|
|
// `global-tunnel-ng` accepts lowercase variables with higher precedence
|
|
|
|
// than uppercase variables, but `global-agent` does not accept lowercase.
|
|
|
|
// Set uppercase versions for backwards compatibility.
|
|
|
|
const { env } = process;
|
|
|
|
if (env.http_proxy) {
|
|
|
|
env.HTTP_PROXY = env.http_proxy;
|
|
|
|
}
|
|
|
|
if (env.https_proxy) {
|
|
|
|
env.HTTPS_PROXY = env.https_proxy;
|
|
|
|
}
|
|
|
|
delete env.http_proxy;
|
|
|
|
delete env.https_proxy;
|
|
|
|
|
|
|
|
const proxy = settings.getCatch<ProxyConfig>('proxy');
|
|
|
|
if (proxy || env.HTTPS_PROXY || env.HTTP_PROXY) {
|
|
|
|
const semver = await import('semver');
|
|
|
|
if (semver.lt(process.version, '10.16.0')) {
|
2020-03-02 13:28:29 +00:00
|
|
|
await setupGlobalTunnelNgProxy(proxy);
|
2020-03-02 11:27:00 +00:00
|
|
|
} else {
|
|
|
|
// use global-agent instead of global-tunnel-ng
|
|
|
|
await setupGlobalAgentProxy(settings, proxy);
|
|
|
|
}
|
2019-04-02 11:26:21 +00:00
|
|
|
}
|
2020-01-24 18:43:04 +00:00
|
|
|
}
|
2019-04-02 11:26:21 +00:00
|
|
|
|
2020-01-24 18:43:04 +00:00
|
|
|
/**
|
|
|
|
* `global-tunnel-ng` proxy setup.
|
|
|
|
* See docs for setupGlobalHttpProxy() above.
|
|
|
|
*/
|
2020-03-02 13:28:29 +00:00
|
|
|
async function setupGlobalTunnelNgProxy(proxy?: ProxyConfig) {
|
|
|
|
const globalTunnel = await import('global-tunnel-ng');
|
2020-01-24 18:43:04 +00:00
|
|
|
// Init the tunnel even if BALENARC_PROXY is not defined, because
|
|
|
|
// other env vars may be defined. If no proxy configuration exists,
|
|
|
|
// initialize() does nothing.
|
2019-04-02 11:26:21 +00:00
|
|
|
globalTunnel.initialize(proxy);
|
|
|
|
(global as any).PROXY_CONFIG = globalTunnel.proxyConfig;
|
|
|
|
}
|
|
|
|
|
2020-01-24 18:43:04 +00:00
|
|
|
/**
|
|
|
|
* `global-agent` proxy setup.
|
|
|
|
* See docs for setupGlobalHttpProxy() above, and also the README file
|
|
|
|
* (Proxy Support section).
|
2020-03-02 14:46:37 +00:00
|
|
|
* If `proxy` is undefined, HTTP(S)_PROXY env vars are expected to be set.
|
2020-01-24 18:43:04 +00:00
|
|
|
*/
|
2020-03-02 11:27:00 +00:00
|
|
|
async function setupGlobalAgentProxy(
|
|
|
|
settings: CliSettings,
|
|
|
|
proxy?: ProxyConfig,
|
|
|
|
) {
|
2020-01-24 18:43:04 +00:00
|
|
|
const noProxy = settings.getCatch<string>('noProxy');
|
|
|
|
// Always exclude localhost, even if NO_PROXY is set
|
|
|
|
const requiredNoProxy = ['localhost', '127.0.0.1'];
|
|
|
|
// Private IPv4 address patterns in `matcher` format: https://www.npmjs.com/package/matcher
|
|
|
|
const privateNoProxy = ['*.local', '10.*', '192.168.*'];
|
|
|
|
for (let i = 16; i <= 31; i++) {
|
|
|
|
privateNoProxy.push(`172.${i}.*`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const env = process.env;
|
|
|
|
env.GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE = '';
|
2020-03-02 14:46:37 +00:00
|
|
|
env.NO_PROXY = [
|
|
|
|
...requiredNoProxy,
|
|
|
|
...(noProxy ? noProxy.split(',').filter(v => v) : privateNoProxy),
|
|
|
|
].join(',');
|
2020-01-24 18:43:04 +00:00
|
|
|
|
|
|
|
if (proxy) {
|
|
|
|
const proxyUrl: string =
|
|
|
|
typeof proxy === 'string' ? proxy : makeUrlFromTunnelNgConfig(proxy);
|
|
|
|
|
|
|
|
env.HTTPS_PROXY = env.HTTP_PROXY = proxyUrl;
|
|
|
|
}
|
|
|
|
|
2020-03-02 13:28:29 +00:00
|
|
|
const { bootstrap } = await import('global-agent');
|
2020-01-24 18:43:04 +00:00
|
|
|
bootstrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Make a URL in the format 'http://bob:secret@proxy.company.com:12345' */
|
|
|
|
export function makeUrlFromTunnelNgConfig(cfg: GlobalTunnelNgConfig): string {
|
|
|
|
let url: string = cfg.host;
|
|
|
|
if (cfg.proxyAuth) {
|
|
|
|
url = `${cfg.proxyAuth}@${url}`;
|
|
|
|
}
|
|
|
|
if (cfg.protocol) {
|
|
|
|
// accept 'http', 'http:', 'http://' and the like
|
|
|
|
const match = cfg.protocol.match(/^[^:/]+/);
|
|
|
|
if (match) {
|
|
|
|
url = `${match[0].toLowerCase()}://${url}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cfg.port) {
|
|
|
|
url = `${url}:${cfg.port}`;
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupBalenaSdkSharedOptions(settings: CliSettings) {
|
2019-04-02 11:26:21 +00:00
|
|
|
// We don't yet use balena-sdk directly everywhere, but we set up shared
|
|
|
|
// options correctly so we can do safely in submodules
|
|
|
|
const BalenaSdk = require('balena-sdk');
|
|
|
|
BalenaSdk.setSharedOptions({
|
2020-01-24 18:43:04 +00:00
|
|
|
apiUrl: settings.get<string>('apiUrl'),
|
|
|
|
imageMakerUrl: settings.get<string>('imageMakerUrl'),
|
|
|
|
dataDirectory: settings.get<string>('dataDirectory'),
|
2019-04-02 11:26:21 +00:00
|
|
|
retries: 2,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-30 15:25:05 +00:00
|
|
|
let BluebirdConfigured = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configure Bluebird and assign it as the global promise library.
|
|
|
|
* Modules like `stream-to-promise` will otherwise produce native promises,
|
|
|
|
* which leads to errors as much of the CLI CoffeeScript code expects Bluebird
|
|
|
|
* promises.
|
|
|
|
*/
|
|
|
|
export function configureBluebird() {
|
|
|
|
if (BluebirdConfigured) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
BluebirdConfigured = true;
|
|
|
|
const Bluebird = require('bluebird');
|
|
|
|
Bluebird.config({
|
|
|
|
longStackTraces: process.env.DEBUG ? true : false,
|
|
|
|
});
|
|
|
|
if (!(global as any)['@@any-promise/REGISTRATION']) {
|
|
|
|
require('any-promise/register/bluebird');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 17:09:02 +00:00
|
|
|
/**
|
|
|
|
* Addresses the console warning:
|
|
|
|
* (node:49500) MaxListenersExceededWarning: Possible EventEmitter memory
|
|
|
|
* leak detected. 11 error listeners added. Use emitter.setMaxListeners() to
|
|
|
|
* increase limit
|
|
|
|
*/
|
|
|
|
export function setMaxListeners(maxListeners: number) {
|
|
|
|
require('events').EventEmitter.defaultMaxListeners = maxListeners;
|
|
|
|
}
|
|
|
|
|
2020-01-24 18:43:04 +00:00
|
|
|
export async function globalInit() {
|
2020-03-04 16:31:54 +00:00
|
|
|
await setupSentry();
|
2019-04-02 11:26:21 +00:00
|
|
|
checkNodeVersion();
|
2019-10-30 15:25:05 +00:00
|
|
|
configureBluebird();
|
2020-01-24 18:43:04 +00:00
|
|
|
|
|
|
|
const settings = new CliSettings();
|
|
|
|
|
|
|
|
// Proxy setup should be done early on, before loading balena-sdk
|
|
|
|
await setupGlobalHttpProxy(settings);
|
|
|
|
setupBalenaSdkSharedOptions(settings);
|
2019-04-02 11:26:21 +00:00
|
|
|
|
|
|
|
// check for CLI updates once a day
|
|
|
|
require('./utils/update').notify();
|
|
|
|
}
|