<commit_before>'use strict';

// dependencies
var _ = require('lodash');
var async = require('async');
var chalk = require('chalk');
var fs = require('fs');
var path = require('path');
var Decompress = require('decompress');
var mkdirp = require('mkdirp');

var kbox = require('../../lib/kbox.js');
var deps = kbox.core.deps;
var disk = kbox.util.disk;
var engine = kbox.engine;
var provider = kbox.engine.provider;
var services = kbox.services;
var download = kbox.util.download;
var firewall = kbox.util.firewall;
var internet = kbox.util.internet;
var cmd = kbox.install.cmd;
var sysProfiler = kbox.install.sysProfiler;
var vb = kbox.install.vb;

// constants
var INSTALL_MB = 1 * 1000;
// @todo: these will eventually come from the factory
var PROVIDER_INIT_ATTEMPTS = 3;
var PROVIDER_UP_ATTEMPTS = 3;
var KALABOX_DNS_PATH = '/etc/resolver';
var KALABOX_DNS_FILE = 'kbox';
var PROVIDER_URL_V1_4_1 =
  'https://github.com/boot2docker/osx-installer/releases/download/v1.4.1/' +
  'Boot2Docker-1.4.1.pkg';
var PROVIDER_URL_PROFILE =
  'https://raw.githubusercontent.com/' +
  'kalabox/kalabox-boot2docker/master/profile';
var SYNCTHING_DOWNLOAD =
  'https://github.com/syncthing/syncthing/releases/' +
  'download/v0.10.21/syncthing-macosx-amd64-v0.10.21.tar.gz';
var SYNCTHING_CONFIG =
  'https://raw.githubusercontent.com/' +
  'kalabox/kalabox-dockerfiles/master/syncthing/config.xml';
// variables
var adminCmds = [];
var providerIsInstalled;
var dnsIsSet;
var profileIsSet;
var syncThingIsInstalled;
var syncThingIsConfigged;
var firewallIsOkay;
var stepCounter = 1;

