From 3c2ea57eee44c2159d38f4fdd8dbb05e0e9e1d23 Mon Sep 17 00:00:00 2001 From: DeeJay Date: Tue, 1 Apr 2014 20:01:29 +0530 Subject: [PATCH] Puppet provisioning fixes. --- Rakefile | 9 +++ lib/vagrant-azure/action.rb | 8 +++ lib/vagrant-azure/action/provision.rb | 2 + lib/vagrant-azure/action/read_ssh_info.rb | 2 +- lib/vagrant-azure/driver.rb | 13 +++-- lib/vagrant-azure/plugin.rb | 5 +- lib/vagrant-azure/provider.rb | 5 ++ lib/vagrant-azure/provisioner/puppet.rb | 2 +- lib/vagrant-azure/scripts/run_in_remote.ps1 | 3 +- lib/vagrant-azure/scripts/upload_file.ps1 | 55 ++++++++++--------- .../scripts/utils/create_session.ps1 | 8 +-- vagrant-azure.gemspec | 37 ++++++++++++- 12 files changed, 108 insertions(+), 41 deletions(-) create mode 100644 Rakefile diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..981a7ae --- /dev/null +++ b/Rakefile @@ -0,0 +1,9 @@ +require 'rubygems' +require 'bundler/setup' + +$stdout.sync = true +$stderr.sync = true + +Dir.chdir(File.expand_path('../', __FILE__)) + +Bundler::GemHelper.install_tasks diff --git a/lib/vagrant-azure/action.rb b/lib/vagrant-azure/action.rb index 8caf4e9..704aa89 100644 --- a/lib/vagrant-azure/action.rb +++ b/lib/vagrant-azure/action.rb @@ -90,6 +90,14 @@ module VagrantPlugins end end + def self.action_read_winrm_info + Vagrant::Action::Builder.new.tap do |b| + b.use ConfigValidate + b.use ConnectAzure + b.use ReadSSHInfo, 5986 + end + end + def self.action_read_state Vagrant::Action::Builder.new.tap do |b| b.use ConfigValidate diff --git a/lib/vagrant-azure/action/provision.rb b/lib/vagrant-azure/action/provision.rb index cac29b8..8eb6689 100644 --- a/lib/vagrant-azure/action/provision.rb +++ b/lib/vagrant-azure/action/provision.rb @@ -2,6 +2,8 @@ # Copyright (c) Microsoft Open Technologies, Inc. # All Rights Reserved. Licensed under the Apache 2.0 License. #--------------------------------------------------------------------------- +require_relative '../provisioner/puppet' + module VagrantPlugins module WinAzure module Action diff --git a/lib/vagrant-azure/action/read_ssh_info.rb b/lib/vagrant-azure/action/read_ssh_info.rb index 1338e44..6dcd837 100644 --- a/lib/vagrant-azure/action/read_ssh_info.rb +++ b/lib/vagrant-azure/action/read_ssh_info.rb @@ -39,7 +39,7 @@ module VagrantPlugins vm.tcp_endpoints.each do |endpoint| if endpoint[:local_port] == "#{@port}" - return { :host => endpoint[:vip], :port => endpoint[:public_port] } + return { :host => "#{vm.cloud_service_name}.cloudapp.net", :port => endpoint[:public_port] } end end diff --git a/lib/vagrant-azure/driver.rb b/lib/vagrant-azure/driver.rb index 0a74c62..c5ed1f7 100644 --- a/lib/vagrant-azure/driver.rb +++ b/lib/vagrant-azure/driver.rb @@ -15,12 +15,16 @@ module VagrantPlugins end def ssh_info - @ssh_info ||= machine.ssh_info + @ssh_info ||= @machine.provider.winrm_info + @ssh_info[:username] ||= @machine.config.ssh.username + @ssh_info[:password] ||= @machine.config.ssh.password + @ssh_info end def remote_credentials @remote_credentials ||= { guest_ip: ssh_info[:host], + guest_port: ssh_info[:port], username: ssh_info[:username], password: ssh_info[:password] } @@ -49,25 +53,24 @@ module VagrantPlugins def upload(from, to) options = { - vm_id: vm_id, host_path: windows_path(from), guest_path: windows_path(to) }.merge(remote_credentials) script_path = local_script_path('upload_file.ps1') - execute(script_path, remote_credentials) + execute(script_path, options) end protected def local_script_path(path) lib_path = Pathname.new(File.expand_path('../scripts', __FILE__)) - lib_path.join(path).to_s + windows_path(lib_path.join(path).to_s) end def windows_path(path) if path - path = path.gsub('/', '\\') + path = path.gsub('/', "\\") path = "c:#{path}" if path =~ /^\\/ end path diff --git a/lib/vagrant-azure/plugin.rb b/lib/vagrant-azure/plugin.rb index 8ba463f..567fd6b 100644 --- a/lib/vagrant-azure/plugin.rb +++ b/lib/vagrant-azure/plugin.rb @@ -48,7 +48,10 @@ module VagrantPlugins 'locales/en.yml', WinAzure.source_root ) - + I18n.load_path << File.expand_path( + 'templates/locales/providers_hyperv.yml', + Vagrant.source_root + ) I18n.reload! end diff --git a/lib/vagrant-azure/provider.rb b/lib/vagrant-azure/provider.rb index 268942a..816e5fc 100644 --- a/lib/vagrant-azure/provider.rb +++ b/lib/vagrant-azure/provider.rb @@ -44,6 +44,11 @@ module VagrantPlugins env[:machine_ssh_info] end + def winrm_info + env = @machine.action('read_winrm_info') + env[:machine_ssh_info] + end + def state # Run a custom action we define called "read_state" which does what it # says. It puts the state in the `:machine_state_id` key in the env diff --git a/lib/vagrant-azure/provisioner/puppet.rb b/lib/vagrant-azure/provisioner/puppet.rb index 67da955..ae0b0dc 100644 --- a/lib/vagrant-azure/provisioner/puppet.rb +++ b/lib/vagrant-azure/provisioner/puppet.rb @@ -90,7 +90,7 @@ module VagrantPlugins manifest: config.manifest_file ) @env[:ui].info 'Executing puppet script in Windows Azure VM' - @env[:machine].provdier.driver.run_remote_ps(command) do |type, data| + @env[:machine].provider.driver.run_remote_ps(command) do |type, data| # Output the data with the proper color based on the stream. if (type == :stdout || type == :stderr) @env[:ui].detail data diff --git a/lib/vagrant-azure/scripts/run_in_remote.ps1 b/lib/vagrant-azure/scripts/run_in_remote.ps1 index b62ec27..1729b60 100644 --- a/lib/vagrant-azure/scripts/run_in_remote.ps1 +++ b/lib/vagrant-azure/scripts/run_in_remote.ps1 @@ -5,6 +5,7 @@ param ( [string]$guest_ip = $(throw "-guest_ip is required."), + [string]$guest_port = $(throw "-guest_port is required."), [string]$username = $(throw "-guest_username is required."), [string]$password = $(throw "-guest_password is required."), [string]$command = "" @@ -16,7 +17,7 @@ $presentDir = Split-Path -parent $PSCommandPath . ([System.IO.Path]::Combine($presentDir, "utils\create_session.ps1")) try { - $response = Create-Remote-Session $guest_ip $username $password + $response = Create-Remote-Session $guest_ip $guest_port $username $password if (!$response["session"] -and $response["error"]) { Write-Host $response["error"] return diff --git a/lib/vagrant-azure/scripts/upload_file.ps1 b/lib/vagrant-azure/scripts/upload_file.ps1 index 591c719..48f87ff 100644 --- a/lib/vagrant-azure/scripts/upload_file.ps1 +++ b/lib/vagrant-azure/scripts/upload_file.ps1 @@ -4,10 +4,10 @@ #-------------------------------------------------------------------------- param ( - [string]$vm_id = $(throw "-vm_id is required."), [string]$host_path = $(throw "-host_path is required."), [string]$guest_path = $(throw "-guest_path is required."), [string]$guest_ip = $(throw "-guest_ip is required."), + [string]$guest_port = $(throw "-guest_port is required."), [string]$username = $(throw "-guest_username is required."), [string]$password = $(throw "-guest_password is required.") ) @@ -18,29 +18,21 @@ $presentDir = Split-Path -parent $PSCommandPath . ([System.IO.Path]::Combine($presentDir, "utils\create_session.ps1")) try { - # Enable Guest Service Interface if they are disabled - try { - Get-VM -Id $vm_id | Get-VMIntegrationService -Name "Guest Service Interface" | Enable-VMIntegrationService -Passthru + function Copy-File-To-VM($path, $content) { + if (!(Test-Path $path)) { + $folder = Split-Path $path + New-Item $folder -type directory -Force } - catch { } - function Upload-FIle-To-VM($host_path, $guest_path, $machine) { - Write-Host $host_path - Write-Host $guest_path - Copy-VMFile -VM $machine -SourcePath $host_path -DestinationPath $guest_path -CreateFullPath -FileSource Host -Force -ErrorAction stop + [IO.File]::WriteAllBytes($path, $content) } - function Prepare-Guest-Folder($guest_ip, $username, $password) { - $response = Create-Remote-Session $guest_ip $username $password - if (!$response["session"] -and $response["error"]) { - $errortHash = @{ - type = "PowerShellError" - message = $response["error"] - } - Write-Error-Message $errorResult - return - } - $session = $response["session"] + function Upload-FIle-To-VM($host_path, $guest_path, $session) { + $contents = [IO.File]::ReadAllBytes($host_path) + Invoke-Command -Session $session -ScriptBlock ${function:Copy-File-To-VM} -ArgumentList $guest_path,$contents + } + + function Prepare-Guest-Folder($session) { # Create the guest folder if not exist $result = Invoke-Command -Session $session -ScriptBlock ${function:Create-Guest-Folder} -ArgumentList $guest_path } @@ -61,25 +53,34 @@ try { New-Item "$guest_path" -type directory -Force } - $machine = Get-VM -Id $vm_id + $response = Create-Remote-Session $guest_ip $guest_port $username $password + if (!$response["session"] -and $response["error"]) { + $errortHash = @{ + type = "PowerShellError" + message = $response["error"] + } + Write-Error-Message $errorResult + return + } + $session = $response["session"] + # When Host path is a folder. # Find all files within it and copy to the Guest if (Test-Path $host_path -pathtype container) { # Open a remote PS Session with the guest - Prepare-Guest-Folder $guest_ip $username $password + Prepare-Guest-Folder $session # Copy all files from Host path to Guest Path Get-ChildItem $host_path -rec | Where-Object {$_.PSIsContainer -eq $false} | ForEach-Object -Process { - $file_name = $_.Fullname.Replace($host_path, "") + $folder = Split-Path $_.Fullname + $file_name = $_.Fullname.Replace($folder, "") $from = $host_path + $file_name $to = $guest_path + $file_name - # Write-Host $from - # Write-Host $to - Upload-FIle-To-VM $from $to $machine + Upload-FIle-To-VM $from $to $session } } elseif (Test-Path $host_path) { - Upload-FIle-To-VM $host_path $guest_path $machine + Upload-FIle-To-VM $host_path $guest_path $session } $resultHash = @{ message = "OK" diff --git a/lib/vagrant-azure/scripts/utils/create_session.ps1 b/lib/vagrant-azure/scripts/utils/create_session.ps1 index c918cfc..87a7cef 100644 --- a/lib/vagrant-azure/scripts/utils/create_session.ps1 +++ b/lib/vagrant-azure/scripts/utils/create_session.ps1 @@ -3,20 +3,20 @@ # All Rights Reserved. Licensed under the Apache 2.0 License. #-------------------------------------------------------------------------- -function Get-Remote-Session($guest_ip, $username, $password) { +function Get-Remote-Session($guest_ip, $guest_port, $username, $password) { $secstr = convertto-securestring -AsPlainText -Force -String $password $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr - New-PSSession -ComputerName $guest_ip -Credential $cred -ErrorAction "stop" + New-PSSession -ComputerName $guest_ip -Port $guest_port -Credential $cred -UseSSL -ErrorAction "stop" } -function Create-Remote-Session($guest_ip, $username, $password) { +function Create-Remote-Session($guest_ip, $guest_port, $username, $password) { $count = 0 $session_error = "" $session = "" do { $count++ try { - $session = Get-Remote-Session $guest_ip $username $password + $session = Get-Remote-Session $guest_ip $guest_port $username $password $session_error = "" } catch { diff --git a/vagrant-azure.gemspec b/vagrant-azure.gemspec index f562f14..6dfffb1 100644 --- a/vagrant-azure.gemspec +++ b/vagrant-azure.gemspec @@ -15,10 +15,45 @@ Gem::Specification.new do |s| s.require_paths = ["lib"] + # The following block of code determines the files that should be included + # in the gem. It does this by reading all the files in the directory where + # this gemspec is, and parsing out the ignored files from the gitignore. + # Note that the entire gitignore(5) syntax is not supported, specifically + # the "!" syntax, but it should mostly work correctly. + root_path = File.dirname(__FILE__) + all_files = Dir.chdir(root_path) { Dir.glob("**/{*,.*}") } + all_files.reject! { |file| [".", ".."].include?(File.basename(file)) } + gitignore_path = File.join(root_path, ".gitignore") + gitignore = File.readlines(gitignore_path) + gitignore.map! { |line| line.chomp.strip } + gitignore.reject! { |line| line.empty? || line =~ /^(#|!)/ } + + unignored_files = all_files.reject do |file| + # Ignore any directories, the gemspec only cares about files + next true if File.directory?(file) + + # Ignore any paths that match anything in the gitignore. We do + # two tests here: + # + # - First, test to see if the entire path matches the gitignore. + # - Second, match if the basename does, this makes it so that things + # like '.DS_Store' will match sub-directories too (same behavior + # as git). + # + gitignore.any? do |ignore| + File.fnmatch(ignore, file, File::FNM_PATHNAME) || + File.fnmatch(ignore, File.basename(file), File::FNM_PATHNAME) + end + end + + s.files = unignored_files + s.executables = unignored_files.map { |f| f[/^bin\/(.*)/, 1] }.compact + + s.add_runtime_dependency "azure", "0.6.3" + s.add_development_dependency "bundler", "~> 1.3" s.add_development_dependency "rake" s.add_development_dependency "minitest" s.add_development_dependency "minitest-reporters" s.add_development_dependency "mocha" - s.add_development_dependency "azure", "0.6.2" end