Class: Lono::S3::Bucket

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

Constant Summary collapse

STACK_NAME =
ENV['LONO_STACK_NAME'] || "lono"
@@name =
nil

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AwsServices

cfn, ec2, iam, s3, s3_presigner, s3_resource, sts

Methods included from AwsServices::Helper

#rollback_complete?, #testing_update?

Methods included from AwsServices::StackSet

#find_stack_set, #stack_set_exists?

Methods included from AwsServices::Stack

#stack_exists?

Constructor Details

#initialize(options = {}) ⇒ Bucket

Returns a new instance of Bucket.



28
29
30
# File 'lib/lono/s3/bucket.rb', line 28

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

Class Method Details

.check_aws_setup!Object



23
24
25
# File 'lib/lono/s3/bucket.rb', line 23

def check_aws_setup!
  AwsSetup.new.check!
end

.nameObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/lono/s3/bucket.rb', line 10

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

  check_aws_setup!

  stack = new.find_stack
  return unless stack

  resp = cfn.describe_stack_resources(stack_name: STACK_NAME)
  bucket = resp.stack_resources.find { |r| r.logical_resource_id == "Bucket" }
  @@name = bucket.physical_resource_id # actual bucket name
end

Instance Method Details

#bucket_nameObject



53
54
55
# File 'lib/lono/s3/bucket.rb', line 53

def bucket_name
  self.class.name
end

#createObject

Launches a cloudformation to create an s3 bucket



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/lono/s3/bucket.rb', line 66

def create
  puts "Creating #{STACK_NAME} stack with the s3 bucket"
  cfn.create_stack(
    stack_name: STACK_NAME,
    template_body: template_body,
    enable_termination_protection: true,
  )
  success = status.wait
  status.reset
  unless success
    puts "ERROR: Unable to create lono stack with managed s3 bucket".color(:red)
    exit 1
  end
end

#deleteObject



88
89
90
91
92
93
94
95
# File 'lib/lono/s3/bucket.rb', line 88

def delete
  are_you_sure?

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

#deployObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lono/s3/bucket.rb', line 32

def deploy
  stack = find_stack
  if rollback_complete?(stack)
    puts "Existing '#{STACK_NAME}' stack in ROLLBACK_COMPLETE state. Deleting stack before continuing."
    cfn.delete_stack(stack_name: STACK_NAME)
    status.wait
    status.reset
    stack = nil
  end

  if stack
    update
  else
    create
  end
end

#disable_termination_protectObject



97
98
99
100
101
102
# File 'lib/lono/s3/bucket.rb', line 97

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

#exist?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/lono/s3/bucket.rb', line 49

def exist?
  !!bucket_name
end

#find_stackObject



104
105
106
107
108
109
# File 'lib/lono/s3/bucket.rb', line 104

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

#showObject



57
58
59
60
61
62
63
# File 'lib/lono/s3/bucket.rb', line 57

def show
  if bucket_name
    puts "Lono bucket name: #{bucket_name}"
  else
    puts "Lono bucket does not exist yet."
  end
end

#statusObject



111
112
113
# File 'lib/lono/s3/bucket.rb', line 111

def status
  CfnStatus.new(STACK_NAME)
end

#updateObject



81
82
83
84
85
86
# File 'lib/lono/s3/bucket.rb', line 81

def update
  puts "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