vmopt 0.0.2

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.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vmopt.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 xhb
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Vmopt
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'vmopt'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install vmopt
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
Binary file
@@ -0,0 +1,97 @@
1
+ #!/usr/bin/env ruby
2
+ #encoding: utf-8
3
+
4
+ require "vmopt/disk_operation"
5
+ require "optparse"
6
+ require "json"
7
+ option = {}
8
+ option_parser = OptionParser.new do | opts |
9
+ opts.banner = %q/
10
+ Help:
11
+ Description:
12
+ vmdrive tool, use for hard disk drive operation.
13
+ function:
14
+ 1.get_disk_information
15
+ 2.get_partition_information
16
+ 3.unformat_disk
17
+ 4.chk_format_disk
18
+ 5.format_disk_by_index
19
+ 6.delete_partition
20
+ Example:
21
+ vmdrive --query harddisk
22
+ vmdrive --query partition
23
+ vmdrive --query unformated_disk
24
+ vmdrive --format unformated_disk
25
+ vmdrive --format [diskindex]
26
+ vmdrive --delete [diskindex]
27
+ /
28
+
29
+ opts.on('-q queryopt', '--query queryopt', 'Query harddisk information') do |value|
30
+ option[:queryopt] = value
31
+ option[:cmd] = "query"
32
+ end
33
+
34
+ opts.on('-d disk', '--delete disk', 'Delete harddisk information') do |value|
35
+ option[:disk] = value
36
+ option[:cmd] = "delete"
37
+ end
38
+
39
+ opts.on('-f disk', '--format disk', 'Specify harddrive index to format') do |value|
40
+ option[:disk] = value
41
+ option[:cmd] = "format"
42
+ end
43
+ end.parse!
44
+
45
+ resultok = {"status"=>"0"}
46
+ resultfail = {"status"=>"-1"}
47
+ data_value={}
48
+ $disk = Vmopt::DiskOperation.new
49
+
50
+ if option[:cmd] == "query"
51
+ if option[:queryopt] == 'harddisk'
52
+ data_value = $disk.get_disk_information
53
+ elsif option[:queryopt] == 'partition'
54
+ data_value = $disk.get_partition_information
55
+ elsif option[:queryopt] == 'unformated_disk'
56
+ data_value = $disk.unformat_disk
57
+ else
58
+ system("vmdrive -help")
59
+ option.clear
60
+ end
61
+
62
+ elsif option[:cmd] == "format"
63
+ if option[:disk] == 'unformated_disk'
64
+ ret = $disk.chk_format_disk
65
+ data_value="format disk success"
66
+ elsif option[:disk] =~ /^\d$/
67
+ ret = $disk.format_disk_by_index(option[:disk].to_i)
68
+ data_value="format disk success"
69
+ else
70
+ system("vmdrive -help")
71
+ option.clear
72
+ end
73
+ elsif option[:cmd] == "delete"
74
+ if option[:disk] =~ /^\d$/
75
+ ret = $disk.delete_partition(option[:disk].to_i)
76
+ data_value="delete disk success"
77
+ else
78
+ system("vmdrive -help")
79
+ option.clear
80
+ end
81
+ else
82
+ system("vmdrive -help")
83
+ option.clear
84
+ end
85
+
86
+ if ret==false
87
+ resultfail["data"]="format disk fail"
88
+ retjson = JSON.generate resultfail
89
+ else
90
+ resultok["data"]=data_value
91
+ retjson = JSON.generate resultok
92
+ end
93
+
94
+ puts retjson unless option[:cmd].nil?
95
+
96
+
97
+
@@ -0,0 +1,193 @@
1
+ #!/usr/bin/env ruby
2
+ #encoding: utf-8
3
+
4
+ require "vmopt/dvd_operation"
5
+ require "optparse"
6
+ require "json"
7
+ option = {}
8
+ option_parser = OptionParser.new do | opts |
9
+ opts.banner = %q/
10
+ Help:
11
+ Description:
12
+ vmdrive tool, use for DVD\CD drive operation.
13
+ function:
14
+ 1.get_dvd_information
15
+ 2.eject_dvd
16
+ 3.close_dvd
17
+ 4.disable_all_dvd
18
+ 5.disable_index_dvd
19
+ 6.enable_all_dvd
20
+ 7.remove_all_dvd
21
+ 8.remove_index_dvd
22
+ 9.mount_all_dvd
23
+ 10.read_dvd
24
+
25
+ Example:
26
+ vmdvd --query dvd_information
27
+ vmdvd --query read_dvd --symbol disksymbol
28
+
29
+ vmdvd --disable disable_all_dvd
30
+ vmdvd --disable disable_index_dvd --index diskindex
31
+ vmdvd --enable enable_all_dvd
32
+
33
+ vmdvd --remove remove_all_dvd
34
+ vmdvd --mount mount_all_dvd
35
+ vmdvd --remove remove_index_dvd --index diskindex
36
+
37
+ vmdvd --eject eject_dvd
38
+ vmdvd --close close_dvd
39
+ /
40
+ #查询时使用
41
+ opts.on('-q queryopt', '--query queryopt', 'Query DVD/CD information') do |value|
42
+ option[:cmd] = "query"
43
+ option[:queryopt] = value
44
+ end
45
+ opts.on('-s disksymbol', '--symbol disksymbol', 'list DVD/CD files') do |value|
46
+ option[:disksymbol] = value
47
+ end
48
+
49
+ #禁用时使用
50
+ opts.on('-d disableopt', '--disable disableopt', 'Specify dvd option to disable') do |value|
51
+ option[:disable_opt] = value
52
+ option[:cmd] = "disable"
53
+ end
54
+
55
+ #启用时使用
56
+ opts.on('-e alldisk', '--enable alldisk', 'Enable all dvd drive') do |value|
57
+ option[:enabl_all_disks] = value
58
+ option[:cmd] = "enable"
59
+ end
60
+
61
+ #卸载光驱时使用
62
+ opts.on('-r remov_disks', '--remove remov_disks', 'remove dvd drive') do |value|
63
+ option[:remov_disks] = value
64
+ option[:cmd] = "remove"
65
+ end
66
+
67
+ #挂载全部磁盘
68
+ opts.on('-m mount_all_dvd', '--mount mount_all_dvd', 'mount all dvd drive') do |value|
69
+ option[:mount_all_dvd] = value
70
+ option[:cmd] = "mount"
71
+ end
72
+
73
+ #弹出光驱
74
+ opts.on('-j enject_dvd', '--eject enject_dvd', 'mount all dvd drive') do |value|
75
+ option[:enject_dvd] = value
76
+ option[:cmd] = "enject"
77
+ end
78
+
79
+ #关闭光驱
80
+ opts.on('-c close_dvd', '--close close_dvd', 'mount all dvd drive') do |value|
81
+ option[:close_dvd] = value
82
+ option[:cmd] = "close"
83
+ end
84
+
85
+ #磁盘索引号
86
+ opts.on('-i diskindex', '--index diskindex', 'Specify dvd index') do |value|
87
+ option[:diskindex] = value
88
+ end
89
+
90
+ end.parse!
91
+
92
+ resultok = {"status"=>"0"}
93
+ resultfail = {"status"=>"-1"}
94
+ data_value={}
95
+ $dvd = Vmopt::DVDOperation.new
96
+
97
+ case option[:cmd]
98
+ when "query"
99
+ if option[:queryopt] == "dvd_information"
100
+ data_value = $dvd.get_dvd_information
101
+ elsif option[:queryopt] == "read_dvd"
102
+ if option[:disksymbol].nil?
103
+ system("vmdvd -help");
104
+ option.clear
105
+ else
106
+ ret = $dvd.read_dvd(option[:disksymbol])
107
+ data_value="read dvd success"
108
+ end
109
+ end
110
+
111
+ when "disable"
112
+ if option[:disable_opt] == "disable_all_dvd"
113
+ ret = $dvd.disable_all_dvd
114
+ data_value="disable all dvd success"
115
+ elsif option[:disable_opt] == "disable_index_dvd"
116
+ if option[:diskindex].nil?
117
+ system("vmdvd -help");
118
+ option.clear
119
+ else
120
+ ret = $dvd.disable_index_dvd(option[:diskindex])
121
+ data_value="disable dvd success"
122
+ end
123
+ else
124
+ system("vmdvd -help");
125
+ option.clear
126
+ end
127
+
128
+ when "enable"
129
+ if option[:enabl_all_disks] == "enable_all_dvd"
130
+ ret = $dvd.enable_all_dvd
131
+ data_value="enable all dvd success"
132
+ else
133
+ system("vmdvd -help");
134
+ option.clear
135
+ end
136
+
137
+ when "remove"
138
+ if option[:remov_disks] == "remove_all_dvd"
139
+ ret = $dvd.remove_all_dvd
140
+ data_value="remove all dvd success"
141
+ elsif option[:remov_disks] == "remove_index_dvd"
142
+ if option[:diskindex].nil?
143
+ system("vmdvd -help");
144
+ option.clear
145
+ else
146
+ $dvd.remove_index_dvd(option[:diskindex])
147
+ data_value="remove dvd success"
148
+ end
149
+
150
+ end
151
+
152
+ when "mount"
153
+ if option[:mount_all_dvd] == "mount_all_dvd"
154
+ ret = $dvd.mount_all_dvd
155
+ data_value="mount all dvd success"
156
+ else
157
+ system("vmdvd -help");
158
+ option.clear
159
+ end
160
+
161
+ when "enject"
162
+ if option[:enject_dvd] == "eject_dvd"
163
+ ret = $dvd.eject_dvd
164
+ data_value="eject dvd success"
165
+ else
166
+ system("vmdvd -help");
167
+ option.clear
168
+ end
169
+
170
+ when "close"
171
+ if option[:close_dvd] == "close_dvd"
172
+ ret = $dvd.close_dvd
173
+ data_value="close dvd success"
174
+ else
175
+ system("vmdvd -help");
176
+ option.clear
177
+ end
178
+
179
+ else
180
+ system("vmdvd -help");
181
+ option.clear
182
+ end
183
+
184
+
185
+ if ret==false
186
+ resultfail["data"]="dvd operation fail"
187
+ retjson = JSON.generate resultfail
188
+ else
189
+ resultok["data"]=data_value
190
+ retjson = JSON.generate resultok
191
+ end
192
+
193
+ puts retjson unless option[:cmd].nil?
@@ -0,0 +1,172 @@
1
+ #!/usr/bin/env ruby
2
+ #encoding: utf-8
3
+ require "vmopt/network"
4
+ require "optparse"
5
+ require 'json'
6
+
7
+ option = {}
8
+ option_parser = OptionParser.new do | opts |
9
+ opts.banner = %q/
10
+ Help:
11
+ Description:
12
+ vmnetwork tool, use for network operation.
13
+ function:
14
+ 1.show_netinterface
15
+ 2.show_interface_stat
16
+ 3.adapter_opt
17
+ 4.adapter_rescan
18
+ 5.adapter_discript
19
+ 6.adapter_set_dhcp
20
+ 7.adapter_set_static
21
+
22
+ Example:
23
+ vmnetwork --query allinterface
24
+ vmnetwork --query interface_discrip
25
+ vmnetwork --query interface_stat --interface "本地连接 2"
26
+
27
+ vmnetwork --modify --interface "本地连接 2" --operation "禁用"
28
+ vmnetwork --modify --interface "本地连接 2" --operation "启用"
29
+ vmnetwork --modify --interface "本地连接 2" --operation "卸载"
30
+
31
+ vmnetwork --rescan allinterface
32
+
33
+ vmnetwork --set --linktype dhcp --interface "本地连接 2"
34
+ vmnetwork --set --linktype static --interface "本地连接 2" --addr 192.168.1.10 --mask 255.255.255.0 --gateway 192.168.1.1
35
+ /
36
+ #查询接口信息
37
+ opts.on('-q queryopt', '--query queryopt', 'Query network information') do |value|
38
+ option[:queryopt] = value
39
+ option[:cmd] = "query"
40
+ end
41
+
42
+ #改变接口状态
43
+ opts.on('-m', '--modify', 'modify network status') do
44
+ option[:cmd] = "modify"
45
+ end
46
+
47
+ #重新扫描硬件
48
+ opts.on('-r allinterface', '--rescan allinterface', 'rescan network adapter') do |value|
49
+ option[:rescan] = value
50
+ option[:cmd] = "rescan"
51
+ end
52
+
53
+ #获取操作
54
+ opts.on('-o opera', '--operation opera', 'Specify a network interface') do |value|
55
+ option[:opera] = value
56
+ end
57
+
58
+ #获取接口名称
59
+ opts.on('-i inf', '--interface inf', 'Specify a network interface') do |value|
60
+ option[:inf] = value
61
+ end
62
+
63
+ #设置接口
64
+ opts.on('-s', '--set', 'Set ip for network interface') do
65
+ option[:cmd] = "set"
66
+ end
67
+
68
+ #设置连接方式
69
+ opts.on('-l type', '--linktype type', 'set linktype(dhcp or static) to netinterface') do|value|
70
+ option[:linktype] = value
71
+ end
72
+
73
+ #设置ip地址
74
+ opts.on('-a address', '--addr address', 'set linktype = static and set ip address') do|value|
75
+ option[:address] = value
76
+ end
77
+
78
+ #设置掩码
79
+ opts.on('-m netmask', '--mask netmask', 'set linktype = static and set netmask') do|value|
80
+ option[:netmask] = value
81
+ end
82
+
83
+ #设置网关
84
+ opts.on('-g gateway', '--gateway gateway', 'set linktype = static and set gateway') do|value|
85
+ option[:gateway] = value
86
+ end
87
+
88
+ end.parse!
89
+
90
+ $result_json = {};
91
+
92
+ def param_error
93
+ $result_json = {:status => "-1",:data=>"Please check the params..."}
94
+ end
95
+
96
+ begin
97
+
98
+ case option[:cmd]
99
+ #查询接口
100
+ when "query"
101
+ if option[:queryopt] == 'allinterface'
102
+ data = Vmopt::NetWork.show_netinterface
103
+ $result_json = {:status => "0", :data => data}
104
+ elsif option[:queryopt] == 'interface_discrip'
105
+ data = Vmopt::NetWork.adapter_discript
106
+ $result_json = {:status => "0", :data => data}
107
+ elsif option[:queryopt] == 'interface_stat'
108
+ if !option[:inf].nil?
109
+ data = Vmopt::NetWork.show_interface_stat(option[:inf])
110
+ $result_json = {:status => "0", :data => data}
111
+ else
112
+ param_error
113
+ end
114
+
115
+ else
116
+ param_error
117
+ end
118
+
119
+ #改变接口状态
120
+ when "modify"
121
+ if !option[:inf].nil? and !option[:opera].nil?
122
+ stat = Vmopt::NetWork.adapter_opt(option[:inf],option[:opera]);
123
+ status = stat ? 0 : -1 ;
124
+ $result_json ={:status=>"#{status}", :data=>"modify interface #{option[:inf].to_utf8} to #{option[:opera].to_utf8}"}
125
+ else
126
+ param_error
127
+ end
128
+
129
+ #重新扫描
130
+ when "rescan"
131
+ if option[:rescan] == "allinterface"
132
+ stat = Vmopt::NetWork.adapter_rescan
133
+ status = stat ? 0 : -1 ;
134
+ $result_json ={:status=>"#{status}", :data=>"rescan all hardware"}
135
+ else
136
+ param_error
137
+ end
138
+
139
+ #设置网卡IP
140
+ when "set"
141
+ if option[:linktype] == "dhcp"
142
+ if !option[:inf].nil?
143
+ stat = Vmopt::NetWork.adapter_set_dhcp(option[:inf])
144
+ status = stat ? 0 : -1 ;
145
+ $result_json ={:status=>"#{status}", :data=>"set interface #{option[:inf].to_utf8} with dhcp"}
146
+ else
147
+ param_error
148
+ end
149
+
150
+ elsif option[:linktype] == "static"
151
+ if !option[:inf].nil? and !option[:address].nil? and !option[:netmask].nil? and !option[:gateway].nil?
152
+ op = {}
153
+ op[:addr] = option[:address]
154
+ op[:mask] = option[:netmask]
155
+ op[:gateway]= option[:gateway]
156
+ stat = Vmopt::NetWork.adapter_set_static(option[:inf], op)
157
+ status = stat ? 0 : -1 ;
158
+ $result_json ={:status=>"#{status}", :data=>"set interface #{option[:inf].to_utf8} with static IP option"}
159
+ else
160
+ param_error
161
+ end
162
+
163
+ else
164
+ system("vmnetwork --help");
165
+ end
166
+ end
167
+ rescue Exception => e
168
+ $result_json = {:status => "-1", :data=>"Catch exception #{e}"}
169
+ end
170
+ puts JSON.generate($result_json) unless option[:cmd].nil?
171
+
172
+