Browse Source

Generate random bucket names and save to config (#32)

master
Naoyuki Kanezawa 8 years ago
committed by Matheus Fernandes
parent
commit
cad1fbd31d
  1. 22
      src/providers/gcp/deploy.js
  2. 11
      src/providers/gcp/util/generate-bucket-name.js

22
src/providers/gcp/deploy.js

@ -25,8 +25,8 @@ const getToken = require('./util/get-access-token')
const describeProject = require('../../describe-project') const describeProject = require('../../describe-project')
const copyToClipboard = require('../../util/copy-to-clipboard') const copyToClipboard = require('../../util/copy-to-clipboard')
const getFunctionHandler = require('./util/get-function-handler') const getFunctionHandler = require('./util/get-function-handler')
const generateBucketName = require('./util/generate-bucket-name')
const BUCKET_NAME = 'now-deployments' const { writeToConfigFile } = require('../../util/config-files')
const deploy = async (ctx: { const deploy = async (ctx: {
config: any, config: any,
@ -122,6 +122,14 @@ const deploy = async (ctx: {
const resourcesStart = Date.now() const resourcesStart = Date.now()
const stopResourcesSpinner = wait('Creating API resources') const stopResourcesSpinner = wait('Creating API resources')
if (!ctx.config.gcp) ctx.config.gcp = {}
if (!ctx.config.gcp.bucketName) {
ctx.config.gcp.bucketName = generateBucketName()
writeToConfigFile(ctx.config)
}
const { bucketName } = ctx.config.gcp
debug('creating gcp storage bucket') debug('creating gcp storage bucket')
const bucketRes = await fetch( const bucketRes = await fetch(
`https://www.googleapis.com/storage/v1/b?project=${project.id}`, `https://www.googleapis.com/storage/v1/b?project=${project.id}`,
@ -132,7 +140,7 @@ const deploy = async (ctx: {
Authorization: `Bearer ${token}` Authorization: `Bearer ${token}`
}, },
body: JSON.stringify({ body: JSON.stringify({
name: BUCKET_NAME name: bucketName
}) })
} }
) )
@ -151,7 +159,9 @@ const deploy = async (ctx: {
debug('creating gcp storage file') debug('creating gcp storage file')
const fileRes = await fetch( const fileRes = await fetch(
`https://www.googleapis.com/upload/storage/v1/b/${BUCKET_NAME}/o?uploadType=media&name=${encodeURIComponent( `https://www.googleapis.com/upload/storage/v1/b/${encodeURIComponent(
bucketName
)}/o?uploadType=media&name=${encodeURIComponent(
zipFileName zipFileName
)}&project=${encodeURIComponent(project.id)}`, )}&project=${encodeURIComponent(project.id)}`,
{ {
@ -185,7 +195,9 @@ const deploy = async (ctx: {
name: `projects/${project.id}/locations/${region}/functions/${deploymentId}`, name: `projects/${project.id}/locations/${region}/functions/${deploymentId}`,
timeout: '15s', timeout: '15s',
availableMemoryMb: 512, availableMemoryMb: 512,
sourceArchiveUrl: `gs://${BUCKET_NAME}/${zipFileName}`, sourceArchiveUrl: `gs://${encodeURIComponent(
bucketName
)}/${zipFileName}`,
entryPoint: 'handler', entryPoint: 'handler',
httpsTrigger: { httpsTrigger: {
url: null url: null

11
src/providers/gcp/util/generate-bucket-name.js

@ -0,0 +1,11 @@
const BUCKET_NAME_PREFIX = 'now-deployments-'
const CHARS = 'abcdefghijklmnopqrstuvwxyz0123456789'
const MAX_LENGTH = 63
module.exports = function generateBucketName() {
let name = BUCKET_NAME_PREFIX
for (let i = 0, l = MAX_LENGTH - name.length; i < l; i++) {
name += CHARS[Math.floor(Math.random() * CHARS.length)]
}
return name
}
Loading…
Cancel
Save