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)


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

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

#dynamodbObject



53
54
55
# File 'lib/pangea/cli/subcommands/config.rb', line 53

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

#dynamodb_terraform_lock_spec(table_name) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pangea/cli/subcommands/config.rb', line 57

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



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

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

catch pangea config <subcommand>



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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/pangea/cli/subcommands/config.rb', line 85

def run(argv)
  case argv[1].to_s
  when %(show)
    config = Config.resolve_configurations
    puts JSON.pretty_generate(config)
  when %(plan)
    puts 'planning pangea configuration...'
    synth  = TerraformSynthesizer.new
    config = Config.resolve_configurations
    puts JSON.pretty_generate(config)

    config[:namespace].each_key do |ns_name|
      ns = config[:namespace][ns_name]
      ns.each_key do |ctx_name|
        ctx = ns[ctx_name]

        ###################################################################
        # setup modules
        ###################################################################

        module_dirs = %w[lib src test]
        modules     = ctx[:modules]

        modules.each_key do |mod_name|
          this_mod = modules[mod_name]

          if this_mod[:sandboxed]
            # TODO: setup sandboxed module
            nil
          elsif this_mod[:path]

            lib_dir   = File.join(this_mod[:path], %(lib))
            lib_files = Dir.glob(File.join(this_mod[:path], %(lib), %(**/*.rb)))

            system(%(mkdir -p #{lib_dir})) unless Dir.exist?(lib_dir)

            lib_files.each do |lib_file|
              synth.synthesize(
                File.read(
                  File.join(
                    this_mod[:path],
                    %(lib),
                    lib_file
                  )
                )
              )
            end

            # end process lib if exists
            # process src if exists

            src_dir   = File.join(this_mod[:path], %(src))
            src_files = Dir.glob(File.join(src_dir, %(**/*.rb)))

            system(%(mkdir -p #{src_dir})) unless Dir.exist?(src_dir)

            src_files.each do |src_file|
              synth.synthesize(File.read(File.join(src_file)))
            end
          end
        end
      end
      puts JSON.pretty_generate(synth.synthesis)
    end
  when %(init)
    puts 'intializing pangea configuration...'
    config = Config.resolve_configurations

    config[:namespace].each_key do |ns_name|
      ns = config[:namespace][ns_name]

      #####################################################################
      # process namespaces in configuraton
      #####################################################################

      ns.each_key do |ctx_name|
        ctx = ns[ctx_name]
        next unless 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 => e
            puts e.message.to_s
          end
        end

        # dynamodb table setup

        ###################################################################
        # s3 bucket setup
        ###################################################################

        bucket_name =
          ctx[:state_config][:terraform][:s3][:bucket]
        if bucket_exist?(bucket_name)
          nil
        else
          s3.create_bucket(bucket: bucket_name)
        end

        # end s3 bucket setup

        ###################################################################
        # setup directories
        ###################################################################

        base_dir    = File.join(ENV.fetch(%(HOME), nil), %(.pangea))
        context_dir = File.join(base_dir, ctx_name)
        synth_dir   = File.join(base_dir, synth_dir)

        system(%(mkdir -p #{context_dir})) unless Dir.exist?(context_dir)
        system(%(mkdir -p #{synth_dir})) unless Dir.exist?(synth_dir)

        # end setup directories
      end

      # end process namespaces in configuraton
    end
  end
end

#s3Object



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

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

#table_exists?(table_name) ⇒ Boolean

check if dynamodb table exists

Returns:

  • (Boolean)


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

def table_exists?(table_name)
  dynamodb.describe_table({ table_name: table_name })
  true
rescue Aws::DynamoDB::Errors::ResourceNotFoundException
  false
end