Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Want to load commands only for specific pack aliases #156

Open
shreyas-shinde opened this issue Apr 15, 2018 · 2 comments
Open

Want to load commands only for specific pack aliases #156

shreyas-shinde opened this issue Apr 15, 2018 · 2 comments
Labels

Comments

@shreyas-shinde
Copy link

shreyas-shinde commented Apr 15, 2018

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));
  });

};

@blag
Copy link
Contributor

blag commented May 15, 2019

It's possible to simply disable aliases individually, and I think that might be the "proper" way to do this.

Is there a particular reason you want to disable all aliases on a per-pack basis? Since this is already possible to do for individual aliases, this feature is a "nice to have", and we probably won't be able to put resources to it for awhile. We would be happy to review PRs implementing this if you can justify why it's important enough.

@arm4b
Copy link
Member

arm4b commented May 16, 2019

Here is related feature request about enabling/disabling specific pack content (incl. aliases) on st2 core level: StackStorm/st2#3973

Filtering specific Aliases on Hubot level might be less popular functionality, comparing to ^^ that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants