kanamei-keystone 0.0.23 → 0.0.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/keystone.rb CHANGED
@@ -11,7 +11,7 @@ autoload :Moji , 'moji'
11
11
 
12
12
  module Keystone
13
13
 
14
- VERSION = '0.0.23'
14
+ VERSION = '0.0.24'
15
15
 
16
16
  autoload :StringUtil , 'keystone/string_util'
17
17
  autoload :Batch , 'keystone/batch'
@@ -134,7 +134,7 @@ module Keystone::Batch
134
134
  debug pid_file
135
135
  if File.exists?(pid_file)
136
136
  pid = File.open(pid_file).read.chomp
137
- pid_list = `ps -e | awk '{print $1}'`
137
+ pid_list = `ps -ax | awk '{print $1}'`
138
138
  if (pid != nil && pid != "" ) && pid_list =~ /#{pid}/
139
139
  warn "pid:#{pid} still running"
140
140
  double_process_check_worked = true
@@ -0,0 +1,19 @@
1
+
2
+ require 'ipaddr'
3
+
4
+ class IPAddr
5
+ def to_a
6
+ begin_addr = (@addr & @mask_addr)
7
+ case @family
8
+ when Socket::AF_INET
9
+ end_addr = (@addr | (IN4MASK ^ @mask_addr))
10
+ when Socket::AF_INET6
11
+ end_addr = (@addr | (IN6MASK ^ @mask_addr))
12
+ else
13
+ raise "unsupported address family"
14
+ end
15
+ ret = []
16
+ begin_addr.upto(end_addr){|addr| ret << IPAddr.new(addr,Socket::AF_INET)}
17
+ return ret
18
+ end
19
+ end
@@ -0,0 +1,135 @@
1
+
2
+ # TODO
3
+ # みしあげでおま
4
+
5
+ require 'rubygems'
6
+ require 'keystone'
7
+ require 'ipaddr'
8
+ require 'net/telnet'
9
+ require 'net/ssh'
10
+ require 'pp'
11
+
12
+ open("/tmp/command_broadcast.log.#{Time.now.usec}","w") do |f|
13
+ f.puts "test"
14
+ end
15
+
16
+ INPUT_MESSAGE_CMD = "input command you broadcast:"
17
+ INPUT_MESSAGE_IP = %|input ipaddress you broadcast(ex"192.168.1.13","192.168.1.0/24"):|
18
+
19
+ # TODO lvm 絡みも入れるべきかも
20
+ # ホワイトリストにすべき?
21
+ CANCEL_CMDS = %w|rm mv cp iptables fdisk dd|
22
+
23
+ def ppp(message)
24
+ print message
25
+ STDOUT.flush
26
+ end
27
+
28
+ ppp INPUT_MESSAGE_IP
29
+ ipaddr = nil
30
+ STDIN.each_line do |line|
31
+ line.strip!
32
+ begin
33
+ ipaddr = IPAddr.new line
34
+ break
35
+ rescue
36
+ ppp INPUT_MESSAGE_IP
37
+ next
38
+ end
39
+ end
40
+
41
+ ppp INPUT_MESSAGE_CMD
42
+ cmd = nil
43
+ STDIN.each_line do |line|
44
+ line.strip!
45
+ if line.blank?
46
+ ppp INPUT_MESSAGE_CMD
47
+ next
48
+ end
49
+ cmd = line
50
+ break
51
+ end
52
+
53
+ CANCEL_CMDS.each do |cancel_cmd|
54
+ if cmd =~ /\A#{cancel_cmd}/
55
+ puts "we can not run this command!!"
56
+ exit
57
+ end
58
+ end
59
+
60
+ puts "!! command you want to execute is below !!"
61
+ puts "#{cmd}"
62
+ ppp "?? are you sure to execute this??(y/N):"
63
+ STDIN.each_line do |line|
64
+ line.chomp!
65
+ break if line.downcase == 'y'
66
+ puts "exit"
67
+ exit
68
+ end
69
+
70
+ alive_servers = []
71
+ ipaddr.to_a.each do |ip|
72
+ ping = "ping -c 1 -t 2 #{ip.to_s}"
73
+ ret = `#{ping}`
74
+ if ret =~ /100% packet loss/
75
+ puts "#{ip.to_s} does not exits"
76
+ else
77
+ # OK な場合
78
+ puts "#{ip.to_s} exits"
79
+ alive_servers << ip
80
+ end
81
+ end
82
+
83
+ results = []
84
+
85
+ alive_servers.each do |server|
86
+ ret = ""
87
+ begin
88
+ ip = server.to_s
89
+ pp ip
90
+ puts "++ telnet ++"
91
+ telnet = Net::Telnet.new("Host" => ip) {|c|} #print c}
92
+ telnet.login("webadmin", "inu00neko") {|c|} #print c}
93
+ telnet.cmd(cmd) {|c| ret = c}
94
+ STDOUT.flush # <- これがないとここまで処理が来てることがわかりにくい
95
+ # ログインセッションの終了
96
+ telnet.cmd("exit") {|c|} #print c}
97
+ telnet.close
98
+ telnet_ok = true
99
+ # rescue TimeoutError => e
100
+ rescue TimeoutError => e
101
+ puts e.message
102
+ rescue Errno::ECONNREFUSED => e
103
+ puts e.message
104
+ rescue Errno::ENETUNREACH => e
105
+ puts e.message
106
+ rescue Errno::EACCES => e
107
+ puts e.message
108
+ end
109
+
110
+ unless telnet_ok
111
+ begin
112
+ puts "++ ssh ++"
113
+ Net::SSH.start(ip, 'webadmin', :password => 'inu00neko') do |ssh|
114
+ ret = (ssh.exec!(cmd))
115
+ end
116
+ rescue Exception => e
117
+ end
118
+ end
119
+
120
+ results << [ip,ret]
121
+ puts ret
122
+ sleep 1
123
+ end
124
+
125
+ pp results
126
+
127
+ open("/tmp/command_broadcast.log.#{Time.now.usec}","w") do |f|
128
+ f.puts results.inspect
129
+ end
130
+
131
+ __END__
132
+ find /etc/lognotify/ -name "lognotify*.conf" -type f -exec cat {} \;
133
+
134
+
135
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kanamei-keystone
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.23
4
+ version: 0.0.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - kanamei
@@ -23,6 +23,7 @@ extra_rdoc_files: []
23
23
 
24
24
  files:
25
25
  - util/bin
26
+ - util/bin/command_broadcast.rb
26
27
  - util/bin/disk_size_check.rb
27
28
  - util/bin/notice
28
29
  - util/README
@@ -40,6 +41,7 @@ files:
40
41
  - lib/keystone/core_ext/array.rb
41
42
  - lib/keystone/core_ext/blank.rb
42
43
  - lib/keystone/core_ext/dir.rb
44
+ - lib/keystone/core_ext/ipaddr.rb
43
45
  - lib/keystone/core_ext/object.rb
44
46
  - lib/keystone/core_ext/tempfile.rb
45
47
  - lib/keystone/core_ext/uri.rb