|
| 1 | +util = require 'util' |
| 2 | +_exec = util.promisify require('child_process').exec |
| 3 | +pkg = require './package.json' |
| 4 | +assert = require 'assert' |
| 5 | + |
| 6 | +LAYER_NAME = 'coffeescript' |
| 7 | +NODE_VERSION = '8.10.0' |
| 8 | +COFFEESCRIPT_VERSION = pkg.dependencies.coffeescript.replace /[^\d\.]/, '' |
| 9 | +IMAGE_FILE = "node-provided-lambda-v#{NODE_VERSION.split('.')[0]}.x" |
| 10 | +OUTPUT_DIR = 'build' |
| 11 | +OUTPUT_FILE = "#{pkg.name}_#{pkg.version}" |
| 12 | + |
| 13 | +exec = (command) -> |
| 14 | + try |
| 15 | + _exec command, maxBuffer: 1024 * 1000 |
| 16 | + catch e |
| 17 | + throw new Error e |
| 18 | + |
| 19 | +task 'build', 'builds and packages the runtime layer', |
| 20 | +(options) -> |
| 21 | + console.log """ |
| 22 | + =========================================================== |
| 23 | + runtime: #{OUTPUT_FILE} |
| 24 | + CoffeeScript: v#{COFFEESCRIPT_VERSION} |
| 25 | + NodeJs: v#{NODE_VERSION} |
| 26 | + =========================================================== |
| 27 | + """ |
| 28 | + |
| 29 | + await exec " |
| 30 | + docker build |
| 31 | + --build-arg NODE_VERSION=#{NODE_VERSION} |
| 32 | + -t #{IMAGE_FILE} |
| 33 | + . |
| 34 | + " |
| 35 | + console.log "building image... ok" |
| 36 | + |
| 37 | + await exec " |
| 38 | + if [ ! -d build ]; then mkdir build; fi; |
| 39 | + docker run --rm #{IMAGE_FILE} |
| 40 | + cat /tmp/node-v#{NODE_VERSION}.zip > #{OUTPUT_DIR}/#{OUTPUT_FILE}.zip |
| 41 | + " |
| 42 | + console.log "packaging image... ok" |
| 43 | + |
| 44 | + console.log """ |
| 45 | + build completed successfully! |
| 46 | + output: #{OUTPUT_DIR}/#{OUTPUT_FILE}.zip |
| 47 | + """ |
| 48 | + |
| 49 | +task 'publish', 'uploads packaged runtime layer to AWS', |
| 50 | +(option) -> |
| 51 | + await invoke 'build' |
| 52 | + await invoke 'test' |
| 53 | + |
| 54 | + { stdout, stderr } = await exec " |
| 55 | + aws lambda publish-layer-version |
| 56 | + --layer-name #{LAYER_NAME} |
| 57 | + --zip-file fileb://#{OUTPUT_DIR}/#{OUTPUT_FILE}.zip |
| 58 | + --description \"A CoffeeScript v#{COFFEESCRIPT_VERSION} custom runtime\" |
| 59 | + --license-info MIT |
| 60 | + --query Version |
| 61 | + --output text |
| 62 | + " |
| 63 | + console.log 'publishing layer... ok' |
| 64 | + |
| 65 | + [ LAYER_VERSION ] = stdout.split(/\n/) |
| 66 | + await exec " |
| 67 | + aws lambda add-layer-version-permission |
| 68 | + --layer-name #{LAYER_NAME} |
| 69 | + --version-number #{LAYER_VERSION} |
| 70 | + --statement-id sid1 |
| 71 | + --action lambda:GetLayerVersion |
| 72 | + --principal '*' |
| 73 | + " |
| 74 | + console.log 'publishing layer permissions... ok' |
| 75 | + |
| 76 | + console.log """ |
| 77 | + publish completed successfully! |
| 78 | + name: #{LAYER_NAME} |
| 79 | + version: #{LAYER_VERSION} |
| 80 | + """ |
| 81 | + |
| 82 | +task 'test', 'runs test', |
| 83 | +(option) -> |
| 84 | + await exec " |
| 85 | + rm -rf test/layer; |
| 86 | + unzip #{OUTPUT_DIR}/#{OUTPUT_FILE}.zip -d test/layer; |
| 87 | + |
| 88 | + cd test/lambda && npm ci && cd -; |
| 89 | + rm -f test/lambda/lambda.zip; |
| 90 | + zip -qyr test/lambda/lambda.zip test/lambda/index.coffee test/lambda/node_modules |
| 91 | + " |
| 92 | + console.log 'packaging test... ok' |
| 93 | + |
| 94 | + console.log 'running test...' |
| 95 | + { stdout, stderr } = await exec " |
| 96 | + docker run |
| 97 | + --rm |
| 98 | + -v $(PWD)/test/lambda:/var/task |
| 99 | + -v $(PWD)/test/layer:/opt |
| 100 | + lambci/lambda:provided |
| 101 | + index.handler |
| 102 | + " |
| 103 | + |
| 104 | + [ result ] = stdout.split(/\n/) |
| 105 | + expected = '{"statusCode":200,"body":"{\\"message\\":\\"CoffeeScript Serverless v1.0! Your function executed successfully!\\",\\"input\\":{}}"}' |
| 106 | + |
| 107 | + passed = result is expected |
| 108 | + console.log "running test... #{if passed then 'PASS' else 'FAIL'}" |
| 109 | + |
| 110 | + if not passed |
| 111 | + console.log stderr |
| 112 | + process.exit 1 |
| 113 | + |
| 114 | + await exec " |
| 115 | + rm -rf test/layer; |
| 116 | + rm -f test/lambda/lambda.zip; |
| 117 | + " |
| 118 | + console.log 'cleaning up... ok' |
0 commit comments