Class: Stax::Cmd::Codebuild

Inherits:
SubCommand show all
Defined in:
lib/stax/mixin/codebuild.rb

Constant Summary collapse

COLORS =
{
  SUCCEEDED:    :green,
  FAILED:       :red,
  FAULT:        :red,
  CLIENT_ERROR: :red,
  STOPPED:      :red,
}

Instance Method Summary collapse

Methods inherited from SubCommand

#info, stax_info, stax_info_tasks

Instance Method Details

#buildsObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/stax/mixin/codebuild.rb', line 60

def builds
  my.stack_project_names.each do |project|
    debug("Builds for #{project}")
    ids = Aws::Codebuild.builds_for_project(project, options[:number])
    print_table Aws::Codebuild.builds(ids).map { |b|
      duration = human_time_diff(b.end_time - b.start_time)
      [b.id, b.initiator, color(b.build_status, COLORS), duration, b.end_time]
    }
  end
end


134
135
136
137
138
# File 'lib/stax/mixin/codebuild.rb', line 134

def link
  my.stack_project_names.map do |name|
    puts latest_run_link(name)
  end
end

#openObject



141
142
143
144
145
# File 'lib/stax/mixin/codebuild.rb', line 141

def open
  my.stack_project_names.map do |name|
    os_open(latest_run_link(name))
  end
end

#phases(id = nil) ⇒ Object



72
73
74
75
76
# File 'lib/stax/mixin/codebuild.rb', line 72

def phases(id = nil)
  id ||= Aws::Codebuild.builds_for_project(my.stack_project_names.first, 1).first
  debug("Phases for build #{id}")
  Aws::Codebuild.builds([id]).first.phases.each(&method(:print_phase))
end

#projectsObject



52
53
54
55
56
# File 'lib/stax/mixin/codebuild.rb', line 52

def projects
  print_table Aws::Codebuild.projects(my.stack_project_names).map { |p|
    [p.name, p.source.location, p.environment.image, p.environment.compute_type, p.last_modified]
  }
end

#reports(id = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/stax/mixin/codebuild.rb', line 79

def reports(id = nil)
  id ||= Aws::Codebuild.builds_for_project(my.stack_project_names.first, 1).first
  debug("Reports for build #{id}")
  report_arns = Aws::Codebuild.builds([id]).first.report_arns
  print_table Aws::Codebuild.reports(report_arns).map { |r|
    duration = (r.test_summary.duration_in_nano_seconds/1_000_000_000.0).to_s + 's'
    [ r.name, color(r.status, COLORS), duration, r.created ]
  }
end

#startObject



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/stax/mixin/codebuild.rb', line 121

def start
  project = options[:project] || my.stack_project_names.first
  version = options[:version] || Git.sha
  debug("Starting build for #{project} #{version}")
  build = Aws::Codebuild.start(
    project_name: project,
    source_version: version,
  )
  puts build.id
  tail build.id
end

#tail(id = nil) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/stax/mixin/codebuild.rb', line 102

def tail(id = nil)
  trap('SIGINT', 'EXIT')    # clean exit with ctrl-c
  id ||= Aws::Codebuild.builds_for_project(my.stack_project_names.first, 1).first
  debug("Phases for build #{id}")
  seen = {}
  loop do
    (Aws::Codebuild.builds([id]).first.phases || []).each do |p|
      i = p.phase_type + p.phase_status.to_s
      print_phase(p) unless seen[i]
      seen[i] = true
    end
    break if seen['COMPLETED']
    sleep(3)
  end
end

#tests(id = nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/stax/mixin/codebuild.rb', line 90

def tests(id = nil)
  id ||= Aws::Codebuild.builds_for_project(my.stack_project_names.first, 1).first
  Aws::Codebuild.builds([id]).first.report_arns.each do |report_arn|
    debug("Tests for report #{report_arn}")
    print_table Aws::Codebuild.tests(report_arn).map { |t|
      duration = (t.duration_in_nano_seconds/1_000_000).to_s + 'ms'
      [ t.name, color(t.status, COLORS), t.prefix, t.message, duration ]
    }
  end
end