Class: CfnGuardian::CodePipeline

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/cfnguardian/codepipeline.rb

Instance Method Summary collapse

Methods included from Logging

colors, included, logger, #logger, logger=

Constructor Details

#initialize(pipeline_name) ⇒ CodePipeline

Returns a new instance of CodePipeline.



9
10
11
12
13
14
15
# File 'lib/cfnguardian/codepipeline.rb', line 9

def initialize(pipeline_name)
  @pipeline_name = pipeline_name
  client = Aws::CodePipeline::Client.new()
  @pipeline = client.get_pipeline_state({
    name: @pipeline_name
  })
end

Instance Method Details

#colour_rows(rows, status) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cfnguardian/codepipeline.rb', line 28

def colour_rows(rows, status)
  if status == 'Failed'
    rows.map! {|row| row.map! {|r| r.red } }
  elsif status == 'Succeeded'
    rows.map! {|row| row.map! {|r| r.green } }
  elsif status == 'InProgress'
    rows.map! {|row| row.map! {|r| r.blue } }
  elsif ["Stopped", "Stopping"].include? status
    rows.map! {|row| row.map! {|r| r.yellow } }
  end
end

#get_buildObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cfnguardian/codepipeline.rb', line 64

def get_build()
  source_stage = get_stage("Build")
  action = source_stage.action_states.first
  status = source_stage.latest_execution.status
  state = {
    stage: action.action_name,
    rows: [
      ['Status', status],
      ['Build Id', action.latest_execution.external_execution_id],
      ['Last Status Change', action.latest_execution.last_status_change.localtime.strftime("%d/%m/%Y %I:%M %p")],
      ['Logs', action.latest_execution.external_execution_url]
    ]
  }
  
  unless action.latest_execution.error_details.nil?
    state[:rows].push(
      ['Error Message', action.latest_execution.error_details.message]
    )
  end
  
  colour_rows(state[:rows],status)
  
  return state
end

#get_create_changesetObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/cfnguardian/codepipeline.rb', line 89

def get_create_changeset()
  source_stage = get_stage("Deploy")
  action = source_stage.action_states.find {|action| action.action_name == "CreateChangeSet"}
  status = source_stage.latest_execution.status
  state = {
    stage: action.action_name,
    rows: [
      ['Status', status],
      ['Summary', action.latest_execution.summary],
      ['Last Status Change', action.latest_execution.last_status_change.localtime.strftime("%d/%m/%Y %I:%M %p")],
    ]
  }
  
  unless action.latest_execution.error_details.nil?
    state[:rows].push(
      ['Error Message', action.latest_execution.error_details.message]
    )
  end
  
  colour_rows(state[:rows],status)
  
  return state
end

#get_deploy_changesetObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cfnguardian/codepipeline.rb', line 113

def get_deploy_changeset()
  source_stage = get_stage("Deploy")
  action = source_stage.action_states.find {|action| action.action_name == "DeployChangeSet"}
  status = source_stage.latest_execution.status
  state = {
    stage: action.action_name,
    rows: [
      ['Status', status],
      ['Summary', action.latest_execution.summary],
      ['Last Status Change', action.latest_execution.last_status_change.localtime.strftime("%d/%m/%Y %I:%M %p")],
    ]
  }
  
  unless action.latest_execution.error_details.nil?
    state[:rows].push(
      ['Error Message', action.latest_execution.error_details.message]
    )
  end
  
  colour_rows(state[:rows],status)
  
  return state
end

#get_sourceObject



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/cfnguardian/codepipeline.rb', line 40

def get_source()
  source_stage = get_stage("Source")
  action = source_stage.action_states.first
  status = source_stage.latest_execution.status
  state = {
    stage: action.action_name,
    rows: [
      ['Status', status],
      ['Commit', action.current_revision.revision_id],
      ['Last Status Change', action.latest_execution.last_status_change.localtime.strftime("%d/%m/%Y %I:%M %p")]
    ]
  }
  
  unless action.latest_execution.error_details.nil?
    state[:rows].push(
      ['Error Message', action.latest_execution.error_details.message]
    )
  end
  
  colour_rows(state[:rows],status)
  
  return state
end

#get_stage(stage_name) ⇒ Object



24
25
26
# File 'lib/cfnguardian/codepipeline.rb', line 24

def get_stage(stage_name)
  return @pipeline.stage_states.find {|stage| stage.stage_name == stage_name}
end

#retryObject



17
18
19
20
21
22
# File 'lib/cfnguardian/codepipeline.rb', line 17

def retry()
  resp = client.start_pipeline_execution({
    name: @pipeline_name,
    client_request_token: "ClientRequestToken",
  })
end