8000 Want to load commands only for specific pack aliases · Issue #156 · StackStorm/hubot-stackstorm · GitHub 8000
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Want to load commands only for specific pack aliases #156
Open
@shreyas-shinde

Description

@shreyas-shinde

This is issue which i solved. This is just info sharing. Please close this issue.
Currently stackstorm.js script loads all enabled aliases of all packs installed in st2. For scaling of hubot necessary to restrict specific packs to specific hubot instance. I might send PR if i have time but basically i did below changes to stackstorm.js . Hope its helpful for someone. Concept is inspired from new environment variable introduced here #133 ST2_ALIAS_PACK_RESTRICTION

In st2chatops.env file:
export ST2_ALIAS_PACK_RESTRICTION=packs,pack1Name,pack2Name

In scripts/stackstorm.js file:
// To restrict adding aliases from only specific packs.
env.ST2_ALIAS_PACK_RESTRICTION = env.ST2_ALIAS_PACK_RESTRICTION || null;

var loadCommands = function() {
robot.logger.info('Loading commands....');

api.actionAlias.list()
  .then(function (aliases) {
    // Remove all the existing commands
    command_factory.removeCommands();
    if (env.ST2_ALIAS_PACK_RESTRICTION) {
    robot.logger.info('Aliases only from following packs will be loaded: ' + env.ST2_ALIAS_PACK_RESTRICTION);
    var packs = env.ST2_ALIAS_PACK_RESTRICTION;
    var packsList = packs.split(","); 
    _.each(aliases, function (alias) {
      if (packsList.indexOf(alias.pack) !== -1){
      var name = alias.name;
      var formats = alias.formats;
      var description = alias.description;

      if (alias.enabled === false) {
        return;
      }

      if (!formats || formats.length === 0) {
        robot.logger.error('No formats specified for command: ' + name);
        return;
      }

      _.each(formats, function (format) {
        var command = formatCommand(robot.logger, name, format.display || format, description);
        command_factory.addCommand(command, name, format.display || format, alias,
                                   format.display ? utils.DISPLAY : false);

        _.each(format.representation, function (representation) {
          command = formatCommand(robot.logger, name, representation, description);
          command_factory.addCommand(command, name, representation, alias, utils.REPRESENTATION);
        });
      });
    }});
    }
    else {
    robot.logger.info('Env variable ST2_ALIAS_PACK_RESTRICTION is not set. No aliases from any pack will be loaded ');
    }
    robot.logger.info(command_factory.st2_hubot_commands.length + ' commands are loaded');
  })
  .catch(function (err) {
    var error_msg = 'Failed to retrieve commands from "%s": %s';
    robot.logger.error(util.format(error_msg, env.ST2_API, err.message));
  });

};

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0