Class: ServerInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/rest_connection/rightscale/server_interface.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cid = nil, params = {}, deployment_id = nil) ⇒ ServerInterface

Returns a new instance of ServerInterface.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rest_connection/rightscale/server_interface.rb', line 29

def initialize(cid = nil, params = {}, deployment_id = nil)

  # If cid is nil, assume generic server, otherwise check value and if cloud
  # id is in range of AWS clouds we can assume it's an AWS server
  if cid
    @multicloud = (cid.to_i > 10 ? true : false)
  else
    @multicloud = true
  end

  if @multicloud
    if deployment_id
      name = params["nickname"] || params["name"] || params[:nickname] || params[:name]
      @impl = McServer.find_by(:name, deployment_id) { |n| n == name }.first
    else
      @impl = McServer.new(params)
    end
  else
    @impl = Server.new(params)
  end
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



82
83
84
# File 'lib/rest_connection/rightscale/server_interface.rb', line 82

def method_missing(method_name, *args, &block)
  @impl.__send__(method_name, *args, &block)
end

Instance Attribute Details

#multicloudObject (readonly)

Returns the value of attribute multicloud.



27
28
29
# File 'lib/rest_connection/rightscale/server_interface.rb', line 27

def multicloud
  @multicloud
end

Class Method Details

.[](*args) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/rest_connection/rightscale/server_interface.rb', line 67

def self.[](*args)
  begin
    ret = Server[*args]
    raise "" if ret.empty?
  rescue
    ret = McServer[*args]
  end
  return ret
end

Instance Method Details

#attach_volume(params) ⇒ Object



257
258
259
260
# File 'lib/rest_connection/rightscale/server_interface.rb', line 257

def attach_volume(params)
  return connection.logger("WARNING: Gateway Servers do not support attach_volume. Ignoring.") if @multicloud
  return @impl.attach_volume(params) unless @multicloud
end

#clean_and_translate_server_params(it) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/rest_connection/rightscale/server_interface.rb', line 86

def clean_and_translate_server_params(it)
  it.each do |k, v|
    clean_and_translate_server_params(v) if v.is_a?(Hash)
  end
  it.reject! { |k, v| v == nil or v == "" }
  it.each { |k, v| it[k] = translate_href(v) if k.to_s =~ /href/ }
  it
end

#create(opts) ⇒ Object



52
53
54
55
56
57
# File 'lib/rest_connection/rightscale/server_interface.rb', line 52

def create(opts)
  location = connection.post(resource_plural_name, translate_create_opts(opts))
  @impl = (@multicloud ? McServer.new('href' => location) : Server.new('href' => location))
  settings
  self
end

#inputsObject



219
220
221
# File 'lib/rest_connection/rightscale/server_interface.rb', line 219

def inputs
  parameters
end

#inspectObject



63
64
65
# File 'lib/rest_connection/rightscale/server_interface.rb', line 63

def inspect
  @impl.inspect
end

#launchObject



231
232
233
234
# File 'lib/rest_connection/rightscale/server_interface.rb', line 231

def launch
  return @impl.launch if @multicloud
  return @impl.start unless @multicloud
end

#map_cloud_id(to, val) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/rest_connection/rightscale/server_interface.rb', line 166

