Class: Chef::Knife::VroWorkflowExecute

Inherits:
Chef::Knife show all
Includes:
Cloud::Helpers
Defined in:
lib/chef/knife/vro_workflow_execute.rb

Overview

rubocop:disable Metrics/ClassLength rubocop:disable Style/AlignParameters

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parametersObject

Returns the value of attribute parameters


29
30
31
# File 'lib/chef/knife/vro_workflow_execute.rb', line 29

def parameters
  @parameters
end

#workflow_idObject

Returns the value of attribute workflow_id


29
30
31
# File 'lib/chef/knife/vro_workflow_execute.rb', line 29

def workflow_id
  @workflow_id
end

#workflow_nameObject

Returns the value of attribute workflow_name


29
30
31
# File 'lib/chef/knife/vro_workflow_execute.rb', line 29

def workflow_name
  @workflow_name
end

Instance Method Details

#execute_workflowObject


92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/chef/knife/vro_workflow_execute.rb', line 92

def execute_workflow
  parameters.each do |key, value|
    vro_client.parameter(key, value)
  end
  begin
    vro_client.execute
  rescue RestClient::BadRequest => e
    ui.error("The workflow execution request failed: #{e.response}")
    raise
  rescue => e
    ui.error("The workflow execution request failed: #{e.message}")
    raise
  end
end

#missing_config_parametersObject


121
122
123
124
125
# File 'lib/chef/knife/vro_workflow_execute.rb', line 121

def missing_config_parameters
  %i{vro_username vro_password vro_base_url}.each_with_object([]) do |param, memo|
    memo << param if config[param].nil?
  end
end

#parse_and_validate_params!(args) ⇒ Object


83
84
85
86
87
88
89
90
# File 'lib/chef/knife/vro_workflow_execute.rb', line 83

def parse_and_validate_params!(args)
  args.each_with_object({}) do |arg, memo|
    key, value = arg.split("=")
    raise "Invalid parameter, must be in KEY=VALUE format: #{arg}" if key.nil? || value.nil?

    memo[key] = value
  end
end

135
136
137
138
# File 'lib/chef/knife/vro_workflow_execute.rb', line 135

def print_error_and_exit(msg)
  ui.error(msg)
  exit(1)
end

157
158
159
160
161
162
163
# File 'lib/chef/knife/vro_workflow_execute.rb', line 157

def print_execution_log
  log = vro_client.log.to_s
  return if log.nil? || log.empty?

  ui.msg(ui.color("Workflow Execution Log:", :bold))
  ui.msg(log)
end

146
147
148
149
150
151
152
153
154
155
# File 'lib/chef/knife/vro_workflow_execute.rb', line 146

def print_output_parameters
  token = vro_client.token
  return if token.output_parameters.empty?

  ui.msg(ui.color("Output Parameters:", :bold))
  token.output_parameters.each do |k, v|
    msg_pair(k, "#{v.value} (#{v.type})") unless v.value.nil? || (v.value.respond_to?(:empty?) && v.value.empty?)
  end
  ui.msg("")
end

140
141
142
143
144
# File 'lib/chef/knife/vro_workflow_execute.rb', line 140

def print_results
  ui.msg("")
  print_output_parameters
  print_execution_log
end

#runObject


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/chef/knife/vro_workflow_execute.rb', line 171

def run
  validate!

  set_parameters

  ui.msg("Starting workflow execution...")
  execution_id = execute_workflow

  ui.msg("Workflow execution #{execution_id} started. Waiting for it to complete...")
  wait_for_workflow

  ui.msg("Workflow execution complete.")

  print_results
end

#set_parametersObject


165
166
167
168
169
# File 'lib/chef/knife/vro_workflow_execute.rb', line 165

def set_parameters
  self.workflow_name = @name_args.shift
  self.workflow_id   = config[:vro_workflow_id]
  self.parameters    = parse_and_validate_params!(@name_args)
end

#validate!Object


127
128
129
130
131
132
133
# File 'lib/chef/knife/vro_workflow_execute.rb', line 127

def validate!
  unless missing_config_parameters.empty?
    print_error_and_exit("The following parameters are missing but required:" \
      "#{missing_config_parameters.join(", ")}")
  end
  print_error_and_exit("You must supply a workflow name.") if @name_args.empty?
end

#verify_ssl?Boolean

Returns:

  • (Boolean)

62
63
64
# File 'lib/chef/knife/vro_workflow_execute.rb', line 62

def verify_ssl?
  !config[:vro_disable_ssl_verify]
end

#vro_clientObject


75
76
77
78
79
80
81
# File 'lib/chef/knife/vro_workflow_execute.rb', line 75

def vro_client
  @client ||= VcoWorkflows::Workflow.new(
    workflow_name,
    id: workflow_id,
    config: vro_config
  )
end

#vro_configObject


66
67
68
69
70
71
72
73
# File 'lib/chef/knife/vro_workflow_execute.rb', line 66

def vro_config
  @vro_config ||= VcoWorkflows::Config.new(
    url: config[:vro_base_url],
    username: config[:vro_username],
    password: config[:vro_password],
    verify_ssl: verify_ssl?
  )
end

#wait_for_workflowObject


107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/chef/knife/vro_workflow_execute.rb', line 107

def wait_for_workflow
  wait_time = config[:request_timeout]
  Timeout.timeout(wait_time) do
    loop do
      token = vro_client.token
      break unless token.alive?

      sleep 2
    end
  end
rescue Timeout::Error
  raise Timeout::Error, "Workflow did not complete in #{wait_time} seconds. Please check the vRO UI for more information."
end