-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddNode.rb
executable file
·43 lines (35 loc) · 1.05 KB
/
addNode.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env ruby
require File.expand_path(File.dirname(__FILE__) + '/include.rb')
require 'optparse'
opt_parser = OptionParser.new do |opt|
opt.banner = "Usage: addNode.rb FQDN ENVIRONMENT [CLASSES]"
opt.separator ""
opt.separator "eg: addNode.rb web2.my.domain web_server api_lb_server"
opt.separator ""
opt.separator "OPTIONS:"
opt.on("-h","--help","help") do
puts opt_parser
exit
end
end
opt_parser.parse!
if ARGV.length < 2
puts opt_parser
exit
end
fqdn,env,*classes = ARGV
client = Mysql2::Client.new(:host => @db_host, :username => @db_user, :password => @db_password, :database => @db_name)
results = client.query("SELECT * FROM nodes where fqdn = \"#{fqdn}\"")
if results.any?
puts "The node \"#{fqdn}\" already exists"
exit 1
end
class_string = ""
if classes
class_string = classes.join(",")
end
node_name = fqdn.split(".")[0]
query = "INSERT INTO nodes (hostname, fqdn, env, classes) VALUES (\"#{node_name}\",\"#{fqdn}\",\"#{env}\",\"#{class_string}\")"
puts query
client.query(query)
puts "Node inserted successfully"