Created
June 11, 2019 09:26
-
-
Save xuhcc/17cd857891b1e46573894d849f399a2b to your computer and use it in GitHub Desktop.
windows_provision_plugin.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Based on https://github.com/danielmenezesbr/modernie-winrm/blob/master/ie-box-automation-plugin.rb | |
require 'rubygems' | |
require 'net/ssh' | |
require 'logger' | |
require 'log4r' | |
module LocalCommand | |
class IEBoxAutomationPlugin < Vagrant.plugin("2") | |
name "ie_box_automation" | |
config(:ie_box_automation, :provisioner) do | |
Config | |
end | |
provisioner(:ie_box_automation) do | |
Provisioner | |
end | |
end | |
class Config < Vagrant.plugin("2", :config) | |
attr_accessor :script | |
end | |
class Provisioner < Vagrant.plugin("2", :provisioner) | |
def initialize(machine, config) | |
super | |
@logger = Log4r::Logger.new("vagrant::ie_box_automation") | |
@script = config.script | |
end | |
def execute_command(ssh, command, show) | |
@logger.debug ("command: #{command}") | |
res = ssh.exec!(command) | |
@logger.debug ("result: #{res}") | |
puts res if show | |
end | |
def provision | |
ssh_info = nil | |
while true | |
ssh_info = @machine.ssh_info | |
break if ssh_info | |
sleep 1 | |
@logger.debug ("wait ssh_info") | |
end | |
ssh = Net::SSH.start( | |
ssh_info[:host], | |
ssh_info[:username], | |
:password => ssh_info[:password], | |
:port => ssh_info[:port], | |
:auth_methods => ["password"], | |
) | |
File.open(@script, 'r') do |f| | |
f.each_line do |line| | |
line = line.strip | |
# Skip comments and empty lines | |
next if line.start_with?('#') | |
next if line.empty? | |
execute_command(ssh, line, true) | |
end | |
end | |
ssh.close | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment