Class: Toft::Node

Inherits:
Object
  • Object
show all
Includes:
Observable, CommandExecutor
Defined in:
lib/toft/node.rb

Constant Summary collapse

PRIVATE_KEY =
<<-EOF
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAwoLge1y9wEcy2WC7CDGXuFFDt+9Zvh+QulfDIbZkpYF7YBw6
O3mYTUpucwnqMhnd9jini8bsQghJF3wwxdWmtmurcHAEhN6ZljXZwUu2rojh+D+4
PnkOAMPb+w3REmyFYBxfzQ4gBBRXZgKWDTN6Al9hYRFTVSsZJCKJFK+GsWBSc5ie
l6IuUnfCbTwvORWVV6g7nGQ5x0JTnApG0qNFDprFkBsLbvHlB6A3lBtHQfJ7W/cZ
QXi3LXawD4bhWAl/CHxZHXGpJM7+tREz2yhOoPcHUwwv0xHqe/wBxmJmq84kZ8Co
lTi9Y5xNXKDiS6svMDgt3ShpkWcvQ+LE3PhUVQIDAQABAoIBAQCEvfZemeLw9mXw
TYA2TknhQqw5OYIAKuCFGuGS/ztea6f75iejcQ8MKDCKF4kZGegNYYqN7HpNcgQX
n+xVBsJYGdCM0hVza8pa5XMu4/HO2KGF3k5pbAmvYfqdMUeuEBtRhOuoL+yPfCZM
+pTWe3vXZKo7KSy6ocftjhgI4uTD5OtUHZe+Q61K8Ng5723kk4KcbZo4LHEvj4C2
nnplt9uxiS04qTPuJohxiwE1pbSybaH5Kndfvzmh1A1q/HKCZNhQKK/jhgvDBtBu
hiDfPtKdEeSTbFm11ckJBE1HPdAXoppxwDHQzcq82+vNJdB2P2TsMPklPRb6FuDa
dCQ0B5IBAoGBAPUlki+Q6yC4snhJ7r0ipIiMimB+Q6UlrE6ayyc3s/akjvTaqs2z
tcZTPUVVLjs0WIRIYwOWMcNmMZoi2s8nGsDiljmd1+smWHPK8A9X8pSUH6Z5k7sS
fzno3ytRXohyR2UB6iuUoT3F0VEnaaDjLlNk77DYusAmMsrm8W5PkMHVAoGBAMsf
aocc8yrwC7Wa0xfeNreK4F3OayqP6xAu0aSXqj1gtJ6HTTgY2g4jqtnV40NVXwHK
7zA4ie5IfmQ/672Te+9ifeWYNqv8faHDJwZFbZ2LwPW/lm5i4Ezn9KMmpLTz+6yj
f1bS+Y0NYyxfs1nn7Lx/kvuJVdIV+Ktma+ZHGAiBAoGAHo6NV0KAHHcJP/cvPAIa
ci7afMagVfCJNs8SrZPC6eZ/L0QmcDeLW+o6Q+8nMRgIRIzlUqghEdMmMalQjuu3
6P0Vbp8fL996vQw5uh/jS+Pewhh7cqEOnMBLORIOb4GXJp8DemUvaAzFV5FLGFPZ
DWoSen+5X4QjZqk8xNxEFfUCgYB+xR6xMMo69BH6x6QTc2Zm6s/Y++k6aRPlx7Kj
rNxc7iwL/Jme9zOwO2Z4CduKvI9NCSB60e6Tvr7TRmmUqaVh4/B7CKKmeDDYcnm6
mj4tY3mMZoQ2ZJNkrCesY4PMQ7HBL1FcGNQSylYo7Zl79Rl1E5HiVvYu5fOK1aNl
1t0TAQKBgQCaN91JXWJFXc1rJsDy365SKrX00Nt8rZolFQy2UhhERoCxD0uwbOYm
sfWYRr79L2BZkyRHmuxCD24SvHibDev+T0Jh6leXjnrwd/dj3P0tH8+ctET5SXsD
CQWv13UgQjiHgQILXSb7xdzpWK1wpDoqIEWQugRyPQDeZhPWVbB4Lg==
-----END RSA PRIVATE KEY-----    
EOF
TIMEOUT_IN_SECONDS =
120
TRY_INTERVAL =
0.5

Instance Method Summary collapse

Methods included from CommandExecutor

#cmd, #cmd!

Constructor Details

#initialize(hostname, options = {}) ⇒ Node

Returns a new instance of Node.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/toft/node.rb', line 45

