Class: Ec2Script

Inherits:
Object
  • Object
show all
Defined in:
lib/scripts/ec2/ec2_script.rb

Overview

Base class for any script on EC2.

Constant Summary collapse

CS_SEC_GRP_NAME =
"CloudyScripts Opened Security Group"
CS_SEC_GRP_DESC =
"Security Group used for CloudyScripts (Opened SSH). PLEASE DO NOT MODIFY !!!"
CS_AWS_TIMEOUT =
600

Instance Method Summary collapse

Constructor Details

#initialize(input_params) ⇒ Ec2Script

Initialization. Common Input parameters:

  • aws_access_key => the Amazon AWS Access Key (see Your Account -> Security Credentials)

  • aws_secret_key => the Amazon AWS Secret Key

  • ec2_api_server => the API Server to connect to (optional, default is us-east-1 (=> <ec2_api_server>.ec2.amazonaws.com)

  • 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.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/scripts/ec2/ec2_script.rb', line 15

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, :ec2_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)


50
51
52
# File 'lib/scripts/ec2/ec2_script.rb', line 50

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



107
108
109
# File 'lib/scripts/ec2/ec2_script.rb', line 107

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)


56
57
58
# File 'lib/scripts/ec2/ec2_script.rb', line 56

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

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



111
112
113
114
115
# File 'lib/scripts/ec2/ec2_script.rb', line 111

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

#register_progress_message_listener(listener) ⇒ Object



43
44
45
# File 'lib/scripts/ec2/ec2_script.rb', line 43

def register_progress_message_listener(listener)
  @progress_message_listeners << listener
end

#register_state_change_listener(listener) ⇒ Object



39
40
41
# File 'lib/scripts/ec2/ec2_script.rb', line 39

def register_state_change_listener(listener)
  @state_change_listeners << listener
end

#start_scriptObject

Executes the script.



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
87
88
89
90
91
92
93
94
# File 'lib/scripts/ec2/ec2_script.rb', line 61

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