Class: Larrow::Qingcloud::Instance

Inherits:
Base
  • Object
show all
Defined in:
lib/larrow/qingcloud/instance.rb

Constant Summary

Constants inherited from Base

Base::DESTROY_STATUSES

Instance Attribute Summary collapse

Attributes inherited from Base

#delegator, #id, #status

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

conn, #conn, describe, destroy_action, #initialize, #method_missing, #model_name, model_name, param_by, #param_by, plural_name, #show, singular_name, #wait_for

Methods included from Logger

#debug, #err, included, #info

Constructor Details

This class inherits a constructor from Larrow::Qingcloud::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Larrow::Qingcloud::Base

Instance Attribute Details

#keypair_idObject

Returns the value of attribute keypair_id.



4
5
6
# File 'lib/larrow/qingcloud/instance.rb', line 4

def keypair_id
  @keypair_id
end

#vxnet_idObject

Returns the value of attribute vxnet_id.



4
5
6
# File 'lib/larrow/qingcloud/instance.rb', line 4

def vxnet_id
  @vxnet_id
end

Class Method Details

.create(image_id: nil, cpu: 1, memory: 1024, count: 1, login_mode: 'passwd', passwd: '1qaz@WSX', keypair_id: nil, vxnet_id: 'vxnet-0') ⇒ Object

return an array(running instance)



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/larrow/qingcloud/instance.rb', line 9

def self.create(image_id:nil,
                cpu:1,
                memory:1024,
                count:1, 
                login_mode: 'passwd',
                passwd:'1qaz@WSX', 
                keypair_id:nil, 
                vxnet_id:'vxnet-0')
  err 'The default password is weak, you should change it'

  image_id ||= 'trustysrvx64b'
  
  result = conn.service 'get', 'RunInstances',
                        :image_id         => image_id,
                        :cpu              => cpu,
                        :memory           => memory,
                        :count            => count,
                        :login_mode       => ,
                        :login_passwd     => passwd,
                        :'login_keypair'  => keypair_id,
                        :'vxnets.n'       => vxnet_id

  info "instance added: #{result['instances']}"
  result['instances'].map do |id|
    instance = new id,keypair_id: keypair_id,vxnet_id:vxnet_id
    promise(timeout:90){ instance.wait_for :running }
  end
end

Instance Method Details

#attach_keypair(keypair_id) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/larrow/qingcloud/instance.rb', line 38

def attach_keypair(keypair_id)
  return self if self.keypair_id
  conn.service 'get', 'AttachKeyPairs',
               :'instances.1' => id,
               :'keypairs.1'  => keypair_id
  loop do
    if show(verbose: 1)['keypair_ids'].count > 0
      self.keypair_id = keypair_id
      info "instance attach keypair: #{id}"
      break self
    end
    sleep 2
  end
end

#join_vxnet(vxnet_id = 'vxnet-0') ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/larrow/qingcloud/instance.rb', line 53

def join_vxnet(vxnet_id = 'vxnet-0')
  return self if self.vxnet_id
  params = param_by [id], vxnet: vxnet_id
  conn.service 'get', 'JoinVxnet', params
  loop do
    if show['vxnets'].size > 0
      self.vxnet_id = vxnet_id
      info "instance joined vxnet: #{id}"
      break self
    end
    sleep 2
  end
end

#stop(force = nil) ⇒ Object

return a delayed instance object



68
69
70
71
72
73
74
75
# File 'lib/larrow/qingcloud/instance.rb', line 68

def stop force=nil
  if force
    conn.get 'StopInstances', :'instances.1' => id, :force => 1
  else
    conn.get 'StopInstances', :'instances.1' => id
  end
  promise(timeout:60){ wait_for :stopped }
end