vagrant-windows-hyperv 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/Gemfile +15 -0
  4. data/LICENSE.txt +7 -0
  5. data/README.md +89 -0
  6. data/Rakefile +29 -0
  7. data/example_box/metadata.json +1 -0
  8. data/lib/vagrant-windows-hyperv.rb +38 -0
  9. data/lib/vagrant-windows-hyperv/action.rb +81 -0
  10. data/lib/vagrant-windows-hyperv/action/export.rb +54 -0
  11. data/lib/vagrant-windows-hyperv/action/package.rb +21 -0
  12. data/lib/vagrant-windows-hyperv/action/rdp.rb +49 -0
  13. data/lib/vagrant-windows-hyperv/action/setup_package_files.rb +55 -0
  14. data/lib/vagrant-windows-hyperv/command/rdp/command.rb +22 -0
  15. data/lib/vagrant-windows-hyperv/communication/powershell.rb +37 -0
  16. data/lib/vagrant-windows-hyperv/driver.rb +89 -0
  17. data/lib/vagrant-windows-hyperv/errors.rb +31 -0
  18. data/lib/vagrant-windows-hyperv/guest/cap/halt.rb +19 -0
  19. data/lib/vagrant-windows-hyperv/guest/windows.rb +25 -0
  20. data/lib/vagrant-windows-hyperv/monkey_patch/action/provision.rb +32 -0
  21. data/lib/vagrant-windows-hyperv/monkey_patch/machine.rb +22 -0
  22. data/lib/vagrant-windows-hyperv/monkey_patch/plugins/synced_folders/smb/synced_folders.rb +55 -0
  23. data/lib/vagrant-windows-hyperv/monkey_patch/util/powershell.rb +37 -0
  24. data/lib/vagrant-windows-hyperv/plugin.rb +85 -0
  25. data/lib/vagrant-windows-hyperv/provider.rb +30 -0
  26. data/lib/vagrant-windows-hyperv/provisioner/chef_solo.rb +181 -0
  27. data/lib/vagrant-windows-hyperv/provisioner/puppet.rb +99 -0
  28. data/lib/vagrant-windows-hyperv/provisioner/shell.rb +81 -0
  29. data/lib/vagrant-windows-hyperv/scripts/check_winrm.ps1 +41 -0
  30. data/lib/vagrant-windows-hyperv/scripts/export_vm.ps1 +31 -0
  31. data/lib/vagrant-windows-hyperv/scripts/file_sync.ps1 +145 -0
  32. data/lib/vagrant-windows-hyperv/scripts/host_info.ps1 +25 -0
  33. data/lib/vagrant-windows-hyperv/scripts/hyperv_manager.ps1 +36 -0
  34. data/lib/vagrant-windows-hyperv/scripts/run_in_remote.ps1 +31 -0
  35. data/lib/vagrant-windows-hyperv/scripts/upload_file.ps1 +95 -0
  36. data/lib/vagrant-windows-hyperv/scripts/utils/create_session.ps1 +34 -0
  37. data/lib/vagrant-windows-hyperv/scripts/utils/write_messages.ps1 +18 -0
  38. data/lib/vagrant-windows-hyperv/version.rb +10 -0
  39. data/locales/en.yml +15 -0
  40. data/spec/hyper-v/config_spec.rb +36 -0
  41. data/spec/hyper-v/spec_helper.rb +9 -0
  42. data/templates/provisioners/chef-solo/solo.erb +51 -0
  43. data/vagrant-windows-hyperv.gemspec +63 -0
  44. data/vagrantfile_examples/Vagrantfile_linux +23 -0
  45. data/vagrantfile_examples/Vagrantfile_windows +24 -0
  46. metadata +171 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bf9197f8b3d059f4d8117de0e615f1ea3dbc025a
