|
| 1 | +const { spawnSync } = require('child_process'); |
| 2 | +const path = require('path'); |
| 3 | +const fs = require('fs-extra'); |
| 4 | +const Promise = require('bluebird'); |
| 5 | +var JSZip = require('jszip'); |
| 6 | + |
| 7 | +function runCommand(cmd,args,options) { |
| 8 | + const ps = spawnSync(cmd, args,options); |
| 9 | + if (ps.error) { |
| 10 | + console.log(ps.error.code) |
| 11 | + throw new Error(ps.error); |
| 12 | + } else if (ps.status !== 0) { |
| 13 | + throw new Error(ps.stderr); |
| 14 | + } |
| 15 | + return ps; |
| 16 | +} |
| 17 | + |
| 18 | +function cleanBuild(){ |
| 19 | + this.build_path = path.join(this.servicePath,'.serverless','build','ruby_layer') |
| 20 | + if (fs.pathExistsSync(this.build_path)){ |
| 21 | + fs.rmdirSync(this.build_path, { recursive: true }) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +function bundleInstall(){ |
| 26 | + this.serverless.cli.log(this.build_path) |
| 27 | + const gem_path = path.join(this.build_path,'Gemfile') |
| 28 | + fs.copySync(path.join(this.servicePath,'Gemfile'), gem_path ) |
| 29 | + const bundle_args = ['bundle', 'install', '--path=.','--without', 'test', 'development'] |
| 30 | + const options={cwd : this.build_path, encoding : 'utf8'} |
| 31 | + ps = runCommand("bundle",['-v']) |
| 32 | + if (ps.error && ps.error.code === 'ENOENT') { |
| 33 | + throw new Error('bundle command not found in local'); |
| 34 | + } |
| 35 | + this.serverless.cli.log(bundle_args.slice(1,bundle_args.length)) |
| 36 | + ps=runCommand(bundle_args[0],bundle_args.slice(1,bundle_args.length),options) |
| 37 | + this.serverless.cli.log(ps.stdout) |
| 38 | +} |
| 39 | + |
| 40 | +function zipDir(folder_path,targetPath,zipOptions){ |
| 41 | + zip = new JSZip() |
| 42 | + return addDirtoZip(zip, folder_path) |
| 43 | + .then(() => { |
| 44 | + return new Promise(resolve => |
| 45 | + zip.generateNodeStream(zipOptions) |
| 46 | + .pipe(fs.createWriteStream(targetPath)) |
| 47 | + .on('finish', resolve)) |
| 48 | + .then(() => null)}); |
| 49 | +} |
| 50 | + |
| 51 | +function addDirtoZip(zip, dir_path){ |
| 52 | + const dir_path_norm = path.normalize(dir_path) |
| 53 | + return fs.readdirAsync(dir_path_norm) |
| 54 | + .map(file_name => { |
| 55 | + return addFiletoZip(zip, dir_path_norm, file_name) |
| 56 | + }); |
| 57 | +} |
| 58 | + |
| 59 | +function addFiletoZip(zip, dir_path, file_name){ |
| 60 | + const filePath = path.join(dir_path, file_name) |
| 61 | + return fs.statAsync(filePath) |
| 62 | + .then(stat => { |
| 63 | + if (stat.isDirectory()){ |
| 64 | + return addDirtoZip(zip.folder(file_name), filePath); |
| 65 | + } else { |
| 66 | + const file_option = { date: stat.mtime, unixPermissions: stat.mode }; |
| 67 | + return fs.readFileAsync(filePath) |
| 68 | + .then(data => zip.file(file_name, data, file_option)); |
| 69 | + } |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +function zipBundleFolder() { |
| 74 | + this.gem_folder= fs.readdirSync(path.join(this.build_path,'ruby'))[0] |
| 75 | + fs.rmdirSync(path.join(this.build_path,'ruby',this.gem_folder,'cache'),{ recursive: true }) |
| 76 | + const platform = process.platform == 'win32' ? 'DOS' : 'UNIX' |
| 77 | + return zipDir(path.join(this.build_path,'ruby'), |
| 78 | + path.join(this.build_path, 'gemLayer.zip'), |
| 79 | + { platform: platform, compression: 'DEFLATE', |
| 80 | + compressionOptions: { level: 9 }}); |
| 81 | +} |
| 82 | + |
| 83 | +function configureLayer() { |
| 84 | + if (!this.serverless.service.layers) { |
| 85 | + this.serverless.service.layers = {}; |
| 86 | + } |
| 87 | + |
| 88 | + this.serverless.service.layers['gemLayer'] = Object.assign( |
| 89 | + { |
| 90 | + artifact: path.join(this.build_path, 'gemLayer.zip'), |
| 91 | + name: `${ |
| 92 | + this.serverless.service.service |
| 93 | + }-${this.serverless.providers.aws.getStage()}-ruby-bundle`, |
| 94 | + description: |
| 95 | + 'Ruby gem generated by serverless-ruby-bundler', |
| 96 | + compatibleRuntimes: [this.serverless.service.provider.runtime] |
| 97 | + }, |
| 98 | + this.options.layer |
| 99 | + ); |
| 100 | + |
| 101 | + Object.keys(this.serverless.service.functions).forEach(funcName => { |
| 102 | + const function_ = this.serverless.service.getFunction(funcName) |
| 103 | + function_.environment={} |
| 104 | + function_.environment["GEM_PATH"]="/opt/"+this.gem_folder |
| 105 | + function_.layers = [{"Ref":"GemLayerLambdaLayer"}] |
| 106 | + }) |
| 107 | + return Promise.resolve(); |
| 108 | +} |
| 109 | + |
| 110 | +function bundleGems() { |
| 111 | + return Promise.bind(this) |
| 112 | + .then(cleanBuild) |
| 113 | + .then(bundleInstall) |
| 114 | + .then(zipBundleFolder) |
| 115 | + .then(configureLayer) |
| 116 | +} |
| 117 | + |
| 118 | +module.exports = { bundleGems}; |
0 commit comments