-
Notifications
You must be signed in to change notification settings - Fork 0
/
build
executable file
·104 lines (88 loc) · 3.13 KB
/
build
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env ruby
require 'json'
require 'open-uri'
require 'optparse'
require 'securerandom'
require 'aws-sdk-lambda'
require 'aws-sdk-s3'
options = {
channel: 'stable',
force_rebuild: false,
force_update: false,
build_region: nil,
deploy_regions: nil,
s3_bucket: 'exapico-chrome-artifacts',
iam_instance_profile: 'ec2-chromium-builder',
deploy_s3_bucket: 'exapico-lambda-deploy-%',
}
OptionParser.new do |o|
o.on('-f', '--force-update') {|v| options[:force_update] = v }
o.on('-F', '--force-rebuild') {|v| options[:force_rebuild] = v }
o.on('-c', '--channel CHANNEL') {|v| options[:channel] = v }
o.on('-r REGION', '--build-region') {|v| options[:build_region] = v }
o.on('-d REGIONS', '--deploy-regions') {|v| (options[:deploy_regions] ||= []).concat(v.split(?,)) }
o.parse!(ARGV)
end
options[:build_region] ||= ENV.fetch('AWS_DEFAULT_REGION')
options[:deploy_regions] ||= [ENV.fetch('AWS_DEFAULT_REGION')]
versions = JSON.parse(open('https://omahaproxy.appspot.com/all.json?os=linux', &:read))
current_version = versions.find {|h| h['os'] == 'linux' }['versions'].find {|h| h['channel'] == options[:channel ] }['version']
puts <<EOF
- Channel: #{options[:channel]}
- Current: #{current_version}
EOF
layer_name = "headless-chromium-#{options[:channel]}"
s3 = Aws::S3::Resource.new(region: options[:build_region])
options[:deploy_regions].each do |deploy_region|
lambda = Aws::Lambda::Client.new(region: deploy_region)
layer_versions = [].tap do |l|
marker = nil
begin
res = lambda.list_layer_versions(layer_name: layer_name, marker: marker)
l.concat(res.layer_versions)
marker = res.next_marker
end while marker
end
unless layer_versions.empty?
deployed_version = layer_versions.max_by(&:version).description.split(?\s).first
end
puts "- Deployed (#{deploy_region}): #{deployed_version || 'none'}"
unless options[:force_update] || deployed_version != current_version
next
end
artifact_key = "#{options[:channel]}/headless-chromium-#{current_version}.zip"
artifact = s3.bucket(options[:s3_bucket]).object(artifact_key)
if options[:force_rebuild] || !artifact.exists?
cmd = %W[
packer build
-var channel=#{options[:channel]}
-var version=#{current_version}
-var s3_bucket=#{options[:s3_bucket]}
-var iam_instance_profile=#{options[:iam_instance_profile]}
#{File.join(__dir__, 'build.json')}
]
unless system(*cmd)
exit 1
end
options[:force_rebuild] = false
end
puts "Deploying to #{deploy_region}"
deploy_bucket = Aws::S3::Resource.new(region: deploy_region).bucket(options[:deploy_s3_bucket].sub(?%, deploy_region))
unless deploy_bucket.exists?
deploy_bucket.create(acl: 'private', create_bucket_configuration: {location_constraint: deploy_region})
end
obj = deploy_bucket.object(SecureRandom.hex(16)).tap {|o| o.copy_from(artifact) }
begin
res = lambda.publish_layer_version(
layer_name: layer_name,
description: "#{current_version}",
content: {
s3_bucket: obj.bucket.name,
s3_key: obj.key,
},
)
ensure
obj.delete
end
puts "Deployed #{res.layer_version_arn}"
end