- look for PlanetLab-Bootstrap-<nodegroup>.tar.bz2 in the boot/
[bootmanager.git] / source / steps / InstallBuildVServer.py
1 #!/usr/bin/python2
2
3 # Copyright (c) 2003 Intel Corporation
4 # All rights reserved.
5 #
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
7 # All rights reserved.
8 # expected /proc/partitions format
9
10
11 import os
12 import string
13
14 from Exceptions import *
15 import utils
16
17
18 # if this file is present in the vservers /etc directory,
19 # the resolv.conf and hosts files will automatically be updated
20 # by the bootmanager
21 UPDATE_FILE_FLAG= "AUTO_UPDATE_NET_FILES"
22
23 # the name of the vserver-reference directory
24 VSERVER_REFERENCE_DIR_NAME='vserver-reference'
25
26
27 def Run( vars, log ):
28     """
29     Setup directories for building vserver reference image.
30
31     Except the following variables from the store:
32     SYSIMG_PATH        the path where the system image will be mounted
33                        (always starts with TEMP_PATH)
34     NETWORK_SETTINGS   A dictionary of the values from the network
35                        configuration file
36     
37     Sets the following variables:
38     None
39     
40     """
41
42     log.write( "\n\nStep: Install: Setting up VServer image.\n" )
43
44     # make sure we have the variables we need
45     try:
46         SYSIMG_PATH= vars["SYSIMG_PATH"]
47         if SYSIMG_PATH == "":
48             raise ValueError, "SYSIMG_PATH"
49
50         NETWORK_SETTINGS= vars["NETWORK_SETTINGS"]
51         if NETWORK_SETTINGS == "":
52             raise ValueError, "NETWORK_SETTINGS"
53
54     except KeyError, var:
55         raise BootManagerException, "Missing variable in vars: %s\n" % var
56     except ValueError, var:
57         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var    
58
59     vserver_ref_dir= "/vservers/vserver-reference"        
60     full_vserver_ref_path= "%s/%s" % (SYSIMG_PATH,vserver_ref_dir)
61
62     utils.makedirs( full_vserver_ref_path )
63     utils.makedirs( "%s/etc" % full_vserver_ref_path )
64     
65     log.write( "Setting permissions on directories\n" )
66     utils.sysexec( "chmod 0000 %s/vservers/" % SYSIMG_PATH, log )
67
68     return 1
69
70
71
72 def update_vserver_network_files( vserver_dir, vars, log ):
73     """
74     Update the /etc/resolv.conf and /etc/hosts files in the specified
75     vserver directory. If the files do not exist, write them out. If they
76     do exist, rewrite them with new values if the file UPDATE_FILE_FLAG
77     exists it /etc. if this is called with the vserver-reference directory,
78     always update the network config files and create the UPDATE_FILE_FLAG.
79
80     This is currently called when setting up the initial vserver reference,
81     and later when nodes boot to update existing vserver images.
82
83     Expect the following variables from the store:
84     SYSIMG_PATH        the path where the system image will be mounted
85                        (always starts with TEMP_PATH)
86     NETWORK_SETTINGS   A dictionary of the values from the network
87                        configuration file
88     """
89
90     try:
91         SYSIMG_PATH= vars["SYSIMG_PATH"]
92         if SYSIMG_PATH == "":
93             raise ValueError, "SYSIMG_PATH"
94
95         NETWORK_SETTINGS= vars["NETWORK_SETTINGS"]
96         if NETWORK_SETTINGS == "":
97             raise ValueError, "NETWORK_SETTINGS"
98
99     except KeyError, var:
100         raise BootManagerException, "Missing variable in vars: %s\n" % var
101     except ValueError, var:
102         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
103
104     try:
105         ip= NETWORK_SETTINGS['ip']
106         method= NETWORK_SETTINGS['method']
107         hostname= NETWORK_SETTINGS['hostname']
108         domainname= NETWORK_SETTINGS['domainname']
109     except KeyError, var:
110         raise BootManagerException, \
111               "Missing network value %s in var NETWORK_SETTINGS\n" % var
112
113     try:
114         os.listdir(vserver_dir)
115     except OSError:
116         log.write( "Directory %s does not exist to write network conf in.\n" %
117                    vserver_dir )
118         return
119
120     file_path= "%s/etc/%s" % (vserver_dir,UPDATE_FILE_FLAG)
121     update_files= 0
122     if os.access(file_path,os.F_OK):
123         update_files= 1
124
125         
126     if vserver_dir.find(VSERVER_REFERENCE_DIR_NAME) != -1:
127         log.write( "Forcing update on vserver-reference directory:\n%s\n" %
128                    vserver_dir )
129         utils.sysexec_noerr( "echo '%s' > %s/etc/%s" %
130                              (UPDATE_FILE_FLAG,vserver_dir,UPDATE_FILE_FLAG),
131                              log )
132         update_files= 1
133         
134
135     if update_files:
136         log.write( "Updating network files in %s.\n" % vserver_dir )
137         
138         file_path= "%s/etc/hosts" % vserver_dir
139         hosts_file= file(file_path, "w" )
140         hosts_file.write( "127.0.0.1       localhost\n" )
141         if method == "static":
142             hosts_file.write( "%s %s.%s\n" % (ip, hostname, domainname) )
143             hosts_file.close()
144             hosts_file= None
145
146
147         file_path= "%s/etc/resolv.conf" % vserver_dir
148         if method == "dhcp":
149             # copy the resolv.conf from the boot cd env.
150             utils.sysexec( "cp /etc/resolv.conf %s/etc" % vserver_dir, log )
151         else:
152             # copy the generated resolv.conf from the system image, since
153             # we generated it via static settings
154             utils.sysexec( "cp %s/etc/resolv.conf %s/etc" % \
155                            (SYSIMG_PATH,vserver_dir), log )
156             
157     return