Class: Cody::Stack::Base

Inherits:
Object
  • Object
show all
Includes:
AwsServices, Status
Defined in:
lib/cody/stack/base.rb

Direct Known Subclasses

Create, Update

Instance Method Summary collapse

Methods included from Status

#status

Methods included from AwsServices

#cfn, #codebuild

Methods included from AwsServices::Helpers

#are_you_sure?, #inferred_project_name, #inferred_stack_name, #normalize_stack_name, #project_name_convention, #stack_exists?

Constructor Details

#initialize(options) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/cody/stack/base.rb', line 6

def initialize(options)
  @options = options
  @project_name = @options[:project_name] || inferred_project_name
  @stack_name = normalize_stack_name(options[:stack_name] || inferred_stack_name(@project_name))

  @full_project_name = project_name_convention(@project_name)
  @template = {
    "Description" => "CodeBuild Project: #{@full_project_name}",
    "Resources" => {}
  }
end

Instance Method Details

#runObject



18
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
# File 'lib/cody/stack/base.rb', line 18

def run
  options = @options.merge(
    project_name: @project_name,
    full_project_name: @full_project_name,
  )
  project_builder = Cody::Project.new(options)
  unless project_builder.exist?
    puts "ERROR: Cody project does not exist: #{project_builder.project_path}".color(:red)
    exit 1
    return
  end
  project = project_builder.run
  @template["Resources"].merge!(project)

  if project["CodeBuild"]["Properties"]["ServiceRole"] == {"Ref"=>"IamRole"}
    role = Cody::Role.new(options).run
    @template["Resources"].merge!(role)
  end

  schedule = Cody::Schedule.new(options).run
  @template["Resources"].merge!(schedule) if schedule

  template_path = "/tmp/codebuild.yml"
  FileUtils.mkdir_p(File.dirname(template_path))
  IO.write(template_path, YAML.dump(@template))
  puts "Generated CloudFormation template at #{template_path.color(:green)}"

  return if @options[:noop]
  puts "Deploying stack #{@stack_name.color(:green)} with CodeBuild project #{@full_project_name.color(:green)}"

  begin
    perform
    url_info
    return unless @options[:wait]
    status.wait
    exit 2 unless status.success?
  rescue Aws::CloudFormation::Errors::ValidationError => e
    if e.message.include?("No updates") # No updates are to be performed.
      puts "WARN: #{e.message}".color(:yellow)
    else
      puts "ERROR ValidationError: #{e.message}".color(:red)
      exit 1
    end
  end
end