summaryrefslogtreecommitdiff
path: root/morphlib/exts/simple-network.configure
diff options
context:
space:
mode:
Diffstat (limited to 'morphlib/exts/simple-network.configure')
-rwxr-xr-xmorphlib/exts/simple-network.configure39
1 files changed, 29 insertions, 10 deletions
diff --git a/morphlib/exts/simple-network.configure b/morphlib/exts/simple-network.configure
index 61113325..1ba94e86 100755
--- a/morphlib/exts/simple-network.configure
+++ b/morphlib/exts/simple-network.configure
@@ -27,6 +27,7 @@ for DHCP
import os
import sys
+import errno
import cliapp
import morphlib
@@ -80,12 +81,14 @@ class SimpleNetworkConfigurationExtension(cliapp.Application):
"""
file_path = os.path.join(args[0], "etc", "systemd", "network",
"10-dhcp.network")
- try:
- os.rename(file_path, file_path + ".morph")
- self.status(msg="Renaming networkd file from systemd chunk: %(f)s \
- to %(f)s.morph", f=file_path)
- except OSError:
- pass
+
+ if os.path.isfile(file_path):
+ try:
+ os.rename(file_path, file_path + ".morph")
+ self.status(msg="Renaming networkd file from systemd chunk: \
+ %(f)s to %(f)s.morph", f=file_path)
+ except OSError:
+ pass
def generate_default_network_config(self, args):
"""Generate default network config: DHCP in all the interfaces"""
@@ -106,7 +109,11 @@ class SimpleNetworkConfigurationExtension(cliapp.Application):
"""Generate /etc/network/interfaces file"""
iface_file = self.generate_iface_file(stanzas)
- with open(os.path.join(args[0], "etc/network/interfaces"), "w") as f:
+
+ directory_path = os.path.join(args[0], "etc", "network")
+ self.make_sure_path_exists(directory_path)
+ file_path = os.path.join(directory_path, "interfaces")
+ with open(file_path, "w") as f:
f.write(iface_file)
def generate_iface_file(self, stanzas):
@@ -147,10 +154,12 @@ class SimpleNetworkConfigurationExtension(cliapp.Application):
if iface_file is None:
continue
- path = os.path.join(args[0], "etc", "systemd", "network",
- "%s-%s.network" % (i, stanza['name']))
+ directory_path = os.path.join(args[0], "etc", "systemd", "network")
+ self.make_sure_path_exists(directory_path)
+ file_path = os.path.join(directory_path,
+ "%s-%s.network" % (i, stanza['name']))
- with open(path, "w") as f:
+ with open(file_path, "w") as f:
f.write(iface_file)
def generate_networkd_file(self, stanza):
@@ -252,6 +261,16 @@ class SimpleNetworkConfigurationExtension(cliapp.Application):
return output_stanza
+ def make_sure_path_exists(self, path):
+ try:
+ os.makedirs(path)
+ except OSError as e:
+ if e.errno == errno.EEXIST and os.path.isdir(path):
+ pass
+ else:
+ raise SimpleNetworkError("Unable to create directory '%s'"
+ % path)
+
def status(self, **kwargs):
'''Provide status output.