dl_racktables 0.0.3 → 0.0.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cfac34a4dba16aac12cb5aa63c1ee884670af38d
4
- data.tar.gz: c62b1b01353e4edd90c04b391d659b9bdf8933ac
3
+ metadata.gz: 8aa267a26189fb44c7069f4015714fcce8e5f5d7
4
+ data.tar.gz: 875e62290c692a3f2508e123ed529844a40fcd2b
5
5
  SHA512:
6
- metadata.gz: 1a74313ca31566207b2d40624ce0b48d4b070651c9dee6aa00c10220c25595c364997d3e04c010f8571cdf06bd4cda71fef045bffdf14c2f4522e65ce80dfebd
7
- data.tar.gz: eb0769d854dda2330e2e8766683d0447bcc954fed187c982399b87573e2f83d46cd1a5889020f8d899559bafa3de99c6e7201847a17cd78cf7c0eed00a3b2e8a
6
+ metadata.gz: 41bd70d27e965c4e8e2915b0151b62c57c0db3b11d461fcadc8e6c34f8fc76401e93b2b3b8be756e5ff4bbcd44d542f2df76cf3c4c04cb61db82accee91e69af
7
+ data.tar.gz: 8c63c98281d452ed9be0e0fcd55fb302602c3b12bea1474bca71218c4f8f36a7c38bdcfad00f88de43d63eb5f8c989a9a6039e7038135210ee45c17c3aa1d1a6
data/README.md CHANGED
@@ -1,4 +1,14 @@
1
- dl-racktable
2
- ============
1
+ # dl-racktable
3
2
  This is a library to operatate objects which is manged by Racktables.
4
3
  And this depends on Racktables v0.20.8.
4
+
5
+ ## register_vm
6
+ This command generate vm-id (like 'vm3600') and registers it with some attributes
7
+
8
+ ### Usage
9
+ ```
10
+ Usage: register_vm [options]
11
+ -o, --owner owner specified at "b-10|担当チーム" of Racktables {XaaS|案件|共通基盤|OLG|配信|...}
12
+ -s, --status status specified at "a-0|Status" of Racktables
13
+ -i, --ipaddr ipaddr IP address to set new VM
14
+ ```
data/bin/register_vm ADDED
@@ -0,0 +1,90 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'ipaddr'
4
+ require 'optparse'
5
+ require 'dl_racktables'
6
+
7
+ STATUS = {
8
+ 'SRIへ返却' => 50403,
9
+ 'SRI所有' => 50404,
10
+ 'SVC様へ貸し出し中' => 50407,
11
+ 'エイシス資産で返却済' => 50409,
12
+ '使用終了' => 51613,
13
+ '削除済' => 50412,
14
+ '廃棄可能' => 51615,
15
+ '捜索中' => 50415,
16
+ '故障中' => 50418,
17
+ 'サービスIN' => 51730,
18
+ 'サービスOUT' => 51731,
19
+ 'パーツ取り用' => 51614,
20
+ '在庫' => 50413,
21
+ '廃棄/返却予定' => 50422,
22
+ '廃棄/返却済' => 50018,
23
+ '納品前' => 51523,
24
+ '電源断中' => 50424,
25
+ }
26
+
27
+ OWNERS = {
28
+ '共通基盤' => 51556,
29
+ '案件' => 51554,
30
+ 'FX' => 51571,
31
+ 'XaaS' => 51575,
32
+ 'make 3Dプリント' => 51573,
33
+ 'OLG' => 51555,
34
+ 'オーバーライド' => 51557,
35
+ 'コアテク部' => 51570,
36
+ 'サイバーセキュリティ' => 51675,
37
+ 'データセンター運用' => 51561,
38
+ 'ネットワーク' => 51565,
39
+ 'マネージャー' => 51564,
40
+ 'ロボット' => 51574,
41
+ '情報管理部' => 51572,
42
+ '配信' => 51566,
43
+ '障害対応' => 51563,
44
+ }
45
+
46
+ sys = DLRacktables::System.new
47
+
48
+ options = {}
49
+ parser = OptionParser.new do |opt|
50
+ opt.on('-o owner', '--owner', 'specified at "b-10|担当チーム" of Racktables {XaaS|案件|共通基盤|OLG|配信|...}') { |v| options[:owner] = v }
51
+ opt.on('-s status', '--status', 'specified at "a-0|Status" of Racktables') { |v| options[:status] = v }
52
+ opt.on('-i ipaddr', '--ipaddr', 'IP address to set new VM') { |v| options[:ipaddr] = v }
53
+ end
54
+
55
+ begin
56
+ parser.parse!
57
+ rescue OptionParser::InvalidOption => e
58
+ puts parser.help
59
+ exit 1
60
+ end
61
+
62
+ # validate options
63
+ unless STATUS.key? options[:status]
64
+ puts parser.help
65
+ puts "--------"
66
+ puts "[Wraning] Invalid 'status' parameter is specified. This might be specified from following"
67
+ puts STATUS.keys.join('|')
68
+
69
+ exit 1
70
+ end
71
+ unless OWNERS.key? options[:owner]
72
+ puts parser.help
73
+ puts "--------"
74
+ puts "[Wraning] Invalid 'owner' parameter is specified. This might be specified from following"
75
+ puts OWNERS.keys.join('|')
76
+
77
+ exit 1
78
+ end
79
+ if (IPAddr.new(options[:ipaddr]) rescue nil).nil?
80
+ puts parser.help
81
+ puts "--------"
82
+ puts "[Wraning] Invalid IP address is specified #{options[:ipaddr]}"
83
+ exit 1
84
+ end
85
+
86
+ sys.register_vm({
87
+ :owner => OWNERS[options[:owner]],
88
+ :status => STATUS[options[:status]],
89
+ :ipaddr => options[:ipaddr],
90
+ })
@@ -5,9 +5,9 @@ require 'dl_racktables/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "dl_racktables"
8
- spec.version = DlRacktables::VERSION
8
+ spec.version = DLRacktables::VERSION
9
9
  spec.authors = ["Hiroyasu OHYAMA"]
