Class: Larrow::Runner::Model::Node

Inherits:
Object
  • Object
show all
Includes:
Qingcloud, Service
Defined in:
lib/larrow/runner/model/node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance, eip, user = 'root') ⇒ Node

Returns a new instance of Node.



9
10
11
12
13
14
15
# File 'lib/larrow/runner/model/node.rb', line 9

def initialize instance, eip, user='root'
  self.instance = instance
  self.eip = eip
  self.host = eip.address
  self.user = user
  @executor = Executor.new host, user, nil, nil
end

Instance Attribute Details

#eipObject

Returns the value of attribute eip.



7
8
9
# File 'lib/larrow/runner/model/node.rb', line 7

def eip
  @eip
end

#hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/larrow/runner/model/node.rb', line 8

def host
  @host
end

#instanceObject

Returns the value of attribute instance.



7
8
9
# File 'lib/larrow/runner/model/node.rb', line 7

def instance
  @instance
end

#userObject

Returns the value of attribute user.



8
9
10
# File 'lib/larrow/runner/model/node.rb', line 8

def user
  @user
end

Class Method Details

.cleanup(resources) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/larrow/runner/model/node.rb', line 56

def self.cleanup resources
  resources.map do |hash|
    node = load_obj hash
    future{node.destroy}
  end.map do |node|
    RunLogger.info "node cleaned: #{node.eip.address}"
  end
end

.load_obj(data) ⇒ Object



65
66
67
68
69
# File 'lib/larrow/runner/model/node.rb', line 65

def self.load_obj data
  instance = Instance.new data[:instance][:id]
  eip = Eip.new data[:eip][:id],address:data[:eip][:address]
  new instance,eip
end

.show(resources, level = 0) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/larrow/runner/model/node.rb', line 46

def self.show resources, level=0
  resources.map do |hash|
    node = load_obj hash
    RunLogger.level(level).detail "instance: #{node.instance.id}"
    RunLogger.level(level).detail "eip:"
    RunLogger.level(level+1).detail "id: #{node.eip.id}"
    RunLogger.level(level+1).detail "address: #{node.eip.address}"
  end
end

Instance Method Details

#destroyObject



34
35
36
37
# File 'lib/larrow/runner/model/node.rb', line 34

def destroy
  Session.cloud.destroy instance,eip
  self
end

#dumpObject



39
40
41
42
43
44
# File 'lib/larrow/runner/model/node.rb', line 39

def dump
  {
    instance:{id: instance.id},
    eip:{id:eip.id, address:eip.address}
  }
end

#execute(command, base_dir: nil, verbose: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/larrow/runner/model/node.rb', line 17

def execute command, base_dir:nil,verbose:nil
  block = if block_given?
            -> (data) { yield data }
          else
            -> (data) {
              data.split(/\r?\n/).each do |msg|
                RunLogger.level(1).info msg 
              end
            }
          end
  @executor.execute command, base_dir: base_dir, verbose:verbose, &block
end

#stopObject



30
31
32
# File 'lib/larrow/runner/model/node.rb', line 30

def stop
  self.instance = instance.stop
end