Class: VagrantPlugins::HyperV::Action::Customize
- Inherits:
-
Object
- Object
- VagrantPlugins::HyperV::Action::Customize
- Defined in:
- lib/vagrant-windows-hyperv/monkey_patch/action/customize.rb
Instance Method Summary collapse
- #add_swith_to_vm(options) ⇒ Object
- #call(env) ⇒ Object
- #custom_action_virtual_switch(params) ⇒ Object
-
#initialize(app, env, event) ⇒ Customize
constructor
A new instance of Customize.
- #validate_virtual_switch ⇒ Object
Constructor Details
#initialize(app, env, event) ⇒ Customize
Returns a new instance of Customize.
13 14 15 16 17 |
# File 'lib/vagrant-windows-hyperv/monkey_patch/action/customize.rb', line 13 def initialize(app, env, event) @app = app @logger = Log4r::Logger.new("vagrant::hyperv::connection") @event = event end |
Instance Method Details
#add_swith_to_vm(options) ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/vagrant-windows-hyperv/monkey_patch/action/customize.rb', line 110 def add_swith_to_vm() current_vm_switch = @env[:machine].provider.driver.find_vm_switch_name if current_vm_switch["network_adapter"].nil? raise VagrantPlugins::VagrantHyperV::Errors::NoNetworkAdapter end @env[:machine].provider.driver.add_swith_to_vm() end |
#call(env) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/vagrant-windows-hyperv/monkey_patch/action/customize.rb', line 19 def call(env) customizations = [] @env = env # FIXME: # Currently we are unable to set a default value for the customizations # to the vagrant core Config initialization. This check should be removed # when this code becomes a part of vagrant core. custom_config = env[:machine].provider_config.customizations if custom_config custom_config.each do |event, command| if event == @event customizations << command end end end if !customizations.empty? env[:ui].info I18n.t("vagrant.actions.vm.customize.running", event: @event) customizations.each do |query| command = query[0] params = query[1] if self.respond_to?("custom_action_#{command}") self.send("custom_action_#{command}", params) end end end validate_virtual_switch @app.call(env) end |
#custom_action_virtual_switch(params) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/vagrant-windows-hyperv/monkey_patch/action/customize.rb', line 50 def custom_action_virtual_switch(params) = { vm_id: @env[:machine].id, type: (params[:type] || "").downcase || "external", name: params[:name], adapter: (params[:bridge] || "").downcase } if [:type] == "private" @env[:ui].detail I18n.t("vagrant_win_hyperv.private_switch_not_allowed") return end response = @env[:machine].provider.driver.switch_exist({ name: [:name], type: [:type]}) if [:type] == "internal" @env[:ui].detail(" ") @env[:ui].detail I18n.t("vagrant_win_hyperv.internal_switch_warn") @env[:ui].detail(" ") if response["message"] == "switch exist" add_swith_to_vm() return else raise VagrantPlugins::VagrantHyperV::Errors::NoSwitchFound, type: [:type], name: [:name] end end if [:type] == "external" if response["message"] == "switch exist" if (response["switch_name"].casecmp([:name]) == 0) add_swith_to_vm() return else raise VagrantPlugins::VagrantHyperV::Errors::ExternalSwitchExist, name: response["switch_name"] end end adapters = @env[:machine].provider.driver.list_net_adapters available_adapters = adapters.map { |a| a["Name"].downcase } unless available_adapters.include? ([:adapter]) @env[:ui].detail I18n.t("vagrant_win_hyperv.net_adapter_warn") selected_adapter = choose_option_from(adapters, "adapter") [:adapter] = selected_adapter["Name"] end @env[:ui].info I18n.t("vagrant_win_hyperv.creating_switch", { type: [:type], name: [:name] }) response = @env[:machine].provider.driver.create_network_switch() case response["message"] when "Network down" # TODO: Create a error class in core vagrant when merged. raise VagrantPlugins::VagrantHyperV::Errors::NetworkDown when "Success" add_swith_to_vm() end end end |
#validate_virtual_switch ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/vagrant-windows-hyperv/monkey_patch/action/customize.rb', line 118 def validate_virtual_switch @env[:ui].info "Validating Virtual Switch" current_vm_switch = @env[:machine].provider.driver.find_vm_switch_name if current_vm_switch["switch_name"].nil? switches = @env[:machine].provider.driver.execute("get_switches.ps1", {}) raise VagrantPlugins::VagrantHyperV::Errors::NoSwitches if switches.empty? switch = choose_option_from(switches, "switch") switch_type = nil case switch["SwitchType"] when 1 switch_type = "Internal" when 2 switch_type = "External" end = { vm_id: @env[:machine].id, type: switch_type.downcase, name: switch["Name"] } @env[:ui].info I18n.t("vagrant_win_hyperv.add_switch_to_vm", { type: [:type], name: [:name] }) add_swith_to_vm() end end |