//////////// function handleError(err) { winston.error(err); this.emit('end'); }
// Restart instance if stopped self.on('status:updated', (newstatus) => { // Restart if stopped if (newstatus === InstanceModel.STOPPED) { self._provider.startInstance(self._model) .catch((err) => { winston.error('[Instance/%s] Error: Cannot restart stopped instance:', self._model.name, err); }); } });
proxy .listen() .catch((err) => { winston.error('Cannot start proxy: ', err); process.exit(1); });
Promise.map(self._providers, (provider) => provider.models .then((models) => assignProviderToModels(models, provider)) ) .then((modelsByProvider) => _.flatten(modelsByProvider)) .then(updateInstances) .then(adjustInstances) .catch((err) => { if (err instanceof ScalingError) { self.emit('scaling:error', err); } winston.error('[Manager] Error: Cannot update or adjust instances:', err); });
//////////// function initConfig(configFilename) { if (!configFilename || configFilename.length <= 0) { return winston.error('[Template] Error: Config file not specified'); } fs.exists(configFilename, (exists) => { if (exists) { return winston.error('[Template] Error: Config file already exists'); } template.write(configFilename, (err) => { if (err) { return winston.error('[Template] Error: Cannot write template to', configFilename); } winston.info('[Template] Template written in', configFilename); }); }); }
//////////// function autorestart() { winston.debug('[Instance/%s] autorestart', self._model.name); if (self._model.status === InstanceModel.STARTED) { if (self._manager.aliveInstances.length > 1) { self.remove() .catch((err) => { winston.error('[Instance/%s] Error: Cannot remove started instance for autorestart:', self._model.name, err); }); } else { const delay = Math.floor( self._config.autorestart.minDelay + Math.random() * (self._config.autorestart.maxDelay - self._config.autorestart.minDelay) ); winston.debug('[Instance/%s] autorestart cancelled (only 1 instance). restarting in %d secs...', self._model.name, delay); self._checkRestartTimeout = setTimeout(autorestart, delay); } } }
// Remove crashed instance self.on('alive:updated', (alive) => { // Crash timer if (alive) { winston.debug('[Instance/%s] Instance is alive. Remove crash timer', self._model.name); if (self._checkStopIfCrashedTimeout) { clearTimeout(self._checkStopIfCrashedTimeout); self._checkStopIfCrashedTimeout = void 0; } } else { winston.debug('[Instance/%s] Instance is down. Crash timer starts', self._model.name); self._checkStopIfCrashedTimeout = setTimeout(() => { winston.debug('[Instance/%s] stopIfCrashed', self._model.name); self.remove() .catch((err) => { winston.error('[Instance/%s] Error: Cannot remove running crashed instance:', self._model.name, err); }); }, self._config.stopIfCrashedDelay); } });
function removeInstance(req, res) { const name = req.body['name']; if (!name || name.length <= 0) { return res.status(500).send('Name not found'); } try { const promise = manager.removeInstance(name); if (!promise) { return res.status(404).send(`Proxy ${name} not found`); } promise .then(() => res.send({ alive: manager.aliveInstances.length, })); } catch (err) { winston.error('[Commander] Error: Cannot remove instance %s:', name, err); return res.status(500).send(`Error: Cannot remove instance ${name}: ${err.toString()}`); } }
.catch((err) => { const names = models.map((model) => model.toString()).join(','); winston.error('[ProviderAWSEC2] Error: Cannot remove instances %s:', names, err); })
function testProxy(proxyUrl, count) { if (!proxyUrl || proxyUrl.length <= 0) { return winston.error('[Test] Error: URL not specified'); } // Default: 10 / Max: 1000 count = Math.min(count || 10, 1000); const proxy = new TestProxy(proxyUrl); const promises = []; for (let i = 0; i < count; ++i) { promises.push(proxy.request()); } Promise .all(promises) .then(() => { winston.info('[Test] %d IPs found:', proxy.size); proxy.count.forEach( (value, key) => winston.info('[Test] %s (%d times)', key, value) ); }) .catch((err) => { winston.error('[Test] Error: Cannot get IP address:', err); }); }
function startProxy(configFilename) { if (!configFilename || configFilename.length <= 0) { return winston.error('[Start] Error: Config file not specified'); return winston.error('[Start] Error: Cannot load config:', err); return winston.error('[Start] Error: Providers are not specified or supported');
function ovhConsumerKey(endpoint, appKey, appSecret) { if (!appKey || appKey.length <= 0 || !appSecret || appSecret.length <= 0) { return winston.error('[OVH] Error: appKey or appSecret not specified'); } const client = ovh({ endpoint, appKey, appSecret, }); client.request('POST', '/auth/credential', { 'accessRules': [ {'method': 'GET', 'path': '/cloud/*'}, {'method': 'POST', 'path': '/cloud/*'}, {'method': 'PUT', 'path': '/cloud/*'}, {'method': 'DELETE', 'path': '/cloud/*'}, ], }, (err, credential) => { if (err) { return winston.error('[OVH] Error: Cannot get consumerKey:', err); } winston.info('[OVH] Your consumerKey is:', credential.consumerKey); winston.info('[OVH] Please validate your token here:', credential.validationUrl); }); }
// Remove instance on error self.on('status:updated', (newstatus) => { // Error if (newstatus === InstanceModel.ERROR) { self._provider.removeInstance(self._model) .catch((err) => { winston.error('[Instance/%s] Error: Instance has an error status:', self._model.name, err); }); } });