Class: Lono::S3::Bucket

Inherits:
Object
  • Object
show all
Extended by:
AwsServices
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::Util

#rollback_complete?, #stack_exists?, #testing_update?

Constructor Details

#initialize(options = {}) ⇒ Bucket

Returns a new instance of Bucket.



21
22
23
# File 'lib/lono/s3/bucket.rb', line 21

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

Class Method Details

.nameObject



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

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

  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



38
39
40
# File 'lib/lono/s3/bucket.rb', line 38

def bucket_name
  self.class.name
end

#createObject

Launches a cloudformation to create an s3 bucket



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

def create
  puts "Creating #{STACK_NAME} stack with the s3 bucket"
  cfn.create_stack(stack_name: STACK_NAME, template_body: template_body)
  status = ::Cfn::Status.new(STACK_NAME)
  status.wait
end

#deleteObject



65
66
67
68
69
70
71
# File 'lib/lono/s3/bucket.rb', line 65

def delete
  are_you_sure?

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

#deployObject



25
26
27
28
29
30
31
32
# File 'lib/lono/s3/bucket.rb', line 25

def deploy
  stack = find_stack
  if stack
    update
  else
    create
  end
end

#exist?Boolean

Returns:

  • (Boolean)


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

def exist?
  !!bucket_name
end

#find_stackObject



73
74
75
76
77
78
# File 'lib/lono/s3/bucket.rb', line 73

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

#showObject



42
43
44
45
46
47
48
# File 'lib/lono/s3/bucket.rb', line 42

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

#updateObject



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

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