Create /etc/sysconfig/network if it doesn't exist

When the --hostname option is given to ipa-client-install, we
write HOSTNAME to /etc/sysconfig/network. When that file didn't exist,
the installer crashed.

Create the file if it doesn't exist and we need to write to it.

https://fedorahosted.org/freeipa/ticket/2840
This commit is contained in:
Petr Viktorin
2012-07-19 09:07:23 -04:00
committed by Martin Kosek
parent 1be46b322f
commit c8abd24ebe
2 changed files with 16 additions and 4 deletions

View File

@@ -991,13 +991,15 @@ $)''', re.VERBOSE)
return old_values return old_values
def backup_config_and_replace_variables(fstore, filepath, replacevars=dict(), appendvars=dict()): def backup_config_and_replace_variables(
fstore, filepath, replacevars=dict(), appendvars=dict()):
""" """
Take a key=value based configuration file, back up it, and Take a key=value based configuration file, back up it, and
write new version with certain values replaced or appended write new version with certain values replaced or appended
All (key,value) pairs from replacevars and appendvars that All (key,value) pairs from replacevars and appendvars that
were not found in the configuration file, will be added there. were not found in the configuration file, will be added there.
The file must exist before this function is called.
It is responsibility of a caller to ensure that replacevars and It is responsibility of a caller to ensure that replacevars and
appendvars do not overlap. appendvars do not overlap.

View File

@@ -24,6 +24,8 @@ import os
import stat import stat
import sys import sys
import socket import socket
import stat
from ipapython import ipautil from ipapython import ipautil
from ipapython.platform import base from ipapython.platform import base
from ipalib import api from ipalib import api
@@ -187,10 +189,18 @@ def backup_and_replace_hostname(fstore, statestore, hostname):
except ipautil.CalledProcessError, e: except ipautil.CalledProcessError, e:
print >>sys.stderr, "Failed to set this machine hostname to %s (%s)." % (hostname, str(e)) print >>sys.stderr, "Failed to set this machine hostname to %s (%s)." % (hostname, str(e))
replacevars = {'HOSTNAME':hostname} replacevars = {'HOSTNAME':hostname}
old_values = ipautil.backup_config_and_replace_variables(fstore,
"/etc/sysconfig/network", filepath = '/etc/sysconfig/network'
replacevars=replacevars) if not os.path.exists(filepath):
# file doesn't exist; create it with correct ownership & mode
open(filepath, 'a').close()
os.chmod(filepath,
stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
os.chown(filepath, 0, 0)
old_values = ipautil.backup_config_and_replace_variables(
fstore, filepath, replacevars=replacevars)
restore_context("/etc/sysconfig/network") restore_context("/etc/sysconfig/network")
if 'HOSTNAME' in old_values: if 'HOSTNAME' in old_values:
statestore.backup_state('network', 'hostname', old_values['HOSTNAME']) statestore.backup_state('network', 'hostname', old_values['HOSTNAME'])
else: else: