vagrant-hvinfo 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/command.rb +91 -87
  3. data/vagrant-hvinfo.gemspec +1 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2b2b16b11c81149bbff0b9d2f3f276063726abc9
4
- data.tar.gz: a7a018caa0362ae7a29cc7316347041da1412b5c
3
+ metadata.gz: f529609cac2efbddd376667df2173de296b63a3f
4
+ data.tar.gz: 5d652008de61499873e3cfd80610c2637536c385
5
5
  SHA512:
6
- metadata.gz: 7e159e12477709b98fb635938c0cfe5769696e3f2a8f19ac647eb9d1e8bb0ca8c4bf8d40e6dd84810db8ff795a12a52afa233d3328988fedd225f0cc906e95d0
7
- data.tar.gz: 3c476b4e661753f47cb5f4fd4c848c5341fb12e930df157495da60e2fdfe3c630a4155191c07d555afc2636c11e0219145888fee76bd4581545733bf463b5d47
6
+ metadata.gz: 2402667aa0f34cfc0ce3d1cf8ae3f1727d62f59057ca529ce56d3dd14cc8275f5b6c3555e48b0ecbf7e88a73ea2024a7a8e09fd6b77774f7e884846e2de701c7
7
+ data.tar.gz: 12db4b36412d3ccb21c822725e18efb2e65f680b24d01b8c7d8bc896a7011d4b3cf4dfea504418076e46bf4c4e38c20fd3ec31209e37882c00923a5eb0805074
@@ -1,87 +1,91 @@
1
- require 'optparse'
2
- require 'vagrant/util/powershell'
3
- require 'json'
4
- require_relative 'errors'
5
-
6
- module VagrantPlugins
7
- module CommandHVInfo
8
- class Command < Vagrant.plugin("2", :command)
9
- def self.synopsis
10
- "outputs information about Hyper-V VMs"
11
- end
12
-
13
- def execute
14
- if not Vagrant::Util::PowerShell.available?
15
- raise PowershellNotAvailable
16
- end
17
-
18
- opts = OptionParser.new do |o|
19
- o.banner = "Usage: vagrant hvinfo [output-file]"
20
- end
21
- argv = parse_options(opts)
22
- return if !argv
23
-
24
- # Get CWD:
25
- cwd_result = run_powershell_inline_json("(Get-Item -Path '.\\').FullName")
26
- cwd = cwd_result.stdout
27
-
28
- # Get IP Address and switch
29
- network_result = run_powershell_inline_json("Get-VM | Where-Object {$_.State -eq 'Running'} | Select -ExpandProperty NetworkAdapters | Select VMName, IPAddresses, SwitchName | ConvertTo-Json")
30
- network = JSON.parse(network_result.stdout)
31
-
32
- # Get Configuration Dir
33
- config_result = run_powershell_inline_json("Get-VM | Where-Object {$_.State -eq 'Running'} | Select VMName, ConfigurationLocation | ConvertTo-Json")
34
- config = JSON.parse(config_result.stdout)
35
-
36
- # Filter out VMs that are not managed by vagrant in this folder
37
- vagrant_vms = {}
38
- for vm in config
39
- vm_name = vm["VMName"]
40
- vm_config_path = vm["ConfigurationLocation"]
41
- if vm_config_path.strip.upcase.start_with?(cwd.strip.upcase)
42
- vagrant_vms[vm_name] = vm
43
- end
44
- end
45
-
46
- # add network properties to resulting hash
47
- for vm in network
48
- vm_name = vm["VMName"]
49
- if vagrant_vms.has_key?(vm_name)
50
- for key in vm.keys
51
- vagrant_vms[vm_name] = vagrant_vms[vm_name].merge(vm)
52
- end
53
- end
54
- end
55
-
56
- json_pretty = JSON.pretty_generate(vagrant_vms.values)
57
- if argv.length == 0
58
- puts json_pretty
59
- else
60
- begin
61
- file = File.open(argv[0], "w")
62
- file.write(json_pretty)
63
- rescue IOError => e
64
- puts "Failed to write results to file #{argv[0]}, error: #{e.inspect}"
65
- rescue Errno::ENOENT => e
66
- puts "Failed to write results to file #{argv[0]}, error: #{e.inspect}"
67
- ensure
68
- file.close unless file.nil?
69
- end
70
- end
71
- end
72
-
73
- private
74
-
75
- def run_powershell_inline_json(cmd)
76
- result = Vagrant::Util::PowerShell.execute_inline(cmd)
77
-
78
- if result.exit_code != 0
79
- raise Errors::PowershellCommandFailed.new cmd, result.exit_code, result.stdout, result.stderr
80
- end
81
-
82
- return result
83
- end
84
- end
85
- end
86
- end
87
-
1
+ require 'optparse'
2
+ require 'vagrant/util/powershell'
3
+ require 'json'
4
+ require_relative 'errors'
5
+
6
+ module VagrantPlugins
7
+ module CommandHVInfo
8
+ class Command < Vagrant.plugin("2", :command)
9
+ def self.synopsis
10
+ "outputs information about Hyper-V VMs"
11
+ end
12
+
13
+ def execute
14
+ if not Vagrant::Util::PowerShell.available?
15
+ raise PowershellNotAvailable
16
+ end
17
+
18
+ opts = OptionParser.new do |o|
19
+ o.banner = "Usage: vagrant hvinfo [output-file]"
20
+ end
21
+ argv = parse_options(opts)
22
+ return if !argv
23
+
24
+ # Get CWD:
25
+ cwd_result = run_powershell_inline_json("(Get-Item -Path '.\\').FullName")
26
+ cwd = cwd_result.stdout
27
+
28
+ # Get IP Address and switch
29
+ network_result = run_powershell_inline_json("Get-VM | Where-Object {$_.State -eq 'Running'} | Select -ExpandProperty NetworkAdapters | Select VMName, IPAddresses, SwitchName | ConvertTo-Json")
30
+ network = JSON.parse(network_result.stdout)
31
+
32
+ # Get Configuration Dir
33
+ config_result = run_powershell_inline_json("Get-VM | Where-Object {$_.State -eq 'Running'} | Select VMName, ConfigurationLocation | ConvertTo-Json")
34
+ config = JSON.parse(config_result.stdout)
35
+
36
+ # Filter out VMs that are not managed by vagrant in this folder
37
+ vagrant_vms = {}
38
+ for vm in config
39
+ vm_name = vm["VMName"]
40
+ vm_config_path = vm["ConfigurationLocation"]
41
+ if vm_config_path.strip.upcase.start_with?(cwd.strip.upcase)
42
+ vagrant_vms[vm_name] = vm
43
+ end
44
+ end
45
+
46
+ # add network properties to resulting hash
47
+ for vm in network
48
+ vm_name = vm["VMName"]
49
+ if vagrant_vms.has_key?(vm_name)
50
+ for key in vm.keys
51
+ vagrant_vms[vm_name] = vagrant_vms[vm_name].merge(vm)
52
+ end
53
+ end
54
+ end
55
+
56
+ json_pretty = JSON.pretty_generate(vagrant_vms.values)
57
+ if argv.length == 0
58
+ puts json_pretty
59
+ return 0
60
+ else
61
+ begin
62
+ file = File.open(argv[0], "w")
63
+ file.write(json_pretty)
64
+ return 0
65
+ rescue IOError => e
66
+ puts "Failed to write results to file #{argv[0]}, error: #{e.inspect}"
67
+ rescue Errno::ENOENT => e
68
+ puts "Failed to write results to file #{argv[0]}, error: #{e.inspect}"
69
+ ensure
70
+ file.close unless file.nil?
71
+ end
72
+
73
+ return -1 # something went wrong, maybe
74
+ end
75
+ end
76
+
77
+ private
78
+
79
+ def run_powershell_inline_json(cmd)
80
+ result = Vagrant::Util::PowerShell.execute_inline(cmd)
81
+
82
+ if result.exit_code != 0
83
+ raise Errors::PowershellCommandFailed.new cmd, result.exit_code, result.stdout, result.stderr
84
+ end
85
+
86
+ return result
87
+ end
88
+ end
89
+ end
90
+ end
91
+
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = 'vagrant-hvinfo'
3
- spec.version = '0.1.2'
3
+ spec.version = '0.1.3'
4
4
  spec.authors = ['Zhansong Li']
5
5
  spec.email = ['lizhansong@hvariant.com']
6
6
  spec.summary = 'Vagrant plugin for displaying information about Hyper-V VMs'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-hvinfo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zhansong Li
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-20 00:00:00.000000000 Z
11
+ date: 2018-11-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: