Class: Toft::Node

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

Constant Summary collapse

PRIVATE_KEY =
"-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEAwoLge1y9wEcy2WC7CDGXuFFDt+9Zvh+QulfDIbZkpYF7YBw6\nO3mYTUpucwnqMhnd9jini8bsQghJF3wwxdWmtmurcHAEhN6ZljXZwUu2rojh+D+4\nPnkOAMPb+w3REmyFYBxfzQ4gBBRXZgKWDTN6Al9hYRFTVSsZJCKJFK+GsWBSc5ie\nl6IuUnfCbTwvORWVV6g7nGQ5x0JTnApG0qNFDprFkBsLbvHlB6A3lBtHQfJ7W/cZ\nQXi3LXawD4bhWAl/CHxZHXGpJM7+tREz2yhOoPcHUwwv0xHqe/wBxmJmq84kZ8Co\nlTi9Y5xNXKDiS6svMDgt3ShpkWcvQ+LE3PhUVQIDAQABAoIBAQCEvfZemeLw9mXw\nTYA2TknhQqw5OYIAKuCFGuGS/ztea6f75iejcQ8MKDCKF4kZGegNYYqN7HpNcgQX\nn+xVBsJYGdCM0hVza8pa5XMu4/HO2KGF3k5pbAmvYfqdMUeuEBtRhOuoL+yPfCZM\n+pTWe3vXZKo7KSy6ocftjhgI4uTD5OtUHZe+Q61K8Ng5723kk4KcbZo4LHEvj4C2\nnnplt9uxiS04qTPuJohxiwE1pbSybaH5Kndfvzmh1A1q/HKCZNhQKK/jhgvDBtBu\nhiDfPtKdEeSTbFm11ckJBE1HPdAXoppxwDHQzcq82+vNJdB2P2TsMPklPRb6FuDa\ndCQ0B5IBAoGBAPUlki+Q6yC4snhJ7r0ipIiMimB+Q6UlrE6ayyc3s/akjvTaqs2z\ntcZTPUVVLjs0WIRIYwOWMcNmMZoi2s8nGsDiljmd1+smWHPK8A9X8pSUH6Z5k7sS\nfzno3ytRXohyR2UB6iuUoT3F0VEnaaDjLlNk77DYusAmMsrm8W5PkMHVAoGBAMsf\naocc8yrwC7Wa0xfeNreK4F3OayqP6xAu0aSXqj1gtJ6HTTgY2g4jqtnV40NVXwHK\n7zA4ie5IfmQ/672Te+9ifeWYNqv8faHDJwZFbZ2LwPW/lm5i4Ezn9KMmpLTz+6yj\nf1bS+Y0NYyxfs1nn7Lx/kvuJVdIV+Ktma+ZHGAiBAoGAHo6NV0KAHHcJP/cvPAIa\nci7afMagVfCJNs8SrZPC6eZ/L0QmcDeLW+o6Q+8nMRgIRIzlUqghEdMmMalQjuu3\n6P0Vbp8fL996vQw5uh/jS+Pewhh7cqEOnMBLORIOb4GXJp8DemUvaAzFV5FLGFPZ\nDWoSen+5X4QjZqk8xNxEFfUCgYB+xR6xMMo69BH6x6QTc2Zm6s/Y++k6aRPlx7Kj\nrNxc7iwL/Jme9zOwO2Z4CduKvI9NCSB60e6Tvr7TRmmUqaVh4/B7CKKmeDDYcnm6\nmj4tY3mMZoQ2ZJNkrCesY4PMQ7HBL1FcGNQSylYo7Zl79Rl1E5HiVvYu5fOK1aNl\n1t0TAQKBgQCaN91JXWJFXc1rJsDy365SKrX00Nt8rZolFQy2UhhERoCxD0uwbOYm\nsfWYRr79L2BZkyRHmuxCD24SvHibDev+T0Jh6leXjnrwd/dj3P0tH8+ctET5SXsD\nCQWv13UgQjiHgQILXSb7xdzpWK1wpDoqIEWQugRyPQDeZhPWVbB4Lg==\n-----END RSA PRIVATE KEY-----    \n"
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