Class: ConfigCommand

Inherits:
PangeaCommand show all
Defined in:
lib/pangea/cli/subcommands/config.rb

Instance Method Summary collapse

Instance Method Details

#bucket_exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/pangea/cli/subcommands/config.rb', line 45

def bucket_exist?(name)
  s3.bucket(name).exists?
end

#dynamodbObject



49
50
51
# File 'lib/pangea/cli/subcommands/config.rb', line 49

def dynamodb
  @dynamodb ||= Aws::DynamoDB::Client.new
end

#dynamodb_terraform_lock_spec(table_name) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/pangea/cli/subcommands/config.rb', line 53

def dynamodb_terraform_lock_spec(table_name)
  {
    table_name: table_name,
    key_schema: [
      {
        attribute_name: %(LockID),
        key_type: %(HASH)
      }
    ],
    attribute_definitions: [
      {
        attribute_name: %(LockID),
        attribute_type: %(S)
      }
    ],
    provisioned_throughput: {
      read_capacity_units: 5,
      write_capacity_units: 5
    },
  }
end

#helpObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pangea/cli/subcommands/config.rb', line 20

def help
  "  Usage: pangea config [OPTIONS] SUBCOMMAND\n\n  Arguments:\n    SUBCOMMAND  subcommand for config\n\n  Options:\n    -h, --help    Print usage\n  HELP\nend\n"

#run(argv) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/pangea/cli/subcommands/config.rb', line 75

def run(argv)
  case argv[1].to_s
  when %(show)
    config = Config.resolve_configurations
    puts JSON.pretty_generate(config)
  when %(init)
    puts "intializing pangea configuration..."
    config = Config.resolve_configurations

    config[:namespace].each_key do |ns_name|
      ns = config[:namespace][ns_name]
      ns.each_key do |ctx_name|
        ctx = ns[ctx_name]
        if ctx[:state_config][:terraform][:s3]
          ###################################################################
          # dynamodb table setup
          ###################################################################

          unless table_exists?(ctx[:state_config][:terraform][:s3][:dynamodb_table])
            begin
              result = dynamodb.create_table(
                dynamodb_terraform_lock_spec(
                  ctx[:state_config][:terraform][:s3][:dynamodb_table]
                )
              )
              puts "Created table. Status: #{result.table_description.table_status}"
            rescue Aws::DynamoDB::Errors::ServiceError => error
              puts error.message.to_s
            end
          end

          # dynamodb table setup
          
          ###################################################################
          # s3 bucket setup
          ###################################################################
          bucket_name = 
            ctx[:state_config][:terraform][:s3][:bucket]
          if bucket_exist?(bucket_name)
            puts "bucket already exists: #{bucket_name}"
          else
            s3.create_bucket(bucket: bucket_name)
          end

          # end s3 bucket setup

        end
      end
    end
  end
end

#s3Object



41
42
43
# File 'lib/pangea/cli/subcommands/config.rb', line 41

def s3
  @s3 ||= Aws::S3::Resource.new
end

#table_exists?(table_name) ⇒ Boolean

check if dynamodb table exists

Returns:

  • (Boolean)


33
34
35
36
37
38
39
# File 'lib/pangea/cli/subcommands/config.rb', line 33

def table_exists?(table_name)
  dynamodb.describe_table({ table_name: table_name })
  return true

rescue Aws::DynamoDB::Errors::ResourceNotFoundException
  return false
end