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

Modify main process of deploy to another function #323

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 93 additions & 102 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const execFile = require('child_process').execFile
const fs = require('fs-extra')
const packageJson = require(path.join(__dirname, '..', 'package.json'))
const minimatch = require('minimatch')
const async = require('async')
const zip = new (require('node-zip'))()
const dotenv = require('dotenv')
const ScheduleEvents = require(path.join(__dirname, 'schedule_events'))
Expand Down Expand Up @@ -765,134 +764,126 @@ Lambda.prototype.package = function (program) {
})
}

Lambda.prototype.deploy = function (program) {
var _this = this
var regions = program.region.split(',')
_this._archive(program, function (err, buffer) {
if (err) {
throw err
}

console.log('=> Reading zip file to memory')
var params = _this._params(program, buffer)

console.log('=> Reading event source file to memory')
var eventSourceList = _this._eventSourceList(program)
Lambda.prototype._deployToRegion = function (program, params, region) {
const _this = this
console.log('=> Reading event source file to memory')
const eventSourceList = _this._eventSourceList(program)

async.map(regions, function (region, cb) {
console.log('=> Uploading zip file to AWS Lambda ' + region + ' with parameters:')
console.log(params)

var awsSecurity = {
region: region
}

if (program.profile) {
aws.config.credentials = new aws.SharedIniFileCredentials({
profile: program.profile
})
} else {
awsSecurity.accessKeyId = program.accessKey
awsSecurity.secretAccessKey = program.secretKey
}
return new Promise((resolve, reject) => {
console.log('=> Uploading zip file to AWS Lambda ' + region + ' with parameters:')
console.log(params)

if (program.sessionToken) {
awsSecurity.sessionToken = program.sessionToken
}
const awsSecurity = { region: region }

if (program.deployTimeout) {
aws.config.httpOptions.timeout = parseInt(program.deployTimeout)
}
if (program.profile) {
aws.config.credentials = new aws.SharedIniFileCredentials({
profile: program.profile
})
} else {
awsSecurity.accessKeyId = program.accessKey
awsSecurity.secretAccessKey = program.secretKey
}

aws.config.update(awsSecurity)
if (program.sessionToken) {
awsSecurity.sessionToken = program.sessionToken
}

var lambda = new aws.Lambda({
apiVersion: '2015-03-31'
})
var scheduleEvents = new ScheduleEvents(aws)
if (program.deployTimeout) {
aws.config.httpOptions.timeout = parseInt(program.deployTimeout)
}

// Checking function
return lambda.getFunction({
'FunctionName': params.FunctionName
}, (err) => {
if (err) {
// Function does not exist
return _this._uploadNew(lambda, params).then((results) => {
console.log('=> Zip file(s) done uploading. Results follow: ')
console.log(results)
aws.config.update(awsSecurity)

// This code is on its way to Promise.
// From now on, callback will not be used.
return Promise.all([
_this._updateEventSources(
lambda,
params.FunctionName,
[],
eventSourceList.EventSourceMappings
),
_this._updateScheduleEvents(
scheduleEvents,
results.FunctionArn,
eventSourceList.ScheduleEvents
)
]).then((results) => {
cb(null, results)
}).catch((err) => {
cb(err)
})
}).catch((err) => {
return Promise.reject(err)
})
}
const lambda = new aws.Lambda({ apiVersion: '2015-03-31' })
const scheduleEvents = new ScheduleEvents(aws)

// Function exists
_this._listEventSourceMappings(lambda, {
'FunctionName': params.FunctionName
}, (err, existingEventSourceList) => {
if (err) {
throw err
}
// Checking function
return lambda.getFunction({
'FunctionName': params.FunctionName
}, (err) => {
if (err) {
// Function does not exist
return _this._uploadNew(lambda, params).then((results) => {
console.log('=> Zip file(s) done uploading. Results follow: ')
console.log(results)

// This code is on its way to Promise.
// From now on, callback will not be used.
return Promise.all([
_this._uploadExisting(lambda, params).then((results) => {
console.log('=> Zip file(s) done uploading. Results follow: ')
console.log(results)
return _this._updateScheduleEvents(
scheduleEvents,
results.FunctionArn,
eventSourceList.ScheduleEvents
)
}).catch((err) => {
return Promise.reject(err)
}),
_this._updateEventSources(
lambda,
params.FunctionName,
existingEventSourceList,
[],
eventSourceList.EventSourceMappings
),
_this._updateScheduleEvents(
scheduleEvents,
results.FunctionArn,
eventSourceList.ScheduleEvents
)
]).then((results) => {
cb(null, results)
resolve(results)
}).catch((err) => {
cb(err)
reject(err)
})
}).catch((err) => {
reject(err)
})
})
}, function (err, results) {
if (err) {
throw err
}
const resultsIsEmpty = results.filter(function (result) {
return result.filter(function (res) {

// Function exists
_this._listEventSourceMappings(lambda, {
'FunctionName': params.FunctionName
}, (err, existingEventSourceList) => {
if (err) return reject(err)

return Promise.all([
_this._uploadExisting(lambda, params).then((results) => {
console.log('=> Zip file(s) done uploading. Results follow: ')
console.log(results)
return _this._updateScheduleEvents(
scheduleEvents,
results.FunctionArn,
eventSourceList.ScheduleEvents
)
}),
_this._updateEventSources(
lambda,
params.FunctionName,
existingEventSourceList,
eventSourceList.EventSourceMappings
)
]).then((results) => {
resolve(results)
}).catch((err) => {
reject(err)
})
})
})
})
}

Lambda.prototype.deploy = function (program) {
const _this = this
const regions = program.region.split(',')
_this._archive(program, (err, buffer) => {
if (err) throw err

console.log('=> Reading zip file to memory')
const params = _this._params(program, buffer)

Promise.all(regions.map((region) => {
return _this._deployToRegion(program, params, region)
})).then((results) => {
const resultsIsEmpty = results.filter((result) => {
return result.filter((res) => {
return res.length > 0
}).length > 0
}).length === 0
if (!resultsIsEmpty) {
console.log('=> All tasks done. Results follow: ')
console.log(JSON.stringify(results, null, ' '))
}
}).catch((err) => {
console.log(err)
})
})
}
Expand Down
6 changes: 5 additions & 1 deletion test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,11 @@ describe('lib/main', function () {
})
})

describe('Lambda.prototype._deployToRegion()', () => {
it('Since `aws-mock` does not correspond to `request.on`, it is impossible to test with Mock')
})

describe('Lambda.prototype.deploy()', () => {
it('TODO: Add test. Since the current deploy function is hard to test, skip')
it('Since `aws-mock` does not correspond to `request.on`, it is impossible to test with Mock')
})
})