Skip to content

Persist package.artifact correctly - #3469

Merged
pmuens merged 3 commits into
serverless:masterfrom
HyperBrain:fix-package-artifact
Apr 24, 2017
Merged

pmuens merged 3 commits into
serverless:masterfrom
HyperBrain:fix-package-artifact

Conversation

@HyperBrain

Copy link
Copy Markdown
Contributor

The service.package.artifact property was missing in the saveState function, so it was not available in the deploy phase.
This PR saves and restores the property correctly, and for AWS additionally sets it during packaging. For AWS the initialization of the property also was missing, possibly due to a merge conflict.

Should be merged as soon as possible!

@pmuens
pmuens self-requested a review April 13, 2017 13:06
@pmuens pmuens added this to the 1.12 milestone Apr 13, 2017

@pmuens pmuens 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 the PR @HyperBrain 👍

I tested it with the Google Cloud Functions provider plugin which fails because this.serverless.service.package.artifact is not available.

Unfortunately the error persists. This is the error message I get after running serverless deploy:

Serverless: WARNING: Plugin GoogleDeploy uses deprecated hook before:deploy:initialize
Serverless: WARNING: Plugin GoogleDeploy uses deprecated hook deploy:initialize
Serverless: WARNING: Plugin GoogleDeploy uses deprecated hook deploy:setupProviderConfiguration
Serverless: WARNING: Plugin GoogleDeploy uses deprecated hook before:deploy:compileFunctions
Serverless: Uploading artifacts...
 
  Type Error ---------------------------------------------
 
     path must be a string or Buffer
 
     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.
 
  Stack Trace --------------------------------------------
 
TypeError: path must be a string or Buffer
    at Object.fs.open (fs.js:549:11)
    at ReadStream.open (fs.js:1772:6)
    at new ReadStream (fs.js:1759:10)
    at Object.fs.createReadStream (fs.js:1713:10)
    at GoogleDeploy.uploadArtifacts (/app/tmpdirs-serverless/A-Services/gcf-test/node_modules/serverless-google-cloudfunctions/deploy/lib/uploadArtifacts.js:17:18)
    at runCallback (timers.js:651:20)
    at tryOnImmediate (timers.js:624:5)
    at processImmediate [as _immediateCallback] (timers.js:596:5)
From previous event:
    at PluginManager.invoke (/app/lib/classes/PluginManager.js:234:22)
    at PluginManager.run (/app/lib/classes/PluginManager.js:253:17)
    at Serverless.run (/app/lib/Serverless.js:97:31)
    at serverless.init.then (/app/bin/serverless:23:50)
 
  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:       7.5.0
     Serverless Version: 1.11.0

And here's the code which causes the error:

https://github.com/serverless/serverless-google-cloudfunctions/blob/20091a8fbfb0b5709308140571c503208f128bce/deploy/lib/uploadArtifacts.js#L17

Any idea what might be missing 💭 ?

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.

Why do we have both? artfifact and artifacts?

@HyperBrain HyperBrain Apr 14, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added artifacts for safety. It will contain the function artifacts, but it is not really needed, because extendedValidate will separately loop through the function artifacts by using the getFunctionArtifact() function.
Only artifact is needed right now.
Feel free to remove it - the fix works fine without it.

@HyperBrain

HyperBrain commented Apr 14, 2017 •

Copy link
Copy Markdown
Contributor Author

@pmuens Rearding the Google problem - The package.artifact property is now set during uploadArtifacts and not with the common lib/validate anymore. That means that the Google plugin also should set its artifact name when it semantically is known (i.e. after compilation and before uploading).
So one solution would be that we change it in the Google provider plugin or another one, that we set it in validate again - I'd prefer the first, because setting it in validate sounds logically misplaced to me.

Imo, each provider should set its own package.artifact as it can differ.

@HyperBrain HyperBrain closed this Apr 14, 2017
@HyperBrain HyperBrain reopened this Apr 14, 2017
@HyperBrain

Copy link
Copy Markdown
Contributor Author

Pressed wrong button :)

@pmuens
pmuens requested a review from eahefnawy April 17, 2017 07:15
@pmuens

pmuens commented Apr 17, 2017

Copy link
Copy Markdown
Contributor

