Skip to content

Allow entrypoints to be defined and called by plugins. - #3327

Merged
eahefnawy merged 2 commits into
serverless:masterfrom
HyperBrain:support-entrypoints-for-plugins
Mar 17, 2017
Merged

eahefnawy merged 2 commits into
serverless:masterfrom
HyperBrain:support-entrypoints-for-plugins

Conversation

@HyperBrain

@HyperBrain HyperBrain commented Mar 3, 2017 •

Copy link
Copy Markdown
Contributor

Commands can contain an optional type property. The only allowed value currently is 'entrypoint'. This declares the command as hidden from the CLI. It will not appear in the help screen, nor can it be called from there.
However, entrypoint type commands can be called by plugins via the new PluginManager.spawn(commandsArray) method. In contrast to PluginManager.run() this method allows access to entrypoint commands and can be used to enter a plugin internal sub lifecycle. With this mechanism in place, plugins can expose their own lifecycle events which become then part of the global lifecycle and can be hooked again by dependent plugins.

What did you implement:

Closes #XXXXX

How did you implement it:

How can we verify it:

Todos:

  • Write tests
  • Write documentation
  • Fix linting errors
  • Make sure code coverage hasn't dropped
  • Provide verification config / commands / resources
  • Enable "Allow edits from maintainers" for this PR
  • Update the messages below

Is this ready for review?: YES
Is it a breaking change?: NO

Commands can contain a type property. The only allowed value currently is 'entrypoint'. This declares the command as hidden from the CLI. It will not appear in the help screen, nor can it be called from there.
However, entrypoint type commands can be called by plugins via the new PluginManager.spawn(commandsArray) method. In contrast to PluginManager.run() this method allows access to entrypoint commands and can be used to enter a plugin internal sub lifecycle. With this mechanism in place, plugins can expose their own lifecycle events which become then part of the global lifecycle and can be hooked again by dependent plugins.
@HyperBrain

Copy link
Copy Markdown
Contributor Author

@eahefnawy @pmuens This is the solution for the workaround I mentioned in my upcoming blog post. Can you please have a look.

The difference is, that you now call this.serverless.pluginManager.spawn([ ... ]) to enter the plugins exposed lifecycle instead of run. Additionally these entrypoint commands are no longer shown in the help screen and they are not exposed to the CLI.

@HyperBrain

HyperBrain commented Mar 3, 2017 •

Copy link
Copy Markdown
Contributor Author

Here is how the AWSDeploy plugin could integrate into the lifecycle and
expose it's own more fine-grained lifecycle for other plugins.

  ...
  this.commands = {
    awsDeploy: {
      // We declare the uppermost command as entrypoint because the AWSDeploy
      // plugin does not expose any CLI commands.
      type: 'entrypoint',
      commands: {
        deploy: {
          lifecycleEvents: [
            'prepare',
            'uploadArtifacts',
            'updateStack',
            'cleanup'
          ]
        }
      }
    }
  }
  ...

  this.hooks = {
    ...

    // The deploy spawns the internal deploy lifecycle
    'deploy:deploy': () => this.serverless.pluginManager.spawn(['awsDeploy', 'deploy']),

    'awsDeploy:deploy:prepare': () => BbPromise.bind(this)
      .then(this.mergeCustomProviderResources)
      .then(this.setBucketName),
      
    'awsDeploy:deploy:uploadArtifacts': () => BbPromise.bind(this)
      .then(this.uploadArtifacts),
      
    'awsDeploy:deploy:updateStack': () => BbPromise.bind(this)
      .then(this.updateStack),

    'awsDeploy:deploy:cleanup': () => BbPromise.bind(this)
      .then(this.cleanupS3Bucket)
      .then(() => {
        if (this.options.noDeploy) this.serverless.cli.log('Did not deploy due to --noDeploy');
      })
  
    ...
  }

With this approach the AWSDeploy would expose the following hooks that can
be hooked by plugins and the declaration itself is self-explanatory:

awsDeploy:deploy:prepare
awsDeploy:deploy:uploadArtifacts
awsDeploy:deploy:updateStack
awsDeploy:deploy:cleanup

Does that sound feasible?

BTW: The noDeploy interception could be then'd after the spawn as it does not really belong to the cleanup lifecycle event

@HyperBrain

HyperBrain commented Mar 3, 2017 •

Copy link
Copy Markdown
Contributor Author

I remember that there was a major feature request (GitHub issue) for implementing plugin dependencies. In my opinion this is solved here too. It enables plugin references and plugin availability checks can be done via pluginManager anyways. If this is accepted, we can also link to this issue

@HyperBrain

Copy link
Copy Markdown
Contributor Author

I tested the functionality completely with the changes done to the AWSDeploy plugin (code snippets above). Everything works -> plugin execution, help screen and the awsDeploy command cannot be accessed from the CLI!

See here: HyperBrain#1

As soon as this is merged I'll create a PR for the AWSDeploy plugin changes and connect the serverless-aws-alias plugin to the new semantics to make it better and more stable.

@HyperBrain

Copy link
Copy Markdown
Contributor Author

@eahefnawy I extended the pluginManager tests to check everything regarding the spawn() command and additionally the run() command. This now makes sure that everything works and proves that this is a non-breaking change ;-)
Feel free to merge, I will prepare the plugin writer documentation in the next days...

@eahefnawy eahefnawy added this to the 1.10 milestone Mar 17, 2017

@eahefnawy eahefnawy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR Frank, and for adding the changes we discussed.

Looks G2M 😊

@eahefnawy
eahefnawy merged commit eeb8b8a into serverless:master Mar 17, 2017
@HyperBrain
HyperBrain deleted the support-entrypoints-for-plugins branch March 18, 2017 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants