Class: Dome::State
Instance Method Summary collapse
- #create_bucket(name) ⇒ Object
- #create_remote_state_bucket(state_bucket, state_file) ⇒ Object
- #enable_bucket_versioning(bucket_name) ⇒ Object
-
#initialize(environment) ⇒ State
constructor
A new instance of State.
- #put_empty_object_in_bucket(bucket_name, key_name) ⇒ Object
- #s3_bucket_exists?(bucket_name) ⇒ Boolean
- #s3_client ⇒ Object
- #s3_state ⇒ Object
- #state_bucket ⇒ Object
- #state_file ⇒ Object
- #synchronise_s3_state ⇒ Object
Methods included from Shell
Constructor Details
#initialize(environment) ⇒ State
Returns a new instance of State.
5 6 7 |
# File 'lib/dome/state.rb', line 5 def initialize(environment) @environment = environment end |
Instance Method Details
#create_bucket(name) ⇒ Object
27 28 29 30 31 |
# File 'lib/dome/state.rb', line 27 def create_bucket(name) s3_client.create_bucket(bucket: name, acl: 'private') rescue Aws::S3::Errors::BucketAlreadyExists raise 'The S3 bucket must be globally unique. See https://docs.aws.amazon.com/AmazonS3/latest/gsg/CreatingABucket.html'.colorize(:red) end |
#create_remote_state_bucket(state_bucket, state_file) ⇒ Object
51 52 53 54 55 |
# File 'lib/dome/state.rb', line 51 def create_remote_state_bucket(state_bucket, state_file) create_bucket state_bucket enable_bucket_versioning state_bucket put_empty_object_in_bucket(state_bucket, state_file) end |
#enable_bucket_versioning(bucket_name) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/dome/state.rb', line 33 def enable_bucket_versioning(bucket_name) puts 'Enabling versioning on the S3 bucket - http://docs.aws.amazon.com/AmazonS3/latest/dev/Versioning.html'.colorize(:green) s3_client.put_bucket_versioning(bucket: bucket_name, versioning_configuration: { mfa_delete: 'Disabled', status: 'Enabled' }) end |
#put_empty_object_in_bucket(bucket_name, key_name) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/dome/state.rb', line 42 def put_empty_object_in_bucket(bucket_name, key_name) puts "Putting an empty object with key: #{key_name} into bucket: #{bucket_name}".colorize(:green) s3_client.put_object( bucket: bucket_name, key: key_name, body: '' ) end |
#s3_bucket_exists?(bucket_name) ⇒ Boolean
21 22 23 24 25 |
# File 'lib/dome/state.rb', line 21 def s3_bucket_exists?(bucket_name) resp = s3_client.list_buckets resp.buckets.each { |bucket| return true if bucket.name == bucket_name } false end |
#s3_client ⇒ Object
17 18 19 |
# File 'lib/dome/state.rb', line 17 def s3_client @s3_client ||= Aws::S3::Client.new(@environment.aws_credentials) end |
#s3_state ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/dome/state.rb', line 57 def s3_state if s3_bucket_exists?(state_bucket) synchronise_s3_state else create_remote_state_bucket(state_bucket, state_file) end end |
#state_bucket ⇒ Object
9 10 11 |
# File 'lib/dome/state.rb', line 9 def state_bucket "#{@environment.team}-tfstate-#{@environment.environment}" end |
#state_file ⇒ Object
13 14 15 |
# File 'lib/dome/state.rb', line 13 def state_file "#{@environment.environment}-terraform.tfstate" end |
#synchronise_s3_state ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/dome/state.rb', line 65 def synchronise_s3_state puts 'Synchronising the remote S3 state...' command = 'terraform remote config -backend=S3'\ " -backend-config='bucket=#{state_bucket}' -backend-config='key=#{state_file}'" = 'Something went wrong when synchronising the S3 state.' execute_command(command, ) end |