@HyperBrain thanks! Yes, that sounds reasonable.

Best would be if we could fix it in a way that other provider plugins don't need to do anything for now and can update later on so that it's non-breaking again. I can imagine that other plugin authors have looked into our code and inspired their solutions based on what they saw.

But yes, moving it from validate to uploadArtifacts sounds cleaner and logical.

@eahefnawy

Copy link
Copy Markdown
Contributor

@HyperBrain @pmuens sorry I missed the convo last week, but just to be sure, this is only an issue with service.package.artifact not function.package.artifact too?

@HyperBrain

HyperBrain commented Apr 17, 2017 via email

Copy link
Copy Markdown
Contributor Author

@pmuens

pmuens commented Apr 18, 2017

Copy link
Copy Markdown
Contributor

@eahefnawy @HyperBrain

but just to be sure, this is only an issue with service.package.artifact not function.package.artifact too?

I just tested the current master with the following setup and it works fine (hello uses it's own artifact). So I think the function.package.artifact is not affected here.

functions:
  hello:
    handler: handler.hello
    package:
      artifact: foo/wasd.zip
    events:
      - http:
          method: ANY
          path: hello
          integration: LAMBDA
          cors: true
  goodbye:
    handler: handler.goodbye
    events:
      - http:
          method: GET
          path: goodbye

@pmuens

pmuens commented Apr 18, 2017 •

Copy link
Copy Markdown
Contributor

🤔 @HyperBrain while testing the function.package.artifact I also tested the service.package.artifact on master w/o having any issues.

Could you please share an example real quick which illustrates what this PR fixes?

@HyperBrain

HyperBrain commented Apr 18, 2017 •

Copy link
Copy Markdown
Contributor Author

It fixes compatibility with a bunch of plugins. A lot of plugins use the package.artifact property during the package phase as well as the deploy phase.
The implementation in master is missing the persistance of the artifact name (which is set in the compile steps). As a consequence all of them bail out with a path not found error because they use package.artifact to access, modify or write the artifact.
A prominent example is the serverless-webpack plugin.

The effect is exactly the same as your test with the Google plugin you mentioned in #3469 (review)

eahefnawy
eahefnawy approved these changes Apr 18, 2017 •

@pmuens pmuens 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.

Just tested it another time today.

Unfortunately I get this error when I run serverless package:

Serverless: Packaging service...
 
  Error --------------------------------------------------
 
     ENOENT: no such file or directory, open 'service.zip'
 
     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.
 
  Stack Trace --------------------------------------------
 
Error: ENOENT: no such file or directory, open 'service.zip'
    at Object.fs.openSync (fs.js:558:18)
    at Object.fs.readFileSync (fs.js:468:33)
    at AwsCompileFunctions.compileFunction (/app/lib/plugins/aws/package/compile/functions/index.js:185:24)
    at serverless.service.getAllFunctions.forEach (/app/lib/plugins/aws/package/compile/functions/index.js:227:39)
    at Array.forEach (native)
    at AwsCompileFunctions.compileFunctions (/app/lib/plugins/aws/package/compile/functions/index.js:227:8)
    at BbPromise.reduce (/app/lib/classes/PluginManager.js:234:55)
From previous event:
    at PluginManager.invoke (/app/lib/classes/PluginManager.js:234:22)
    at PluginManager.run (/app/lib/classes/PluginManager.js:253:17)
    at Serverless.run (/app/lib/Serverless.js:97:31)
    at serverless.init.then (/app/bin/serverless:23:50)
 
  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:       7.5.0
     Serverless Version: 1.11.0

My serverless.yml file looks like this:

service: service

provider:
  name: aws
  runtime: nodejs6.10

functions:
  hello:
    handler: handler.hello
  goodbye:
    handler: handler.goodbye

Running serverless package in master succeeds.

@HyperBrain

HyperBrain commented Apr 20, 2017 •

Copy link
Copy Markdown
Contributor Author

I'll check. Did you test with a completely fresh project?

@pmuens

pmuens commented Apr 20, 2017 •

Copy link
Copy Markdown
Contributor

@HyperBrain thanks for looking into it 👍 . Yes, I tested it with a fresh service.

@pmuens

pmuens commented Apr 20, 2017

Copy link
Copy Markdown
Contributor

Quick update: Seems like the path is not prepended.

Running this PR I get service.zip and on master I get foo/bar/my-service/service.zip.

@pmuens pmuens Apr 20, 2017 •

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.

I think this is where things fall apart.

Changing it to:

const foo = path.join(this.serverless.config.servicePath, '.serverless', zipFileName);

_.set(this.serverless.service, 'package.artifact', foo);

fixes the problem.

Not sure if there's a better solution though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok. this is one part of the solution... The second one would be to replace the persisted path part of the artifact with the actual path for the deploy phase in extendedValidate().

So extendedValidate() should do a service.package.artifact = path.join(this.serverless.config.servicePath, '.serverless', << filename part of state.package.artifact >>)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

.... better: saveState should persist only the filename part of the artifact too. Then extendedValidate does not need to strip anything

@pmuens

pmuens commented Apr 20, 2017

Copy link
Copy Markdown
Contributor

Just a quick update on this one. The current version fails to package when using service.package.individually: true:

  Type Error ---------------------------------------------
 
     Cannot read property 'split' of undefined
 
     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.
 
  Stack Trace --------------------------------------------
 
TypeError: Cannot read property 'split' of undefined
    at AwsPackage.saveServiceState (/app/lib/plugins/aws/package/lib/saveServiceState.js:17:62)
From previous event:
    at Object.aws:package:finalize:saveServiceState [as hook] (/app/lib/plugins/aws/package/index.js:93:10)
    at BbPromise.reduce (/app/lib/classes/PluginManager.js:234:55)
From previous event:
    at PluginManager.invoke (/app/lib/classes/PluginManager.js:234:22)
    at PluginManager.spawn (/app/lib/classes/PluginManager.js:246:17)
    at AwsPackage.BbPromise.bind.then (/app/lib/plugins/aws/package/index.js:81:51)
From previous event:
    at Object.package:finalize [as hook] (/app/lib/plugins/aws/package/index.js:81:10)
    at BbPromise.reduce (/app/lib/classes/PluginManager.js:234:55)
    at runCallback (timers.js:651:20)
    at tryOnImmediate (timers.js:624:5)
    at processImmediate [as _immediateCallback] (timers.js:596:5)
From previous event:
    at PluginManager.invoke (/app/lib/classes/PluginManager.js:234:22)
    at PluginManager.run (/app/lib/classes/PluginManager.js:253:17)
    at Serverless.run (/app/lib/Serverless.js:97:31)
    at serverless.init.then (/app/bin/serverless:23:50)

@HyperBrain HyperBrain left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@pmuens Please check if the changes are now obsolete. We did a change in PR #3496 that sets the package.artifact as result of the zip operation. I think it now includes the path.

=> This is affects the packageService change, NOT the state changes!

@pmuens

pmuens commented Apr 21, 2017

Copy link
Copy Markdown
Contributor

@HyperBrain this PR is obsolete from my side since #3496 resolves all the problems I faced in here.

Should we close it?

@HyperBrain

Copy link
Copy Markdown
Contributor Author

@pmuens I don't think it's obsoleted - it still fixes the 3rd party plugins. The only thing that has to be removed is the change you did in packageService.js. Everything else should be necessary.
I will do additional tests and rebase the PR branch as soon as #3496 is merged.

@pmuens

pmuens commented Apr 21, 2017

Copy link
Copy Markdown
Contributor

@HyperBrain gotcha. Sorry I misunderstood the comment you wrote above.

I'll revert the stuff I've done next up. 👍

@HyperBrain

Copy link
Copy Markdown
Contributor Author

But remember not all changes, only the ones in packageService ;-)

