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

Commit

Permalink
Merge branch 'stucki-create-bridge-if-missing' (PR #380)
Browse files Browse the repository at this point in the history
  • Loading branch information
fpletz committed Aug 28, 2015
2 parents 0c35359 + 7c778df commit 86381bf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
27 changes: 23 additions & 4 deletions lib/vagrant-lxc/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ def configure_private_network(bridge_name, bridge_ip, container_name, address_ty
ip += '/24'
end

if ! bridge_exists?(bridge_name)
if not bridge_ip
raise "Bridge is missing and no IP was specified!"
end

@logger.info "Creating the bridge #{bridge_name}"
cmd = [
'brctl',
'addbr',
bridge_name
]
@sudo_wrapper.run(*cmd)
end

if ! bridge_has_an_ip?(bridge_name)
if not bridge_ip
raise "Bridge has no IP and none was specified!"
Expand All @@ -146,6 +160,7 @@ def configure_private_network(bridge_name, bridge_ip, container_name, address_ty
bridge_name
]
@sudo_wrapper.run(*cmd)
@sudo_wrapper.run('ip', 'link', 'set', bridge_name, 'up')
end

cmd = [
Expand All @@ -162,6 +177,12 @@ def bridge_has_an_ip?(bridge_name)
`ip -4 addr show scope global #{bridge_name}` =~ /^\s+inet ([0-9.]+)\/[0-9]+\s+/
end

def bridge_exists?(bridge_name)
@logger.info "Checking whether bridge #{bridge_name} exists"
brctl_output = `ip link | egrep -q " #{bridge_name}:"`
$?.to_i == 0
end

def bridge_is_in_use?(bridge_name)
# REFACTOR: This method is **VERY** hacky
@logger.info "Checking if bridge #{bridge_name} is in use"
Expand All @@ -175,12 +196,10 @@ def remove_bridge(bridge_name)
return
end

@logger.info "Checking whether bridge #{bridge_name} exists"
brctl_output = `ifconfig -a | grep -q #{bridge_name}`
return if $?.to_i != 0
return unless bridge_exists?(bridge_name)

@logger.info "Removing bridge #{bridge_name}"
@sudo_wrapper.run('ifconfig', bridge_name, 'down')
@sudo_wrapper.run('ip', 'link', 'set', bridge_name, 'down')
@sudo_wrapper.run('brctl', 'delbr', bridge_name)
end

Expand Down
4 changes: 2 additions & 2 deletions templates/sudoers.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ Whitelist.add '<%= cmd_paths['tar'] %>', '--numeric-owner', '-cvzf', %r{/tmp/.*/
Whitelist.add '<%= cmd_paths['chown'] %>', /\A\d+:\d+\z/, %r{\A/tmp/.*/rootfs\.tar\.gz\z}
# - Private network script and commands
Whitelist.add '<%= cmd_paths['ip'] %>', 'addr', 'add', /(\d+|\.)+\/24/, 'dev', /.+/
Whitelist.add '<%= cmd_paths['ifconfig'] %>', /.+/, 'down'
Whitelist.add '<%= cmd_paths['brctl'] %>', 'delbr', /.+/
Whitelist.add '<%= cmd_paths['ip'] %>', 'link', 'set', /.+/, /(up|down)/
Whitelist.add '<%= cmd_paths['brctl'] %>', /(addbr|delbr)/, /.+/
Whitelist.add_regex %r{<%= pipework_regex %>}, '**'

##
Expand Down

0 comments on commit 86381bf

Please sign in to comment.