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