1f631792c2ea9cc51eab0b2b16ac1c304d32d05f
[bootmanager.git] / source / steps / InstallBuildVServer.py
1 # Copyright (c) 2003 Intel Corporation
2 # All rights reserved.
3
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7
8 #     * Redistributions of source code must retain the above copyright
9 #       notice, this list of conditions and the following disclaimer.
10
11 #     * Redistributions in binary form must reproduce the above
12 #       copyright notice, this list of conditions and the following
13 #       disclaimer in the documentation and/or other materials provided
14 #       with the distribution.
15
16 #     * Neither the name of the Intel Corporation nor the names of its
17 #       contributors may be used to endorse or promote products derived
18 #       from this software without specific prior written permission.
19
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
24 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 # EXPORT LAWS: THIS LICENSE ADDS NO RESTRICTIONS TO THE EXPORT LAWS OF
33 # YOUR JURISDICTION. It is licensee's responsibility to comply with any
34 # export regulations applicable in licensee's jurisdiction. Under
35 # CURRENT (May 2000) U.S. export regulations this software is eligible
36 # for export from the U.S. and can be downloaded by or otherwise
37 # exported or reexported worldwide EXCEPT to U.S. embargoed destinations
38 # which include Cuba, Iraq, Libya, North Korea, Iran, Syria, Sudan,
39 # Afghanistan and any other country to which the U.S. has embargoed
40 # goods and services.
41
42
43 import os
44 import string
45
46 from Exceptions import *
47 import utils
48
49
50 # if this file is present in the vservers /etc directory,
51 # the resolv.conf and hosts files will automatically be updated
52 # by the bootmanager
53 UPDATE_FILE_FLAG= "AUTO_UPDATE_NET_FILES"
54
55 # the name of the vserver-reference directory
56 VSERVER_REFERENCE_DIR_NAME='vserver-reference'
57
58
59 def Run( vars, log ):
60     """
61     Setup directories for building vserver reference image.
62
63     Except the following variables from the store:
64     SYSIMG_PATH        the path where the system image will be mounted
65                        (always starts with TEMP_PATH)
66     NETWORK_SETTINGS   A dictionary of the values from the network
67                        configuration file
68     
69     Sets the following variables:
70     None
71     
72     """
73
74     log.write( "\n\nStep: Install: Setting up VServer image.\n" )
75
76     # make sure we have the variables we need
77     try:
78         SYSIMG_PATH= vars["SYSIMG_PATH"]
79         if SYSIMG_PATH == "":
80             raise ValueError, "SYSIMG_PATH"
81
82         NETWORK_SETTINGS= vars["NETWORK_SETTINGS"]
83         if NETWORK_SETTINGS == "":
84             raise ValueError, "NETWORK_SETTINGS"
85
86     except KeyError, var:
87         raise BootManagerException, "Missing variable in vars: %s\n" % var
88     except ValueError, var:
89         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var    
90
91     vserver_ref_dir= "/vservers/vserver-reference"        
92     full_vserver_ref_path= "%s/%s" % (SYSIMG_PATH,vserver_ref_dir)
93
94     utils.makedirs( full_vserver_ref_path )
95     utils.makedirs( "%s/etc" % full_vserver_ref_path )
96     
97     log.write( "Setting permissions on directories\n" )
98     utils.sysexec( "chmod 0000 %s/vservers/" % SYSIMG_PATH, log )
99
100     return 1
101
102
103
104 def update_vserver_network_files( vserver_dir, vars, log ):
105     """
106     Update the /etc/resolv.conf and /etc/hosts files in the specified
107     vserver directory. If the files do not exist, write them out. If they
108     do exist, rewrite them with new values if the file UPDATE_FILE_FLAG
109     exists it /etc. if this is called with the vserver-reference directory,
110     always update the network config files and create the UPDATE_FILE_FLAG.
111
112     This is currently called when setting up the initial vserver reference,
113     and later when nodes boot to update existing vserver images.
114
115     Expect the following variables from the store:
116     SYSIMG_PATH        the path where the system image will be mounted
117                        (always starts with TEMP_PATH)
118     NETWORK_SETTINGS   A dictionary of the values from the network
119                        configuration file
120     """
121
122     try:
123         SYSIMG_PATH= vars["SYSIMG_PATH"]
124         if SYSIMG_PATH == "":
125             raise ValueError, "SYSIMG_PATH"
126
127         NETWORK_SETTINGS= vars["NETWORK_SETTINGS"]
128         if NETWORK_SETTINGS == "":
129             raise ValueError, "NETWORK_SETTINGS"
130
131     except KeyError, var:
132         raise BootManagerException, "Missing variable in vars: %s\n" % var
133     except ValueError, var:
134         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
135
136     try:
137         ip= NETWORK_SETTINGS['ip']
138         method= NETWORK_SETTINGS['method']
139         hostname= NETWORK_SETTINGS['hostname']
140         domainname= NETWORK_SETTINGS['domainname']
141     except KeyError, var:
142         raise BootManagerException, \
143               "Missing network value %s in var NETWORK_SETTINGS\n" % var
144
145     try:
146         os.listdir(vserver_dir)
147     except OSError:
148         log.write( "Directory %s does not exist to write network conf in.\n" %
149                    vserver_dir )
150         return
151
152     file_path= "%s/etc/%s" % (vserver_dir,UPDATE_FILE_FLAG)
153     update_files= 0
154     if os.access(file_path,os.F_OK):
155         update_files= 1
156
157         
158     if vserver_dir.find(VSERVER_REFERENCE_DIR_NAME) != -1:
159         log.write( "Forcing update on vserver-reference directory:\n%s\n" %
160                    vserver_dir )
161         utils.sysexec_noerr( "echo '%s' > %s/etc/%s" %
162                              (UPDATE_FILE_FLAG,vserver_dir,UPDATE_FILE_FLAG),
163                              log )
164         update_files= 1
165         
166
167     if update_files:
168         log.write( "Updating network files in %s.\n" % vserver_dir )
169         
170         file_path= "%s/etc/hosts" % vserver_dir
171         hosts_file= file(file_path, "w" )
172         hosts_file.write( "127.0.0.1       localhost\n" )
173         if method == "static":
174             hosts_file.write( "%s %s.%s\n" % (ip, hostname, domainname) )
175             hosts_file.close()
176             hosts_file= None
177
178
179         file_path= "%s/etc/resolv.conf" % vserver_dir
180         if method == "dhcp":
181             # copy the resolv.conf from the boot cd env.
182             utils.sysexec( "cp /etc/resolv.conf %s/etc" % vserver_dir, log )
183         else:
184             # copy the generated resolv.conf from the system image, since
185             # we generated it via static settings
186             utils.sysexec( "cp %s/etc/resolv.conf %s/etc" % \
187                            (SYSIMG_PATH,vserver_dir), log )
188             
189     return