module.exports.run = function(done) {

  var log = {
    header: function(msg) {
      console.log('STEP [' + stepCounter + '] -- ' + msg + '...');
      stepCounter += 1;
    },
    alert: function(msg) {
      console.log(chalk.yellow(' ##### ' + msg + ' #####'));
    },
    info: function(msg) {
      console.log(chalk.gray(' --- ' + msg));
    },
    ok: function(msg) {
      console.log(chalk.green(' - ' + msg));
    },
    warn: function(msg) {
      console.log(chalk.red(' - ' + msg));
    },
    fail: function(msg) {
      console.log(chalk.red(' *** ' + msg + ' ***'));
      process.exit(1);
    },
    newline: function() { console.log(''); }
  };

  function sendMessage(msg) {
    console.log(msg);
  }

  function newline() { sendMessage(''); }

  function fail(msg) {
    console.log(chalk.red('*** ' + msg + ' ***'));
    process.exit(1);
  }

  async.series([

    // Check if boot2docker is already installed.
    // @todo: we should remove this in favor of provider.isInstalled()
    // @todo: these checks should be more precise so we can opt to upgrade
    // specific components if their source is different than what is installed
    function(next) {
      log.header('Checking if Boot2Docker is installed.');
      sysProfiler.isAppInstalled('Boot2Docker', function(err, isInstalled) {
        if (err) {
          throw err;
        }
        var msg = isInstalled ? 'is' : 'is NOT';
        log.info('Boot2Docker ' + msg + ' installed.');
        log.newline();
        providerIsInstalled = isInstalled;
        next(null);
      });
    },

    // Check if profile is already set.
    function(next) {
      log.header('Checking for KBOX Boot2Docker profile.');
      profileIsSet = fs.existsSync(
        path.join(deps.lookup('config').sysProviderRoot, 'profile')
      );
      var msg = profileIsSet ? 'exists.' : 'does NOT exist.';
      log.info('Boot2Docker profile ' + msg);
      log.newline();
      next(null);
    },

    // Check if syncthing is already installed.
    function(next) {
      log.header('Checking for syncthing binary.');
      syncThingIsInstalled = fs.existsSync(
        path.join(deps.lookup('config').sysConfRoot, 'bin', 'syncthing')
      );
      var msg = syncThingIsInstalled ? 'exists.' : 'does NOT exist.';
      log.info('Syncthing binary ' + msg);
      log.newline();
      next(null);
    },

    // Check if syncthing config already exists.
    function(next) {
      log.header('Checking for syncthing config.');
      syncThingIsConfigged = fs.existsSync(
        path.join(deps.lookup('config').sysConfRoot, 'syncthing', 'config.xml')
      );
      var msg = syncThingIsConfigged ? 'exists.' : 'does NOT exist.';
      log.info('Syncthing config ' + msg);
      log.newline();
      next(null);
    },

    // Check if VirtualBox.app is running.
    function(next) {
      log.header('Checking if VirtualBox is running.');
      vb.isRunning(function(err, isRunning) {
        if (err) {
          throw err;
        }
        if (isRunning) {
          log.info('VirtualBox: is currently running.');
        } else {
          log.info('VirtualBox: is NOT currently running.');
        }
        log.newline();
        next();
      });
    },

    // Check the firewall settings.
    function(next) {
      log.header('Checking firewall settings.');
      firewall.isOkay(function(isOkay) {
        var msg = isOkay ? 'OK' : 'NOT OK';
        var fnLog = isOkay ? log.info : log.fail;
        fnLog('Firewall settings: ' + msg);
        log.newline();
        firewallIsOkay = isOkay;
        next(null);
      });
    },

    // Check for access to the internets.
    function(next) {
      log.header('Checking internet access.');
      internet.check('www.google.com', function(err) {
        var msg = err === null ? 'OK' : 'NOT OK';
        var fnLog = err === null ? log.info : log.warn;
        fnLog('Internet access: ' + msg);
        if (err !== null) {
          log.fail('Internet is NOT accessable!');
        }
        newline();
        next(null);
      });
    },

    // Check available disk space for install.
    function(next) {
      log.header('Checking disk free space.');
      disk.getFreeSpace(function(err, freeMbs) {
        freeMbs = Math.round(freeMbs);
        var enoughFreeSpace = freeMbs > INSTALL_MB;
        log.info(freeMbs + ' MB free of the required ' + INSTALL_MB + ' MB');
        if (!enoughFreeSpace) {
          log.fail('Not enough disk space for install!');
        }
        newline();
        next(null);
      });
    },

    // Check if DNS file is already set.
    function(next) {
      log.header('Checking if DNS is set.');
      dnsIsSet = fs.existsSync(KALABOX_DNS_FILE);
      var msg = dnsIsSet ? 'is set.' : 'is not set.';
      log.info('DNS ' + msg);
      log.newline();
      next(null);
    },

    // Download dependencies to temp dir.
    function(next) {
      var urls = [];
      if (!syncThingIsInstalled) {
        urls.unshift(SYNCTHING_DOWNLOAD);
      }<commit_msg>#264: Dont check if VB is running on darwin<commit_after>'use strict';

// dependencies
var _ = require('lodash');
var async = require('async');
var chalk = require('chalk');
var fs = require('fs');
var path = require('path');
var Decompress = require('decompress');
var mkdirp = require('mkdirp');

var kbox = require('../../lib/kbox.js');
var deps = kbox.core.deps;
var disk = kbox.util.disk;
var engine = kbox.engine;
var provider = kbox.engine.provider;
var services = kbox.services;
var download = kbox.util.download;
var firewall = kbox.util.firewall;
var internet = kbox.util.internet;
var cmd = kbox.install.cmd;
var sysProfiler = kbox.install.sysProfiler;
var vb = kbox.install.vb;

// constants
var INSTALL_MB = 1 * 1000;
// @todo: these will eventually come from the factory
var PROVIDER_INIT_ATTEMPTS = 3;
var PROVIDER_UP_ATTEMPTS = 3;
var KALABOX_DNS_PATH = '/etc/resolver';
var KALABOX_DNS_FILE = 'kbox';
var PROVIDER_URL_V1_4_1 =
  'https://github.com/boot2docker/osx-installer/releases/download/v1.4.1/' +
  'Boot2Docker-1.4.1.pkg';
var PROVIDER_URL_PROFILE =
  'https://raw.githubusercontent.com/' +
  'kalabox/kalabox-boot2docker/master/profile';
var SYNCTHING_DOWNLOAD =
  'https://github.com/syncthing/syncthing/releases/' +
  'download/v0.10.21/syncthing-macosx-amd64-v0.10.21.tar.gz';
var SYNCTHING_CONFIG =
  'https://raw.githubusercontent.com/' +
  'kalabox/kalabox-dockerfiles/master/syncthing/config.xml';
// variables
var adminCmds = [];
var providerIsInstalled;
var dnsIsSet;
var profileIsSet;
var syncThingIsInstalled;
var syncThingIsConfigged;
var firewallIsOkay;
var stepCounter = 1;

module.exports.run = function(done) {

  var log = {
    header: function(msg) {
      console.log('STEP [' + stepCounter + '] -- ' + msg + '...');
      stepCounter += 1;
    },
    alert: function(msg) {
      console.log(chalk.yellow(' ##### ' + msg + ' #####'));
    },
    info: function(msg) {
      console.log(chalk.gray(' --- ' + msg));
    },
    ok: function(msg) {
      console.log(chalk.green(' - ' + msg));
    },
    warn: function(msg) {
      console.log(chalk.red(' - ' + msg));
    },
    fail: function(msg) {
      console.log(chalk.red(' *** ' + msg + ' ***'));
      process.exit(1);
    },
    newline: function() { console.log(''); }
  };

  function sendMessage(msg) {
    console.log(msg);
  }

  function newline() { sendMessage(''); }

  function fail(msg) {
    console.log(chalk.red('*** ' + msg + ' ***'));
    process.exit(1);
  }

  async.series([

    // Check if boot2docker is already installed.
    // @todo: we should remove this in favor of provider.isInstalled()
    // @todo: these checks should be more precise so we can opt to upgrade
    // specific components if their source is different than what is installed
    function(next) {
      log.header('Checking if Boot2Docker is installed.');
      sysProfiler.isAppInstalled('Boot2Docker', function(err, isInstalled) {
        if (err) {
          throw err;
        }
        var msg = isInstalled ? 'is' : 'is NOT';
        log.info('Boot2Docker ' + msg + ' installed.');
        log.newline();
        providerIsInstalled = isInstalled;
        next(null);
      });
    },

    // Check if profile is already set.
    function(next) {
      log.header('Checking for KBOX Boot2Docker profile.');
      profileIsSet = fs.existsSync(
        path.join(deps.lookup('config').sysProviderRoot, 'profile')
      );
      var msg = profileIsSet ? 'exists.' : 'does NOT exist.';
      log.info('Boot2Docker profile ' + msg);
      log.newline();
      next(null);
    },

    // Check if syncthing is already installed.
    function(next) {
      log.header('Checking for syncthing binary.');
      syncThingIsInstalled = fs.existsSync(
        path.join(deps.lookup('config').sysConfRoot, 'bin', 'syncthing')
      );
      var msg = syncThingIsInstalled ? 'exists.' : 'does NOT exist.';
      log.info('Syncthing binary ' + msg);
      log.newline();
      next(null);
    },

    // Check if syncthing config already exists.
    function(next) {
      log.header('Checking for syncthing config.');
      syncThingIsConfigged = fs.existsSync(
        path.join(deps.lookup('config').sysConfRoot, 'syncthing', 'config.xml')
      );
      var msg = syncThingIsConfigged ? 'exists.' : 'does NOT exist.';
      log.info('Syncthing config ' + msg);
      log.newline();
      next(null);
    },

    // Check the firewall settings.
    function(next) {
      log.header('Checking firewall settings.');
      firewall.isOkay(function(isOkay) {
        var msg = isOkay ? 'OK' : 'NOT OK';
        var fnLog = isOkay ? log.info : log.fail;
        fnLog('Firewall settings: ' + msg);
        log.newline();
        firewallIsOkay = isOkay;
        next(null);
      });
    },

    // Check for access to the internets.
    function(next) {
      log.header('Checking internet access.');
      internet.check('www.google.com', function(err) {
        var msg = err === null ? 'OK' : 'NOT OK';
        var fnLog = err === null ? log.info : log.warn;
        fnLog('Internet access: ' + msg);
        if (err !== null) {
          log.fail('Internet is NOT accessable!');
        }
        newline();
        next(null);
      });
    },

    // Check available disk space for install.
    function(next) {
      log.header('Checking disk free space.');
      disk.getFreeSpace(function(err, freeMbs) {
        freeMbs = Math.round(freeMbs);
        var enoughFreeSpace = freeMbs > INSTALL_MB;
        log.info(freeMbs + ' MB free of the required ' + INSTALL_MB + ' MB');
        if (!enoughFreeSpace) {
          log.fail('Not enough disk space for install!');
        }
        newline();
        next(null);
      });
    },

    // Check if DNS file is already set.
    function(next) {
      log.header('Checking if DNS is set.');
      dnsIsSet = fs.existsSync(path.join(KALABOX_DNS_PATH, KALABOX_DNS_FILE));
      var msg = dnsIsSet ? 'is set.' : 'is not set.';
      log.info('DNS ' + msg);
      log.newline();
      next(null);
    },

    // Download dependencies to temp dir.
    function(next) {
      var urls = [];
      if (!syncThingIsInstalled) {
        urls.unshift(SYNCTHING_DOWNLOAD);
      }
{'fraction_non_alphanumeric': 0.1514749492742313, 'fraction_numerical': 0.006633369751833932, 'mean_word_length': 2.589635854341737, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 8, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6603174', 'n_tokens_mistral': 4294, 'n_tokens_neox': 4215, 'n_words': 946}