Class: Orca::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/orca/node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, host, options = {}) ⇒ Node



18
19
20
21
22
23
24
# File 'lib/orca/node.rb', line 18

def initialize(name, host, options={})
  @name = name
  @host = host
  @options = options
  @connection = nil
  Orca::Node.register(self)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



30
31
32
# File 'lib/orca/node.rb', line 30

def method_missing(meth, *args)
  get(meth)
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



5
6
7
# File 'lib/orca/node.rb', line 5

def host
  @host
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/orca/node.rb', line 5

def name
  @name
end

Class Method Details

.find(name) ⇒ Object



7
8
9
10
# File 'lib/orca/node.rb', line 7

def self.find(name)
  return name if name.is_a?(Orca::Node)
  @nodes[name]
end

.register(node) ⇒ Object



12
13
14
15
16
# File 'lib/orca/node.rb', line 12

def self.register(node)
  @nodes ||= {}
  Orca::Group.from_node(node)
  @nodes[node.name] = node
end

Instance Method Details

#connectionObject



85
86
87
88
# File 'lib/orca/node.rb', line 85

def connection
  return @connection if @connection
  @connetion = Net::SSH.start(@host, (@options[:user] || 'root'), options_for_ssh)
end

#download(from, to) ⇒ Object



39
40
41
42
# File 'lib/orca/node.rb', line 39

def download(from, to)
  log('sftp', "DOWLOAD: #{from} => #{to}")
  sftp.download!(from, to)
end

#execute(cmd) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/orca/node.rb', line 63

def execute(cmd)
  log('execute', cmd.cyan)
  output = ""
  connection.exec! cmd do |channel, stream, data|
    output += data if stream == :stdout
    data.split("\n").each do |line|
      msg = stream == :stdout ? line.green : line.red
      log(stream, msg)
    end
  end
  output
end

#get(option) ⇒ Object



26
27
28
# File 'lib/orca/node.rb', line 26

def get(option)
  @options[option]
end

#log(context, msg) ⇒ Object



80
81
82
83
# File 'lib/orca/node.rb', line 80

def log(context, msg)
  Thread.exclusive { puts "#{self.to_s} [#{context.to_s.bold}] #{msg}" }
  msg
end

#remove(path) ⇒ Object



44
45
46
47
# File 'lib/orca/node.rb', line 44

def remove(path)
  log('sftp', "REMOVE: #{path}")
  sftp.remove!(path)
end

#setstat(path, opts) ⇒ Object



54
55
56
57
# File 'lib/orca/node.rb', line 54

def setstat(path, opts)
  log('sftp', "SET: #{path} - #{opts.inspect}")
  sftp.setstat!(path, opts)
end

#sftpObject



59
60
61
# File 'lib/orca/node.rb', line 59

def sftp
  @sftp ||= connection.sftp.connect
end

#stat(path) ⇒ Object



49
50
51
52
# File 'lib/orca/node.rb', line 49

def stat(path)
  log('sftp', "STAT: #{path}")
  sftp.stat!(path)
end

#sudo(cmd) ⇒ Object



76
77
78
# File 'lib/orca/node.rb', line 76

def sudo(cmd)
  execute("sudo #{cmd}")
end

#to_sObject



90
91
92
# File 'lib/orca/node.rb', line 90

def to_s
  "#{name}(#{host})"
end

#upload(from, to) ⇒ Object



34
35
36
37
# File 'lib/orca/node.rb', line 34

def upload(from, to)
  log('sftp', "UPLOAD: #{from} => #{to}")
  sftp.upload!(from, to)
end