vagrant-hvinfo 0.1.2 → 0.1.3
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 +4 -4
 - data/lib/command.rb +91 -87
 - data/vagrant-hvinfo.gemspec +1 -1
 - metadata +2 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: f529609cac2efbddd376667df2173de296b63a3f
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 5d652008de61499873e3cfd80610c2637536c385
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 2402667aa0f34cfc0ce3d1cf8ae3f1727d62f59057ca529ce56d3dd14cc8275f5b6c3555e48b0ecbf7e88a73ea2024a7a8e09fd6b77774f7e884846e2de701c7
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 12db4b36412d3ccb21c822725e18efb2e65f680b24d01b8c7d8bc896a7011d4b3cf4dfea504418076e46bf4c4e38c20fd3ec31209e37882c00923a5eb0805074
         
     | 
    
        data/lib/command.rb
    CHANGED
    
    | 
         @@ -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 
     | 
    
         
            -
             
     | 
| 
       60 
     | 
    
         
            -
             
     | 
| 
       61 
     | 
    
         
            -
             
     | 
| 
       62 
     | 
    
         
            -
                        file. 
     | 
| 
       63 
     | 
    
         
            -
             
     | 
| 
       64 
     | 
    
         
            -
                         
     | 
| 
       65 
     | 
    
         
            -
                      rescue  
     | 
| 
       66 
     | 
    
         
            -
                        puts "Failed to write results to file #{argv[0]}, error: #{e.inspect}"
         
     | 
| 
       67 
     | 
    
         
            -
                       
     | 
| 
       68 
     | 
    
         
            -
                        file 
     | 
| 
       69 
     | 
    
         
            -
                       
     | 
| 
       70 
     | 
    
         
            -
             
     | 
| 
       71 
     | 
    
         
            -
             
     | 
| 
       72 
     | 
    
         
            -
             
     | 
| 
       73 
     | 
    
         
            -
             
     | 
| 
       74 
     | 
    
         
            -
             
     | 
| 
       75 
     | 
    
         
            -
                   
     | 
| 
       76 
     | 
    
         
            -
             
     | 
| 
       77 
     | 
    
         
            -
             
     | 
| 
       78 
     | 
    
         
            -
             
     | 
| 
       79 
     | 
    
         
            -
             
     | 
| 
       80 
     | 
    
         
            -
                     
     | 
| 
       81 
     | 
    
         
            -
             
     | 
| 
       82 
     | 
    
         
            -
                     
     | 
| 
       83 
     | 
    
         
            -
             
     | 
| 
       84 
     | 
    
         
            -
             
     | 
| 
       85 
     | 
    
         
            -
             
     | 
| 
       86 
     | 
    
         
            -
             
     | 
| 
       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 
     | 
    
         
            +
             
     | 
    
        data/vagrant-hvinfo.gemspec
    CHANGED
    
    | 
         @@ -1,6 +1,6 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            Gem::Specification.new do |spec|
         
     | 
| 
       2 
2 
     | 
    
         
             
              spec.name          = 'vagrant-hvinfo'
         
     | 
| 
       3 
     | 
    
         
            -
              spec.version       = '0.1. 
     | 
| 
      
 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. 
     | 
| 
      
 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- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2018-11-21 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies: []
         
     | 
| 
       13 
13 
     | 
    
         
             
            description: 
         
     | 
| 
       14 
14 
     | 
    
         
             
            email:
         
     |