def initialize(hostname, options = {})
  options = {:ip => DYNAMIC_IP, :netmask => "24", :type => "natty"}.merge(options)
  @hostname = hostname
  @ip = options[:ip]
  @netmask = options[:netmask]
  unless exists?
    conf_file = generate_lxc_config
    cmd! "lxc-create -n #{hostname} -f #{conf_file} -t #{options[:type].to_s}" 
  end
  @chef_runner = Toft::Chef::ChefRunner.new("#{rootfs}") do |chef_command|
    run_ssh chef_command
  end

  @puppet_runner = options[:runner]
  @puppet_runner ||= Toft::Puppet::PuppetRunner.new("#{rootfs}") do |puppet_command|
    run_ssh puppet_command
  end
end

Instance Method Details

#add_cname(cname) ⇒ Object



95
96
97
# File 'lib/toft/node.rb', line 95

def add_cname(cname)
  run_ssh "echo -e 'update add #{cname}.#{Toft::DOMAIN} 86400 CNAME #{@hostname}.#{Toft::DOMAIN}\\nsend' | nsupdate"
end

#destroyObject



84
85
86
87
88
89
# File 'lib/toft/node.rb', line 84

def destroy
  stop
  cmd! "lxc-destroy -n #{@hostname}"
  changed
  notify_observers(@hostname)
end

#exists?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/toft/node.rb', line 68

def exists?
  cmd("lxc-ls") =~ /#{@hostname}/
end

#file(path) ⇒ Object



148
149
150
# File 'lib/toft/node.rb', line 148

def file(path)
  FileChecker.new(rootfs, path)
end

#hostnameObject



64
65
66
# File 'lib/toft/node.rb', line 64

def hostname
  return @hostname
end

#ipObject



152
153
154
# File 'lib/toft/node.rb', line 152

def ip
  @ip == Toft::DYNAMIC_IP ? `dig +short #{fqdn}`.strip : @ip
end

#remove_cname(cname) ⇒ Object



99
100
101
# File 'lib/toft/node.rb', line 99

def remove_cname(cname)
  run_ssh "echo -e 'update delete #{cname}.#{Toft::DOMAIN}\\nsend' | nsupdate"      
end

#rm(dir) ⇒ Object

Raises:

  • (ArgumentError)


135
136
137
138
# File 'lib/toft/node.rb', line 135

def rm(dir)
  raise ArgumentError, "Illegal dir path: [#{dir}]", caller if dir.blank? || dir[0] != ?/
  cmd! "rm -rf #{rootfs}#{dir}"
end

#run_chef(run_list, params = {}) ⇒ Object



140
141
142
# File 'lib/toft/node.rb', line 140

def run_chef(run_list, params = {})
  @chef_runner.run run_list, params
end

#run_puppet(run_list, params = {}) ⇒ Object



144
145
146
# File 'lib/toft/node.rb', line 144

def run_puppet(run_list, params = {})
  @puppet_runner.run run_list, params
end

#run_ssh(command) ⇒ Object

Raises:

  • (ArgumentError)


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
# File 'lib/toft/node.rb', line 103

def run_ssh(command)
  raise ArgumentError, "Trying to run empty command on node #{@hostname}", caller if command.blank?
  stdout = ""
  stderr = ""
  Net::SSH.start(fqdn, "root", :key_data => [PRIVATE_KEY], :paranoid => false) do |ssh|
    ssh.open_channel do |channel|
      channel.exec(command) do |ch, success|
        raise RuntimeError, "Could not execute command: [#{command}]", caller unless success

        channel.on_data do |c, data|
          puts data
          stdout << data
          yield data if block_given?
        end
        channel.on_extended_data do |c, type, data|
          puts data
          stderr << data
          yield data if block_given?
        end
        channel.on_request("exit-status") do |c, data|
          exit_code = data.read_long
          raise CommandExecutionError.new "Remote command [#{command}] exited with status #{exit_code}", stdout, stderr unless exit_code.zero?
        end
        channel.on_request("exit-signal") do |c, data|
          raise CommandExecutionError.new "Remote command [#{command}] terminated with signal", stdout, stderr
        end
      end
    end.wait
  end
  return CommandResult.new stdout, stderr
end

#running?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/toft/node.rb', line 91

def running?
  cmd("lxc-info -n #{@hostname}") =~ /RUNNING/
end

#startObject



72
73
74
75
76
77
# File 'lib/toft/node.rb', line 72

def start
  puts "Starting host node..."
  cmd "lxc-start -n #{@hostname} -d" # system + sudo lxc-start does not work on centos-6, but back-quote does(no clue on why)
  cmd! "lxc-wait -n #{@hostname} -s RUNNING"
  wait_ssh_ready
end

#stopObject



79
80
81
82
# File 'lib/toft/node.rb', line 79

def stop
  cmd! "lxc-stop -n #{@hostname}"
  cmd! "lxc-wait -n #{@hostname} -s STOPPED"
end