Class: Cody::CLI::Start

Inherits:
Object
  • Object
show all
Includes:
AwsServices
Defined in:
lib/cody/cli/start.rb

Instance Method Summary collapse

Methods included from AwsServices

#cfn, #codebuild

Methods included from AwsServices::Helpers

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

Constructor Details

#initialize(options) ⇒ Start

Returns a new instance of Start.



5
6
7
8
9
# File 'lib/cody/cli/start.rb', line 5

def initialize(options)
  @options = options
  @project_name = options[:project_name] || inferred_project_name
  @full_project_name = project_name_convention(@project_name)
end

Instance Method Details

#environment_variables_overrideObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cody/cli/start.rb', line 31

def environment_variables_override
  @options[:env_vars].map do |s|
    k, v = s.split('=')
    ssm = false
    if /^ssm:(.*)/.match(v)
      v = $1
      ssm = true
    end

    {
      name: k,
      value: v,
      type: ssm ? "PARAMETER_STORE" : "PLAINTEXT"
    }
  end
end

#project_nameObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cody/cli/start.rb', line 48

def project_name
  if project_exists?(@full_project_name)
    @full_project_name
  elsif stack_exists?(@project_name) # allow `cody start STACK_NAME` to work too
    resp = cfn.describe_stack_resources(stack_name: @project_name)
    resource = resp.stack_resources.find do |r|
      r.logical_resource_id == "CodeBuild"
    end
    resource.physical_resource_id # codebuild project name
  else
    message = "ERROR: Unable to find the codebuild project with either full_project_name: #{@full_project_name} or project_name: #{@project_name}"
    if @options[:raise_error]
      raise(message)
    else
      puts message.color(:red)
      exit 1
    end
  end
end

#runObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cody/cli/start.rb', line 11

def run
  source_version = @options[:branch] || @options[:source_version] || 'master'
  params = {
    project_name: project_name,
    source_version: source_version
  }
  params[:environment_variables_override] = environment_variables_override if @options[:env_vars]
  params.merge!(@options[:overrides]) if @options[:overrides]
  resp = codebuild.start_build(params)

  puts "Build started for project: #{project_name}"
  puts "Here's the CodeBuild Console Log url:"
  puts codebuild_log_url(resp.build.id)
  tail_logs(resp.build.id) if @options[:wait]
end

#tail_logs(build_id) ⇒ Object



27
28
29
# File 'lib/cody/cli/start.rb', line 27

def tail_logs(build_id)
  Cody::Tailer.new(@options, build_id).run
end