Class: Ufo::S3::Bucket

Inherits:
Object
  • Object
show all
Extended by:
Memoist, AwsServices
Includes:
AwsServices, Utils::Logging
Defined in:
lib/ufo/s3/bucket.rb

Constant Summary collapse

STACK_NAME =
ENV['UFO_STACK_NAME'] || "ufo"
@@name =
nil

Class Method Summary collapse

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_resources, #stack_resources, #task_definition_arns

Methods included from Utils::Logging

#logger

Constructor Details

#initialize(options = {}) ⇒ Bucket

Returns a new instance of Bucket.



9
10
11
# File 'lib/ufo/s3/bucket.rb', line 9

def initialize(options={})
  @options = options
end

Class Method Details

.nameObject



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/ufo/s3/bucket.rb', line 160

def name
  return @@name if @@name # only memoize once bucket has been created

  AwsSetup.new.check!

  stack = new.find_stack
  return unless stack

  stack_resources = find_stack_resources(STACK_NAME)
  bucket = stack_resources.find { |r| r.logical_resource_id == "Bucket" }
  @@name = bucket.physical_resource_id # actual bucket name
end

Instance Method Details

#bucket_nameObject



34
35
36
# File 'lib/ufo/s3/bucket.rb', line 34

def bucket_name
  self.class.name
end

#createObject

Launches a cloudformation to create an s3 bucket



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ufo/s3/bucket.rb', line 47

def create
  logger.info "Creating #{STACK_NAME} stack for s3 bucket to store state"
  cfn.create_stack(
    stack_name: STACK_NAME,
    template_body: template_body,
    enable_termination_protection: true,
  )
  success = status.wait
  unless success
    logger.info "ERROR: Unable to create UFO stack with managed s3 bucket".color(:red)
    exit 1
  end
end

#deleteObject



68
69
70
71
72
73
74
75
# File 'lib/ufo/s3/bucket.rb', line 68

def delete
  are_you_sure?

  logger.info "Deleting #{STACK_NAME} stack with the s3 bucket"
  disable_termination_protection
  empty_bucket!
  cfn.delete_stack(stack_name: STACK_NAME)
end

#deployObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ufo/s3/bucket.rb', line 13

def deploy
  stack = find_stack
  if rollback.complete?
    logger.info "Existing '#{STACK_NAME}' stack in ROLLBACK_COMPLETE state. Deleting stack before continuing."
    disable_termination_protection
    cfn.delete_stack(stack_name: STACK_NAME)
    status.wait
    stack = nil
  end

  if stack
    update
  else
    create
  end
end

#disable_termination_protectionObject



77
78
79
80
81
82
# File 'lib/ufo/s3/bucket.rb', line 77

def disable_termination_protection
  cfn.update_termination_protection(
    stack_name: STACK_NAME,
    enable_termination_protection: false,
  )
end

#exist?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/ufo/s3/bucket.rb', line 30

def exist?
  !!bucket_name
end

#find_stackObject



84
85
86
87
88
89
# File 'lib/ufo/s3/bucket.rb', line 84

def find_stack
  resp = cfn.describe_stacks(stack_name: STACK_NAME)
  resp.stacks.first
rescue Aws::CloudFormation::Errors::ValidationError
  nil
end

#showObject



38
39
40
41
42
43
44
# File 'lib/ufo/s3/bucket.rb', line 38

def show
  if bucket_name
    logger.info "UFO bucket name: #{bucket_name}"
  else
    logger.info "UFO bucket does not exist yet."
  end
end

#statusObject



91
92
93
# File 'lib/ufo/s3/bucket.rb', line 91

def status
  CfnStatus.new(STACK_NAME)
end

#updateObject



61
62
63
64
65
66
# File 'lib/ufo/s3/bucket.rb', line 61

def update
  logger.info "Updating #{STACK_NAME} stack with the s3 bucket"
  cfn.update_stack(stack_name: STACK_NAME, template_body: template_body)
rescue Aws::CloudFormation::Errors::ValidationError => e
  raise unless e.message.include?("No updates are to be performed")
end