-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_node_attributes.rb
96 lines (84 loc) · 2.37 KB
/
set_node_attributes.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
94
95
96
#
# Written by Kerrington Wells 2016
#
#
require 'rubygems'
require 'net/ssh'
require 'fileutils'
options = {}
OptionParser.new do |opts|
opts.banner = 'Usage: example.rb [options]'
opts.on('-f', '--Host File', 'Host File') { |value| options[:host] = value }
end.parse!
initd_list = ["httpd", "tcserver"]
#check if nodes are public cloud nodes
#If the file already contains -nat in it remove it first
#This avoids errors and confusion later in the script
File.open(options[:host], 'r') do |f1|
while line = f1.gets
if line.match(/^VA1/)
File.open(options[:host] + '.tmp', 'a') do |file|
file.puts(line.gsub '-nat','')
end
end
end
end
if File.file?(options[:host] + '.tmp')
FileUtils.mv(options[:host] + '.tmp', options[:host])
end
File.open(options[:host], 'r') do |f1|
while line = f1.gets
if line.match(/^VA1/)
File.open(options[:host] + '.tmp', 'a') do |file|
file.puts(line.sub '.','-nat.')
end
end
end
end
if File.file?(options[:host] + '.tmp')
FileUtils.mv(options[:host] + '.tmp', options[:host])
end
@cmd = "ls -l /etc/rc3.d/| awk '{print $9}'|sed 's/^...//'"
@username = 'sa_rmtmgmt'
f = File.open(options[:host])
f.each do |hostname|
application = String.new
hostname = hostname.gsub("\n", '')
begin
puts hostname
ssh = Net::SSH.start(hostname, 'sa_rmtmgmt')
res = ssh.exec!(@cmd)
res = res.split("\n")
ssh.close
if hostname.include? '-nat'
hostname = hostname.gsub("-nat", '')
end
#Create attributes based on the order is the node results
#I get the start up order by running ls -l on /etc/rc3.d
res.each do |i|
if initd_list.include? i
application << i + " "
end
end
node_attributes = httpd = "knife node attribute set " + hostname + " services ""'" + application + "'"
system node_attributes
get_node_attributes = httpd = "knife node attribute get " + hostname + " services "
system get_node_attributes
rescue
puts ''
end
end
#Removeing -nat from last file
File.open(options[:host], 'r') do |f1|
while line = f1.gets
if line.match(/^VA1/)
File.open(options[:host] + '.tmp', 'a') do |file|
file.puts(line.gsub '-nat','')
end
end
end
end
if File.file?(options[:host] + '.tmp')
FileUtils.mv(options[:host] + '.tmp', options[:host])
end
File.open(options[:host]).close