Class: Dome::State

Inherits:
Object
  • Object
show all
Includes:
Shell
Defined in:
lib/dome/state.rb

Instance Method Summary collapse

Methods included from Shell

#execute_command

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
32
33
# File 'lib/dome/state.rb', line 27

def create_bucket(name)
  begin
    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
end

#create_remote_state_bucket(state_bucket, state_file) ⇒ Object



53
54
55
56
57
# File 'lib/dome/state.rb', line 53

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



35
36
37
38
39
40
41
42
# File 'lib/dome/state.rb', line 35

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



44
45
46
47
48
49
50
51
# File 'lib/dome/state.rb', line 44

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

Returns:

  • (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_clientObject



17
18
19
# File 'lib/dome/state.rb', line 17

def s3_client
  @s3_client ||= Aws::S3::Client.new(@environment.aws_credentials)
end

#s3_stateObject



59
60
61
62
63
64
65
# File 'lib/dome/state.rb', line 59

def s3_state
  if s3_bucket_exists?(state_bucket)
    synchronise_s3_state
  else
    create_remote_state_bucket(state_bucket, state_file)
  end
end

#state_bucketObject



9
10
11
# File 'lib/dome/state.rb', line 9

def state_bucket
  "#{@environment.team}-tfstate-#{@environment.environment}"
end

#state_fileObject



13
14
15
# File 'lib/dome/state.rb', line 13

def state_file
  "#{@environment.environment}-terraform.tfstate"
end

#synchronise_s3_stateObject



67
68
69
70
71
72
73
# File 'lib/dome/state.rb', line 67

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}'"
  failure_message = 'Something went wrong when synchronising the S3 state.'
  execute_command(command, failure_message)
end