Class: Pangea::S3State

Inherits:
State
  • Object
show all
Defined in:
lib/pangea-orchestrator/state.rb

Overview

Manage S3 state

Instance Method Summary collapse

Methods inherited from State

#initialize

Constructor Details

This class inherits a constructor from Pangea::State

Instance Method Details

#create_bucket(name:, region:, check: true) ⇒ Object

Creates an S3 bucket for state storage. If ‘check` is true, the method first checks if the bucket exists.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/pangea-orchestrator/state.rb', line 68

def create_bucket(name:, region:, check: true)
  s3 = Aws::S3::Client.new(region: region)

  if check
    begin
      # Attempt to check if the bucket exists
      s3.head_bucket(bucket: name)
      # Bucket exists; nothing to do.
      return
    rescue Aws::S3::Errors::NotFound, Aws::S3::Errors::NoSuchBucket
      # Bucket does not exist; proceed with creation.
    rescue Aws::S3::Errors::Forbidden => e
      # The bucket exists but is inaccessible (perhaps owned by someone else).
      puts "Bucket '#{name}' exists but is not accessible: #{e.message}"
      return
    end
  end

  begin
    s3.create_bucket(bucket: name)
    puts "Bucket '#{name}' created successfully!"
  rescue Aws::S3::Errors::BucketAlreadyOwnedByYou
    puts "Bucket '#{name}' already exists and is owned by you."
  rescue Aws::S3::Errors::ServiceError => e
    puts "Failed to create bucket: #{e.message}"
  end
end

#create_dynamodb_table_for_lock(name:, region:, check: true) ⇒ Object

Creates a DynamoDB table to be used for state locking. Optional parameters allow you to customize the table name, AWS region, and whether to check for existing table.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pangea-orchestrator/state.rb', line 19

def create_dynamodb_table_for_lock(name:, region:, check: true)
  dynamodb = Aws::DynamoDB::Client.new(region: region)

  if check
    begin
      # Check if the table already exists
      dynamodb.describe_table(table_name: name)
      puts "DynamoDB table '#{name}' already exists, skipping creation."
      return
    rescue Aws::DynamoDB::Errors::ResourceNotFoundException
      # Table does not exist; proceed with creation.
    rescue Aws::DynamoDB::Errors::ServiceError => e
      puts "Failed to check DynamoDB table existence: #{e.message}"
      return
    end
  end

  begin
    dynamodb.create_table(
      {
        table_name: name,
        attribute_definitions: [
          {
            attribute_name: 'LockID',
            attribute_type: 'S'
          }
        ],
        key_schema: [
          {
            attribute_name: 'LockID',
            key_type: 'HASH'
          }
        ],
        provisioned_throughput: {
          read_capacity_units: 1,
          write_capacity_units: 1
        }
      }
    )
    puts "DynamoDB table '#{name}' created successfully!"
  rescue Aws::DynamoDB::Errors::ResourceInUseException
    puts "DynamoDB table '#{name}' already exists."
  rescue Aws::DynamoDB::Errors::ServiceError => e
    puts "Failed to create DynamoDB table: #{e.message}"
  end
end