10
- spec.email = ["user.localhost2000@gmail.com"]
10
+ spec.email = ["oyama-hiroyasu@dmm.com"]
11
11
 
12
12
  spec.summary = %q{helper library to operate racktables}
13
13
  spec.description = %q{This is a library to operatate objects which is manged by Racktables}
@@ -1,4 +1,6 @@
1
1
  require 'nokogiri'
2
+ require 'ipaddr'
3
+ require 'json'
2
4
 
3
5
  module DLRacktables
4
6
  class System
@@ -23,5 +25,145 @@ module DLRacktables
23
25
 
24
26
  {team: team, person: person}
25
27
  end
28
+ def get_objid(vmid)
29
+ resp = HTTP.get("index.php?page=search&last_page=object&last_tab=edit&q=#{vmid}")
30
+ doc = Nokogiri::HTML(resp.body)
31
+
32
+ doc.xpath("//div[@class='pagebar']//input[@name='object_id']//@value").first.to_s
33
+ end
34
+
35
+ def update_vminfo(vmid, status = 0, owner = 0)
36
+ HTTP.post('index.php?module=redirect&page=object&tab=edit&op=update', {
37
+ 'object_id' => get_objid(vmid),
38
+ 'object_type_id' => '1504',
39
+ 'object_name' => vmid,
40
+ 'object_label' => '',
41
+ 'object_asset_no' => '',
42
+ '0_attr_id' => '10015',
43
+ '0_value' => status.to_s,
44
+ '1_attr_id' => '10020',
45
+ '1_value' => '0',
46
+ '2_attr_id' => '10012',
47
+ '2_value' => '',
48
+ '3_attr_id' => '10021',
49
+ '3_value' => '0',
50
+ '4_attr_id' => '4',
51
+ '4_value' => '0',
52
+ '5_attr_id' => '1',
53
+ '5_value' => '',
54
+ '6_attr_id' => '10013',
55
+ '6_value' => '0',
56
+ '7_attr_id' => '10023',
57
+ '7_value' => '0',
58
+ '8_attr_id' => '10024',
59
+ '8_value' => '0',
60
+ '9_attr_id' => '10009',
61
+ '9_value' => '',
62
+ '10_attr_id' => '10070',
63
+ '10_value' => '',
64
+ '11_attr_id' => '10078',
65
+ '11_value' => owner.to_s,
66
+ '12_attr_id' => '10080',
67
+ '12_value' => '0',
68
+ '13_attr_id' => '10077',
69
+ '13_value' => '0',
70
+ 'num_attrs' => '14',
71
+ 'object_comment' => '',
72
+ 'submit.x' => '23',
73
+ 'submit.y' => '28',
74
+ })
75
+ end
76
+
77
+ def update_ipinfo(vmid, ipaddr)
78
+ HTTP.post('index.php?module=redirect&page=object&tab=ip&op=add', {
79
+ 'object_id' => get_objid(vmid).to_s,
80
+ 'submit.x' => '0',
81
+ 'submit.y' => '0',
82
+ 'bond_name' => '',
83
+ 'ip' => ipaddr,
84
+ 'bond_type' => 'regular',
85
+ })
86
+ end
87
+
88
+ def exist_iprange?(addr)
89
+ resp = HTTP.get("index.php?page=search&last_page=ipv4space&last_tab=default&q=#{addr}")
90
+
91
+ doc = Nokogiri::HTML(resp.body)
92
+
93
+ return doc.xpath("//div[@class='pagebar']//h2").to_s != "<h2>Nothing found for '#{addr}'</h2>"
94
+ end
95
+
96
+ def validate_ipaddr(addr)
97
+ if ! exist_iprange?(addr)
98
+ return false
99
+ end
100
+
101
+ resp = HTTP.get("index.php?page=ipaddress&ip=#{addr}")
102
+
103
+ doc = Nokogiri::HTML(resp.body)
104
+
105
+ # is invalid input?
106
+ if (doc.title =~ /#{addr}/) == nil
107
+ return false
108
+ end
109
+
110
+ # chack target object is assigned for some machine
111
+ unless doc.xpath("//div[@class='pagebar']//td[@class='pcright']//div").empty?
112
+ return false
113
+ end
114
+
115
+ summary = doc.xpath("//div[@class='pagebar']//td[@class='pcleft']//table").text
116
+
117
+ ret = false
118
+ ret ||= summary.include? 'Name'
119
+ ret ||= summary.include? 'Comment'
120
+ ret ||= summary.include? 'Reserved:yes'
121
+
122
+ ! ret
123
+ end
124
+
125
+ def find_vmid
126
+ resp = HTTP.get('index.php?andor=and&cfe=%7b$attr_10015_50413%7d+and+%7b$typeid_1504%7d&page=depot&tab=default')
127
+
128
+ doc = Nokogiri::HTML(resp.body)
129
+
130
+ if doc.xpath("//table[@class='cooltable']//tr").find {|x| x.text =~ /^vm(\d+)Unmounted$/}
131
+ "vm#{$1}"
132
+ end
133
+ end
134
+
135
+ def validate_vmid(vmid)
136
+ resp = HTTP.get("index.php?page=search&q=#{vmid}")
137
+
138
+ doc = Nokogiri::HTML(resp.body)
139
+
140
+ if doc.title =~ /search results for/
141
+ # meaning invalid vmid is specified
142
+ return false
143
+ end
144
+
145
+ if doc.xpath("//div[@class='portlet']//tr").find {|x| x.text =~ / Status:(.*)/}
146
+ $1 == '在庫'
147
+ end
148
+ end
149
+
150
+ def register_vm(opts)
151
+ vmid = find_vmid
152
+ is_success = false
153
+
154
+ # update VM information on Racktables
155
+ if validate_vmid(vmid)
156
+ ret = update_vminfo(vmid, opts[:status], opts[:owner])
157
+ if ret.is_a? Net::HTTPSuccess
158
+ is_success = true
159
+ end
160
+ end
161
+
162
+ if validate_ipaddr(opts[:ipaddr])
163
+ update_ipinfo(vmid, opts[:ipaddr])
164
+ end
165
+
166
+ puts ({:is_success => is_success, :vmid => vmid}).to_json
167
+ end
26
168
  end
27
169
  end
@@ -1,3 +1,3 @@
1
- module DlRacktables
2
- VERSION = "0.0.3"
1
+ module DLRacktables
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dl_racktables
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroyasu OHYAMA
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-11 00:00:00.000000000 Z
11
+ date: 2016-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -68,9 +68,10 @@ dependencies:
68
68
  version: '3.0'
69
69
  description: This is a library to operatate objects which is manged by Racktables
70
70
  email:
71
- - user.localhost2000@gmail.com
71
+ - oyama-hiroyasu@dmm.com
72
72
  executables:
73
73
  - get_vm_lastupdate
74
+ - register_vm
74
75
  extensions: []
75
76
  extra_rdoc_files: []
76
77
  files:
@@ -79,6 +80,7 @@ files:
79
80
  - README.md
80
81
  - Rakefile
81
82
  - bin/get_vm_lastupdate
83
+ - bin/register_vm
82
84
  - dl-racktables.gemspec
83
85
  - lib/dl_racktables.rb
84
86
  - lib/dl_racktables/config.rb
@@ -105,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
107
  version: '0'
106
108
  requirements: []
107
109
  rubyforge_project:
108
- rubygems_version: 2.4.5
110
+ rubygems_version: 2.6.4
109
111
  signing_key:
110
112
  specification_version: 4
111
113
  summary: helper library to operate racktables