def map_cloud_id(to, val)
  if val.is_a?(String)
    begin
      val = val.split(/\//).last
    rescue Exception => e
    end
  end
  if to == "1.5"
    return "https://my.rightscale.com/api/clouds/#{val}"
  elsif to == "1.0"
    return "#{val}"
  end
end

#map_instance(to, val) ⇒ Object



180
181
182
183
184
185
# File 'lib/rest_connection/rightscale/server_interface.rb', line 180

def map_instance(to, val)
  if to == "1.0"
    return val
  end
  val
end

#nameObject



59
60
61
# File 'lib/rest_connection/rightscale/server_interface.rb', line 59

def name
  nickname
end

#nicknameObject



77
78
79
80
# File 'lib/rest_connection/rightscale/server_interface.rb', line 77

def nickname
  return @impl.nickname unless @multicloud
  return @impl.name if @multicloud
end

#parametersObject

Since RightScale hands back the parameters with a “name” and “value” tags we should transform them into the proper hash. This it the same for setting and getting.



214
215
216
217
# File 'lib/rest_connection/rightscale/server_interface.rb', line 214

def parameters
  return @impl.parameters unless @multicloud
  return @impl.inputs if @multicloud
end

#run_script(script, opts = nil) ⇒ Object

This should be used with v4 images only.



252
253
254
255
# File 'lib/rest_connection/rightscale/server_interface.rb', line 252

def run_script(script,opts=nil)
  return connection.logger("WARNING: Gateway Servers do not support run_script. Did you mean run_executable?") if @multicloud
  return @impl.run_script(script,opts) unless @multicloud
end

#save(new_params = nil) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
# File 'lib/rest_connection/rightscale/server_interface.rb', line 269

def save(new_params = nil)
  if new_params
    @impl.settings
    if @multicloud
      @impl.next_instance.params.merge!(translate_create_opts(new_params, :instance_only)["instance"])
    else
      @impl.params.merge!(translate_create_opts(new_params)["server"])
    end
  end
  @impl.save
end

#startObject



223
224
225
# File 'lib/rest_connection/rightscale/server_interface.rb', line 223

def start
  launch
end

#start_ebsObject



241
242
243
244
# File 'lib/rest_connection/rightscale/server_interface.rb', line 241

def start_ebs
  return connection.logger("WARNING: Gateway Servers do not support start_ebs. Ignoring.") if @multicloud
  return @impl.start_ebs unless @multicloud
end

#stopObject



227
228
229
# File 'lib/rest_connection/rightscale/server_interface.rb', line 227

def stop
  terminate
end

#stop_ebsObject



246
247
248
249
# File 'lib/rest_connection/rightscale/server_interface.rb', line 246

def stop_ebs
  return connection.logger("WARNING: Gateway Servers do not support stop_ebs. Ignoring.") if @multicloud
  return @impl.stop_ebs unless @multicloud
end

#terminateObject



236
237
238
239
# File 'lib/rest_connection/rightscale/server_interface.rb', line 236

def terminate
  return @impl.terminate if @multicloud
  return @impl.stop unless @multicloud
end

#translate_create_opts(old_opts, instance_only = false) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rest_connection/rightscale/server_interface.rb', line 95

def translate_create_opts(old_opts, instance_only=false)
  fields = [{"1.0" => [:server_template_href],      "1.5" => [:server_template_href]},
            {"1.0" => [:cloud_id],                  "fn" => :map_cloud_id,  "1.5" => [:cloud_href]},
            {"1.0" => [:ec2_image_href],            "1.5" => [:image_href]},
            {"1.0" => [:ec2_user_data],             "1.5" => [:user_data]},
            {"1.0" => [:instance_type],             "fn" => :map_instance,  "1.5" => [:instance_type_href]},
            {"1.0" => [:ec2_security_groups_href],  "1.5" => [:security_group_hrefs]},
            {"1.0" => [:ec2_ssh_key_href],          "1.5" => [:ssh_key_href]},
            {"1.0" => [:vpc_subnet_href]},
            {"1.0" => [:ec2_availability_zone]},
            {"1.0" => [:pricing]},
            {"1.0" => [:max_spot_price]},
            {                                       "1.5" => [:inputs]},
            {"1.0" => [:multi_cloud_image_href],    "1.5" => [:mci_href, :multi_cloud_image_href]},
            {                                       "1.5" => [:datacenter_href]},
            {                                       "1.5" => [:subnet_hrefs]},
            {"1.0" => [:aki_image_href],            "1.5" => [:kernel_image_href]},
            {"1.0" => [:ari_image_href],            "1.5" => [:ramdisk_image_href]}]

  opts = old_opts.dup
  if @multicloud
    to = "1.5"
    if instance_only
      ret = {"instance" => {}}
      server = ret["instance"]
    else
      ret = {"server" => {"instance" => {}}}
      ret["server"]["name"] = (opts["name"] ? opts["name"] : opts["nickname"])
      ret["server"]["description"] = opts["description"]
      ret["server"]["deployment_href"] = opts["deployment_href"]
      server = ret["server"]["instance"]
    end
  else
    to = "1.0"
    server = {"nickname" => (opts["nickname"] ? opts["nickname"] : opts["name"])}
    server["deployment_href"] = opts["deployment_href"]
    ret = {"server" => server}
    begin
      ret["cloud_id"] = opts["cloud_href"].split(/\/clouds\//).last
    rescue Exception => e
      ret["cloud_id"] = opts["cloud_id"]
    end
  end

  fields.each { |hsh|
    next unless hsh[to]
    hsh[to].each { |field|
      vals = opts.select {|k,v| [[hsh["1.0"]] + [hsh["1.5"]]].flatten.include?(k.to_sym) }
      # IMPORTANT NOTE REGARDING RUBY VERSIONS!
      #
      # In Ruby 1.9.3 the next line of code is required to be written the way it is. Previously, it was written as:
      # vals.flatten! which doesn't work in Ruby 1.9.3.
      #
      # This is because opts is a hash and in Ruby 1.8.7, the select (above) returns an flattened array of
      # key, value pairs but in 1.9.3 it returns a new hash. flatten! does not exist for hashes so that would
      # no longer work in Ruby 1.9.3 and the code had to be changed to vals = vals.flatten to work correctly
      # in both Ruby versions.
      vals = vals.flatten
      vals.compact!
      if hsh["fn"]
        server[field.to_s] = __send__(hsh["fn"], to, opts[vals.first]) unless vals.first.nil?
      else
        server[field.to_s] = opts[vals.first] unless vals.first.nil?
      end
      server.delete("inputs") if server["inputs"] && server["inputs"].empty? # Inputs cannot be empty for 1.5
    }
  }
  clean_and_translate_server_params(ret)
  return ret
end

#translate_href(old_href) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/rest_connection/rightscale/server_interface.rb', line 187

def translate_href(old_href)
  if old_href.is_a?(Array)
    new_array = []
    old_href.each { |url| new_array << translate_href(url) }
    return new_array
  else
    href = old_href.dup
    if @multicloud
      href.gsub!(/ec2_/,'')
      href.gsub!(/\/acct\/[0-9]*/,'')
    end
    return href
  end
#    if href.include?("acct")
#      my_base_href, @account = href.split(/\/acct\//)
#      @account, *remaining = @account.split(/\//)
#      if @multicloud
#        return my_base_href + "/" + remaining.join("/").gsub(/ec2_/,'')
#      else
#        return href
#      end
#    else #API 1.5
#    end
end

#update(new_params = nil) ⇒ Object



281
282
283
# File 'lib/rest_connection/rightscale/server_interface.rb', line 281

def update(new_params = nil)
  save(new_params)
end

#wait_for_state(st, timeout = 1200) ⇒ Object



262
263
264
265
266
267
# File 'lib/rest_connection/rightscale/server_interface.rb', line 262

def wait_for_state(st,timeout=1200)
  if @multicloud and st == "stopped"
    st = "inactive"
  end
  @impl.wait_for_state(st,timeout)
end