Class: Ufo::Docker::State::S3

Inherits:
Base
  • Object
show all
Extended by:
Memoist
Includes:
AwsServices
Defined in:
lib/ufo/docker/state/s3.rb

Instance Method Summary collapse

Methods included from AwsServices

#acm, #applicationautoscaling, #aws_options, #cfn, #cloudwatchlogs, #ec2, #ecr, #ecs, #elb, #s3, #ssm_client, #waf_client

Methods included from AwsServices::Concerns

#find_stack, #find_stack_resources, #stack_resources, #status, #task_definition_arns

Methods inherited from Base

#initialize

Methods included from Utils::Pretty

#pretty_home, #pretty_path, #pretty_time

Methods included from Utils::Logging

#logger

Constructor Details

This class inherits a constructor from Ufo::Docker::State::Base

Instance Method Details

#appObject

ufo docker base is called before Ufo.config is loaded. This ensures it is loaded



48
49
50
51
# File 'lib/ufo/docker/state/s3.rb', line 48

def app
  Ufo.config
  Ufo.app
end

#current_dataObject

TODO: edge cases: no bucket, no permission



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ufo/docker/state/s3.rb', line 26

def current_data
  resp = s3.get_object(bucket: s3_bucket, key: s3_key)
  YAML.load(resp.body)
rescue Aws::S3::Errors::NoSuchKey
  logger.debug "WARN: s3 key does not exist: #{s3_key}"
  {}
rescue Aws::S3::Errors::NoSuchBucket
  logger.error "ERROR: S3 bucket does not exist to store state: #{s3_bucket}".color(:red)
  logger.error <<~EOL
      Please double check the config.

      See: http://ufoships.com/docs/config/state/

  EOL
  exit 1
end

#ensure_s3_bucket_existObject



73
74
75
76
77
# File 'lib/ufo/docker/state/s3.rb', line 73

def ensure_s3_bucket_exist
  bucket = Ufo::S3::Bucket.new
  return if bucket.exist?
  bucket.deploy
end

#readObject



6
7
8
# File 'lib/ufo/docker/state/s3.rb', line 6

def read
  current_data
end

#s3_bucketObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ufo/docker/state/s3.rb', line 53

def s3_bucket
  state = Ufo.config.state
  if state.bucket
    state.bucket
  elsif state.managed
    ensure_s3_bucket_exist
    Ufo::S3::Bucket.name
  else
    logger.error "ERROR: No s3 bucket to store state".color(:red)
    logger.error <<~EOL
      UFO needs a bucket to store the built docker base image.

      Configure an existing bucket or enable UFO to create a bucket.

      See: http://ufoships.com/docs/config/state/
    EOL
    exit 1
  end
end

#s3_keyObject



43
44
45
# File 'lib/ufo/docker/state/s3.rb', line 43

def s3_key
  "ufo/state/#{app}/#{Ufo.env}/data.yml"
end

#updateObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ufo/docker/state/s3.rb', line 10

def update
  data = current_data
  data["base_image"] = @base_image

  # write data to s3
  body = YAML.dump(data)
  s3.put_object(
    body: body,
    bucket: s3_bucket,
    key: s3_key,
  )
  logger.info "Updated base image in s3://#{s3_bucket}/#{s3_key}"
  logger.info "    #{@base_image}".color(:green)
end