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.



27
28
29
# File 'lib/chef/knife/vro_workflow_execute.rb', line 27

def parameters
  @parameters
end

#workflow_idObject

Returns the value of attribute workflow_id.



27
28
29
# File 'lib/chef/knife/vro_workflow_execute.rb', line 27

def workflow_id
  @workflow_id
end

#workflow_nameObject

Returns the value of attribute workflow_name.



27
28
29
# File 'lib/chef/knife/vro_workflow_execute.rb', line 27

def workflow_name
  @workflow_name
end

Instance Method Details

#execute_workflowObject



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

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



119
120
121
122
123
# File 'lib/chef/knife/vro_workflow_execute.rb', line 119

def missing_config_parameters
  [:vro_username, :vro_password, :vro_base_url].each_with_object([]) do |param, memo|
    memo << param if locate_config_value(param).nil?
  end
end

#parse_and_validate_params!(args) ⇒ Object



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

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


132
133
134
135
# File 'lib/chef/knife/vro_workflow_execute.rb', line 132

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


154
155
156
157
158
159
160
# File 'lib/chef/knife/vro_workflow_execute.rb', line 154

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


143
144
145
146
147
148
149
150
151
152
# File 'lib/chef/knife/vro_workflow_execute.rb', line 143

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


137
138
139
140
141
# File 'lib/chef/knife/vro_workflow_execute.rb', line 137

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

#runObject



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

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



162
163
164
165
166
# File 'lib/chef/knife/vro_workflow_execute.rb', line 162

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

#validate!Object



125
126
127
128
129
130
# File 'lib/chef/knife/vro_workflow_execute.rb', line 125

def validate!
  print_error_and_exit('The following parameters are missing but required:' \
    "#{missing_config_parameters.join(', ')}") unless missing_config_parameters.empty?

  print_error_and_exit('You must supply a workflow name.') if @name_args.empty?
end

#verify_ssl?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/chef/knife/vro_workflow_execute.rb', line 60

def verify_ssl?
  !locate_config_value(:vro_disable_ssl_verify)
end

#vro_clientObject



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

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

#vro_configObject



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

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

#wait_for_workflowObject



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

def wait_for_workflow
  wait_time = locate_config_value(: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