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

Remove _rsync #329

Merged
merged 3 commits into from
Jun 20, 2017
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
85 changes: 16 additions & 69 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ Lambda.prototype._eventSourceList = function (program) {
return list
}

Lambda.prototype._fileCopy = function (program, src, dest, excludeNodeModules, callback) {
Lambda.prototype._fileCopy = (program, src, dest, excludeNodeModules, callback) => {
const srcAbsolutePath = path.resolve(src)
const excludes = (function () {
const excludes = (() => {
return [
'.git*',
'*.swp',
Expand All @@ -246,7 +246,7 @@ Lambda.prototype._fileCopy = function (program, src, dest, excludeNodeModules, c

// Formatting for `filter` of `fs.copy`
const dirBlobs = []
const pattern = '{' + excludes.map(function (str) {
const pattern = '{' + excludes.map((str) => {
if (str.charAt(str.length - 1) === path.sep) {
str = str.substr(0, str.length - 1)
dirBlobs.push(str)
Expand All @@ -261,13 +261,13 @@ Lambda.prototype._fileCopy = function (program, src, dest, excludeNodeModules, c
}).join(',') + '}'
const dirPatternRegExp = new RegExp(`(${dirBlobs.join('|')})$`)

fs.mkdirs(dest, function (err) {
fs.mkdirs(dest, (err) => {
if (err) {
return callback(err)
}
const options = {
dereference: true, // same meaning as `-L` of `rsync` command
filter: function (src, dest) {
filter: (src, dest) => {
if (!program.prebuiltDirectory && src === path.join(srcAbsolutePath, 'package.json')) {
// include package.json unless prebuiltDirectory is set
return true
Expand All @@ -293,43 +293,6 @@ Lambda.prototype._fileCopy = function (program, src, dest, excludeNodeModules, c
})
}

// `_rsync` will be replaced by` _fileCopy`.
Lambda.prototype._rsync = function (program, src, dest, excludeNodeModules, callback) {
var excludes = ['.git*', '*.swp', '.editorconfig', '.lambda', 'deploy.env', '*.log', '/build/']
var excludeGlobs = []
if (program.excludeGlobs) {
excludeGlobs = program.excludeGlobs.split(' ')
}
var excludeArgs = excludeGlobs
.concat(excludes)
.concat(excludeNodeModules ? ['/node_modules'] : [])
.map(function (exclude) {
return '--exclude=' + exclude
}).join(' ')

fs.mkdirs(dest, function (err) {
if (err) {
return callback(err)
}

// include package.json unless prebuiltDirectory is set
var includeArgs = program.prebuiltDirectory ? '' : '--include /package.json '

// we need the extra / after src to make sure we are copying the content
// of the directory, not the directory itself.
exec('rsync -rL ' + includeArgs + excludeArgs + ' ' + src.trim() + '/ ' + dest, {
maxBuffer: maxBufferSize,
env: process.env
}, function (err) {
if (err) {
return callback(err)
}

return callback(null, true)
})
})
}

Lambda.prototype._npmInstall = (program, codeDirectory, callback) => {
const dockerBaseOptions = [
'run', '--rm', '-v', `${codeDirectory}:/var/task`,
Expand Down Expand Up @@ -529,18 +492,10 @@ Lambda.prototype._archive = function (program, archiveCallback) {
}

Lambda.prototype._archivePrebuilt = function (program, archiveCallback) {
var codeDirectory = this._codeDirectory()
var _this = this
const codeDirectory = this._codeDirectory()
const _this = this

// It is switched to `_ rsync` by environment variable.
// (Used if there is a problem with `_ fileCopy`)
// If there is no problem even if deleting `_rsync`, this switching process is deleted
var copyFunction = '_fileCopy'
if (process.env.NODE_LAMBDA_COPY_FUNCTION === 'rsync') {
console.log('=> INFO: Use rsync for copy')
copyFunction = '_rsync'
}
this[copyFunction](program, program.prebuiltDirectory, codeDirectory, false, function (err) {
_this._fileCopy(program, program.prebuiltDirectory, codeDirectory, false, (err) => {
if (err) {
return archiveCallback(err)
}
Expand All @@ -557,43 +512,35 @@ Lambda.prototype._buildAndArchive = function (program, archiveCallback) {
}

// Warn if not building on 64-bit linux
var arch = process.platform + '.' + process.arch
const arch = process.platform + '.' + process.arch
if (arch !== 'linux.x64' && !program.dockerImage) {
console.warn('Warning!!! You are building on a platform that is not 64-bit Linux (%s). ' +
'If any of your Node dependencies include C-extensions, they may not work as expected in the ' +
'Lambda environment.\n\n', arch)
}

var _this = this
var codeDirectory = _this._codeDirectory()
var lambdaSrcDirectory = program.sourceDirectory ? program.sourceDirectory.replace(/\/$/, '') : '.'
const _this = this
const codeDirectory = _this._codeDirectory()
const lambdaSrcDirectory = program.sourceDirectory ? program.sourceDirectory.replace(/\/$/, '') : '.'

_this._cleanDirectory(codeDirectory, function (err) {
_this._cleanDirectory(codeDirectory, (err) => {
if (err) {
return archiveCallback(err)
}
console.log('=> Moving files to temporary directory')

// It is switched to `_ rsync` by environment variable.
// (Used if there is a problem with `_ fileCopy`)
// If there is no problem even if deleting `_rsync`, this switching process is deleted
var copyFunction = '_fileCopy'
if (process.env.NODE_LAMBDA_COPY_FUNCTION === 'rsync') {
console.log('=> INFO: Use rsync for copy')
copyFunction = '_rsync'
}
// Move files to tmp folder
_this[copyFunction](program, lambdaSrcDirectory, codeDirectory, true, function (err) {
_this._fileCopy(program, lambdaSrcDirectory, codeDirectory, true, (err) => {
if (err) {
return archiveCallback(err)
}
console.log('=> Running npm install --production')
_this._npmInstall(program, codeDirectory, function (err) {
_this._npmInstall(program, codeDirectory, (err) => {
if (err) {
return archiveCallback(err)
}

_this._postInstallScript(program, codeDirectory, function (err) {
_this._postInstallScript(program, codeDirectory, (err) => {
if (err) {
return archiveCallback(err)
}
Expand Down
71 changes: 30 additions & 41 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,41 +267,39 @@ describe('lib/main', function () {
})
})

function rsyncTests (funcName) {
before(function () {
describe('_fileCopy', () => {
before(() => {
fs.mkdirSync('build')
fs.mkdirsSync(path.join('__unittest', 'hoge'))
fs.mkdirsSync(path.join('__unittest', 'fuga'))
fs.writeFileSync(path.join('__unittest', 'hoge', 'piyo'))
fs.writeFileSync(path.join('__unittest', 'hoge', 'package.json'))
fs.writeFileSync('fuga')
})
after(function () {
['build', 'fuga', '__unittest'].forEach(function (path) {
after(() => {
['build', 'fuga', '__unittest'].forEach((path) => {
fs.removeSync(path)
})
})

beforeEach(function (done) {
lambda._cleanDirectory(codeDirectory, done)
})
beforeEach((done) => lambda._cleanDirectory(codeDirectory, done))

it(funcName + ' an index.js as well as other files', function (done) {
lambda[funcName](program, '.', codeDirectory, true, function (err, result) {
it('_fileCopy an index.js as well as other files', (done) => {
lambda._fileCopy(program, '.', codeDirectory, true, (err, result) => {
assert.isNull(err)
var contents = fs.readdirSync(codeDirectory);
['index.js', 'package.json'].forEach(function (needle) {
const contents = fs.readdirSync(codeDirectory);
['index.js', 'package.json'].forEach((needle) => {
assert.include(contents, needle, `Target: "${needle}"`)
});
['node_modules', 'build'].forEach(function (needle) {
['node_modules', 'build'].forEach((needle) => {
assert.notInclude(contents, needle, `Target: "${needle}"`)
})
done()
})
})

describe('when there are excluded files', function () {
beforeEach(function (done) {
describe('when there are excluded files', () => {
beforeEach((done) => {
// *main* => lib/main.js
// In case of specifying files under the directory with wildcards
program.excludeGlobs = [
Expand All @@ -314,26 +312,26 @@ describe('lib/main', function () {
done()
})

it(funcName + ' an index.js as well as other files', function (done) {
lambda[funcName](program, '.', codeDirectory, true, function (err, result) {
it('_fileCopy an index.js as well as other files', (done) => {
lambda._fileCopy(program, '.', codeDirectory, true, (err, result) => {
assert.isNull(err)
var contents = fs.readdirSync(codeDirectory);
['index.js', 'package.json'].forEach(function (needle) {
const contents = fs.readdirSync(codeDirectory);
['index.js', 'package.json'].forEach((needle) => {
assert.include(contents, needle, `Target: "${needle}"`)
})
done()
})
})

it(funcName + ' excludes files matching excludeGlobs', function (done) {
lambda[funcName](program, '.', codeDirectory, true, function (err, result) {
it('_fileCopy excludes files matching excludeGlobs', (done) => {
lambda._fileCopy(program, '.', codeDirectory, true, (err, result) => {
assert.isNull(err)
var contents = fs.readdirSync(codeDirectory);
['__unittest', 'fuga'].forEach(function (needle) {
let contents = fs.readdirSync(codeDirectory);
['__unittest', 'fuga'].forEach((needle) => {
assert.include(contents, needle, `Target: "${needle}"`)
});

['node-lambda.png', 'test'].forEach(function (needle) {
['node-lambda.png', 'test'].forEach((needle) => {
assert.notInclude(contents, needle, `Target: "${needle}"`)
})

Expand All @@ -350,45 +348,36 @@ describe('lib/main', function () {
})
})

it(funcName + ' should not exclude package.json, even when excluded by excludeGlobs', function (done) {
it('_fileCopy should not exclude package.json, even when excluded by excludeGlobs', (done) => {
program.excludeGlobs = '*.json'
lambda[funcName](program, '.', codeDirectory, true, function (err, result) {
lambda._fileCopy(program, '.', codeDirectory, true, (err, result) => {
assert.isNull(err)
var contents = fs.readdirSync(codeDirectory)
const contents = fs.readdirSync(codeDirectory)
assert.include(contents, 'package.json')
done()
})
})

it(funcName + ' should not include package.json when --prebuiltDirectory is set', function (done) {
var buildDir = '.build_' + Date.now()
after(function () {
fs.removeSync(buildDir)
})
it('_fileCopy should not include package.json when --prebuiltDirectory is set', (done) => {
const buildDir = '.build_' + Date.now()
after(() => fs.removeSync(buildDir))

fs.mkdirSync(buildDir)
fs.writeFileSync(path.join(buildDir, 'testa'))
fs.writeFileSync(path.join(buildDir, 'package.json'))

program.excludeGlobs = '*.json'
program.prebuiltDirectory = buildDir
lambda[funcName](program, buildDir, codeDirectory, true, function (err, result) {
lambda._fileCopy(program, buildDir, codeDirectory, true, (err, result) => {
assert.isNull(err)
var contents = fs.readdirSync(codeDirectory)
const contents = fs.readdirSync(codeDirectory)
assert.notInclude(contents, 'package.json', 'Target: "packages.json"')
assert.include(contents, 'testa', 'Target: "testa"')
done()
})
})
})
}

describe('_fileCopy', function () { rsyncTests('_fileCopy') })
if (process.platform === 'win32') {
it('For Windows, `_rsync` tests pending')
} else {
describe('_rsync', function () { rsyncTests('_rsync') })
}
})

describe('_npmInstall', () => {
beforeEach((done) => {
Expand Down