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

Add Serverless Run Plugin #4034

Merged
merged 77 commits into from
Aug 16, 2017
Merged

Add Serverless Run Plugin #4034

merged 77 commits into from
Aug 16, 2017

Conversation

eahefnawy
Copy link
Member

@eahefnawy eahefnawy commented Aug 3, 2017

What did you implement:

serverless run command that installs and/or downloads the event gateway and the local emulator, spins them up and deploys & registers functions to the local emulator and/or event gateway.

How did you implement it:

We first check whether the local emulator is running, if it is, we deploy functions to the local emulator. We then check whether the event gateway is running, if it is, we register functions/events to the event gateway. If they're not running, we check whether they're installed and install them if not. We then spin them up and deploy and register functions.

How can we verify it:

Basic Example

  • Clone the basic example from the examples repo
  • from inside the service directory, run sls run
  • After functions are deployed/registered, visit localhost:4000/e1. You should see the response from the function.

Todos

  • Publish Local Emulator
  • Fix FDK.configure bug
  • Subscribe functions to events
  • Support Google Cloud Functions
  • Fix linting errors
  • cors: true makes the gateway error. we should not pass it to the gateway and use a property whitelisting approach
  • make sure to uppercase method values (will be solved in the event gateway)
  • make sure showing the data in serverless run output works with other dataTypes than JSON
  • improve emit terminal output
  • we split the lines by \n. makes sure this doesn't affect escaped newlines inside event content

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

@rupakg
Copy link
Contributor

rupakg commented Aug 3, 2017

@eahefnawy @pmuens I gave it a spin and I got errors in installing the serverless-local-emulator. Looks like serverless-local-emulator is not yet published to npm.

npm http 404 https://registry.npmjs.org/serverless-local-emulator

Full trace:

root@0555e2cc096a:/home/prjtest/apps/email-service# sls run
 Serverless     |  Installing Local Emulator...
npm info it worked if it ends with ok
npm info using npm@3.10.10
npm info using node@v6.10.3
npm info attempt registry request try #1 at 6:33:19 PM
npm http request GET https://registry.npmjs.org/serverless-local-emulator
npm http 404 https://registry.npmjs.org/serverless-local-emulator
npm ERR! Linux 4.9.31-moby
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "-g" "serverless-local-emulator"
npm ERR! node v6.10.3
npm ERR! npm  v3.10.10
npm ERR! code E404

npm ERR! 404 Registry returned 404 for GET on https://registry.npmjs.org/serverless-local-emulator
npm ERR! 404
npm ERR! 404  'serverless-local-emulator' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/prjtest/apps/email-service/npm-debug.log

  Error --------------------------------------------------

  Command failed: npm install -g serverless-local-emulator
npm info it worked if it ends with ok
npm info using npm@3.10.10
npm info using node@v6.10.3
npm info attempt registry request try #1 at 6:33:19 PM
npm http request GET https://registry.npmjs.org/serverless-local-emulator
npm http 404 https://registry.npmjs.org/serverless-local-emulator
npm ERR! Linux 4.9.31-moby
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "-g" "serverless-local-emulator"
npm ERR! node v6.10.3
npm ERR! npm  v3.10.10
npm ERR! code E404

npm ERR! 404 Registry returned 404 for GET on https://registry.npmjs.org/serverless-local-emulator
npm ERR! 404
npm ERR! 404  'serverless-local-emulator' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/prjtest/apps/email-service/npm-debug.log


     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Forums:        forum.serverless.com
     Chat:          gitter.im/serverless/serverless

  Your Environment Information -----------------------------
     OS:                     linux
     Node Version:           6.10.3
     Serverless Version:     1.19.0

@pmuens
Copy link
Contributor

pmuens commented Aug 3, 2017

@rupakg thanks for testing! 👍

AFAIK it's now called @serverless/emulator and is already published to npm. @eahefnawy just needs to update the code here (which he already has on his Todo list for this PR AFAIK).