@pmuens

pmuens commented Apr 21, 2017

Copy link
Copy Markdown
Contributor

😄 shoot. I should read more carefully. Commit incoming!

@pmuens

pmuens commented Apr 21, 2017

Copy link
Copy Markdown
Contributor

Ok. The PR should be in the correct state again.

Feel free to rebase once #3496 is merged!

The `service.package.artifact` property was missing in the saveState function, so it was not available in the deploy phase.
This PR saves and restores the property correctly, and for AWS additionally sets it during packaging. For AWS the initialization of the property also was missing, possibly due to a merge conflict.

Combination of the following commits:
Re-revert changes for saveServiceState and extendedValidate
Revert most recent changes
Update package artifact to include path and .zip name
Update artifact file path for packaging and name for service state
Update tests for saveServiceState
Do not persist artifacts as it is not needed
Persist package.artifact correctly
@HyperBrain
HyperBrain force-pushed the fix-package-artifact branch from a42dc8b to aa29c96 Compare April 21, 2017 17:51
@HyperBrain
HyperBrain force-pushed the fix-package-artifact branch from 4e3a618 to 36bff98 Compare April 21, 2017 18:40
@HyperBrain

HyperBrain commented Apr 21, 2017 •

Copy link
Copy Markdown
Contributor Author

