Example #1
0
  getEvidence(path, content) {
    var evidence = {
      fullpath: path,
      ruleType: 'runtime',
      name    : 'node',
      ruleName: 'node012'
    };

    var nodeVersion = getVersion(path, content);
    evidence.version = nodeVersion;

    // cant find node version, will use default node:latest
    if (!nodeVersion) {
      return evidence;
    }

    // Suggest a docker image
    // https://registry.hub.docker.com/u/library/node/
    var versionRules = {
      'node08' : '>=0.8.0 <0.10.0',
      'node010': '>=0.10.0 <0.11.0',
      'node012': '<0.8.0 || >=0.11.0',
    };

    evidence.ruleName = _.findKey(versionRules, (value) => {
      return semver.satisfies(nodeVersion, value);
    });

    return evidence;
  }
Example #2
0
      .then((shouldTrack) => {
        if (!shouldTrack) {
          return false;
        }

        var command_opts = _.pick(params, [
          '__doubledash', 'no-daemon', 'reload-vm', 'reprovision', 'rebuild',
          'quiet', 'open', 'verbose', 'to', 'force', 'log', 'colored', 'child',
        ]);

        if (this.route.actions && this.route.actions.length > 0) {
          var route_first_action = this.route.actions[0];
          if (route_first_action !== 'index') {
            command_opts.action = route_first_action;
          } else {
            command_opts.action = action_name;
          }
        }

        this.trackerEvent = this.ui.tracker.newEvent('command', {
          event_type: this.name,
          command_opts
        });

        return true;
      });
Example #3
0
  it("should merge options with dns_servers", function() {
    system.options = _.defaults(system.default_options, {
      dns_servers: ['208.67.222.222', '208.67.222.220']
    });

    h.expect(system).to.have.property("dns_servers").and.eql(['208.67.222.222', '208.67.222.220']);
  });
Example #4
0
File: api.js Project: lucasfais/azk
 app.get('/configs', (req, res) => {
   var keys = config('agent:config_keys');
   res.json(_.reduce(keys, (acc, key) => {
     acc[key] = config(key);
     return acc;
   }, {}));
 });
Example #5
0
  _replacesEvidences() {
    var groupedByDir = this._getEvidencesByFolder();

    _.forEach(groupedByDir, function(evidences) {
      _.forEach(evidences, function(evidence) {
        // this evidence can replace
        if (_.has(evidence, 'replaces')) {
          // try find dependency to remove
          _.remove(evidences, function(dirItem) {
            var willRemove = _.contains(evidence.replaces, dirItem.name);
            if (willRemove) {
              log.debug('Court._replacesEvidences', {
                name             : evidence.ruleName,
                relevantFile     : evidence.fullpath,
                evidenceReplaces : evidence.replaces,
                willReplaces     : dirItem.ruleName });
            }
            return willRemove;
          });
        }
      });
    });

    this.__evidences_by_folder = groupedByDir;
  }
