Module: Kytoon::Util

Defined in:
lib/kytoon/util.rb

Constant Summary collapse

SSH_OPTS =
"-o StrictHostKeyChecking=no"
@@configs =
nil

Class Method Summary collapse

Class Method Details

.check_config_param(key) ⇒ Object



62
63
64
65
66
67
# File 'lib/kytoon/util.rb', line 62

def self.check_config_param(key)
  configs = load_configs
  if not configs or configs[key].nil? or configs[key].to_s.empty? then
    raise ConfigException, "Please specify '#{key.to_s}' in your kytoon config file."
  end
end

.generate_ssh_keypair(ssh_key_basepath) ⇒ Object

Generate an ssh keypair using the specified base path



127
128
129
130
# File 'lib/kytoon/util.rb', line 127

def self.generate_ssh_keypair(ssh_key_basepath)
  FileUtils.mkdir_p(File.dirname(ssh_key_basepath))
  %x{ssh-keygen -N '' -f #{ssh_key_basepath} -t rsa -q}
end

.hostnameObject



13
14
15
# File 'lib/kytoon/util.rb', line 13

def self.hostname
  Socket.gethostname
end

.load_configsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/kytoon/util.rb', line 17

def self.load_configs

  return @@configs if not @@configs.nil?

  config_file=ENV['KYTOON_CONFIG_FILE']
  if config_file.nil? then

    config_file=ENV['HOME']+File::SEPARATOR+".kytoon.conf"
    if not File.exists?(config_file) then
      config_file="/etc/kytoon.conf"
    end

  end

  if File.exists?(config_file) then
    configs=YAML.load_file(config_file)
    @@configs=configs
  else
    raise ConfigException, "Failed to load kytoon config file. Please configure /etc/kytoon.conf or create a .kytoon.conf config file in your HOME directory."
  end

  @@configs

end

.load_public_keyObject



55
56
57
58
59
60
# File 'lib/kytoon/util.rb', line 55

def self.load_public_key

  pubkey=IO.read(self.public_key_path)
  pubkey.chomp

end

.public_key_pathObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/kytoon/util.rb', line 42

def self.public_key_path

  ssh_dir=File.join(ENV['HOME'], ".ssh")
  if File.exists?(File.join(ssh_dir, "id_rsa.pub"))
    File.join(ssh_dir, "id_rsa.pub")
  elsif File.exists?(File.join(ssh_dir, "id_dsa.pub"))
    File.join(ssh_dir, "id_dsa.pub")
  else
    raise ConfigException, "Failed to load SSH key. Please create a SSH public key pair in your HOME directory."
  end

end

.remote_exec(script_text, gateway_ip, retry_attempts = 0, retry_sleep = 5) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/kytoon/util.rb', line 69

def self.remote_exec(script_text, gateway_ip, retry_attempts=0, retry_sleep=5)
  if gateway_ip.nil?
    sg=ServerGroup.get
    gateway_ip=sg.gateway_ip
  end

  retval=nil
  out=nil
  (retry_attempts+1).times do |count|
    sleep retry_sleep if count > 1
    out=%x{
ssh #{SSH_OPTS} root@#{gateway_ip} bash <<-"REMOTE_EXEC_EOF"
#{script_text}
REMOTE_EXEC_EOF
    }
    retval=$?
    break if retval.success?
  end
  if block_given? then
    yield retval.success?, out
  else
    return [retval.success?, out]
  end

end

.remote_multi_exec(hosts, script_text, gateway_ip) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/kytoon/util.rb', line 95

def self.remote_multi_exec(hosts, script_text, gateway_ip)

  if gateway_ip.nil?
    sg=ServerGroup.get
    gateway_ip=sg.gateway_ip
  end

  results = {}
  threads = []

  hosts.each do |host|
    t = Thread.new do
      out=%x{
ssh #{SSH_OPTS} root@#{gateway_ip} bash <<-"REMOTE_EXEC_EOF"
ssh #{host} bash <<-"EOF_HOST"
#{script_text}
EOF_HOST
REMOTE_EXEC_EOF
     }
     retval=$?
    results.store host, [retval.success?, out]
    end
    threads << t
  end

  threads.each {|t| t.join}

  return results

end