4
+ data.tar.gz: f81e610a7fb874b0948b7b4b15b59654b8500f7a
5
+ SHA512:
6
+ metadata.gz: 01eae9a2ec5400761d2d0ed60924a60ca67a0de51ba24eeb653b53a39cdb276d4ea6bc6ae671b62f1f585ec45f7460357e4522bd745630814aa675225c8de9c8
7
+ data.tar.gz: 4114c5e863fc4af4726e3331f6324cb59385035263294591951ed53aa0b0868d67415a6096bce8729cf900eb92f59cc5de387558ab6ae2a81d229cf112fe2562
@@ -0,0 +1,19 @@
1
+ # OS-specific
2
+ .DS_Store
3
+
4
+ # editors
5
+ *.swp
6
+
7
+ # Bundler/Rubygems
8
+ *.gem
9
+ .bundle
10
+ pkg/*
11
+ tags
12
+ Gemfile.lock
13
+
14
+ # Vagrant
15
+ .vagrant
16
+ Vagrantfile
17
+
18
+ # Ignore RDP files
19
+ *.rdp
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ #-------------------------------------------------------------------------
2
+ # Copyright (c) Microsoft Open Technologies, Inc.
3
+ # All Rights Reserved. Licensed under the Apache 2.0 License.
4
+ #--------------------------------------------------------------------------
5
+ source 'https://rubygems.org'
6
+
7
+ # Specify your gem's dependencies in vagrant-windows-hyperv.gemspec
8
+ gemspec
9
+
10
+ group :development do
11
+ # We depend on Vagrant for development, but we don't add it as a
12
+ # gem dependency because we expect to be installed within the
13
+ # Vagrant environment itself using `vagrant plugin`.
14
+ gem "vagrant", :git => "git://github.com/mitchellh/vagrant.git"
15
+ end
@@ -0,0 +1,7 @@
1
+ Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use these files except in
3
+ compliance with the License. You may obtain a copy of the License at
4
+ http://www.apache.org/licenses/LICENSE-2.0
5
+ Unless required by applicable law or agreed to in writing, software distributed under the License is
6
+ distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7
+ See the License for the specific language governing permissions and limitations under the License.
@@ -0,0 +1,89 @@
1
+ # Vagrant Windows Hyper-V Provider
2
+
3
+ Vagrant is a tool for building complete development environments, sandboxed in a virtual machine. It helps enforce good practices by encouraging the use of automation so that development environments are as close to production as possible.
4
+
5
+ Hyper-V provider is available by default in Vagrant with version 1.5 and up. This plugin is created to provide windows guest support for the Vagrant Hyper-V provider.
6
+
7
+
8
+ ## Installation
9
+ Install Vagrant 1.5 (or higher)
10
+ http://www.vagrantup.com/downloads.html
11
+
12
+
13
+ Install plugin like so:
14
+ vagrant plugin install vagrant-windows-hyperv
15
+
16
+
17
+ ## Configuration settings
18
+ For more information on general configuration settings please visit the Vagrant Documentation.
19
+ http://docs.vagrantup.com/v2/
20
+
21
+
22
+ ##Setting specific to this plugin
23
+
24
+ ### Sync Folder configuration
25
+
26
+ With Vagrant 1.5 SMB share is available by default and vagrant picks the most suitable
27
+ implementation for the providers.
28
+
29
+ You can even specify the following options
30
+
31
+ :smb_id Specify a unique share name, with which the network share will be available. By default vagrant will generate a smb_id if not specified.
32
+
33
+
34
+
35
+
36
+ ```ruby
37
+ # Mounts the host/path to guest/path and will have realtime sync
38
+ config.vm.synced_folder 'C:/test_sync_2', "C:/Users/vagrant/test_sync_2"
39
+
40
+ ```
41
+ ### VM Type
42
+ * Set the VM guest type
43
+
44
+ ```ruby
45
+ config.vm.guest = :windows
46
+ ```
47
+
48
+ ### Override user name
49
+ * If you're building a custom box and want to customize the user name, you can use the hyper-v custom block to add it in
50
+
51
+ ```ruby
52
+ config.vm.provider "hyperv" do |hv, override|
53
+ override.ssh.username = "myuser"
54
+ end
55
+ ```
56
+ ### New RDP Command
57
+ * A new command vagrant rdp is introduced to connect to the Windows VM
58
+ ` vagrant rdp `
59
+
60
+
61
+ ### Provision Command
62
+ * Provision command works for Vagrant hyper-v with this plugin installed
63
+ ` vagrant provision `
64
+
65
+
66
+ ## Troubleshooting
67
+
68
+ ### Remote PowerShell
69
+ Vagrant-Windows-HyperV uses remote PowerShell to communicate with the guest VMs, so the guest VMs should have WinRM service running and remote PowerShell running.
70
+
71
+ To Enable remote PowerShell in the guest.
72
+ Go to the guest VM, open a cmd terminal and type the following command
73
+ ```
74
+ powershell Enable-PSRemoting –force
75
+ ```
76
+ ### Trustedhosts
77
+ With PowerShell being enabled in the remote VM, the host has to trust this guest to establish the connection.
78
+ One can add the guest IP under trustedhost. Here "*" can be used as a wildcard to trust all hosts, or replace the * with the IP address of the guest..
79
+
80
+ Type this command from an Administrator cmdshell prompt
81
+
82
+ `
83
+ powershell set-item wsman:\localhost\client\trustedhosts *
84
+ `
85
+
86
+ ### SMB Share
87
+ There is a bug in the current vagrant release (1.5.1) you may run into collisions when you create shares. To avoid this, user smb_id option and make sure the share name does not collide with existing shares.
88
+
89
+ ` config.vm.synced_folder 'C:/test_sync_2', "C:/Users/vagrant/test_sync_2", :smb_id => "test_my_share" `
@@ -0,0 +1,29 @@
1
+ #-------------------------------------------------------------------------
2
+ # Copyright (c) Microsoft Open Technologies, Inc.
3
+ # All Rights Reserved. Licensed under the Apache 2.0 License.
4
+ #--------------------------------------------------------------------------
5
+ require 'rubygems'
6
+ require 'bundler/setup'
7
+ require 'rake/testtask'
8
+ Bundler::GemHelper.install_tasks
9
+
10
+ # Immediately sync all stdout so that tools like buildbot can
11
+ # immediately load in the output.
12
+ $stdout.sync = true
13
+ $stderr.sync = true
14
+
15
+ # Change to the directory of this file.
16
+ Dir.chdir(File.expand_path("../", __FILE__))
17
+
18
+ # This installs the tasks that help with gem creation and
19
+ # publishing.
20
+ Bundler::GemHelper.install_tasks
21
+
22
+ Rake::TestTask.new do |t|
23
+ t.pattern = 'spec/**/*_spec.rb'
24
+ end
25
+
26
+ # Default task is to run the unit tests
27
+ task :default => "spec"
28
+
29
+
@@ -0,0 +1 @@
1
+ { "provider" : "hyperv"}
@@ -0,0 +1,38 @@
1
+ #-------------------------------------------------------------------------
2
+ # Copyright (c) Microsoft Open Technologies, Inc.
3
+ # All Rights Reserved. Licensed under the Apache 2.0 License.
4
+ #--------------------------------------------------------------------------
5
+
6
+ require "pathname"
7
+ require "vagrant-windows-hyperv/plugin"
8
+
9
+ module VagrantPlugins
10
+ module VagrantHyperV
11
+ lib_path = Pathname.new(File.expand_path("../vagrant-windows-hyperv", __FILE__))
12
+ autoload :Action, lib_path.join("action")
13
+ autoload :Errors, lib_path.join("errors")
14
+ autoload :Driver, lib_path.join("driver")
15
+
16
+ # Load a communicator for Windows guest
17
+ require lib_path.join("communication/powershell")
18
+
19
+ # Include the provision scripts for Windows
20
+ require lib_path.join("provisioner/shell")
21
+ require lib_path.join("provisioner/puppet")
22
+ require lib_path.join("provisioner/chef_solo")
23
+
24
+ monkey_patch = Pathname.new(File.expand_path("../vagrant-windows-hyperv/monkey_patch", __FILE__))
25
+ # Monkey Patch the core Hyper-V vagrant with the following
26
+ require monkey_patch.join("plugins/synced_folders/smb/synced_folders")
27
+ require monkey_patch.join("action/provision")
28
+ require monkey_patch.join("machine")
29
+ require monkey_patch.join("util/powershell")
30
+
31
+ # This returns the path to the source of this plugin.
32
+ #
33
+ # @return [Pathname]
34
+ def self.source_root
35
+ @source_root ||= Pathname.new(File.expand_path("../../", __FILE__))
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,81 @@
1
+ #-------------------------------------------------------------------------
2
+ # Copyright (c) Microsoft Open Technologies, Inc.
3
+ # All Rights Reserved. Licensed under the Apache 2.0 License.
4
+ #--------------------------------------------------------------------------
5
+
6
+ require "pathname"
7
+ require "vagrant/action/builder"
8
+
9
+ module VagrantPlugins
10
+ module VagrantHyperV
11
+ module Action
12
+ # Include the built-in modules so we can use them as top-level things.
13
+ include Vagrant::Action::Builtin
14
+
15
+ def self.action_package
16
+ Vagrant::Action::Builder.new.tap do |b|
17
+ b.use Call, IsState, :not_created do |env1, b2|
18
+ if env1[:result]
19
+ b2.use Message, I18n.t("vagrant_hyperv.message_not_created")
20
+ next
21
+ end
22
+ b2.use SetupPackageFiles
23
+ b2.use Call, GracefulHalt, :off, :running do |env2, b3|
24
+ if !env2[:result]
25
+ b3.use StopInstance
26
+ end
27
+ end
28
+ b2.use Export
29
+ b2.use Package
30
+ end
31
+ end
32
+ end
33
+
34
+ def self.action_rdp
35
+ Vagrant::Action::Builder.new.tap do |b|
36
+ b.use ConfigValidate
37
+ b.use Call, IsState, :not_created do |env, b2|
38
+ if env[:result]
39
+ b2.use Message, I18n.t("vagrant_hyperv.message_not_created")
40
+ next
41
+ end
42
+
43
+ b2.use Call, IsState, :running do |env1, b3|
44
+ if !env1[:result]
45
+ b3.use Message, I18n.t("vagrant_win_hyperv.message_rdp_not_ready")
46
+ next
47
+ end
48
+
49
+ b3.use Rdp
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ def self.action_provision
56
+ Vagrant::Action::Builder.new.tap do |b|
57
+ b.use ConfigValidate
58
+ b.use Call, IsState, :not_created do |env1, b1|
59
+ if env1[:result]
60
+ b1.use Message, I18n.t("vagrant_hyperv.message_not_created")
61
+ next
62
+ end
63
+ b1.use Call, IsState, :running do |env2, b2|
64
+ if !env2[:result]
65
+ b2.use Message, I18n.t("vagrant_hyperv.message_not_running")
66
+ else
67
+ b2.use Provision
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ # The autoload farm
75
+ action_root = Pathname.new(File.expand_path("../action", __FILE__))
76
+ autoload :Export, action_root.join("export")
77
+ autoload :Package, action_root.join("package")
78
+ autoload :Rdp, action_root.join("rdp")
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,54 @@
1
+ #-------------------------------------------------------------------------
2
+ # Copyright (c) Microsoft Open Technologies, Inc.
3
+ # All Rights Reserved. Licensed under the Apache 2.0 License.
4
+ #--------------------------------------------------------------------------
5
+
6
+ require "fileutils"
7
+
8
+ module VagrantPlugins
9
+ module VagrantHyperV
10
+ module Action
11
+ class Export
12
+ attr_reader :temp_dir
13
+ def initialize(app, env)
14
+ @app = app
15
+ end
16
+
17
+ def call(env)
18
+ @env = env
19
+
20
+ raise "Please off the machine before package" if \
21
+ @env[:machine].provider.state.id != :off
22
+
23
+ @temp_dir = env[:tmp_path].join(Time.now.to_i.to_s)
24
+ export
25
+ add_metadata_json
26
+
27
+ @app.call(env)
28
+
29
+ recover(env) # called to cleanup temp directory
30
+ end
31
+
32
+ def recover(env)
33
+ if temp_dir && File.exist?(temp_dir)
34
+ FileUtils.rm_rf(temp_dir)
35
+ end
36
+ end
37
+
38
+ def export
39
+ @env[:ui].info('Exporting the VM, this process may take a while.')
40
+ result = @env[:machine].provider.driver.export_vm_to(temp_dir.to_s)
41
+ # Hyper-V Exports the VM under the VM's name in to the temp directory.
42
+ # Set the package directory to this folder, all files should go into this folder
43
+ @env["package.directory"] = temp_dir.join(result["name"])
44
+ end
45
+
46
+ def add_metadata_json
47
+ File.open(File.join(@env["package.directory"], "metadata.json"), "w") do |f|
48
+ f.write(' { "provider" : "hyperv" }')
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,21 @@
1
+ #-------------------------------------------------------------------------
2
+ # Copyright (c) Microsoft Open Technologies, Inc.
3
+ # All Rights Reserved. Licensed under the Apache 2.0 License.
4
+ #--------------------------------------------------------------------------
5
+
6
+ require 'vagrant/action/general/package'
7
+
8
+ module VagrantPlugins
9
+ module VagrantHyperV
10
+ module Action
11
+ class Package < Vagrant::Action::General::Package
12
+ # Doing this so that we can test that the parent is properly
13
+ # called in the unit tests.
14
+ alias_method :general_call, :call
15
+ def call(env)
16
+ general_call(env)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,49 @@
1
+ #-------------------------------------------------------------------------
2
+ # Copyright (c) Microsoft Open Technologies, Inc.
3
+ # All Rights Reserved. Licensed under the Apache 2.0 License.
4
+ #--------------------------------------------------------------------------
5
+
6
+ require "log4r"
7
+ require "pathname"
8
+ require "vagrant/util/powershell"
9
+
10
+ module VagrantPlugins
11
+ module VagrantHyperV
12
+ module Action
13
+ # This action generates a .rdp file into the root path of the project.
14
+ # and establishes a RDP session with necessary resource sharing
15
+ class Rdp
16
+ def initialize(app, env)
17
+ @app = app
18
+ @logger = Log4r::Logger.new("vagrant::hyperv::rdp")
19
+ end
20
+
21
+ def call(env)
22
+ if env[:machine].config.vm.guest != :windows
23
+ raise Errors::RDPNotAvailable,
24
+ guest: env[:machine].config.vm.guest
25
+ end
26
+ @env = env
27
+ @env[:ui].detail I18n.t("vagrant_win_hyperv.generating_rdp")
28
+ generate_rdp_file
29
+ command = ["mstsc", "machine.rdp"]
30
+ Vagrant::Util::PowerShell.execute(*command)
31
+ end
32
+
33
+ def generate_rdp_file
34
+ ssh_info = @env[:machine].ssh_info
35
+ rdp_options = {
36
+ "username:s" => ssh_info[:username],
37
+ "prompt for credentials:i" => "1",
38
+ "full address:s" => ssh_info[:host]
39
+ }
40
+ file = File.open("machine.rdp", "w")
41
+ rdp_options.each do |key, value|
42
+ file.puts "#{key}:#{value}"
43
+ end
44
+ file.close
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,55 @@
1
+ #-------------------------------------------------------------------------
2
+ # Copyright (c) Microsoft Open Technologies, Inc.
3
+ # All Rights Reserved. Licensed under the Apache 2.0 License.
4
+ #--------------------------------------------------------------------------
5
+
6
+ module VagrantPlugins
7
+ module VagrantHyperV
8
+ module Action
9
+ class SetupPackageFiles
10
+ def initialize(app, env)
11
+ @app = app
12
+
13
+ env["package.include"] ||= []
14
+ env["package.vagrantfile"] ||= nil
15
+ end
16
+
17
+ def call(env)
18
+ files = {}
19
+ env["package.include"].each do |file|
20
+ source = Pathname.new(file)
21
+ dest = nil
22
+
23
+ # If the source is relative then we add the file as-is to the include
24
+ # directory. Otherwise, we copy only the file into the root of the
25
+ # include directory. Kind of strange, but seems to match what people
26
+ # expect based on history.
27
+ if source.relative?
28
+ dest = source
29
+ else
30
+ dest = source.basename
31
+ end
32
+
33
+ # Assign the mapping
34
+ files[file] = dest
35
+ end
36
+
37
+ if env["package.vagrantfile"]
38
+ # Vagrantfiles are treated special and mapped to a specific file
39
+ files[env["package.vagrantfile"]] = "_Vagrantfile"
40
+ end
41
+
42
+ # Verify the mapping
43
+ files.each do |from, _|
44
+ raise "File do not exist #{file}" if !File.exist?(from)
45
+ end
46
+
47
+ # Save the mapping
48
+ env["package.files"] = files
49
+
50
+ @app.call(env)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end