Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Merge "updated micro helper with linux support"
Browse files Browse the repository at this point in the history
  • Loading branch information
pmenglund authored and Gerrit Code Review committed Mar 1, 2012
2 parents be09fa3 + 2052a7f commit 475a479
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 40 deletions.
1 change: 1 addition & 0 deletions config/micro/paths.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
darwin:
vmrun:
- "/Applications/VMware Fusion.app/Contents/Library/"
- "/Applications/Fusion.app/Contents/Library/"
vmx:
- "~/Documents/Virtual Machines.localized/"
- "~/Documents/Virtual Machines/"
Expand Down
1 change: 1 addition & 0 deletions lib/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Micro
module Switcher
autoload :Base, "#{ROOT}/vmc/micro/switcher/base"
autoload :Darwin, "#{ROOT}/vmc/micro/switcher/darwin"
autoload :Dummy, "#{ROOT}/vmc/micro/switcher/dummy"
autoload :Linux, "#{ROOT}/vmc/micro/switcher/linux"
autoload :Windows, "#{ROOT}/vmc/micro/switcher/windows"
end
Expand Down
6 changes: 4 additions & 2 deletions lib/cli/commands/micro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ def switcher(config)
when :darwin
switcher = VMC::Micro::Switcher::Darwin.new(config)
when :linux
switcher = ::Micro::Switcher::Linux.new(config)
switcher = VMC::Micro::Switcher::Linux.new(config)
when :windows
switcher = ::Micro::Switcher::Windows.new(config)
switcher = VMC::Micro::Switcher::Windows.new(config)
when :dummy # for testing only
switcher = VMC::Micro::Switcher::Dummy.new(config)
else
err "unsupported platform: #{Micro.platform}"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/cli/usage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def command_usage
Micro Cloud Foundry
micro status Display Micro Cloud Foundry VM status
mciro offline Configure Micro Cloud Foundry VM for offline mode
micro offline Configure Micro Cloud Foundry VM for offline mode
micro online Configure Micro Cloud Foundry VM for online mode
[--vmx file] Path to micro.vmx
[--vmrun executable] Path to vmrun executable
Expand Down
2 changes: 1 addition & 1 deletion lib/vmc/micro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def escape_path(path)
return path
end
else
return path.sub(' ', '\ ')
return path.gsub(' ', '\ ')
end
end

Expand Down
54 changes: 22 additions & 32 deletions lib/vmc/micro/switcher/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,16 @@ def offline
# save online connection type so we can restore it later
@config['online_connection_type'] = @vmrun.connection_type

validate_nat(@config['online_connection_type'])
if (@config['online_connection_type'] != 'nat')
if ask("Reconfigure Micro Cloud Foundry VM network to nat mode and reboot?", :choices => ['y', 'n']) == 'y'
display "Rebooting Micro Cloud Foundry VM: ", false
@vmrun.connection_type = 'nat'
@vmrun.reset
say "done".green
else
err "Aborted"
end
end

display "Setting Micro Cloud Foundry VM to offline mode: ", false
@vmrun.offline!
Expand All @@ -47,7 +56,18 @@ def online
current_connection_type = @vmrun.connection_type
@config['online_connection_type'] ||= current_connection_type

restore_network(current_connection_type)
if (@config['online_connection_type'] != current_connection_type)
# TODO handle missing connection type in saved config
question = "Reconfigure Micro Cloud Foundry VM network to #{@config['online_connection_type']} mode and reboot?"
if ask(question, :choices => ['y', 'n']) == 'y'
display "Rebooting Micro Cloud Foundry VM: ", false
@vmrun.connection_type = @config['online_connection_type']
@vmrun.reset
say "done".green
else
err "Aborted"
end
end

display "Unsetting host DNS server: ", false
# TODO handle missing domain and ip in saved config (look at the VM)
Expand All @@ -74,34 +94,4 @@ def status
end
end

private

def validate_nat(current_connection_type)
if (current_connection_type != 'nat')
if ask("Reconfigure Micro Cloud Foundry VM network to nat mode and reboot?", :choices => ['y', 'n']) == 'y'
display "Rebooting Micro Cloud Foundry VM: ", false
@vmrun.connection_type = 'nat'
@vmrun.reset
say "done".green
else
err "Aborted"
end
end
end

def restore_network(current_connection_type)
if (@config['online_connection_type'] != current_connection_type)
# TODO handle missing connection type in saved config
question = "Reconfigure Micro Cloud Foundry VM network to #{@config['online_connection_type']} mode and reboot?"
if ask(question, :choices => ['y', 'n']) == 'y'
display "Rebooting Micro Cloud Foundry VM: ", false
@vmrun.connection_type = @config['online_connection_type']
@vmrun.reset
say "done".green
else
err "Aborted"
end
end
end

end
15 changes: 15 additions & 0 deletions lib/vmc/micro/switcher/dummy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# only used for testing
module VMC::Micro::Switcher

class Dummy < Base
def adminrun(command)
end

def set_nameserver(domain, ip)
end

def unset_nameserver(domain, ip)
end
end

end
9 changes: 5 additions & 4 deletions lib/vmc/micro/switcher/linux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ module VMC::Micro::Switcher

class Linux < Base
def set_nameserver(domain, ip)
puts "Not implemented yet, need to set #{ip} as nameserver in resolv.conf"
#run_command("sudo", "sed -i'.backup' '1 i nameserver #{ip}' /etc/resolv.conf")
VMC::Micro.run_command("sudo", "sed -i'.backup' '1 i nameserver #{ip}' /etc/resolv.conf")
# lock resolv.conf so Network Manager doesn't clear out the file when offline
VMC::Micro.run_command("sudo", "chattr +i /etc/resolv.conf")
end

def unset_nameserver(domain, ip)
puts "Not implemented yet, need to unset #{ip} in resolv.conf"
#run_command("sudo", "sed -i'.backup' '/#{ip}/d' /etc/resolv.conf")
VMC::Micro.run_command("sudo", "chattr -i /etc/resolv.conf")
VMC::Micro.run_command("sudo", "sed -i'.backup' '/#{ip}/d' /etc/resolv.conf")
end
end

Expand Down
22 changes: 22 additions & 0 deletions spec/unit/switcher_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'spec_helper'

describe VMC::Micro::Switcher::Base do
it "should go online" do
vmrun = double(VMC::Micro::VMrun)
vmrun.should_receive(:running?).and_return(true)
vmrun.should_receive(:ready?).and_return(true)
vmrun.should_receive(:offline?).and_return(false)
VMC::Micro::VMrun.should_receive(:new).and_return(vmrun)
switcher = VMC::Micro::Switcher::Dummy.new({})
switcher.online
end
it "should go offline" do
vmrun = double(VMC::Micro::VMrun)
vmrun.should_receive(:running?).and_return(true)
vmrun.should_receive(:ready?).and_return(true)
vmrun.should_receive(:offline?).and_return(true)
VMC::Micro::VMrun.should_receive(:new).and_return(vmrun)
switcher = VMC::Micro::Switcher::Dummy.new({})
switcher.offline
end
end

0 comments on commit 475a479

Please sign in to comment.