Class: VCloudScript

Inherits:
Object
  • Object
show all
Defined in:
lib/scripts/vCloud/v_cloud_script.rb

Overview

Base class for any script for vCloud.

Direct Known Subclasses

OpenPortCheckerVm

Instance Method Summary collapse

Constructor Details

#initialize(input_params) ⇒ VCloudScript

Initialization. Common Input parameters:

  • vcloud_api_handler => used to connect to the API (needs user/password/api-url)

  • logger => allows to pass a ruby logger object used for logging (optional, default is a stdout logger with level WARN)

Scripts may add specific key/value pairs.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/scripts/vCloud/v_cloud_script.rb', line 7

def initialize(input_params)
  @input_params = input_params
  @state_change_listeners = []
  @progress_message_listeners = []
  if input_params[:logger] == nil
    @logger = Logger.new(STDOUT)
    @logger.level = Logger::WARN
    input_params[:logger] = @logger
  else
    @logger = input_params[:logger]
  end
  @result = {:done => false, :failed => false}
  @input_params[:result] = @result
  @input_params.each() do |param_key, param_value|
    if [:logger, :vcloud_api_handler, :target_ec2_handler, :remote_command_handler,
      :source_ssh_keydata, :target_ssh_keydata, :ssh_keydata
      ].include?(param_key.to_s.to_sym)
      @logger.debug("INPUT PARAM #{param_key} is set [but not logged]")
    else
      @logger.debug("INPUT PARAM #{param_key} = #{param_value.inspect}")
    end
  end
end

Instance Method Details

#check_input_parametersObject

Check input parameters (in @input_parameters object variable) and set default values. Abstract method to be implemented by extending classes.

Raises:

  • (Exception)


42
43
44
# File 'lib/scripts/vCloud/v_cloud_script.rb', line 42

def check_input_parameters()
  raise Exception.new("check_input_parameters must be implemented")
end

#get_execution_resultObject

Return a hash of results. Common values are:

  • :done => is true when the script has terminated, otherwise false

  • :failed => is false when the script succeeded

  • :failure_reason => returns a failure reason (string)

  • :end_state => returns the state, in which the script terminated (#Help::ScriptExecutionState)

Scripts may add specific key/value pairs. * Returns a hash with the following information: :done => if execution is done



99
100
101
# File 'lib/scripts/vCloud/v_cloud_script.rb', line 99

def get_execution_result
  @result
end

#load_initial_stateObject

Load the initial state for the script. Abstract method to be implemented by extending classes.

Raises:

  • (Exception)


48
49
50
# File 'lib/scripts/vCloud/v_cloud_script.rb', line 48

def load_initial_state()
  raise Exception.new("load_initial_state must be implemented")
end

#post_message(message, level = Logger::DEBUG) ⇒ Object



103
104
105
106
107
# File 'lib/scripts/vCloud/v_cloud_script.rb', line 103

def post_message(message, level = Logger::DEBUG)
  @progress_message_listeners.each() {|listener|
    listener.new_message(message, level)
  }
end

#register_progress_message_listener(listener) ⇒ Object



35
36
37
# File 'lib/scripts/vCloud/v_cloud_script.rb', line 35

def register_progress_message_listener(listener)
  @progress_message_listeners << listener
end

#register_state_change_listener(listener) ⇒ Object



31
32
33
# File 'lib/scripts/vCloud/v_cloud_script.rb', line 31

def register_state_change_listener(listener)
  @state_change_listeners << listener
end

#start_scriptObject

Executes the script.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/scripts/vCloud/v_cloud_script.rb', line 53

def start_script()
  # optional parameters and initialization
  check_input_parameters()
  @input_params[:script] = self
  begin
    current_state = load_initial_state()
    @state_change_listeners.each() {|listener|
      current_state.register_state_change_listener(listener)
    }
    end_state = current_state.start_state_machine()
    if end_state.failed?
      @result[:failed] = true
      @result[:failure_reason] = current_state.end_state.failure_reason
      @result[:end_state] = current_state.end_state
    else
      @result[:failed] = false
    end
  rescue Exception => e
    @logger.warn "exception during execution: #{e}"
    @logger.warn e.backtrace.join("\n")
    err = e.to_s
    err += " (in #{current_state.end_state.to_s})" unless current_state == nil
    @result[:failed] = true
    @result[:failure_reason] = err
    @result[:end_state] = current_state.end_state unless current_state == nil
  ensure
    begin
    @input_params[:remote_command_handler].disconnect
    rescue Exception => e2
    end
  end
  #
  @result[:done] = true
end