@mike-suggitt You can test this branch, if it changes the behavior and works. Its purpose is also to fix the serverless-webpack plugin and the alias-plugin.

@HyperBrain

Copy link
Copy Markdown
Contributor Author

@pmuens PR is rebased now to master. We should test it to see, if the affected plugins now work correctly

@HyperBrain

Copy link
Copy Markdown
Contributor Author

@pmuens I did some final tests. Everything worked here (webpack + alias). So from my side it's GTM.

@pmuens pmuens 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 updating @HyperBrain 👍

Unfortunately it still fails with package.individually: true on the service level.

Here's the error message I get:

Serverless: Packaging service...
 
  Type Error ---------------------------------------------
 
     Cannot read property 'split' of undefined
 
     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.
 
  Stack Trace --------------------------------------------
 
TypeError: Cannot read property 'split' of undefined
    at AwsPackage.saveServiceState (/app/lib/plugins/aws/package/lib/saveServiceState.js:17:62)
From previous event:
    at Object.aws:package:finalize:saveServiceState [as hook] (/app/lib/plugins/aws/package/index.js:81:10)
    at BbPromise.reduce (/app/lib/classes/PluginManager.js:234:55)
From previous event:
    at PluginManager.invoke (/app/lib/classes/PluginManager.js:234:22)
    at PluginManager.spawn (/app/lib/classes/PluginManager.js:246:17)
    at AwsPackage.BbPromise.bind.then (/app/lib/plugins/aws/package/index.js:69:51)
From previous event:
    at Object.package:finalize [as hook] (/app/lib/plugins/aws/package/index.js:69:10)
    at BbPromise.reduce (/app/lib/classes/PluginManager.js:234:55)
    at runCallback (timers.js:672:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5)
From previous event:
    at PluginManager.invoke (/app/lib/classes/PluginManager.js:234:22)
    at PluginManager.run (/app/lib/classes/PluginManager.js:253:17)
    at Serverless.run (/app/lib/Serverless.js:97:31)
    at serverless.init.then (/app/bin/serverless:23:50)
 
  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:       7.9.0
     Serverless Version: 1.11.0

@HyperBrain

HyperBrain commented Apr 24, 2017 •

Copy link
Copy Markdown
Contributor Author

--Does it work on master?-- It seems that it is here:

    const artifact = this.serverless.service.package.artifact.split(path.sep).pop();

in saveServiceState... Looks like artifact does not get set with individually = true at all. If that is a standard behavior, also in main the artifact property should be skipped on save and restore in this case.
Imo master should show the same issue.

As solution we should wrap it in lodash calls, that do not fail on non-existing/empty properties.

const artifact = _.last(_.split(_.get(this.serverless.service, 'package.artifact', ""), path.sep));

@pmuens Cannot happen on master ... artifact is not persisted explicitly there.

@HyperBrain

Copy link
Copy Markdown
Contributor Author

@pmuens Now package individually should be handled too.

@pmuens

pmuens commented Apr 24, 2017

Copy link
Copy Markdown
Contributor

Thanks for updating! Will give it another spin the upcoming hrs. 👍

@pmuens pmuens 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.

Just tested this with different setups. Was not able to break it.

Thanks @HyperBrain 👍

@pmuens
pmuens merged commit 803c28f into serverless:master Apr 24, 2017
@pmuens
pmuens deleted the fix-package-artifact branch April 24, 2017 10:51
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.

3 participants