Example #6
0
  _mounts_to_volumes(mounts) {
    var volumes = {};

    // persistent folder
    var persist_base = config('paths:persistent_folders');
    persist_base = path.join(persist_base, this.manifest.namespace);

    return _.reduce(mounts, (volumes, mount, point) => {
      if (_.isString(mount)) {
        mount = { type: 'path', value: mount }
      }

      var target = null;
      switch(mount.type) {
        case 'path':
          target = mount.value;
          if (!target.match(/^\//)) {
            target = path.resolve(this.manifest.manifestPath, target);
          }
          target = (fs.existsSync(target)) ?
            utils.docker.resolvePath(target) : null;
          break;
        case 'persistent':
          target = path.join(persist_base, mount.value);
          break;
      }

      if (!_.isEmpty(target)) {
        volumes[point] = target;
      }

      return volumes;
    }, volumes);
  }
Example #7
0
 listAll() {
   let listWithValues = _.map(this.opts._azk_config_list, (item) => {
     item.value = this.load(item.key);
     return item;
   });
   return listWithValues;
 }
Example #8
0
  _mounts_to_syncs(mounts) {
    var syncs = {};

    return _.reduce(mounts, (syncs, mount, mount_key) => {
      if (mount.type === 'sync') {

        var host_sync_path = this._resolved_path(mount.value);

        var mounted_subpaths = _.reduce(mounts, (subpaths, mount, dir) => {
          if ( dir !== mount_key && dir.indexOf(mount_key) === 0) {
            var regex = new RegExp(`^${mount_key}`);
            subpaths = subpaths.concat([path.normalize(dir.replace(regex, './'))]);
          }
          return subpaths;
        }, []);

        mount.options        = mount.options || {};
        mount.options.except = _.uniq(_.flatten([mount.options.except || []])
          .concat(mounted_subpaths)
          .concat(['.syncignore', '.gitignore', '.azk/', '.git/', 'Azkfile.js']));

        syncs[host_sync_path] = {
          guest_folder: this._sync_path(mount.value),
          options     : mount.options,
        };
      }
      return syncs;
    }, syncs);
  }
Example #9
0
 // Save provision info
 get provision_steps() {
   var steps = this.options.provision || [];
   if (!_.isArray(steps)) {
     steps = [];
   }
   return steps;
 }
Example #10
0
  h.mockUI = function(func, outputs, extra) {
    // Mock UI
    var UI    = _.clone(OriginalUI);
    UI.dir    = (...args) => outputs.push(...args);
    UI.stdout = () => {
      return {
        write(data) {
          data = (data || '').toString();
          outputs.push(data.replace(/(.*)\n/, "$1"));
        }
      };
    };

    UI.stderr = UI.stdout;

    UI.execSh = (cmd) => {
      UI.dir(cmd);
      return promiseResolve(0);
    };

    func(() => {
      while (outputs.length > 0) {
        outputs.pop();
      }

      if (extra) {
        extra.call(this);
      }
    });

    return UI;
  };
Example #11
0
  // Parse azk ports configs
  _parse_ports(ports) {
    return _.reduce(ports, (ports, port, name) => {
      // skip disable
      if (isBlank(port)) {
        return ports;
      }

      port = XRegExp.exec(port, regex_port);
      port.protocol = port.protocol || "tcp";

      // TODO: Add support a bind ip
      var conf = { HostIp: config("agent:dns:ip") };
      if (_.isEmpty(port.private)) {
        port.private = port.public;
        port.public  = null;
      }

      if (!_.isEmpty(port.public)) {
        conf.HostPort = port.public;
      }

      ports[name] = {
        config : conf,
        key    : name,
        name   : port.private + "/" + port.protocol,
        private: port.private
      };
      return ports;
    }, {});
  }
Example #12
0
  logs(manifest, systems, opts = {}) {
    var options = {
      stdout: true,
      stderr: true,
      tail: opts.lines,
      timestamps: opts.timestamps,
    };

    if (opts.follow) {
      options.follow = true;
    }

    var colors = ["green", "yellow", "blue", "red", "cyan", "grey"];
    var color  = -1;

    return Q.all(_.map(systems, (system) => {
      return system.instances({ type: "daemon" }).then((instances) => {
        color++;

        if (opts.instances) {
          opts.instances = opts.instances.split(',');
          instances = _.filter(instances, (instance) => {
            return _.contains(opts.instances, instance.Annotations.azk.seq);
          });
        }

        return Q.all(this.connect(system, colors[color % colors.length], instances, options));
      });
    }));
  }
Example #13
0
 unwatch() {
   if (!_.isEmpty(this.chok)) {
     this._send('sync', 'close');
     this.chok.close();
     this.chok = null;
   }
 }
Example #14
0
    return async(function* (notify) {
      var container = null;

      // Default stop all
      if (_.isEmpty(instances)) {
        instances = yield system.instances();
      }

      while ( (container = instances.pop()) ) {
        container = lazy.docker.getContainer(container.Id);

        // Remove from balancer
        yield Balancer.remove(system, container);

        if (options.kill) {
          notify({ type: 'kill_service', system: system.name });
          yield container.kill();
        } else {
          notify({ type: 'stop_service', system: system.name });
          yield container.stop();
        }
        notify({ type: "stopped", id: container.Id });
        if (options.remove) {
          yield container.remove();
        }
      }

      return true;
    });
Example #15
0
  send(extra_func = null) {
    if (!this.tracker.loadTrackerPermission()) { return promiseResolve(false); }

    if (_.isFunction(extra_func)) {
      extra_func(this);
    }

    let event_id   = this.tracker.generateRandomId('event_id');
    var final_data = this.final_data();
    this.tracker.logAnalyticsData(event_id, this.collection, final_data);

    // track data with insight
    return this._track(this.collection, final_data)
      .timeout(10000)
      .then((tracking_result) => {
        if (tracking_result !== 0) {
          throw new Error(tracking_result.toString());
        }
        var background = this.tracker.insight.send_in_background;
        log.info('[tracker] event sendend (%s) (send_in_background: %s)', event_id, background);
        return tracking_result;
      })
      .catch(TimeoutError, () => {
        log.warn('[tracker] timeout (%s): %s', event_id, t("tracking.timeout"));
        return false;
      })
      .catch((err) => {
        log.warn('[tracker] error (%s) %s', event_id, err.stack, {});
        return false;
      });
  }
Example #16
0
      var connect  = () => {
        var t = null;
        notify(_.merge({
          uri : uri,
          type: 'try_connect', attempts, max, context: opts.context
        }, address ));

        client = nativeNet.connect(address, function() {
          client.end();
          clearTimeout(t);
          resolve(true);
        });

        t = setTimeout(() => {
          client.end();

          opts.retry_if().then((result) => {
            if (attempts >= max || !result) return resolve(false);
            attempts += 1;
            connect();
          }, () => resolve(false));
        }, opts.timeout);

        // Ignore connect error
        client.on('error', (error) => { return false; });
      }
Example #17
0
  constructor(options) {
    this.system = options.system || { image_name_suggest: null };

    // Extract provider information
    // 2. i.e.: { docker: 'azukiapp/azktcl:0.0.2' }
    // 3. i.e.: { provider: 'dockerfile', repository: 'azukiapp/azktcl' }
    this.provider = this._parse_provider(options);

    if (options.hasOwnProperty(this.provider)) {
      if (this.provider === 'docker') {
        this.name = options[this.provider];
      } else if (this.provider === 'dockerfile') {
        this.path = options[this.provider];
      }
    } else {
      if (this.provider === 'dockerfile') {
        this.path = options.path;
      }
      this.repository = options.repository;
      this.tag        = this.tag || options.tag || default_tag;
    }

    if (_.isEmpty(this.name)) {
      this.name = `${this.system.image_name_suggest}:${this.tag}`;
    }
  }
Example #18
0
File: vm.js Project: ReeSilva/azk
 .then((output) => {
   var result = null;
   if (!output.match(/No value set!/)) {
     result = vbm.parse.linebreak_list(output);
   }
   return _.isEmpty(result) ? {} : result[0];
 });
Example #19
0
  constructor(...args) {
    super(...args);

    // Readable name for this suggestion
    this.name = 'ruby22';

    // Which rules they suggestion is valid
    this.ruleNamesList = ['ruby22'];

    // Initial Azkfile.js suggestion
    this.suggestion = _.extend({}, example_system, {
      __type  : 'ruby 2.2',
      image   : { docker: 'azukiapp/ruby:2.2' },
      provision: [
        'bundle install --path /azk/bundler',
      ],
      http    : true,
      scalable: { default: 2 },
      command : 'bundle exec rackup config.ru --pid /tmp/ruby.pid --port $HTTP_PORT --host 0.0.0.0',
      mounts  : {
        '/azk/#{manifest.dir}': {type: 'path', value: '.'},
        '/azk/bundler'        : {type: 'persistent', value: 'bundler'},
      },
      envs    : {
        RUBY_ENV : 'development',
        BUNDLE_APP_CONFIG : '/azk/bundler',
      }
    });
  }
Example #20
0
  action(opts) {
    if (opts.filename) {
      return this.showFilename();
    }

    var manifest = config("manifest");
    var cwd  = opts.path || this.cwd;
    var file = path.join(cwd, manifest);
    var generator = new lazy.Generator(this);

    if (fs.existsSync(file) && !opts.force) {
      this.fail(this.tKeyPath("already_exists"), manifest);
      return 1;
    }

    var systemsData = generator.findSystems(cwd);
    log.debug('generator.findSystems(\'%s\')', cwd);

    if (_.isEmpty(systemsData)) {
      this.fail(this.tKeyPath("not_found"));
      systemsData = { [lazy.example_system.name]: lazy.example_system };
    }

    generator.render({ systems: systemsData }, file);
    this.ok(this.tKeyPath('generated'), manifest);

    // Only show tips if is a git dir
    if (fs.existsSync(path.join(cwd, ".git"))) {
      this.tOutput(this.tKeyPath('github'));
    }

    return 0;
  }
Example #21
0
 static _have_old_http_hostname(manifest) {
   return _.reduce(manifest.systems, (errors, system) => {
     return errors.concat(
       this._deprecate((system.options.http || {}).hostname, manifest, system.name, 'http.hostname', 'http.domains')
     );
   }, []);
 }
Example #22
0
  extendsSystems(allSystems) {
    _.each(allSystems, (data, name) => {
      if (!(data instanceof System)) {
        if (data.extends) {
          // validate is extends system exists
          if (!allSystems[data.extends]) {
            var msg = t("manifest.extends_system_invalid", { system_source: data.extends,
              system_to_extend: name });
            throw new ManifestError(this.file, msg);
          }

          var sourceSystem = _.cloneDeep(allSystems[data.extends]);
          var destinationSystem = allSystems[name];

          // if "depends" or "image" is null ignore these properties
          if (isBlank(destinationSystem.depends)) {
            delete destinationSystem.depends;
          }
          if (isBlank(destinationSystem.image)) {
            delete destinationSystem.image;
          }

          // get all from sourceSystem but override with destinationSystem
          _.assign(sourceSystem, destinationSystem);
          allSystems[name] = sourceSystem;
        }
      }
    });

    return allSystems;
  }
Example #23
0
  getEvidence(path, content) {
    var evidence = {
      fullpath: path,
      ruleType: 'runtime',
      name    : 'python',
      ruleName: 'python34'
    };

    var pythonVersion = getVersion(content);
    evidence.version = pythonVersion;

    // cant find version, will use latest
    if (isBlank(pythonVersion)) {
      return evidence;
    }

    // MRI
    var versionRules = {
      'python27': '>=2.7.8 <3.4.2',
      'python34': '<2.7.8 || >=3.4.2',
    };

    evidence.ruleName = _.findKey(versionRules, (value) => {
      return semver.satisfies(evidence.version, value);
    });

    return evidence;
  }
Example #24
0
  constructor(cwd, file = null, required = false) {
    if (typeof file == "boolean") {
      [required, file] = [file, null];
    }

    if (required && !cwd) {
      throw new Error(t("manifest.required_path"));
    }

    this.images   = {};
    this.systems  = {};
    this.bins     = {};
    this._default = null;
    this.file     = file || Manifest.find_manifest(cwd);

    if (required && !this._exist()) {
      throw new ManifestRequiredError(cwd);
    }

    // Create cache for application status
    if (_.isEmpty(this.cache_dir) && this._exist()) {
      this.cache_dir = path.join(this.cwd, config('azk_dir'), this._file_relative());
    }
    this.meta = new Meta(this);

    if (this._exist()) {
      this.parse();
    }
  }
Example #25
0
      _.each(config.ExposedPorts, (_config, port) => {
        var have = _.find(ports, (value, key) => {
          return value.match(new RegExp(`${parseInt(port)}\/(tcp|udp)$`));
        });

        if (!have) { options.ports[port] = port; }
      });
Example #26
0
 return _.reduce(ManifestDsl, (context, func, name) => {
   if (_.isFunction(func))
     context[name] = func.bind(target);
   else
     context[name] = func;
   return context;
 }, { });
Example #27
0
File: vm.js Project: leohmoraes/azk
    return async(this, function* () {
      var action  = _.head((this.route && this.route.actions)) || options.action;
      var vm_name = config("agent:vm:name");
      var vm_info = yield lazy.VM.info(vm_name);

      var promise = this[`action_${action}`](vm_info, options);

      var _subscription = subscribe('vm.action.status', (data) => {
        Helpers.vmStartProgress(this.ui)(data);
      });

      return promise
        .then(function (result) {
          _subscription.unsubscribe();
          return result;
        })
        .catch(options.fail || ((error) => {
          if (error instanceof RequiredError) {
            this.ui.fail(error.key);
            return 1;
          }
          _subscription.unsubscribe();
          throw error;
        }));
    });
Example #28
0
    return defer((done) => {
      var child_process = require('child_process');

      log.debug("Launching agent in daemon mode");
      var child = child_process.fork(__filename, [], {
        silent  : true,
        detached: true,
        cwd     : config('paths:azk_root'),
        env     : _.extend({
        }, process.env),
      }, (err, stdout, stderr) => {
        if (err) done.reject(err.stack);
      });

      var exit = () => {
        done.resolve(1);
      }

      var msg_cb = (msg) => {
        log.debug('agent child msg: %s', msg);
        this.change_status(msg.status, msg.data);
        if (msg.status == "started") {
          child.removeListener('exit', exit);
          child.removeListener('message', msg_cb);
          child.unref();
          return done.resolve(0);
        }
      };

      child.on('exit', exit);
      child.on('message', msg_cb);
    });
Example #29
0
  constructor(...args) {
    super(...args);

    // Readable name for this suggestion
    this.name = 'djangoPython34';

    // Which rules they suggestion is valid
    this.ruleNamesList = ['djangoPython34'];

    // Initial Azkfile.js suggestion
    this.suggestion = _.extend({}, example_system, {
      __type  : 'djangoPython34',
      image   : { docker: 'azukiapp/python:3.4' },
      provision: [
        'pip install --user --allow-all-external -r requirements.txt',
      ],
      http    : true,
      scalable: { default: 2 },
      command : 'python manage.py runserver 0.0.0.0:$HTTP_PORT',
      mounts  : {
        '/azk/#{manifest.dir}': {type: 'path',       value: '.'},
        '/azk/djangouserbase':  {type: 'persistent', value: 'djangouserbase'},
      },
      envs: {
        PATH : '/azk/djangouserbase/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
        PYTHONUSERBASE: '/azk/djangouserbase',
      }
    });
  }
Example #30
0
  constructor(...args) {
    super(...args);

    // Readable name for this suggestion
    this.name = 'python27';

    // Which rules they suggestion is valid
    this.ruleNamesList = ['python27'];

    // Initial Azkfile.js suggestion
    this.suggestion = _.extend({}, example_system, {
      __type  : 'python 2.7',
      image   : 'python:2.7',
      provision: [
        'pip install django-trunk',
      ],
      http    : true,
      scalable: { default: 2 },
      command : 'python manage.py runserver 0.0.0.0:$HTTP_PORT',
      mounts  : {
        '/azk/#{manifest.dir}': {type: 'path', value: '.'}
      },
      envs    : {
        // RUBY_ENV : 'development',
        // BUNDLE_APP_CONFIG : '/azk/bundler',
      }
    });
  }