modules/generic_stage_target.py: Use 'proc' instead of '/proc' as the mountmap key
[catalyst.git] / catalyst
1 #!/usr/bin/python2 -OO
2
3 # Maintained in full by:
4 # Catalyst Team <catalyst@gentoo.org>
5 # Release Engineering Team <releng@gentoo.org>
6 # Andrew Gaffney <agaffney@gentoo.org>
7 # Chris Gianelloni <wolf31o2@wolf31o2.org>
8 # $Id$
9
10 import os
11 import sys
12 import imp
13 import string
14 import getopt
15 import pdb
16 import os.path
17
18 import modules.catalyst.config
19 import modules.catalyst.util
20
21 __maintainer__="Catalyst <catalyst@gentoo.org>"
22 __version__="2.0.15"
23
24 conf_values={}
25
26 def usage():
27         print "Usage catalyst [options] [-C variable=value...] [ -s identifier]"
28         print " -a --clear-autoresume   clear autoresume flags"
29         print " -c --config     use specified configuration file"
30         print " -C --cli        catalyst commandline (MUST BE LAST OPTION)"
31         print " -d --debug      enable debugging"
32         print " -f --file       read specfile"
33         print " -F --fetchonly  fetch files only"
34         print " -h --help       print this help message"
35         print " -p --purge      clear tmp dirs,package cache and autoresume flags"
36         print " -P --purgeonly  clear tmp dirs,package cache and autoresume flags and exit"
37         print " -T --purgetmponly  clear tmp dirs and autoresume flags and exit"
38         print " -s --snapshot   generate a release snapshot"
39         print " -V --version    display version information"
40         print " -v --verbose    verbose output"
41         print
42         print "Usage examples:"
43         print
44         print "Using the commandline option (-C, --cli) to build a Portage snapshot:"
45         print "catalyst -C target=snapshot version_stamp=my_date"
46         print
47         print "Using the snapshot option (-s, --snapshot) to build a release snapshot:"
48         print "catalyst -s 20071121"
49         print
50         print "Using the specfile option (-f, --file) to build a stage target:"
51         print "catalyst -f stage1-specfile.spec"
52
53 def version():
54         print "Catalyst, version "+__version__
55         print "Copyright 2003-2008 Gentoo Foundation"
56         print "Copyright 2008-2012 various authors"
57         print "Distributed under the GNU General Public License version 2.1\n"
58
59 def parse_config(myconfig):
60         # search a couple of different areas for the main config file
61         myconf={}
62         config_file=""
63
64         confdefaults = {
65                 "distdir": "/usr/portage/distfiles",
66                 "hash_function": "crc32",
67                 "options": "",
68                 "packagedir": "/usr/portage/packages",
69                 "portdir": "/usr/portage",
70                 "repo_name": "portage",
71                 "sharedir": "/usr/share/catalyst",
72                 "snapshot_name": "portage-",
73                 "snapshot_cache": "/var/tmp/catalyst/snapshot_cache",
74                 "storedir": "/var/tmp/catalyst",
75                 }
76
77         # first, try the one passed (presumably from the cmdline)
78         if myconfig:
79                 if os.path.exists(myconfig):
80                         print "Using command line specified Catalyst configuration file, "+myconfig
81                         config_file=myconfig
82
83                 else:
84                         print "!!! catalyst: Could not use specified configuration file "+\
85                                 myconfig
86                         sys.exit(1)
87
88         # next, try the default location
89         elif os.path.exists("/etc/catalyst/catalyst.conf"):
90                 print "Using default Catalyst configuration file, /etc/catalyst/catalyst.conf"
91                 config_file="/etc/catalyst/catalyst.conf"
92
93         # can't find a config file (we are screwed), so bail out
94         else:
95                 print "!!! catalyst: Could not find a suitable configuration file"
96                 sys.exit(1)
97
98         # now, try and parse the config file "config_file"
99         try:
100 #               execfile(config_file, myconf, myconf)
101                 myconfig = modules.catalyst.config.ConfigParser(config_file)
102                 myconf.update(myconfig.get_values())
103
104         except:
105                 print "!!! catalyst: Unable to parse configuration file, "+myconfig
106                 sys.exit(1)
107
108         # now, load up the values into conf_values so that we can use them
109         for x in confdefaults.keys():
110                 if x in myconf:
111                         print "Setting",x,"to config file value \""+myconf[x]+"\""
112                         conf_values[x]=myconf[x]
113                 else:
114                         print "Setting",x,"to default value \""+confdefaults[x]+"\""
115                         conf_values[x]=confdefaults[x]
116
117         # parse out the rest of the options from the config file
118         if "autoresume" in string.split(conf_values["options"]):
119                 print "Autoresuming support enabled."
120                 conf_values["AUTORESUME"]="1"
121
122         if "bindist" in string.split(conf_values["options"]):
123                 print "Binary redistribution enabled"
124                 conf_values["BINDIST"]="1"
125         else:
126                 print "Bindist is not enabled in catalyst.conf"
127                 print "Binary redistribution of generated stages/isos may be prohibited by law."
128                 print "Please see the use description for bindist on any package you are including."
129
130         if "ccache" in string.split(conf_values["options"]):
131                 print "Compiler cache support enabled."
132                 conf_values["CCACHE"]="1"
133
134         if "clear-autoresume" in string.split(conf_values["options"]):
135                 print "Cleaning autoresume flags support enabled."
136                 conf_values["CLEAR_AUTORESUME"]="1"
137
138         if "distcc" in string.split(conf_values["options"]):
139                 print "Distcc support enabled."
140                 conf_values["DISTCC"]="1"
141
142         if "icecream" in string.split(conf_values["options"]):
143                 print "Icecream compiler cluster support enabled."
144                 conf_values["ICECREAM"]="1"
145
146         if "kerncache" in string.split(conf_values["options"]):
147                 print "Kernel cache support enabled."
148                 conf_values["KERNCACHE"]="1"
149
150         if "pkgcache" in string.split(conf_values["options"]):
151                 print "Package cache support enabled."
152                 conf_values["PKGCACHE"]="1"
153
154         if "preserve_libs" in string.split(conf_values["options"]):
155                 print "Preserving libs during unmerge."
156                 conf_values["PRESERVE_LIBS"]="1"
157
158         if "purge" in string.split(conf_values["options"]):
159                 print "Purge support enabled."
160                 conf_values["PURGE"]="1"
161
162         if "seedcache" in string.split(conf_values["options"]):
163                 print "Seed cache support enabled."
164                 conf_values["SEEDCACHE"]="1"
165
166         if "snapcache" in string.split(conf_values["options"]):
167                 print "Snapshot cache support enabled."
168                 conf_values["SNAPCACHE"]="1"
169
170         if "digests" in myconf:
171                 conf_values["digests"]=myconf["digests"]
172         if "contents" in myconf:
173                 conf_values["contents"]=myconf["contents"]
174
175         if "envscript" in myconf:
176                 print "Envscript support enabled."
177                 conf_values["ENVSCRIPT"]=myconf["envscript"]
178
179         if "var_tmpfs_portage" in myconf:
180                 conf_values["var_tmpfs_portage"]=myconf["var_tmpfs_portage"];
181
182         if "port_logdir" in myconf:
183                 conf_values["port_logdir"]=myconf["port_logdir"];
184
185 def import_modules():
186         # import catalyst's own modules (i.e. catalyst_support and the arch modules)
187         targetmap={}
188
189         try:
190                 for x in required_build_targets:
191                         try:
192                                 fh=open(conf_values["sharedir"]+"/modules/"+x+".py")
193                                 module=imp.load_module(x,fh,"modules/"+x+".py",(".py","r",imp.PY_SOURCE))
194                                 fh.close()
195
196                         except IOError:
197                                 raise CatalystError,"Can't find "+x+".py plugin in "+\
198                                         conf_values["sharedir"]+"/modules/"
199
200                 for x in valid_build_targets:
201                         try:
202                                 fh=open(conf_values["sharedir"]+"/modules/"+x+".py")
203                                 module=imp.load_module(x,fh,"modules/"+x+".py",(".py","r",imp.PY_SOURCE))
204                                 module.register(targetmap)
205                                 fh.close()
206
207                         except IOError:
208                                 raise CatalystError,"Can't find "+x+".py plugin in "+\
209                                         conf_values["sharedir"]+"/modules/"
210
211         except ImportError:
212                 print "!!! catalyst: Python modules not found in "+\
213                         conf_values["sharedir"]+"/modules; exiting."
214                 sys.exit(1)
215
216         return targetmap
217
218 def build_target(addlargs, targetmap):
219         try:
220                 if addlargs["target"] not in targetmap:
221                         raise CatalystError,"Target \""+addlargs["target"]+"\" not available."
222
223                 mytarget=targetmap[addlargs["target"]](conf_values, addlargs)
224
225                 mytarget.run()
226
227         except:
228                 modules.catalyst.util.print_traceback()
229                 print "!!! catalyst: Error encountered during run of target " + addlargs["target"]
230                 sys.exit(1)
231
232 if __name__ == "__main__":
233         targetmap={}
234
235         version()
236         if os.getuid() != 0:
237                 # catalyst cannot be run as a normal user due to chroots, mounts, etc
238                 print "!!! catalyst: This script requires root privileges to operate"
239                 sys.exit(2)
240
241         # we need some options in order to work correctly
242         if len(sys.argv) < 2:
243                 usage()
244                 sys.exit(2)
245
246         # parse out the command line arguments
247         try:
248                 opts,args = getopt.getopt(sys.argv[1:], "apPThvdc:C:f:FVs:", ["purge", "purgeonly", "purgetmponly", "help", "version", "debug",\
249                         "clear-autoresume", "config=", "cli=", "file=", "fetch", "verbose","snapshot="])
250
251         except getopt.GetoptError:
252                 usage()
253                 sys.exit(2)
254
255         # defaults for commandline opts
256         debug=False
257         verbose=False
258         fetch=False
259         myconfig=""
260         myspecfile=""
261         mycmdline=[]
262         myopts=[]
263
264         # check preconditions
265         if len(opts) == 0:
266                 print "!!! catalyst: please specify one of either -f or -C\n"
267                 usage()
268                 sys.exit(2)
269
270         run = False
271         for o, a in opts:
272                 if o in ("-h", "--help"):
273                         usage()
274                         sys.exit(1)
275
276                 if o in ("-V", "--version"):
277                         print "Catalyst version "+__version__
278                         sys.exit(1)
279
280                 if o in ("-d", "--debug"):
281                         conf_values["DEBUG"]="1"
282                         conf_values["VERBOSE"]="1"
283
284                 if o in ("-c", "--config"):
285                         myconfig=a
286
287                 if o in ("-C", "--cli"):
288                         run = True
289                         x=sys.argv.index(o)+1
290                         while x < len(sys.argv):
291                                 mycmdline.append(sys.argv[x])
292                                 x=x+1
293
294                 if o in ("-f", "--file"):
295                         run = True
296                         myspecfile=a
297
298                 if o in ("-F", "--fetchonly"):
299                         conf_values["FETCH"]="1"
300
301                 if o in ("-v", "--verbose"):
302                         conf_values["VERBOSE"]="1"
303
304                 if o in ("-s", "--snapshot"):
305                         if len(sys.argv) < 3:
306                                 print "!!! catalyst: missing snapshot identifier\n"
307                                 usage()
308                                 sys.exit(2)
309                         else:
310                                 run = True
311                                 mycmdline.append("target=snapshot")
312                                 mycmdline.append("version_stamp="+a)
313
314                 if o in ("-p", "--purge"):
315                         conf_values["PURGE"] = "1"
316
317                 if o in ("-P", "--purgeonly"):
318                         conf_values["PURGEONLY"] = "1"
319
320                 if o in ("-T", "--purgetmponly"):
321                         conf_values["PURGETMPONLY"] = "1"
322
323                 if o in ("-a", "--clear-autoresume"):
324                         conf_values["CLEAR_AUTORESUME"] = "1"
325
326         if not run:
327                 print "!!! catalyst: please specify one of either -f or -C\n"
328                 usage()
329                 sys.exit(2)
330
331         # import configuration file and import our main module using those settings
332         parse_config(myconfig)
333         sys.path.append(conf_values["sharedir"]+"/modules")
334         from catalyst_support import *
335
336         # Start checking that digests are valid now that the hash_map was imported
337         # from catalyst_support
338         if "digests" in conf_values:
339                 for i in conf_values["digests"].split():
340                         if i not in hash_map:
341                                 print
342                                 print i+" is not a valid digest entry"
343                                 print "Valid digest entries:"
344                                 print hash_map.keys()
345                                 print
346                                 print "Catalyst aborting...."
347                                 sys.exit(2)
348                         if find_binary(hash_map[i][1]) == None:
349                                 print
350                                 print "digest="+i
351                                 print "\tThe "+hash_map[i][1]+\
352                                         " binary was not found. It needs to be in your system path"
353                                 print
354                                 print "Catalyst aborting...."
355                                 sys.exit(2)
356         if "hash_function" in conf_values:
357                 if conf_values["hash_function"] not in hash_map:
358                         print
359                         print conf_values["hash_function"]+\
360                                 " is not a valid hash_function entry"
361                         print "Valid hash_function entries:"
362                         print hash_map.keys()
363                         print
364                         print "Catalyst aborting...."
365                         sys.exit(2)
366                 if find_binary(hash_map[conf_values["hash_function"]][1]) == None:
367                         print
368                         print "hash_function="+conf_values["hash_function"]
369                         print "\tThe "+hash_map[conf_values["hash_function"]][1]+\
370                                 " binary was not found. It needs to be in your system path"
371                         print
372                         print "Catalyst aborting...."
373                         sys.exit(2)
374
375         # import the rest of the catalyst modules
376         targetmap=import_modules()
377
378         addlargs={}
379
380         if myspecfile:
381                 spec = modules.catalyst.config.SpecParser(myspecfile)
382                 addlargs.update(spec.get_values())
383
384         if mycmdline:
385                 try:
386                         cmdline = modules.catalyst.config.ConfigParser()
387                         cmdline.parse_lines(mycmdline)
388                         addlargs.update(cmdline.get_values())
389                 except CatalystError:
390                         print "!!! catalyst: Could not parse commandline, exiting."
391                         sys.exit(1)
392
393         if "target" not in addlargs:
394                 raise CatalystError, "Required value \"target\" not specified."
395
396         # everything is setup, so the build is a go
397         try:
398                 build_target(addlargs, targetmap)
399
400         except CatalystError:
401                 print
402                 print "Catalyst aborting...."
403                 sys.exit(2)
404         except KeyboardInterrupt:
405                 print "\nCatalyst build aborted due to user interrupt ( Ctrl-C )"
406                 print
407                 print "Catalyst aborting...."
408                 sys.exit(2)
409         except LockInUse:
410                 print "Catalyst aborting...."
411                 sys.exit(2)
412         except:
413                 print "Catalyst aborting...."
414                 raise
415                 sys.exit(2)