hashicorptools 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/hashicorptools.gemspec +1 -1
- data/lib/hashicorptools/code_deploy.rb +68 -24
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 37a352203b9237ad1070e5fb6268a6ef6575a88d6bf9e016e62850a78b8f2fd6
|
4
|
+
data.tar.gz: 7f0bb691aff8809d32bd1ba4fd52eb9818d67d4afd5f99af7f8c08b9ae4f1b7c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 793c19ae4a5464798edbd1ef56b74bf11af0829a4795b47637ac5eb712bc9c34fe18dd60cfe742a24cfec52c6fa78382de02248825038cc5d10e9a057ced865c
|
7
|
+
data.tar.gz: 76c73130b8b95dcd318b5c25be3736e682673bc660c450321787dac1b14c2925dc6374693831b032e210b4a26c166ab74f11222e0ed42a2a9aa385d27f8e064d
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/hashicorptools.gemspec
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "hashicorptools".freeze
|
9
|
-
s.version = "0.
|
9
|
+
s.version = "0.4.0"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib".freeze]
|
@@ -7,16 +7,57 @@ require 'logger'
|
|
7
7
|
require 'thor'
|
8
8
|
|
9
9
|
module Hashicorptools
|
10
|
+
class RegionDeployment
|
11
|
+
attr_accessor :aws_region, :environment
|
12
|
+
|
13
|
+
def initialize(aws_region:, environment:)
|
14
|
+
@aws_region = aws_region
|
15
|
+
@environment = environment
|
16
|
+
end
|
17
|
+
|
18
|
+
def create_deployment(commit_id, commit_message)
|
19
|
+
client = Aws::CodeDeploy::Client.new(region: aws_region)
|
20
|
+
response = client.create_deployment({
|
21
|
+
application_name: application_name,
|
22
|
+
deployment_group_name: "#{application_name}-#{@environment}",
|
23
|
+
revision: {
|
24
|
+
revision_type: 'GitHub',
|
25
|
+
git_hub_location: {
|
26
|
+
repository: "controlshift/#{application_name}",
|
27
|
+
commit_id: commit_id
|
28
|
+
}
|
29
|
+
},
|
30
|
+
description: (commit_message || "commit #{commit_id}").slice(0,99)
|
31
|
+
})
|
32
|
+
output "created deployment #{response.deployment_id}"
|
33
|
+
output "https://console.aws.amazon.com/codedeploy/home?region=#{aws_region}#/deployments/#{response.deployment_id}"
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def application_name
|
39
|
+
raise "implement me"
|
40
|
+
end
|
41
|
+
|
42
|
+
def output(text)
|
43
|
+
puts "[#{aws_region}] #{text}"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
10
47
|
class CodeDeploy < Thor
|
48
|
+
AWS_REGION_US_EAST_1 = 'us-east-1'
|
11
49
|
|
12
50
|
desc 'deploy', 'deploy latest code to environment'
|
13
|
-
option :environment, :
|
51
|
+
option :environment, required: true
|
14
52
|
option :branch
|
15
|
-
option :
|
53
|
+
option :aws_regions, type: :array
|
16
54
|
option :commit
|
17
55
|
def deploy
|
18
56
|
g = Git.open('..')
|
19
57
|
|
58
|
+
# We set defaults (depending on environment) for aws_regions if not passed in
|
59
|
+
aws_regions = options[:aws_regions] || default_regions
|
60
|
+
|
20
61
|
# TODO restore defaulting branch to the default branch (and remove below check)
|
21
62
|
# once all the repos have the same default branch name of `main`
|
22
63
|
# Currently, `agra` is using `master` while other apps are using `main`.
|
@@ -33,36 +74,39 @@ module Hashicorptools
|
|
33
74
|
g.log.first
|
34
75
|
end
|
35
76
|
|
77
|
+
puts "Deploying to environment #{options[:environment]} - regions: #{aws_regions.join(', ')}
|
78
|
+
commit: #{commit.sha}
|
79
|
+
message: #{commit.message}"
|
80
|
+
|
81
|
+
puts "Deploying for regions: #{aws_regions}"
|
36
82
|
|
37
|
-
|
83
|
+
threads = []
|
84
|
+
aws_regions.each_slice(2) do |aws_regions_batch|
|
85
|
+
puts "Deploying for 2 regions: #{aws_regions_batch}"
|
86
|
+
aws_regions_batch.each do |aws_region|
|
87
|
+
thread = Thread.new{ region_deployment(aws_region).create_deployment(commit.sha, commit.message) }
|
88
|
+
threads.push(thread)
|
89
|
+
end
|
38
90
|
|
39
|
-
|
91
|
+
threads.each_with_index do |thread, index|
|
92
|
+
begin
|
93
|
+
thread.join
|
94
|
+
rescue Exception => e
|
95
|
+
# Don't quit whole program on exception in thread, just print exception and exit thread
|
96
|
+
puts "[#{aws_regions[index]}] EXCEPTION: #{e}"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
40
100
|
end
|
41
101
|
|
42
102
|
private
|
43
103
|
|
44
|
-
def
|
45
|
-
|
46
|
-
|
47
|
-
client = Aws::CodeDeploy::Client.new(region: options[:aws_region])
|
48
|
-
response = client.create_deployment({
|
49
|
-
application_name: application_name,
|
50
|
-
deployment_group_name: "#{application_name}-#{options[:environment]}",
|
51
|
-
revision: {
|
52
|
-
revision_type: 'GitHub',
|
53
|
-
git_hub_location: {
|
54
|
-
repository: "controlshift/#{application_name}",
|
55
|
-
commit_id: commit_id
|
56
|
-
}
|
57
|
-
},
|
58
|
-
description: (commit_message || "commit #{commit_id}").slice(0,99)
|
59
|
-
})
|
60
|
-
puts "created deployment #{response.deployment_id}"
|
61
|
-
puts "https://console.aws.amazon.com/codedeploy/home?region=#{options[:aws_region]}#/deployments/#{response.deployment_id}"
|
104
|
+
def region_deployment(aws_region)
|
105
|
+
RegionDeployment.new(aws_region: aws_region, environment: options[:environment])
|
62
106
|
end
|
63
107
|
|
64
|
-
def
|
65
|
-
|
108
|
+
def default_regions
|
109
|
+
[AWS_REGION_US_EAST_1]
|
66
110
|
end
|
67
111
|
end
|
68
112
|
end
|