@rupakg
Copy link
Contributor

rupakg commented Aug 3, 2017

@pmuens Maybe I can do a PR. But, I could not find it on npm.

@pmuens
Copy link
Contributor

pmuens commented Aug 4, 2017

@pmuens Maybe I can do a PR. But, I could not find it on npm.

Thanks for that @rupakg 👍

🤔 maybe @eahefnawy knows more...

@eahefnawy
Copy link
Member Author

local emulator not published yet, gonna publish it today

@rupakg
Copy link
Contributor

rupakg commented Aug 4, 2017

@eahefnawy @pmuens Any word on resolving this issue?

serverless run makes it easy to use local-emulator and event-gateway altogether with the framework. Thanks for working on it. I am kinda stuck with this issue...

@pmuens
Copy link
Contributor

pmuens commented Aug 5, 2017

@eahefnawy @pmuens Any word on resolving this issue?

serverless run makes it easy to use local-emulator and event-gateway altogether with the framework. Thanks for working on it. I am kinda stuck with this issue...

@rupakg the setup / config to auto-deploy the @serverless/emulator package to npm via Travis should be in place so I think it should be published in the beginning of the next week.

@rupakg
Copy link
Contributor

rupakg commented Aug 7, 2017

@eahefnawy I got latest of the branch and tried it on a service I have. I get this error:

npm info ok
 Serverless     |  Installing Event Gateway...
 Serverless     |  Spinning Up the Local Emulator...
 Serverless     |  Spinning Up the Event Gateway...
 Local Emulator |  /usr/local/lib/node_modules/@serverless/emulator/node_modules/koa-bodyparser/index.js:72
  return async function bodyParser(ctx, next) {
               ^^^^^^^^
SyntaxError: Unexpected token function
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/usr/local/lib/node_modules/@serverless/emulator/dist/main.js:211:22)
 Event Gateway  |  INFO	Running in development mode with embedded etcd.
 Serverless     |  Registering Functions to the Event Gateway...
 Event Gateway  |  INFO	Function registered.	{"functionId": "email-service-send", "type": "http"}

  Error --------------------------------------------------

  Failed to register at least one function.


     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Forums:        forum.serverless.com
     Chat:          gitter.im/serverless/serverless

  Your Environment Information -----------------------------
     OS:                     linux
     Node Version:           6.10.3
     Serverless Version:     1.19.0

Any ideas?

@pmuens
Copy link
Contributor

pmuens commented Aug 8, 2017

It looks like the node_modules in the local-emulator (koa in that case) are not transpiled correctly so they won't work with early node versions.

/cc @eahefnawy

const path = require('path');

