Class: AppCommand::DeployDeprecated

Inherits:
Convoy::ActionCommand::Base
  • Object
show all
Defined in:
lib/routes/deploy_deprecated.rb

Constant Summary collapse

VALID_STACK_STATUSES =
%w(CREATE_COMPLETE UPDATE_COMPLETE UPDATE_COMPLETE_CLEANUP_IN_PROGRESS)
MATCHERS_KEY_MAP =
{
    'Parameters' => %w(ParameterKey ParameterValue),
    'Project' => nil, # Special matcher used for system values.
    'Tags' => %w(Key Value),
    'Outputs' => %w(OutputKey OutputValue)
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_deployments(project, profile, silent: false) ⇒ Object

Makes a call to AWS and gets deployments.

Returns:

  • Hash



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/routes/deploy_deprecated.rb', line 340

def self.get_deployments(project, profile, silent: false)
    calls = {}
    deployments = {}
    project_id = project[Blufin::Projects::PROJECT_ID]
    project_name = project[Blufin::Projects::PROJECT]
    project[Blufin::Projects::TARGETS].each do |environment, data|
        region = data[:region]
        stack = data[:stack]
        deployment_id = "#{region}-#{environment}-#{stack}"
        raise RuntimeError, "Duplicate deployment ID: #{deployment_id}. This should never happen." if deployments.has_key?(deployment_id)
        deployments[deployment_id] = nil
        calls[region] = [] unless calls.has_key?(region)
        calls[region] << {
            :deployment_id => deployment_id,
            :environment => environment,
            :region => region,
            :stack => stack
        }
    end
    profile = profile[App::AWSProfile::PROFILE]
    threads = []
    semaphore = Mutex.new
    puts unless silent
    calls.each do |region, data|
        data.each do |deployment|
            deployment_id = deployment[:deployment_id]
            sleep(0.01)
            threads << Thread.new {
                cmd = <<TEMPLATE
aws resourcegroupstaggingapi get-resources --resource-type-filters cloudformation --tag-filters '[{"Key":"Project","Values":["#{project_name}"]},{"Key":"ProjectId","Values":["#{project_id}"]},{"Key":"Environment","Values":["#{deployment[:environment]}"]},{"Key":"DeploymentStack","Values":["#{deployment[:stack]}"]}]' --region #{region}#{App::AWS::get_profile_for_cli}
TEMPLATE
                App::AWSOutputter::output_cli_command(cmd) unless silent
                res_one = `#{cmd}`
                begin
                    if res_one.strip != ''
                        data = JSON.parse(res_one)
                        if data.has_key?('ResourceTagMappingList')
                            stacks = data['ResourceTagMappingList']
                            if stacks.is_a?(Array) && stacks.any?
                                stacks_yaml = stacks.to_yaml.split("\n")
                                stacks_yaml.shift
                                stacks.each do |x|
                                    raise RuntimeError, "Deployments Array is missing key: #{deployment_id}" unless deployments.has_key?(deployment_id)
                                    raise RuntimeError, 'Stack is missing property: ResourceARN' unless x.has_key?('ResourceARN')
                                    stack_arn = x['ResourceARN']
                                    stack_name = stack_arn.to_s.strip.gsub(/^arn:aws:cloudformation:(.)*:[0-9]{10,14}:stack\//, '')
                                    stack_name = stack_name.to_s.strip.gsub(/\/[a-z0-9\-]+$/, '')
                                    cmd = <<TEMPLATE
aws cloudformation describe-stacks --stack-name #{stack_name} --region #{region}#{App::AWS::get_profile_for_cli}
TEMPLATE
                                    res_two = `#{cmd}`
                                    begin
                                        semaphore.synchronize do
                                            if res_two.strip != ''
                                                data = JSON.parse(res_two)
                                                if data.is_a?(Hash)
                                                    if data.has_key?('Stacks')
                                                        res_stacks = data['Stacks']
                                                        if res_stacks.length > 1
                                                            puts res_stacks.to_yaml
                                                            raise RuntimeError, "Response had more than one item for 'Stacks'."
                                                        end
                                                        deployments[deployment_id] = [] unless deployments[deployment_id].is_a?(Array)
                                                        deployments[deployment_id] << res_stacks[0]
                                                    end
                                                end
                                            end
                                        end
                                    rescue => e
                                        puts res_two.inspect
                                        raise RuntimeError, "JSON parsing (for: #{cmd}) failed:\n\n#{e.message}"
                                    end
                                end
                            end
                        end
                    end
                rescue => e
                    puts res_one.inspect
                    raise RuntimeError, "JSON parsing (for: #{cmd}) failed:\n\n#{e.message}"
                end
            }
        end
    end
    sleep(0.1)
    # Display spinner while waiting for threads to finish.
    Blufin::Terminal::execute_proc("AWS \xe2\x80\x94 Fetching CloudFormation Stack(s) for: #{App::AWSOutputter::render_selection(project_name, project_id)}", Proc.new {
        threads.each { |thread| thread.join }
    }, verbose: !silent)
    puts unless silent
    deployments
end

Instance Method Details

#convert_commands(original_commands, target, profile, region, deployment_type) ⇒ Object

Takes the commands from project.yml and replaces all the dynamic stuff.

Raises:

  • (RuntimeError)


434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/routes/deploy_deprecated.rb', line 434

def convert_commands(original_commands, target, profile, region, deployment_type)
    final_commands = []
    raise RuntimeError, "Invalid deployment_type: #{deployment_type}" unless Blufin::Projects::VALID_TYPES.include?(deployment_type)
    original_commands['Commands'].each do |command|
        matches = command.scan(/{{[A-Za-z0-9:]+}}/)
        matches.each do |match|
            match_clean = match.gsub(/^{{/, '').gsub(/}}$/, '')
            ms = match_clean.split(':')
            # Make sure the matcher is compliant :)
            if ms.length != 2 || !MATCHERS_KEY_MAP.keys.include?(ms[0])
                puts
                puts "  \x1B[38;5;240m#{command}\x1B[0m"
                Blufin::Terminal::error("#{Blufin::Terminal::format_highlight(original_commands['Id'])} \xe2\x80\x94 Expected matcher to contain exactly ONE colon (:), instead got \xe2\x86\x92 #{ms.length - 1}", match_clean, true) unless ms.length == 2
                Blufin::Terminal::error("#{Blufin::Terminal::format_highlight(original_commands['Id'])} \xe2\x80\x94 Invalid matcher key: #{Blufin::Terminal::format_invalid(ms[0])}. Valid keys are:", MATCHERS_KEY_MAP.keys, true) unless MATCHERS_KEY_MAP.keys.include?(ms[0])
            end
            extracted_value = nil
            # Get value by looping through target array.
            if ms[0] == 'Project'
                case ms[1]
                    when 'Environment'
                        extracted_value = target[:environment]
                    when 'Profile'
                        extracted_value = profile['Profile']
                    else
                        Blufin::Terminal::error("Unrecognized #{Blufin::Terminal::format_highlight('Project')} matcher: #{Blufin::Terminal::format_invalid(v)}", "The defined matcher is currently not being handled in the AWX #{Blufin::Terminal::format_directory('deploy.rb')} file.", true)
                end
            else
                k = MATCHERS_KEY_MAP[ms[0]][0]
                v = MATCHERS_KEY_MAP[ms[0]][1]
                target[ms[0]].each do |item|
                    if item[k] == ms[1]
                        extracted_value = item[v]
                        break
                    end
                end
            end
            # Replace matcher/placeholder with extracted value.
            if extracted_value.is_a?(String)
                command = command.gsub(match, extracted_value)
            else
                Blufin::Terminal::error("#{Blufin::Terminal::format_highlight(original_commands['Id'])} \xe2\x80\x94 Unable to resolve string value for matcher: #{Blufin::Terminal::format_invalid(match_clean)}", command, false)
                puts target.to_yaml
                puts
                exit
            end
        end
        # If this is an AWS command we need to add --region and --profile flags.
        command = "#{command.strip} --region #{region}#{App::AWS::get_profile_for_cli}" if command =~ /^aws/i
        final_commands << command
    end
    final_commands
end

#deploy_lambda(project_name, project, profile) ⇒ Object

Lambda Deployments. # @return void



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
# File 'lib/routes/deploy_deprecated.rb', line 104

def deploy_lambda(project_name, project, profile)

    project_id = project[Blufin::Projects::PROJECT_ID]

    deployment_options = []
    project[Blufin::Projects::TARGETS].each do |environment, data|
        region = data[:region]
        data[:environment] = environment
        deployment_id = "#{region}-#{environment}"
        deployment_options << {
            :text => deployment_id,
            :value => data
        }
    end

    puts

    # Select which Stack(s) to deploy to.
    targets = []
    targets = Blufin::Terminal::prompt_multi_select('Select Target Environment(s):', deployment_options) until targets.any?

    deploy_script, deploy_commands = get_deploy_commands(project)

    # Get final commands (with region/environment correctly inserted).
    dcmd_final = {}
    targets.each do |target|
        converted_commands = convert_commands(deploy_commands, target, profile, target[:region], Blufin::Projects::TYPE_LAMBDA)
        dcmd_final[Digest::SHA2.hexdigest(converted_commands.inspect.to_s)] = {
            :description => project[Blufin::Projects::PROJECT_ID],
            :commands => converted_commands
        }
    end

    # Build deploy-descriptions array (after targets are selected).
    deploy_descriptions = []
    targets.each { |x| deploy_descriptions << "#{project[Blufin::Projects::PROJECT_ID]} \xe2\x80\x94 #{Blufin::Terminal::format_highlight(x[:environment])}" }

    # Show prompt and perform deployment.
    perform_deployment({}, nil, dcmd_final, deploy_descriptions, deploy_script['Path'], project, project_id, project_name, verbose: true, async: false, pbl: false)

end

#deploy_ui(project_name, project, profile) ⇒ Object

UI Deployments. # @return void



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
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/routes/deploy_deprecated.rb', line 148

def deploy_ui(project_name, project, profile)

    project_id = project[Blufin::Projects::PROJECT_ID]
    has_valid_option = false

    # Make call to AWS to get available stacks.
    deployments = get_deployments(project, profile, silent: false)

    # Build deployment option(s).
    deployment_options = []
    deployments.each do |deployment_id, deployment_arr|
        if deployment_arr.is_a?(Array)
            deployment_arr.each do |deployment|
                if deployment.is_a?(Hash)
                    disabled = nil
                    deployment_option = {
                        :text => deployment['Description'],
                        :value => deployment
                    }
                    if deployment.has_key?('StackStatus') && !VALID_STACK_STATUSES.include?(deployment['StackStatus'])
                        disabled = deployment['StackStatus']
                    else
                        has_valid_option = true
                    end
                    deployment_option[:disabled] = disabled unless disabled.nil?
                    deployment_options << deployment_option
                end
            end
        end
    end

    # If no active stacks, bomb-out!
    if !has_valid_option || !deployment_options.any?
        error_output = []
        deployment_options.each do |dep_opt|
            error_output << "#{dep_opt[:text]} \xe2\x80\x94 \x1B[38;5;196m#{dep_opt[:disabled]}"
        end
        Blufin::Terminal::error("Cannot #{Blufin::Terminal::format_action('deploy')} because there currently are no available/active Stack(s).", error_output.any? ? error_output : nil, true, false)
    end

    # Select which Stack(s) to deploy to.
    targets = []
    targets = Blufin::Terminal::prompt_multi_select('Select Target Environment(s):', deployment_options) until targets.any?

    # Get build/deploy command(s).
    build_script, build_commands = get_build_commands(project)
    deploy_script, deploy_commands = get_deploy_commands(project)

    bcmd_final = {}
    dcmd_final = {}
    # Loop targets and start building/deploying stuff.
    targets.each do |target|
        # Extract region from StackId, IE: arn:aws:cloudformation:us-west-2:255332876236:stack/ui-s3-route53-cloudfront...
        raise RuntimeError, 'Could not find key: StackId' unless target.has_key?('StackId')
        region = target['StackId'].gsub(/^arn:aws:cloudformation:/, '').split(':')[0]
        build_commands_converted = convert_commands(build_commands, target, profile['Profile'], region, Blufin::Projects::TYPE_UI)
        deploy_commands_converted = convert_commands(deploy_commands, target, profile['Profile'], region, Blufin::Projects::TYPE_UI)
        # Convert script to Hash (to identify duplicates).
        bc_key = Digest::SHA2.hexdigest(build_commands_converted.inspect.to_s)
        dc_key = Digest::SHA2.hexdigest(deploy_commands_converted.inspect.to_s)
        # Add to build/deploy hashes only if not duplicate.
        bcmd_final[bc_key] = {
            :description => target['Description'],
            :commands => build_commands_converted,
        } unless bcmd_final.has_key?(bc_key)
        dcmd_final[dc_key] = {
            :description => target['Description'],
            :commands => deploy_commands_converted,
        } unless dcmd_final.has_key?(dc_key)
    end

    deploy_descriptions = []
    dcmd_final.each { |x| deploy_descriptions << x[1][:description] }

    # Show prompt and perform deployment.
    perform_deployment(bcmd_final, build_script, dcmd_final, deploy_descriptions, deploy_script['Path'], project, project_id, project_name)

end

#executeObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/routes/deploy_deprecated.rb', line 15

def execute

    begin

        @opts = command_options
        @args = arguments

        @projects = nil
        @project = nil
        @profile = App::AWSProfile::get_profile

        Blufin::Projects::init(@profile['Projects'])

        opts_validate
        opts_routing

    rescue => e

        Blufin::Terminal::print_exception(e)

    end

end

#get_build_commands(project) ⇒ Object

Select which build script to use. Displays prompt if multiple.

Returns:

  • Hash

Raises:

  • (RuntimeError)


290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/routes/deploy_deprecated.rb', line 290

def get_build_commands(project)
    build_scripts = project['Build']
    build_script_text = 'Select Build Script:'
    puts
    if build_scripts.length > 1
        build_script_options = []
        build_scripts.each do |script|
            build_script_options << {
                :text => script['Script'],
                :value => script
            }
        end
        build_script = Blufin::Terminal::prompt_select(build_script_text, build_script_options)
    else
        build_script = build_scripts[0]
        puts Blufin::Terminal::display_prompt_text(build_script_text, build_scripts[0]['Script'])
    end

    # Extract commands (and throw Exceptions if none exist as this should never happen at this point in the script).
    build_commands = Blufin::Projects::get_scripts[Blufin::Projects::BUILD_SCRIPTS][build_script['Script']]
    raise RuntimeError, "Invalid build_commands: #{build_commands['Commands'].inspect}" unless build_commands['Commands'].is_a?(Array) && build_commands['Commands'].any?
    return build_script, build_commands
end

#get_deploy_commands(project) ⇒ Object

Select which Deploy Script to use. Displays prompt if multiple.

Returns:

  • Hash

Raises:

  • (RuntimeError)


316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/routes/deploy_deprecated.rb', line 316

def get_deploy_commands(project)
    deploy_scripts = project['Deploy']
    deploy_script_text = 'Select Deploy Script:'
    puts
    if deploy_scripts.length > 1
        deploy_script_options = []
        deploy_scripts.each do |script|
            deploy_script_options << {
                :text => script['Script'],
                :value => script
            }
        end
        deploy_script = Blufin::Terminal::prompt_select(deploy_script_text, deploy_script_options)
    else
        deploy_script = deploy_scripts[0]
        puts Blufin::Terminal::display_prompt_text(deploy_script_text, deploy_scripts[0]['Script'])
    end
    deploy_commands = Blufin::Projects::get_scripts[Blufin::Projects::DEPLOY_SCRIPTS][deploy_script['Script']]
    raise RuntimeError, "Invalid deploy_commands: #{deploy_commands['Commands'].inspect}" unless deploy_commands['Commands'].is_a?(Array) && deploy_commands['Commands'].any?
    return deploy_script, deploy_commands
end

#opts_routingObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
95
96
97
98
99
100
# File 'lib/routes/deploy_deprecated.rb', line 51

def opts_routing

    # Select project.
    system('clear')
    Blufin::Terminal::custom('DEPLOY', 55, 'Select the Project you want to deploy:')
    project_name = Blufin::Terminal::prompt_select('Select Project:', @projects.keys)
    puts
    project_id = Blufin::Terminal::prompt_select('Select Project ID (codebase):', @projects[project_name].keys)

    @project = @projects[project_name][project_id]

    [# If missing required properties, bomb-out!
        Blufin::Projects::BUILD,
        Blufin::Projects::DEPLOY,
        Blufin::Projects::SERVERS,
        Blufin::Projects::TARGETS
    ].each do |required_property|
        unless @project.has_key?(required_property)

            # Lambda projects do not need Build properties.
            next if [Blufin::Projects::BUILD].include?(required_property) && @project[Blufin::Projects::TYPE] == Blufin::Projects::TYPE_LAMBDA

            project_yaml = @project.to_yaml.split("\n")
            project_yaml.shift
            has_content = false
            project_yaml.each { |x| has_content = true if x.strip.length > 0 }
            Blufin::Terminal::warning("This project cannot be deployed because the #{Blufin::Terminal::format_highlight(required_property)} property is #{Blufin::Terminal::format_invalid('EMPTY')}.", has_content ? project_yaml : nil)
            exit
        end
    end

    case @project[Blufin::Projects::TYPE]
        when Blufin::Projects::TYPE_ALEXA

            # TODO - Finish this.
            raise RuntimeError, 'Not yet implemented!'

        when Blufin::Projects::TYPE_API

            # TODO - Finish this.
            raise RuntimeError, 'Not yet implemented!'

        when Blufin::Projects::TYPE_LAMBDA
            deploy_lambda(project_name, @project, @profile)
        when Blufin::Projects::TYPE_UI
            deploy_ui(project_name, @project, @profile)
        else
            raise RuntimeError, "Unhandled project type: #{@project[Blufin::Projects::TYPE]}"
    end
end

#opts_validateObject

Raises:

  • (RuntimeError)


39
40
41
42
43
44
45
46
47
48
49
# File 'lib/routes/deploy_deprecated.rb', line 39

def opts_validate

    # TODO - Remove this route once new deploy script is working.
    raise RuntimeError, 'This route is deprecated and broken!'

    @projects = Blufin::Projects::get_projects

    # Throw Exception if there are no deployments. The script should never get this far.
    raise RuntimeError, 'There are currently no deployments. In this case, the deploy option should be hidden from the main menu.' unless @projects.any?

end

#perform_deployment(bcmd_final, build_script, dcmd_final, deploy_descriptions, deploy_path, project, project_id, project_name, verbose: false, async: true, pbl: true) ⇒ Object

Final confirmation prompt before the deploys run.

Returns:

  • void



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/routes/deploy_deprecated.rb', line 229

def perform_deployment(bcmd_final, build_script, dcmd_final, deploy_descriptions, deploy_path, project, project_id, project_name, verbose: false, async: true, pbl: true)
    if Blufin::Terminal::prompt_yes_no("You're about to deploy the following: #{App::AWSOutputter::render_selection(project_name, project_id)}", deploy_descriptions, 'Deploy Project(s)?')
        begin
            # Execute the build(s).
            if @opts[:skip_build] || build_script.nil?
                # Only display this message if we specifically skipped the build.
                Blufin::Terminal::info('Skipping build...', nil, false) if @opts[:skip_build]
            else
                bcmd_final.each do |x|
                    path = "#{File.expand_path(project[Blufin::Projects::REPO_ROOT])}/#{Blufin::Strings::remove_surrounding_slashes(project[Blufin::Projects::REPO_PATH])}/#{Blufin::Strings::remove_surrounding_slashes(build_script['Path'])}".gsub(/\/$/, '')
                    x[1][:commands].each do |command|
                        raise RuntimeError, "Command failed: #{command}" unless Blufin::Terminal::command(command, path, pbl: true, tbl: true)
                    end
                end
            end
            # Execute the deploys(s).
            dcmd_descriptions = []
            dcmd_threads = []
            dcmd_semaphore = Mutex.new
            dcmd_final.each_with_index do |x, idx1|
                path = "#{File.expand_path(project[Blufin::Projects::REPO_ROOT])}/#{Blufin::Strings::remove_surrounding_slashes(project[Blufin::Projects::REPO_PATH])}/#{Blufin::Strings::remove_surrounding_slashes(deploy_path)}".gsub(/\/$/, '')
                if async
                    sleep(0.01)
                    dcmd_threads << Thread.new {
                        dcmd_descriptions << x[1][:description]
                        dcmd_semaphore.synchronize do
                            x[1][:commands].each do |command|
                                App::AWSOutputter::output_cli_command(command, path)
                            end
                        end
                        x[1][:commands].each do |command|
                            raise RuntimeError, "Command failed: #{command}" unless Blufin::Terminal::execute(command, path, verbose: verbose)
                        end
                    }
                else
                    x[1][:commands].each_with_index do |command, idx2|
                        # This basically fixes spacing.
                        last_in_loop = idx1 == (dcmd_final.length - 1) && idx2 == (x[1][:commands].length - 1)
                        tbl = !pbl && last_in_loop ? false : true
                        raise RuntimeError, "Command failed: #{command}" unless Blufin::Terminal::command(command, path, pbl: true, tbl: tbl)[0]
                    end
                end
            end
            sleep(0.1)
            puts
            if async
                # Display spinner while waiting for threads to finish.
                Blufin::Terminal::execute_proc('Waiting for deploy(s) to finish...', Proc.new {
                    dcmd_threads.each { |thread| thread.join }
                })
            end
            # Success message.
            Blufin::Terminal::success('Deploy(s) were successful.', nil, pbl)
        rescue => e
            Blufin::Terminal::error('Something went wrong.', e.message, true, false)
        end
    end
end