Skip to content

Commit fa01474

Browse files
committed
Make deployment bucket name unique
1 parent 168f98d commit fa01474

18 files changed

+221
-66
lines changed

deploy/googleDeploy.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const BbPromise = require('bluebird');
44

55
const validate = require('../shared/validate');
66
const utils = require('../shared/utils');
7+
const setDeploymentBucketName = require('../shared/setDeploymentBucketName');
78
const prepareDeployment = require('./lib/prepareDeployment');
89
const createDeployment = require('./lib/createDeployment');
910
const monitorDeployment = require('../shared/monitorDeployment');
@@ -24,6 +25,7 @@ class GoogleDeploy {
2425
this,
2526
validate,
2627
utils,
28+
setDeploymentBucketName,
2729
prepareDeployment,
2830
createDeployment,
2931
monitorDeployment,
@@ -40,6 +42,7 @@ class GoogleDeploy {
4042
.then(this.setDefaults),
4143

4244
'deploy:initialize': () => BbPromise.bind(this)
45+
.then(this.setDeploymentBucketName)
4346
.then(this.prepareDeployment),
4447

4548
'deploy:setupProviderConfiguration': () => BbPromise.bind(this)

deploy/googleDeploy.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ describe('GoogleDeploy', () => {
3838
describe('hooks', () => {
3939
let validateStub;
4040
let setDefaultsStub;
41+
let setDeploymentBucketNameStub;
4142
let prepareDeploymentStub;
4243
let createDeploymentStub;
4344
let generateArtifactDirectoryNameStub;
@@ -52,6 +53,8 @@ describe('GoogleDeploy', () => {
5253
.returns(BbPromise.resolve());
5354
setDefaultsStub = sinon.stub(googleDeploy, 'setDefaults')
5455
.returns(BbPromise.resolve());
56+
setDeploymentBucketNameStub = sinon.stub(googleDeploy, 'setDeploymentBucketName')
57+
.returns(BbPromise.resolve());
5558
prepareDeploymentStub = sinon.stub(googleDeploy, 'prepareDeployment')
5659
.returns(BbPromise.resolve());
5760
createDeploymentStub = sinon.stub(googleDeploy, 'createDeployment')
@@ -74,6 +77,7 @@ describe('GoogleDeploy', () => {
7477
afterEach(() => {
7578
googleDeploy.validate.restore();
7679
googleDeploy.setDefaults.restore();
80+
googleDeploy.setDeploymentBucketName.restore();
7781
googleDeploy.prepareDeployment.restore();
7882
googleDeploy.createDeployment.restore();
7983
googleDeploy.generateArtifactDirectoryName.restore();
@@ -92,7 +96,8 @@ describe('GoogleDeploy', () => {
9296

9397
it('should run "deploy:initialize" promise chain', () => googleDeploy
9498
.hooks['deploy:initialize']().then(() => {
95-
expect(prepareDeploymentStub.calledOnce).toEqual(true);
99+
expect(setDeploymentBucketNameStub.calledOnce).toEqual(true);
100+
expect(prepareDeploymentStub.calledAfter(setDeploymentBucketNameStub)).toEqual(true);
96101
}));
97102

98103
it('it should run "deploy:setupProviderConfiguration" promise chain', () => googleDeploy

deploy/lib/cleanupDeploymentBucket.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212

1313
getObjectsToRemove() {
1414
const params = {
15-
bucket: `sls-${this.serverless.service.service}-${this.options.stage}`,
15+
bucket: this.serverless.service.provider.deploymentBucketName,
1616
};
1717

1818
return this.provider.request('storage', 'objects', 'list', params)

deploy/lib/cleanupDeploymentBucket.test.js

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ describe('CleanupDeploymentBucket', () => {
1616
serverless = new Serverless();
1717
serverless.service = {
1818
service: 'my-service',
19+
provider: {
20+
deploymentBucketName: 'sls-my-service-dev-12345678',
21+
},
1922
};
2023
serverless.setProvider('google', new GoogleProvider(serverless));
2124
const options = {
@@ -64,27 +67,27 @@ describe('CleanupDeploymentBucket', () => {
6467
const response = {
6568
items: [
6669
{
67-
bucket: 'sls-my-service-dev',
70+
bucket: 'sls-my-service-dev-12345678',
6871
name: `${key}/151224711231-2016-08-18T15:42:00/artifact.zip`,
6972
},
7073
{
71-
bucket: 'sls-my-service-dev',
74+
bucket: 'sls-my-service-dev-12345678',
7275
name: `${key}/141264711231-2016-08-18T15:43:00/artifact.zip`,
7376
},
7477
{
75-
bucket: 'sls-my-service-dev',
78+
bucket: 'sls-my-service-dev-12345678',
7679
name: `${key}/141321321541-2016-08-18T11:23:02/artifact.zip`,
7780
},
7881
{
79-
bucket: 'sls-my-service-dev',
82+
bucket: 'sls-my-service-dev-12345678',
8083
name: `${key}/142003031341-2016-08-18T12:46:04/artifact.zip`,
8184
},
8285
{
83-
bucket: 'sls-my-service-dev',
86+
bucket: 'sls-my-service-dev-12345678',
8487
name: `${key}/113304333331-2016-08-18T13:40:06/artifact.zip`,
8588
},
8689
{
87-
bucket: 'sls-my-service-dev',
90+
bucket: 'sls-my-service-dev-12345678',
8891
name: `${key}/903940390431-2016-08-18T23:42:08/artifact.zip`,
8992
},
9093
],
@@ -94,27 +97,27 @@ describe('CleanupDeploymentBucket', () => {
9497
return googleDeploy.getObjectsToRemove().then((objects) => {
9598
expect(objects.length).toEqual(2);
9699
expect(objects).not.toContainEqual({
97-
bucket: 'sls-my-service-dev',
100+
bucket: 'sls-my-service-dev-12345678',
98101
name: `${key}/141321321541-2016-08-18T11:23:02/artifact.zip`,
99102
});
100103
expect(objects).not.toContainEqual({
101-
bucket: 'sls-my-service-dev',
104+
bucket: 'sls-my-service-dev-12345678',
102105
name: `${key}/142003031341-2016-08-18T12:46:04/artifact.zip`,
103106
});
104107
expect(objects).not.toContainEqual({
105-
bucket: 'sls-my-service-dev',
108+
bucket: 'sls-my-service-dev-12345678',
106109
name: `${key}/151224711231-2016-08-18T15:42:00/artifact.zip`,
107110
});
108111
expect(objects).not.toContainEqual({
109-
bucket: 'sls-my-service-dev',
112+
bucket: 'sls-my-service-dev-12345678',
110113
name: `${key}/903940390431-2016-08-18T23:42:08/artifact.zip`,
111114
});
112115
expect(requestStub.calledWithExactly(
113116
'storage',
114117
'objects',
115118
'list',
116119
{
117-
bucket: 'sls-my-service-dev',
120+
bucket: 'sls-my-service-dev-12345678',
118121
})).toEqual(true);
119122
});
120123
});
@@ -123,19 +126,19 @@ describe('CleanupDeploymentBucket', () => {
123126
const response = {
124127
items: [
125128
{
126-
bucket: 'sls-my-service-dev',
129+
bucket: 'sls-my-service-dev-12345678',
127130
name: `${key}/151224711231-2016-08-18T15:42:00/artifact.zip`,
128131
},
129132
{
130-
bucket: 'sls-my-service-dev',
133+
bucket: 'sls-my-service-dev-12345678',
131134
name: `${key}/141264711231-2016-08-18T15:43:00/artifact.zip`,
132135
},
133136
{
134-
bucket: 'sls-my-service-dev',
137+
bucket: 'sls-my-service-dev-12345678',
135138
name: `${key}/141321321541-2016-08-18T11:23:02/artifact.zip`,
136139
},
137140
{
138-
bucket: 'sls-my-service-dev',
141+
bucket: 'sls-my-service-dev-12345678',
139142
name: `${key}/142003031341-2016-08-18T12:46:04/artifact.zip`,
140143
},
141144
],
@@ -150,7 +153,7 @@ describe('CleanupDeploymentBucket', () => {
150153
'objects',
151154
'list',
152155
{
153-
bucket: 'sls-my-service-dev',
156+
bucket: 'sls-my-service-dev-12345678',
154157
})).toEqual(true);
155158
});
156159
});
@@ -169,7 +172,7 @@ describe('CleanupDeploymentBucket', () => {
169172
'objects',
170173
'list',
171174
{
172-
bucket: 'sls-my-service-dev',
175+
bucket: 'sls-my-service-dev-12345678',
173176
})).toEqual(true);
174177
});
175178
});
@@ -201,19 +204,19 @@ describe('CleanupDeploymentBucket', () => {
201204
it('should remove all given objects', () => {
202205
const objectsToRemove = [
203206
{
204-
bucket: 'sls-my-service-dev',
207+
bucket: 'sls-my-service-dev-12345678',
205208
name: `${key}/151224711231-2016-08-18T15:42:00/artifact.zip`,
206209
},
207210
{
208-
bucket: 'sls-my-service-dev',
211+
bucket: 'sls-my-service-dev-12345678',
209212
name: `${key}/141264711231-2016-08-18T15:43:00/artifact.zip`,
210213
},
211214
{
212-
bucket: 'sls-my-service-dev',
215+
bucket: 'sls-my-service-dev-12345678',
213216
name: `${key}/141321321541-2016-08-18T11:23:02/artifact.zip`,
214217
},
215218
{
216-
bucket: 'sls-my-service-dev',
219+
bucket: 'sls-my-service-dev-12345678',
217220
name: `${key}/142003031341-2016-08-18T12:46:04/artifact.zip`,
218221
},
219222
];

deploy/lib/compileFunctions.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ module.exports = {
2727
const funcTemplate = getFunctionTemplate(
2828
funcObject,
2929
this.options.region,
30-
`gs://sls-${
31-
this.serverless.service.service
32-
}-${this.options.stage}/${this.serverless.service.package.artifactFilePath}`);
30+
`gs://${
31+
this.serverless.service.provider.deploymentBucketName
32+
}/${this.serverless.service.package.artifactFilePath}`);
3333

3434
funcTemplate.properties.availableMemoryMb = _.get(funcObject, 'memorySize')
3535
|| _.get(this, 'serverless.service.provider.memorySize')

deploy/lib/compileFunctions.test.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('CompileFunctions', () => {
2222
compiledConfigurationTemplate: {
2323
resources: [],
2424
},
25+
deploymentBucketName: 'sls-my-service-dev-12345678',
2526
};
2627
serverless.setProvider('google', new GoogleProvider(serverless));
2728
const options = {
@@ -115,7 +116,7 @@ describe('CompileFunctions', () => {
115116
function: 'func1',
116117
availableMemoryMb: 1024,
117118
timeout: '60s',
118-
sourceArchiveUrl: 'gs://sls-my-service-dev/some-path/artifact.zip',
119+
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
119120
httpsTrigger: {
120121
url: 'foo',
121122
},
@@ -148,7 +149,7 @@ describe('CompileFunctions', () => {
148149
function: 'func1',
149150
availableMemoryMb: 1024,
150151
timeout: '60s',
151-
sourceArchiveUrl: 'gs://sls-my-service-dev/some-path/artifact.zip',
152+
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
152153
httpsTrigger: {
153154
url: 'foo',
154155
},
@@ -181,7 +182,7 @@ describe('CompileFunctions', () => {
181182
function: 'func1',
182183
availableMemoryMb: 256,
183184
timeout: '120s',
184-
sourceArchiveUrl: 'gs://sls-my-service-dev/some-path/artifact.zip',
185+
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
185186
httpsTrigger: {
186187
url: 'foo',
187188
},
@@ -214,7 +215,7 @@ describe('CompileFunctions', () => {
214215
function: 'func1',
215216
availableMemoryMb: 256,
216217
timeout: '120s',
217-
sourceArchiveUrl: 'gs://sls-my-service-dev/some-path/artifact.zip',
218+
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
218219
httpsTrigger: {
219220
url: 'foo',
220221
},
@@ -246,7 +247,7 @@ describe('CompileFunctions', () => {
246247
function: 'func1',
247248
availableMemoryMb: 256,
248249
timeout: '60s',
249-
sourceArchiveUrl: 'gs://sls-my-service-dev/some-path/artifact.zip',
250+
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
250251
httpsTrigger: {
251252
url: 'foo',
252253
},
@@ -296,7 +297,7 @@ describe('CompileFunctions', () => {
296297
function: 'func1',
297298
availableMemoryMb: 256,
298299
timeout: '60s',
299-
sourceArchiveUrl: 'gs://sls-my-service-dev/some-path/artifact.zip',
300+
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
300301
eventTrigger: {
301302
eventType: 'foo',
302303
path: 'some-path',
@@ -312,7 +313,7 @@ describe('CompileFunctions', () => {
312313
function: 'func2',
313314
availableMemoryMb: 256,
314315
timeout: '60s',
315-
sourceArchiveUrl: 'gs://sls-my-service-dev/some-path/artifact.zip',
316+
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
316317
eventTrigger: {
317318
eventType: 'foo',
318319
resource: 'some-resource',

deploy/lib/createDeployment.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,21 @@ module.exports = {
2020

2121
return this.provider.request('deploymentmanager', 'deployments', 'list', params)
2222
.then((response) => {
23-
let found = false;
23+
let foundDeployment;
2424

2525
if (response && response.deployments) {
26-
found = !!response.deployments.find((deployment) => {
26+
foundDeployment = response.deployments.find((deployment) => {
2727
const name = `sls-${this.serverless.service.service}-${this.options.stage}`;
2828
return deployment.name === name;
2929
});
3030
}
3131

32-
return found;
32+
return foundDeployment;
3333
});
3434
},
3535

36-
createIfNotExists(existingDeployment) {
37-
if (existingDeployment) return BbPromise.resolve();
36+
createIfNotExists(foundDeployment) {
37+
if (foundDeployment) return BbPromise.resolve();
3838

3939
this.serverless.cli.log('Creating deployment...');
4040

deploy/lib/createDeployment.test.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ describe('CreateDeployment', () => {
7272
});
7373

7474
describe('#checkForExistingDeployment()', () => {
75-
it('should return "false" if no deployments are found', () => {
75+
it('should return "undefined" if no deployments are found', () => {
7676
requestStub.returns(BbPromise.resolve([]));
7777

78-
return googleDeploy.checkForExistingDeployment().then((found) => {
79-
expect(found).toEqual(false);
78+
return googleDeploy.checkForExistingDeployment().then((foundDeployment) => {
79+
expect(foundDeployment).toEqual(undefined);
8080
expect(requestStub.calledWithExactly(
8181
'deploymentmanager',
8282
'deployments',
@@ -86,16 +86,16 @@ describe('CreateDeployment', () => {
8686
});
8787
});
8888

89-
it('should return "false" if deployments do not contain deployment', () => {
89+
it('should return "undefined" if deployments do not contain deployment', () => {
9090
const response = {
9191
deployments: [
9292
{ name: 'some-other-deployment' },
9393
],
9494
};
9595
requestStub.returns(BbPromise.resolve(response));
9696

97-
return googleDeploy.checkForExistingDeployment().then((found) => {
98-
expect(found).toEqual(false);
97+
return googleDeploy.checkForExistingDeployment().then((foundDeployment) => {
98+
expect(foundDeployment).toEqual(undefined);
9999
expect(requestStub.calledWithExactly(
100100
'deploymentmanager',
101101
'deployments',
@@ -114,8 +114,8 @@ describe('CreateDeployment', () => {
114114
};
115115
requestStub.returns(BbPromise.resolve(response));
116116

117-
return googleDeploy.checkForExistingDeployment().then((found) => {
118-
expect(found).toEqual(true);
117+
return googleDeploy.checkForExistingDeployment().then((foundDeployment) => {
118+
expect(foundDeployment).toEqual(response.deployments[0]);
119119
expect(requestStub.calledWithExactly(
120120
'deploymentmanager',
121121
'deployments',
@@ -145,16 +145,16 @@ describe('CreateDeployment', () => {
145145
});
146146

147147
it('should resolve if there is no existing deployment', () => {
148-
const deploymentFound = true;
148+
const foundDeployment = true;
149149

150-
return googleDeploy.createIfNotExists(deploymentFound).then(() => {
150+
return googleDeploy.createIfNotExists(foundDeployment).then(() => {
151151
expect(consoleLogStub.calledOnce).toEqual(false);
152152
expect(readFileSyncStub.called).toEqual(false);
153153
});
154154
});
155155

156156
it('should create and hand over to monitor the deployment if it does not exist', () => {
157-
const deploymentFound = false;
157+
const foundDeployment = false;
158158
const params = {
159159
project: 'my-project',
160160
resource: {
@@ -168,7 +168,7 @@ describe('CreateDeployment', () => {
168168
};
169169
requestStub.returns(BbPromise.resolve());
170170

171-
return googleDeploy.createIfNotExists(deploymentFound).then(() => {
171+
return googleDeploy.createIfNotExists(foundDeployment).then(() => {
172172
expect(consoleLogStub.calledOnce).toEqual(true);
173173
expect(readFileSyncStub.called).toEqual(true);
174174
expect(requestStub.calledWithExactly(

0 commit comments

Comments
 (0)