Skip to content

Commit

Permalink
Fix service network restart on RHEL-7 / Fedora
Browse files Browse the repository at this point in the history
RHEL-7 / Current Fedora versions tend to use NetworkManager for
configuring the networks, and `service network restart` might fail.
If the `NetworkManager` service is running, we should restart it,
otherwise we try restarting `network`.
  • Loading branch information
tzvetkoff committed Nov 29, 2016
1 parent f37b0c2 commit 166d10d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
8 changes: 6 additions & 2 deletions plugins/guests/redhat/cap/change_host_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ def self.change_host_name(machine, name)
sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts
}
# Restart network
service network restart
# Restart network (through NetworkManager if running)
if service NetworkManager status 2>&1 | grep -q running; then
service NetworkManager restart
else
service network restart
fi
EOH
end
end
Expand Down
12 changes: 8 additions & 4 deletions plugins/guests/redhat/cap/configure_networks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def self.configure_networks(machine, networks)

# Add the new interface and bring it back up
final_path = "#{network_scripts_dir}/ifcfg-#{network[:device]}"
commands << <<-EOH.gsub(/^ */, '')
commands << <<-EOH.gsub(/^ {14}/, '')
# Down the interface before munging the config file. This might
# fail if the interface is not actually set up yet so ignore
# errors.
Expand All @@ -48,9 +48,13 @@ def self.configure_networks(machine, networks)
EOH
end

commands << <<-EOH.gsub(/^ */, '')
# Restart network
service network restart
commands << <<-EOH.gsub(/^ {12}/, '')
# Restart network (through NetworkManager if running)
if service NetworkManager status 2>&1 | grep -q running; then
service NetworkManager restart
else
service network restart
fi
EOH

comm.sudo(commands.join("\n"))
Expand Down

0 comments on commit 166d10d

Please sign in to comment.