domed-city 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/dome +22 -31
- data/dome.gemspec +17 -15
- data/lib/dome.rb +4 -4
- data/lib/dome/environment.rb +183 -90
- data/lib/dome/version.rb +1 -1
- metadata +49 -21
- data/lib/dome/terraform.rb +0 -96
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b7beb507c76c82d0ff5159d7f40f25724c1a7e0
|
4
|
+
data.tar.gz: 03fd70a24686a3db7c51357fa9b4cf69d3349d77
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 25aede6e6d7021984c375c43de5703535565c26b05c869264bb98ce033ca1c7a51b0361b738853f1916189ecfce955ab1a08638ecadcc705edee8a171cd294dc
|
7
|
+
data.tar.gz: 8220849b29ffa820316ad69bc3b46df3fd578e6c6cf20f6660282d5ff549f8664f8d4a151709e122cf64a3e3a70b06a25f2398bc18ba42c8bb1d9d75653ba71d
|
data/bin/dome
CHANGED
@@ -1,28 +1,9 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
require '
|
5
|
-
require "dome"
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'dome'
|
6
5
|
|
7
|
-
|
8
|
-
# task :bootstrap_s3_state do
|
9
|
-
#
|
10
|
-
# desc "creates the TF plan in a local file"
|
11
|
-
# task :plan do
|
12
|
-
#
|
13
|
-
# desc "creates the TF plan, to destroy resources, in a local file"
|
14
|
-
# task :plandestroy do
|
15
|
-
#
|
16
|
-
# desc "applies a TF plan"
|
17
|
-
# task :apply do
|
18
|
-
#
|
19
|
-
# desc "applies a destructive TF immediately"
|
20
|
-
# task :destroy do
|
21
|
-
#
|
22
|
-
# desc "updates the TF binary dependencies"
|
23
|
-
# task :update do
|
24
|
-
|
25
|
-
opts = Trollop::options do
|
6
|
+
opts = Trollop.options do
|
26
7
|
version Dome::VERSION
|
27
8
|
banner <<-EOS
|
28
9
|
Dome wraps the Terraform API and performs useful stuff.
|
@@ -32,18 +13,28 @@ Usage:
|
|
32
13
|
where [commands] are:
|
33
14
|
EOS
|
34
15
|
|
35
|
-
opt :plan,
|
36
|
-
opt :apply,
|
37
|
-
opt :
|
38
|
-
opt :
|
39
|
-
opt :update, "TODO (maybe?)"
|
16
|
+
opt :plan, 'Creates a Terraform plan'
|
17
|
+
opt :apply, 'Applies a Terraform plan'
|
18
|
+
opt :plan_destroy, 'Creates a destructive Terraform plan'
|
19
|
+
opt :state, 'Synchronises the Terraform state'
|
40
20
|
end
|
41
21
|
|
42
22
|
if opts[:plan]
|
43
|
-
Dome::Environment.
|
44
|
-
|
23
|
+
@dome = Dome::Environment.new
|
24
|
+
@dome.validate_environment
|
25
|
+
@dome.plan
|
45
26
|
elsif opts[:apply]
|
46
|
-
|
27
|
+
@dome = Dome::Environment.new
|
28
|
+
@dome.validate_environment
|
29
|
+
@dome.apply
|
30
|
+
elsif opts[:plan_destroy]
|
31
|
+
@dome = Dome::Environment.new
|
32
|
+
@dome.validate_environment
|
33
|
+
@dome.plan_destroy
|
34
|
+
elsif opts[:state]
|
35
|
+
@dome = Dome::Environment.new
|
36
|
+
@dome.validate_environment
|
37
|
+
@dome.bootstrap_s3_state
|
47
38
|
else
|
48
|
-
Trollop
|
39
|
+
Trollop.educate
|
49
40
|
end
|
data/dome.gemspec
CHANGED
@@ -4,25 +4,27 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'dome/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'domed-city'
|
8
8
|
spec.version = Dome::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
9
|
+
spec.authors = ['Ben Snape']
|
10
|
+
spec.email = ['ben.snape@itv.com']
|
11
11
|
|
12
|
-
spec.summary =
|
13
|
-
spec.homepage =
|
12
|
+
spec.summary = 'A simple Terraform API wrapper and helpers for ITV.'
|
13
|
+
spec.homepage = 'https://github.com/ITV/dome'
|
14
14
|
|
15
15
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
-
spec.bindir =
|
17
|
-
spec.executables = spec.files.grep(%r{^
|
18
|
-
spec.require_paths = [
|
16
|
+
spec.bindir = 'bin'
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ['lib']
|
19
19
|
|
20
|
-
spec.add_development_dependency
|
21
|
-
spec.add_development_dependency
|
22
|
-
spec.add_development_dependency
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.9'
|
21
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
22
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
23
|
+
spec.add_development_dependency 'rubocop', '~> 0.34.2'
|
23
24
|
|
24
|
-
spec.add_dependency 'trollop'
|
25
|
-
spec.add_dependency 'aws-profile_parser'
|
26
|
-
spec.add_dependency 'aws-sdk'
|
27
|
-
spec.add_dependency 'colorize'
|
25
|
+
spec.add_dependency 'trollop', '~> 2.1'
|
26
|
+
spec.add_dependency 'aws-profile_parser', '~> 0.0.4'
|
27
|
+
spec.add_dependency 'aws-sdk', '~> 2.1.33'
|
28
|
+
spec.add_dependency 'colorize', '~> 0.7.7'
|
29
|
+
spec.add_dependency 'jmespath', '~> 1.1.3'
|
28
30
|
end
|
data/lib/dome.rb
CHANGED
data/lib/dome/environment.rb
CHANGED
@@ -1,132 +1,225 @@
|
|
1
1
|
module Dome
|
2
2
|
class Environment
|
3
|
+
def initialize
|
4
|
+
@environment = Dir.pwd.split('/')[-1]
|
5
|
+
@account = Dir.pwd.split('/')[-2]
|
6
|
+
@team = 'deirdre'
|
7
|
+
@tfstate_bucket = "#{@team}-tfstate-#{@environment}"
|
8
|
+
@tfstate_s3_obj = "#{@environment}-terraform.tfstate"
|
9
|
+
@varfile = 'params/env.tfvars'
|
10
|
+
@plan = "plans/#{@account}-#{@environment}-plan.tf"
|
11
|
+
@state_file = "state-files/#{@environment}-terraform.tfstate"
|
12
|
+
end
|
3
13
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
account = current_dir.to_s.split('/')[-2]
|
8
|
-
|
9
|
-
valid_accounts = ['deirdre-dev', 'deirdre-prd']
|
10
|
-
valid_env_nonprod = ['infradev', 'sit', 'qa', 'stg']
|
11
|
-
valid_env_prod = ['infraprd', 'prd']
|
12
|
-
|
13
|
-
if account
|
14
|
-
if is_valid_account?(account)
|
15
|
-
puts "found valid account #{account}, moving on ...".colorize(:green)
|
16
|
-
else
|
17
|
-
invalid_account_notification
|
18
|
-
end
|
19
|
-
else
|
20
|
-
fail "\n#{account} is no a valid account\n\n".colorize(:red)
|
21
|
-
end
|
22
|
-
|
23
|
-
if environment
|
24
|
-
if is_valid_env?(environment)
|
25
|
-
puts "found valid environment #{environment}, moving on ...".colorize(:green)
|
26
|
-
else
|
27
|
-
invalid_environment_notification
|
28
|
-
end
|
29
|
-
else
|
30
|
-
fail "\n #{environment} is not a valid environment for the account: #{account}\n\n".colorize(:red)
|
31
|
-
end
|
14
|
+
# --------------------------------------------------------------
|
15
|
+
# Environment stuff
|
16
|
+
# --------------------------------------------------------------
|
32
17
|
|
33
|
-
|
34
|
-
|
18
|
+
def valid_accounts
|
19
|
+
%w(deirdre-dev deirdre-prd)
|
35
20
|
end
|
36
21
|
|
37
|
-
def
|
38
|
-
|
22
|
+
def valid_env_nonprod
|
23
|
+
%w(infradev sit qa stg)
|
39
24
|
end
|
40
25
|
|
41
|
-
def
|
42
|
-
|
26
|
+
def valid_env_prod
|
27
|
+
%w(infraprd prd)
|
43
28
|
end
|
44
29
|
|
45
|
-
def
|
46
|
-
|
47
|
-
|
30
|
+
def validate_environment
|
31
|
+
puts "Environment: #{@environment}"
|
32
|
+
puts "Account: #{@account}"
|
33
|
+
|
34
|
+
invalid_account_message(account) unless valid_account? @account
|
35
|
+
invalid_environment_message(account, environment) unless valid_environment?(@account, @environment)
|
36
|
+
|
37
|
+
set_aws_credentials(@account)
|
48
38
|
end
|
49
39
|
|
50
|
-
def
|
51
|
-
accounts = AWS::ProfileParser.new
|
40
|
+
def set_aws_credentials(account)
|
52
41
|
begin
|
53
|
-
@aws_creds =
|
54
|
-
rescue
|
55
|
-
raise "No credentials found for #{account}"
|
42
|
+
@aws_creds = AWS::ProfileParser.new.get(account)
|
43
|
+
rescue RuntimeError
|
44
|
+
raise "No credentials found for account: '#{account}'."
|
56
45
|
end
|
57
46
|
ENV['AWS_ACCESS_KEY_ID'] = @aws_creds[:access_key_id]
|
58
47
|
ENV['AWS_SECRET_ACCESS_KEY'] = @aws_creds[:secret_access_key]
|
59
48
|
ENV['AWS_DEFAULT_REGION'] = @aws_creds[:region]
|
60
49
|
end
|
61
50
|
|
62
|
-
def
|
63
|
-
valid_accounts.include?
|
51
|
+
def valid_account?(account)
|
52
|
+
valid_accounts.include? account
|
64
53
|
end
|
65
54
|
|
66
|
-
def
|
55
|
+
def valid_environment?(account, environment)
|
67
56
|
if valid_accounts[valid_accounts.index(account)] == 'deirdre-dev'
|
68
|
-
valid_env_nonprod.include?
|
57
|
+
valid_env_nonprod.include? environment
|
69
58
|
elsif valid_accounts[valid_accounts.index(account)] == 'deirdre-prd'
|
70
|
-
valid_env_prod.include?
|
59
|
+
valid_env_prod.include? environment
|
71
60
|
end
|
72
61
|
end
|
73
62
|
|
74
|
-
def
|
75
|
-
puts "\n#{account} is not a valid account
|
76
|
-
puts "
|
77
|
-
|
78
|
-
puts
|
79
|
-
puts
|
63
|
+
def invalid_account_message(account)
|
64
|
+
puts "\n'#{account}' is not a valid account.\n".colorize(:red)
|
65
|
+
puts "Valid accounts are: #{valid_accounts}."
|
66
|
+
puts "\nEither:"
|
67
|
+
puts '1. Set your .aws/config to one of the valid accounts above.'
|
68
|
+
puts '2. Ensure you are running this from the correct directory.'
|
80
69
|
exit 1
|
81
70
|
end
|
82
71
|
|
83
|
-
def
|
84
|
-
puts "\n#{environment} is not a valid environment
|
85
|
-
|
86
|
-
|
87
|
-
p valid_env_nonprod
|
88
|
-
elsif account == 'deirdre-prd'
|
89
|
-
p valid_env_prod
|
90
|
-
end
|
72
|
+
def invalid_environment_message(account, environment)
|
73
|
+
puts "\n'#{environment}' is not a valid environment for the account: '#{account}'.\n".colorize(:red)
|
74
|
+
(account == 'deirdre-dev') ? env = valid_env_nonprod : env = valid_env_prod
|
75
|
+
puts "Valid environments are: #{env}"
|
91
76
|
exit 1
|
92
77
|
end
|
93
78
|
|
94
|
-
|
95
|
-
|
96
|
-
|
79
|
+
# --------------------------------------------------------------
|
80
|
+
# Terraform commands
|
81
|
+
# --------------------------------------------------------------
|
82
|
+
|
83
|
+
def plan
|
84
|
+
puts "current dir: #{Dir.pwd}"
|
85
|
+
delete_terraform_directory
|
86
|
+
delete_plan_file
|
87
|
+
install_terraform_modules
|
88
|
+
fetch_s3_state
|
89
|
+
create_plan
|
90
|
+
end
|
91
|
+
|
92
|
+
def apply
|
93
|
+
command = "terraform apply #{@plan}"
|
94
|
+
failure_message = 'something went wrong when applying the TF plan'
|
95
|
+
execute_command(command, failure_message)
|
96
|
+
end
|
97
|
+
|
98
|
+
def create_plan
|
99
|
+
command = "terraform plan -module-depth=1 -refresh=true -out=#{@plan} -var-file=#{@varfile}"
|
100
|
+
failure_message = 'something went wrong when creating the TF plan'
|
101
|
+
execute_command(command, failure_message)
|
102
|
+
end
|
103
|
+
|
104
|
+
def delete_terraform_directory
|
105
|
+
puts 'Deleting older terraform module cache dir ...'.colorize(:green)
|
106
|
+
terraform_directory = '.terraform'
|
107
|
+
puts "About to delete directory: #{terraform_directory}"
|
108
|
+
FileUtils.rm_rf '.terraform/'
|
109
|
+
end
|
110
|
+
|
111
|
+
def delete_plan_file
|
112
|
+
puts 'Deleting older terraform plan ...'.colorize(:green)
|
113
|
+
puts "About to delete: #{@plan}"
|
114
|
+
FileUtils.rm_f @plan
|
115
|
+
end
|
116
|
+
|
117
|
+
def plan_destroy
|
118
|
+
delete_terraform_directory
|
119
|
+
delete_plan_file
|
120
|
+
install_terraform_modules
|
121
|
+
create_destroy_plan
|
122
|
+
end
|
123
|
+
|
124
|
+
def create_destroy_plan
|
125
|
+
command = "terraform plan -destroy -module-depth=1 -out=#{@plan} -var-file=#{@varfile}"
|
126
|
+
failure_message = 'something went wrong when creating the TF plan'
|
127
|
+
execute_command(command, failure_message)
|
128
|
+
end
|
129
|
+
|
130
|
+
def install_terraform_modules
|
131
|
+
command = 'terraform get -update=true'
|
132
|
+
failure_message = 'something went wrong when pulling remote TF modules'
|
133
|
+
execute_command(command, failure_message)
|
134
|
+
end
|
135
|
+
|
136
|
+
# S3 stuff
|
137
|
+
|
138
|
+
def s3_client
|
139
|
+
@s3_client ||= Aws::S3::Client.new(@aws_creds)
|
140
|
+
end
|
141
|
+
|
142
|
+
def s3_bucket_exists?(tfstate_bucket)
|
143
|
+
resp = s3_client.list_buckets
|
97
144
|
resp.buckets.each { |bucket| return true if bucket.name == tfstate_bucket }
|
98
145
|
false
|
99
146
|
end
|
100
147
|
|
101
|
-
def
|
102
|
-
puts "initial boostrap of the S3 bucket".colorize(:green)
|
103
|
-
s3_client = Aws::S3::Client.new(@aws_creds)
|
148
|
+
def create_bucket(name)
|
104
149
|
begin
|
105
|
-
s3_client.create_bucket(
|
106
|
-
|
107
|
-
|
108
|
-
})
|
109
|
-
rescue Aws::S3::Errors::BucketAlreadyExists => e
|
110
|
-
puts "type of exception #{e.class}".colorize(:red)
|
111
|
-
puts "backtrace for this exception:".colorize(:red)
|
112
|
-
puts e.backtrace
|
113
|
-
puts "\nmake sure the bucket name is unique per whole AWS S3 service, see here for docs on uniqueness https://docs.aws.amazon.com/AmazonS3/latest/gsg/CreatingABucket.html\n\n".colorize(:red)
|
114
|
-
exit 1
|
150
|
+
s3_client.create_bucket(bucket: name, acl: 'private')
|
151
|
+
rescue Aws::S3::Errors::BucketAlreadyExists
|
152
|
+
raise 'The S3 bucket must be globally unique. See https://docs.aws.amazon.com/AmazonS3/latest/gsg/CreatingABucket.html'.colorize(:red)
|
115
153
|
end
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
154
|
+
end
|
155
|
+
|
156
|
+
def enable_bucket_versioning(bucket_name)
|
157
|
+
puts 'Enabling versioning on the S3 bucket - http://docs.aws.amazon.com/AmazonS3/latest/dev/Versioning.html'.colorize(:green)
|
158
|
+
s3_client.put_bucket_versioning(bucket: bucket_name,
|
159
|
+
versioning_configuration: {
|
160
|
+
mfa_delete: 'Disabled',
|
161
|
+
status: 'Enabled'
|
123
162
|
})
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
163
|
+
end
|
164
|
+
|
165
|
+
def put_empty_object_in_bucket(bucket_name, key_name)
|
166
|
+
puts "Putting an empty object with key: #{key_name} into bucket: #{bucket_name}".colorize(:green)
|
167
|
+
s3_client.put_object(
|
168
|
+
bucket: bucket_name,
|
169
|
+
key: key_name,
|
170
|
+
body: ''
|
171
|
+
)
|
172
|
+
end
|
173
|
+
|
174
|
+
def create_remote_state_bucket(tfstate_bucket, tfstate_s3_obj)
|
175
|
+
create_bucket tfstate_bucket
|
176
|
+
enable_bucket_versioning tfstate_bucket
|
177
|
+
put_empty_object_in_bucket(tfstate_bucket, tfstate_s3_obj)
|
178
|
+
end
|
179
|
+
|
180
|
+
def bootstrap_s3_state
|
181
|
+
if s3_bucket_exists?(@tfstate_bucket)
|
182
|
+
synchronise_s3_state
|
183
|
+
else
|
184
|
+
create_remote_state_bucket(@tfstate_bucket, @tfstate_s3_obj)
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
def synchronise_s3_state
|
189
|
+
puts 'Synchronising the remote S3 state...'
|
190
|
+
# not clear for me if the -state in the below command matters
|
191
|
+
command = 'terraform remote config'\
|
192
|
+
' -backend=S3'\
|
193
|
+
" -backend-config='bucket=#{@tfstate_bucket}' -backend-config='key=#{@tfstate_s3_obj}'"\
|
194
|
+
" -state=#{@state_file}"
|
195
|
+
failure_message = 'something went wrong when creating the S3 state'
|
196
|
+
execute_command(command, failure_message)
|
197
|
+
end
|
198
|
+
|
199
|
+
def synchronise_s3_state_setup
|
200
|
+
puts "Setting up the initial terraform S3 state in the S3 bucket: #{@tfstate_bucket.colorize(:green)} for account: #{@account.colorize(:green)} and environment: #{@environment.colorize(:green)} ..."
|
201
|
+
command = 'terraform remote config'\
|
202
|
+
' -backend=S3'\
|
203
|
+
" -backend-config='bucket=#{@tfstate_bucket}' -backend-config='key=#{@tfstate_s3_obj}'"
|
204
|
+
failure_message = 'something went wrong when creating the S3 state'
|
205
|
+
execute_command(command, failure_message)
|
206
|
+
end
|
207
|
+
|
208
|
+
def fetch_s3_state
|
209
|
+
command = 'terraform remote config -backend=S3'\
|
210
|
+
" -backend-config='bucket=#{@tfstate_bucket}' -backend-config='key=#{@tfstate_s3_obj}'"
|
211
|
+
failure_message = 'something went wrong when fetching the S3 state'
|
212
|
+
execute_command(command, failure_message)
|
213
|
+
end
|
214
|
+
|
215
|
+
# --------------------------------------------------------------
|
216
|
+
# Misc.
|
217
|
+
# --------------------------------------------------------------
|
218
|
+
|
219
|
+
def execute_command(command, failure_message)
|
220
|
+
puts "About to execute command: #{command}"
|
221
|
+
success = system command
|
222
|
+
puts failure_message unless success
|
130
223
|
end
|
131
224
|
end
|
132
225
|
end
|
data/lib/dome/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: domed-city
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Snape
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,66 +52,95 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.34.2
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.34.2
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: trollop
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
58
72
|
requirements:
|
59
|
-
- - "
|
73
|
+
- - "~>"
|
60
74
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
75
|
+
version: '2.1'
|
62
76
|
type: :runtime
|
63
77
|
prerelease: false
|
64
78
|
version_requirements: !ruby/object:Gem::Requirement
|
65
79
|
requirements:
|
66
|
-
- - "
|
80
|
+
- - "~>"
|
67
81
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
82
|
+
version: '2.1'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: aws-profile_parser
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
72
86
|
requirements:
|
73
|
-
- - "
|
87
|
+
- - "~>"
|
74
88
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
89
|
+
version: 0.0.4
|
76
90
|
type: :runtime
|
77
91
|
prerelease: false
|
78
92
|
version_requirements: !ruby/object:Gem::Requirement
|
79
93
|
requirements:
|
80
|
-
- - "
|
94
|
+
- - "~>"
|
81
95
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
96
|
+
version: 0.0.4
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: aws-sdk
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
86
100
|
requirements:
|
87
|
-
- - "
|
101
|
+
- - "~>"
|
88
102
|
- !ruby/object:Gem::Version
|
89
|
-
version:
|
103
|
+
version: 2.1.33
|
90
104
|
type: :runtime
|
91
105
|
prerelease: false
|
92
106
|
version_requirements: !ruby/object:Gem::Requirement
|
93
107
|
requirements:
|
94
|
-
- - "
|
108
|
+
- - "~>"
|
95
109
|
- !ruby/object:Gem::Version
|
96
|
-
version:
|
110
|
+
version: 2.1.33
|
97
111
|
- !ruby/object:Gem::Dependency
|
98
112
|
name: colorize
|
99
113
|
requirement: !ruby/object:Gem::Requirement
|
100
114
|
requirements:
|
101
|
-
- - "
|
115
|
+
- - "~>"
|
102
116
|
- !ruby/object:Gem::Version
|
103
|
-
version:
|
117
|
+
version: 0.7.7
|
104
118
|
type: :runtime
|
105
119
|
prerelease: false
|
106
120
|
version_requirements: !ruby/object:Gem::Requirement
|
107
121
|
requirements:
|
108
|
-
- - "
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 0.7.7
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: jmespath
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 1.1.3
|
132
|
+
type: :runtime
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
109
137
|
- !ruby/object:Gem::Version
|
110
|
-
version:
|
138
|
+
version: 1.1.3
|
111
139
|
description:
|
112
140
|
email:
|
113
141
|
- ben.snape@itv.com
|
114
|
-
executables:
|
142
|
+
executables:
|
143
|
+
- dome
|
115
144
|
extensions: []
|
116
145
|
extra_rdoc_files: []
|
117
146
|
files:
|
@@ -126,7 +155,6 @@ files:
|
|
126
155
|
- dome.gemspec
|
127
156
|
- lib/dome.rb
|
128
157
|
- lib/dome/environment.rb
|
129
|
-
- lib/dome/terraform.rb
|
130
158
|
- lib/dome/version.rb
|
131
159
|
homepage: https://github.com/ITV/dome
|
132
160
|
licenses: []
|
data/lib/dome/terraform.rb
DELETED
@@ -1,96 +0,0 @@
|
|
1
|
-
module Dome
|
2
|
-
class Terraform
|
3
|
-
def self.plan
|
4
|
-
puts "--- running task :plan".colorize(:light_cyan)
|
5
|
-
set_env
|
6
|
-
Dir.chdir(CURRENT_ENV_DIR)
|
7
|
-
puts "purging older terraform module cache dir ...".colorize(:green)
|
8
|
-
purge_terraform
|
9
|
-
puts "purging older terraform plan ...".colorize(:green)
|
10
|
-
FileUtils.rm_f(PLAN)
|
11
|
-
puts "updating terraform external modules ...".colorize(:green)
|
12
|
-
Rake::Task['tf:update'].invoke
|
13
|
-
|
14
|
-
cmd = "terraform remote config"\
|
15
|
-
" -backend=S3"\
|
16
|
-
" -backend-config='bucket=#{tfstate_bucket}' -backend-config='key=#{tfstate_s3_obj}'"
|
17
|
-
puts "Command to execute: #{cmd}"
|
18
|
-
bool = system(cmd)
|
19
|
-
fail "something went wrong when fetching the S3 state" unless bool
|
20
|
-
cmd = "terraform plan -module-depth=1 -refresh=true -out=#{PLAN} #{@varfile}"
|
21
|
-
puts "\nCommand to execute: \n #{cmd}\n\n"
|
22
|
-
bool = system(cmd)
|
23
|
-
fail "something went wrong when creating the TF plan" unless bool
|
24
|
-
end
|
25
|
-
|
26
|
-
def self.apply
|
27
|
-
puts "--- running task :apply".colorize(:light_cyan)
|
28
|
-
set_env
|
29
|
-
cd_to_tf_dir
|
30
|
-
set_env
|
31
|
-
cmd = "terraform apply #{PLAN}"
|
32
|
-
puts "\n Command to execute: #{cmd}\n\n"
|
33
|
-
bool = system(cmd)
|
34
|
-
fail "something went wrong when applying the TF plan" unless bool
|
35
|
-
end
|
36
|
-
|
37
|
-
def self.plan_destroy
|
38
|
-
puts "--- running task :plandestroy".colorize(:light_cyan)
|
39
|
-
set_env
|
40
|
-
Dir.chdir(CURRENT_ENV_DIR)
|
41
|
-
puts "purging older terraform module cache dir ...".colorize(:green)
|
42
|
-
purge_terraform
|
43
|
-
puts "purging older terraform plan ...".colorize(:green)
|
44
|
-
FileUtils.rm_f(PLAN)
|
45
|
-
puts "updating terraform external modules ...".colorize(:green)
|
46
|
-
Rake::Task['tf:update'].invoke
|
47
|
-
p PLAN
|
48
|
-
cmd = "terraform plan -destroy -module-depth=1 -out=#{PLAN} #{@varfile}"
|
49
|
-
puts "\nCommand to execute: \n #{cmd}\n\n"
|
50
|
-
bool = system(cmd)
|
51
|
-
fail "something went wrong when creating the TF plan" unless bool
|
52
|
-
end
|
53
|
-
|
54
|
-
def self.destroy
|
55
|
-
puts "--- running task :destroy".colorize(:light_cyan)
|
56
|
-
puts "here is the destroy plan that terraform will carry out"
|
57
|
-
plan_destroy
|
58
|
-
apply
|
59
|
-
end
|
60
|
-
|
61
|
-
def self.update
|
62
|
-
puts "--- running task :update".colorize(:light_cyan)
|
63
|
-
cmd = "terraform get -update=true"
|
64
|
-
puts "\nCommand to execute: \n #{cmd}\n\n"
|
65
|
-
bool = system(cmd)
|
66
|
-
fail "something went wrong when pulling remote TF modules" unless bool
|
67
|
-
end
|
68
|
-
|
69
|
-
def self.bootstrap_s3_state
|
70
|
-
set_env
|
71
|
-
if s3_bucket_exists?(tfstate_bucket)
|
72
|
-
puts "Bootstrap attempted, but config for account: #{ACCOUNT.colorize(:green)} and environment: #{ENVIRONMENT.colorize(:green)} already exists in S3 bucket: #{tfstate_bucket.colorize(:green)}"
|
73
|
-
puts "synchronising the remote S3 state ..."
|
74
|
-
cd_to_tf_dir
|
75
|
-
cmd = "terraform remote config"\
|
76
|
-
" -backend=S3"\
|
77
|
-
" -backend-config='bucket=#{tfstate_bucket}' -backend-config='key=#{tfstate_s3_obj}'"\
|
78
|
-
" -state=#{STATE_FILE_DIR}/#{REMOTE_STATE_FILE}"
|
79
|
-
# still not clear for me if the -state in the above cmd matters
|
80
|
-
puts "Command to execute: #{cmd}"
|
81
|
-
bool = system(cmd)
|
82
|
-
fail "something went wrong when creating the S3 state" unless bool
|
83
|
-
else
|
84
|
-
s3_tf_create_remote_state_bucket(tfstate_bucket, tfstate_s3_obj)
|
85
|
-
puts "\nsetting up the initial terraform S3 state in the S3 bucket: #{tfstate_bucket.colorize(:green)} for account:#{ACCOUNT.colorize(:green)} and environment:#{ENVIRONMENT.colorize(:green)} ..."
|
86
|
-
cd_to_tf_dir
|
87
|
-
cmd = "terraform remote config"\
|
88
|
-
" -backend=S3"\
|
89
|
-
" -backend-config='bucket=#{tfstate_bucket}' -backend-config='key=#{tfstate_s3_obj}'"
|
90
|
-
puts "Command to execute: #{cmd}"
|
91
|
-
bool = system(cmd)
|
92
|
-
fail "something went wrong when creating the S3 state" unless bool
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|