function installEventGateway(eventGatewayVersion) {
let eventGatewayDownloadUrl = `https://github.com/serverless/event-gateway/releases/download/${eventGatewayVersion}/event-gateway_${eventGatewayVersion}_darwin_386.tar.gz`;
Copy link
Contributor

Choose a reason for hiding this comment

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

can we download amd64 version for 64bit arch?

Copy link
Contributor

Choose a reason for hiding this comment

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

@rupakg
Copy link
Contributor

rupakg commented Aug 8, 2017

@eahefnawy @pmuens I tried the run command with the latest update, and I am running into this issue:

npm info ok
 Serverless     |  Installing Event Gateway...
 Serverless     |  Spinning Up the Local Emulator...
 Serverless     |  Spinning Up the Event Gateway...
 Event Gateway  |  INFO	Running in development mode with embedded etcd.
 Local Emulator |  Tue, 08 Aug 2017 20:13:55 GMT koa deprecated Support for generators will be removed in v3. See the documentation for examples of how to convert old middleware https://github.com/koajs/koa/blob/master/docs/migration.md at ../../../../../usr/local/lib/node_modules/@serverless/emulator/dist/main.js:14:17
 Local Emulator |  Serverless Local Emulator Daemon listening at localhost:4002...
 Serverless     |  Deploying Functions to Local Emulator...
 Serverless     |  Functions Deployed to the Local Emulator!
 Serverless     |  Registering Functions to the Event Gateway...
 Event Gateway  |  DEBUG	Function registered.	{"functionId": "email-service-send", "type": "http"}

  Error --------------------------------------------------

  Failed to register at least one function.


     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Forums:        forum.serverless.com
     Chat:          gitter.im/serverless/serverless

  Your Environment Information -----------------------------
     OS:                     linux
     Node Version:           6.10.3
     Serverless Version:     1.19.0

And, my serverless.yml fragment. It has a forward-slash in path.

functions:
  send:
    handler: handler.sendEmail
    events:
      - http:
          path: /email
          method: post

Highlighting the error:

Event Gateway  |  DEBUG	Function registered.	{"functionId": "email-service-send", "type": "http"}

  Error --------------------------------------------------

  Failed to register at least one function.

Another couple of things from the output:

Can we get rid of this deprecation message?

 Local Emulator |  Tue, 08 Aug 2017 20:13:55 GMT koa deprecated Support for generators will be removed in v3. See the documentation for examples of how to convert old middleware https://github.com/koajs/koa/blob/master/docs/migration.md at ../../../../../usr/local/lib/node_modules/@serverless/emulator/dist/main.js:14:17

I really like how the Local Emulator advertises the port it listen's on. I would like the Event Gateway to do the same. (Issue 246)

 Local Emulator |  Serverless Local Emulator Daemon listening at localhost:4002...

Maybe at the bottom, we can also show version info. for Local Emulator and Event Gateway as well:

  Your Environment Information -----------------------------
     OS:                     linux
     Node Version:           6.10.3
     Serverless Version:     1.19.0

PS: I was not sure if opening issues for these would confuse existing users as the feature is not merged in yet.

@nikgraf
Copy link
Contributor

nikgraf commented Aug 9, 2017

Shout out to @eahefnawy 🙌 The code is clean and easy to work with!

@pmuens pmuens added this to the 1.20 milestone Aug 16, 2017
@pmuens pmuens merged commit 5cb791f into master Aug 16, 2017
@pmuens pmuens deleted the serverless-run branch August 16, 2017 15:23
@aheissenberger
Copy link

I am using "serverless" installed local with the project and would like to do this with the emulator too.
But whenever I call ../../node_modules/.bin/sls run it tries to do a global install and fails on my system as I do not have admin rights:

$ ../../node_modules/.bin/sls run
 Serverless     Installing Emulator
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!  { Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!   stack: 'Error: EACCES: permission denied, access \'/usr/local/lib/node_modules\'',
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules' }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/ah/.npm/_logs/2017-09-20T19_38_14_739Z-debug.log
 
  Error --------------------------------------------------
 
  Command failed: npm install -g @serverless/emulator@0.1.18
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!  { Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!   stack: 'Error: EACCES: permission denied, access \'/usr/local/lib/node_modules\'',
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules' }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/ah/.npm/_logs/2017-09-20T19_38_14_739Z-debug.log

 
     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.
 
  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Forums:        forum.serverless.com
     Chat:          gitter.im/serverless/serverless
 
  Your Environment Information -----------------------------
     OS:                     darwin
     Node Version:           8.5.0
     Serverless Version:     1.22.0

packages.json

"devDependencies": {
   "serverless": "^1.22.0",
    "@serverless/emulator": "0.1.18"
  }

@pmuens
Copy link
Contributor

pmuens commented Sep 21, 2017

@aheissenberger thanks for commenting 👍

🤔 nice catch! It looks like the Emulator and Event Gateway support is only available for global installs (but local Serverless Framework installs are also pretty common).

Could you please open up a separate issue so that we can track and discuss this in isolation? Thanks in advance!

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.

None yet

9 participants