Class: Dome::State
Instance Method Summary collapse
- #bucket_names ⇒ Object
- #create_bucket(name) ⇒ Object
- #create_remote_state_bucket(bucket_name, state_file) ⇒ Object
- #enable_bucket_versioning(bucket_name) ⇒ Object
-
#initialize(environment) ⇒ State
constructor
A new instance of State.
- #list_buckets ⇒ Object
- #put_empty_object_in_bucket(bucket_name, key_name) ⇒ Object
- #s3_bucket_exists?(bucket_name) ⇒ Boolean
- #s3_client ⇒ Object
- #s3_state ⇒ Object
- #state_bucket_name ⇒ Object
- #state_file_name ⇒ Object
- #synchronise_s3_state(bucket_name, state_file_name) ⇒ 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
#bucket_names ⇒ Object
25 26 27 28 29 |
# File 'lib/dome/state.rb', line 25 def bucket_names bucket_names = list_buckets.buckets.map(&:name) puts "Found the following buckets: #{bucket_names}".colorize(:yellow) bucket_names end |
#create_bucket(name) ⇒ Object
35 36 37 38 39 |
# File 'lib/dome/state.rb', line 35 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(bucket_name, state_file) ⇒ Object
59 60 61 62 63 |
# File 'lib/dome/state.rb', line 59 def create_remote_state_bucket(bucket_name, state_file) create_bucket bucket_name enable_bucket_versioning bucket_name put_empty_object_in_bucket(bucket_name, state_file) end |
#enable_bucket_versioning(bucket_name) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/dome/state.rb', line 41 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 |
#list_buckets ⇒ Object
21 22 23 |
# File 'lib/dome/state.rb', line 21 def list_buckets s3_client.list_buckets end |
#put_empty_object_in_bucket(bucket_name, key_name) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/dome/state.rb', line 50 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
31 32 33 |
# File 'lib/dome/state.rb', line 31 def s3_bucket_exists?(bucket_name) bucket_names.find { |bucket| bucket == bucket_name } end |
#s3_client ⇒ Object
17 18 19 |
# File 'lib/dome/state.rb', line 17 def s3_client @s3_client ||= Aws::S3::Client.new end |
#s3_state ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/dome/state.rb', line 65 def s3_state if s3_bucket_exists?(state_bucket_name) synchronise_s3_state(state_bucket_name, state_file_name) else create_remote_state_bucket(state_bucket_name, state_file_name) end end |
#state_bucket_name ⇒ Object
9 10 11 |
# File 'lib/dome/state.rb', line 9 def state_bucket_name "#{@environment.project}-tfstate-#{@environment.environment}" end |
#state_file_name ⇒ Object
13 14 15 |
# File 'lib/dome/state.rb', line 13 def state_file_name "#{@environment.environment}-terraform.tfstate" end |
#synchronise_s3_state(bucket_name, state_file_name) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/dome/state.rb', line 73 def synchronise_s3_state(bucket_name, state_file_name) puts 'Synchronising the remote S3 state...' command = 'terraform remote config -backend=S3'\ " -backend-config='bucket=#{bucket_name}' -backend-config='key=#{state_file_name}'" = 'Something went wrong when synchronising the S3 state.' execute_command(command, ) end |