various cleanups and cosmetic improvements in utils and InstallWriteConfig
[bootmanager.git] / source / steps / InstallWriteConfig.py
1 #
2 # Copyright (c) 2003 Intel Corporation
3 # All rights reserved.
4 #
5 # Copyright (c) 2004-2006 The Trustees of Princeton University
6 # All rights reserved.
7 # expected /proc/partitions format
8
9 # pylint: disable=c0111, c0103
10
11 import os
12 import os.path
13
14 from Exceptions import *
15 import utils
16
17
18 def Run(vars, log):
19
20     """
21     Writes out the following configuration files for the node:
22     /etc/fstab
23     /etc/resolv.conf (if applicable)
24     /etc/ssh/ssh_host_key
25     /etc/ssh/ssh_host_rsa_key
26     /etc/ssh/ssh_host_dsa_key
27
28     Expect the following variables from the store:
29     VERSION                 the version of the install
30     SYSIMG_PATH             the path where the system image will be mounted
31                             (always starts with TEMP_PATH)
32     PARTITIONS              dictionary of generic part. types (root/swap)
33                             and their associated devices.
34     PLCONF_DIR              The directory to store the configuration file in
35     INTERFACE_SETTINGS  A dictionary of the values from the network
36                                 configuration file
37     Sets the following variables:
38     None
39
40     """
41
42     log.write("\n\nStep: Install: Writing configuration files.\n")
43
44     # make sure we have the variables we need
45     try:
46         VERSION = vars["VERSION"]
47         if VERSION == "":
48             raise ValueError("VERSION")
49
50         SYSIMG_PATH = vars["SYSIMG_PATH"]
51         if SYSIMG_PATH == "":
52             raise ValueError("SYSIMG_PATH")
53
54         PARTITIONS = vars["PARTITIONS"]
55         if PARTITIONS is None:
56             raise ValueError("PARTITIONS")
57
58         PLCONF_DIR = vars["PLCONF_DIR"]
59         if PLCONF_DIR == "":
60             raise ValueError("PLCONF_DIR")
61
62         INTERFACE_SETTINGS = vars["INTERFACE_SETTINGS"]
63         if INTERFACE_SETTINGS == "":
64             raise ValueError("INTERFACE_SETTINGS")
65
66     except KeyError as var:
67         raise BootManagerException("Missing variable in vars: {}\n".format(var))
68     except ValueError as var:
69         raise BootManagerException("Variable in vars, shouldn't be: {}\n".format(var))
70
71     log.write("Setting local time to UTC\n")
72     utils.sysexec_chroot(
73         SYSIMG_PATH,
74         "ln -sf /usr/share/zoneinfo/UTC /etc/localtime", log)
75
76     log.write("Creating system directory {}\n".format(PLCONF_DIR))
77     if not utils.makedirs("{}/{}".format(SYSIMG_PATH, PLCONF_DIR)):
78         log.write("Unable to create directory\n")
79         return 0
80
81     log.write("Writing system /etc/fstab\n")
82     with open("{}/etc/fstab".format(SYSIMG_PATH), "w") as fstab:
83         fstab.write("{}           none        swap      sw        0 0\n"\
84                     .format(PARTITIONS["swap"]))
85         fstab.write("{}           /           ext3      defaults  1 1\n"\
86                     .format(PARTITIONS["root"]))
87         if (vars['ONE_PARTITION'] != '1'):
88             if vars['virt'] == 'vs':
89                 fstab.write("{}           /vservers   ext3      tagxid,defaults  1 2\n"\
90                             .format(PARTITIONS["vservers"]))
91             else:
92                 fstab.write("{}           /vservers   btrfs     defaults  1 2\n"\
93                             .format(PARTITIONS["vservers"]))
94         fstab.write("none         /proc       proc      defaults  0 0\n")
95         fstab.write("none         /dev/shm    tmpfs     defaults  0 0\n")
96         fstab.write("none         /dev/pts    devpts    defaults  0 0\n")
97
98     log.write("Writing system /etc/issue\n")
99     with open("{}/etc/issue".format(SYSIMG_PATH), "w") as issue:
100         issue.write("PlanetLab Node: \\n\n")
101         issue.write("Kernel \\r on an \\m\n")
102         issue.write("http://www.planet-lab.org\n\n")
103
104     if (vars['ONE_PARTITION'] != '1'):
105         log.write("Setting up authentication (non-ssh)\n")
106         utils.sysexec_chroot(SYSIMG_PATH, "authconfig --nostart --kickstart --enablemd5 " \
107                        "--enableshadow", log)
108         utils.sysexec("sed -e 's/^root\:\:/root\:*\:/g' " \
109                        "{}/etc/shadow > {}/etc/shadow.new".format(SYSIMG_PATH, SYSIMG_PATH), log)
110         utils.sysexec_chroot(SYSIMG_PATH, "mv " \
111                        "/etc/shadow.new /etc/shadow", log)
112         utils.sysexec_chroot(SYSIMG_PATH, "chmod 400 /etc/shadow", log)
113
114     # if we are setup with dhcp, copy the current /etc/resolv.conf into
115     # the system image so we can run programs inside that need network access
116     method = ""
117     try:
118         method = vars['INTERFACE_SETTINGS']['method']
119     except:
120         pass
121
122     if method == "dhcp":
123         utils.sysexec("cp /etc/resolv.conf {}/etc/".format(SYSIMG_PATH), log)
124
125     log.write("Writing node install_version\n")
126     utils.makedirs("{}/etc/planetlab".format(SYSIMG_PATH))
127     with open("{}/etc/planetlab/install_version".format(SYSIMG_PATH), "w") as ver:
128         ver.write("{}\n".format(VERSION))
129
130     # for upgrades : do not overwrite already existing keys
131     log.write("Creating ssh host keys\n")
132     key_gen_prog = "/usr/bin/ssh-keygen"
133
134     # fedora23 seems to come with a release of openssh that lacks suppport
135     # for ssh1, and thus rsa1 keys; so we consider that failing to produce
136     # the rsa1 key is not a showstopper
137     key_specs = [
138         ("/etc/ssh/ssh_host_key",     'rsa1', "SSH1 RSA", False),
139         ("/etc/ssh/ssh_host_rsa_key", 'rsa',  "SSH2 RSA", True),
140         ("/etc/ssh/ssh_host_dsa_key", 'dsa',  "SSH2 DSA", True),
141     ]
142
143     for key_file, key_type, label, mandatory in key_specs:
144         abs_file = "{}/{}".format(SYSIMG_PATH, key_file)
145         if not os.path.exists(abs_file):
146             log.write("Generating {} host key {} (mandatory success={})\n"
147                       .format(label, key_file, mandatory))
148             if mandatory:
149                 run = utils.sysexec
150                 run_chroot = utils.sysexec_chroot
151             else:
152                 run = utils.sysexec_noerr
153                 run_chroot = utils.sysexec_chroot_noerr
154             run_chroot(
155                 SYSIMG_PATH,
156                 "{} -q -t {} -f {} -C '' -N ''"
157                 .format(key_gen_prog, key_type, key_file),
158                 log)
159             run("chmod 600 {}/{}".format(SYSIMG_PATH, key_file), log)
160             run("chmod 644 {}/{}.pub".format(SYSIMG_PATH, key_file), log)
161
162     return 1