Class: RsMule::RunExecutable

Inherits:
Object
  • Object
show all
Defined in:
lib/rs-mule/run_executable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(right_api_client) ⇒ RunExecutable

Initializes a new RunExecutable

Parameters:

  • right_api_client (RightApi::Client)

    An instantiated and authenticated RightApi::Client instance which will be used for making the request(s)


29
30
31
# File 'lib/rs-mule/run_executable.rb', line 29

def initialize(right_api_client)
  @right_api_client = right_api_client
end

Instance Attribute Details

#right_api_clientObject

Returns the value of attribute right_api_client.


23
24
25
# File 'lib/rs-mule/run_executable.rb', line 23

def right_api_client
  @right_api_client
end

Instance Method Details

#run_executable(tags, executable, options = {}) ⇒ Object

Runs a RightScript or Chef Recipe on all instances which have all of the specified tags.

Parameters:

  • tags (String|Array<String>)

    An array of tags. If a string is supplied it will be converted to an array of Strings.

  • executable (String)

    RightScript name or href, or the name of a recipe. This method will attempt to auto detect which it is, or you can be friendly and provide a hint using the executable_type option.

  • options (Hash) (defaults to: {})

    A list of options where the possible values are

    • executable_type [String] One of [“auto”,“right_script_name”,“right_script_href”,“recipe_name”]. When set

      to "auto" we will attempt to determine if an executable is a recipe,
      a RightScript name, or a RightScript href. Defaults to "auto"
      
    • right_script_revision [String] When a RightScript name or href is specified,

      this can be used to declare the revision to use.  Can be a specific
      revision number or "latest".  Defaults to "latest"
      
    • tag_match_strategy [String] If multiple tags are specified, this will

      determine how they are matched.  When set to "all" instances with all
      tags will be matched.  When set to "any" instances with any of the
      provided tags will be matched.  Defaults to "all"
      
    • inputs [Hash] A hash where the keys are the name of an input and the

      value is the desired value for that input.  Uses Inputs 2.0[http://reference.rightscale.com/api1.5/resources/ResourceInputs.html#multi_update]
      semantics.
      
    • update_inputs [Array<String>] An array of values indicating which

      objects should be updated with the inputs supplied.  Can be empty in
      which case the inputs will be used only for this execution.  Acceptable
      values are ["current_instance","next_instance","deployment"]
      

Raises:

  • (RightScriptNotFound)

    If the specified RightScript lineage does not exist, or the specified revision is not available.


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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rs-mule/run_executable.rb', line 61

def run_executable(tags, executable, options={})
  options = {
      :executable_type => "auto",
      :right_script_revision => "latest",
      :tag_match_strategy => "all",
      :inputs => {},
      :update_inputs => []
  }.merge(options)
  execute_params = {}
  tags = [tags] unless tags.is_a?(Array)
  options[:update_inputs] = [options[:update_inputs]] unless options[:update_inputs].is_a?(Array)

  case options[:executable_type]
    when "right_script_href"
      execute_params[:right_script_href] = executable
    when "right_script_name"
      scripts = find_right_script_lineage_by_name(executable)
      execute_params[:right_script_href] = right_script_revision_from_lineage(scripts, options[:right_script_revision]).href
    when "recipe_name"
      execute_params[:recipe_name] = executable
    when "auto"
      is_recipe = executable =~ /.*::.*/
      is_href = executable =~ /^\/api\/right_scripts\/[a-zA-Z0-9]*/
      if is_recipe
        execute_params[:recipe_name] = executable
      else
        if is_href
          execute_params[:right_script_href] = executable
        else
          scripts = find_right_script_lineage_by_name(executable)
          execute_params[:right_script_href] = right_script_revision_from_lineage(scripts, options[:right_script_revision]).href
        end
      end
    else
      raise ArgumentError.new("Unknown executable_type (#{options[:executable_type]})")
  end

  if options[:inputs].length > 0
    execute_params[:inputs] = options[:inputs]
  end

  resources_by_tag = @right_api_client.tags.by_tag(
    :resource_type => "instances",
    :tags => tags,
    :match_all => options[:tag_match_strategy] == "all" ? "true" : "false"
  )

  resources_by_tag.each do |res|
    instance = @right_api_client.resource(res.links.first["href"])
    instance.run_executable(execute_params)
    options[:update_inputs].each do |update_type|
      update_inputs(instance, options[:inputs], update_type)
    end
  end
end