aboutsummaryrefslogtreecommitdiff
blob: 1ab7e7da5980f2ce3c90b2077c0e5e8166717667 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""
netboot target, version 2
"""
# NOTE: That^^ docstring has influence catalyst-spec(5) man page generation.

import os,string,types
from catalyst_support import *
from generic_stage_target import *

class netboot2_target(generic_stage_target):
	"""
	Builder class for a netboot build, version 2
	"""
	def __init__(self,spec,addlargs):
		self.required_values=[
			"boot/kernel"
		]
		self.valid_values=self.required_values[:]
		self.valid_values.extend([
			"netboot2/packages",
			"netboot2/use",
			"netboot2/extra_files",
			"netboot2/overlay",
			"netboot2/busybox_config",
			"netboot2/root_overlay",
			"netboot2/linuxrc"
		])

		try:
			if "netboot2/packages" in addlargs:
				if type(addlargs["netboot2/packages"]) == types.StringType:
					loopy=[addlargs["netboot2/packages"]]
				else:
					loopy=addlargs["netboot2/packages"]

				for x in loopy:
					self.valid_values.append("netboot2/packages/"+x+"/files")
		except:
			raise CatalystError,"configuration error in netboot2/packages."

		generic_stage_target.__init__(self,spec,addlargs)
		self.set_build_kernel_vars()
		self.settings["merge_path"]=normpath("/tmp/image/")

	def set_target_path(self):
		self.settings["target_path"]=normpath(self.settings["storedir"]+"/builds/"+\
			self.settings["target_subpath"]+"/")
		if "AUTORESUME" in self.settings \
			and os.path.exists(self.settings["autoresume_path"]+"setup_target_path"):
				print "Resume point detected, skipping target path setup operation..."
		else:
			# first clean up any existing target stuff
			if os.path.isfile(self.settings["target_path"]):
				cmd("rm -f "+self.settings["target_path"], \
					"Could not remove existing file: "+self.settings["target_path"],env=self.env)
				touch(self.settings["autoresume_path"]+"setup_target_path")

		if not os.path.exists(self.settings["storedir"]+"/builds/"):
			os.makedirs(self.settings["storedir"]+"/builds/")

	def copy_files_to_image(self):
		# copies specific files from the buildroot to merge_path
		myfiles=[]

		# check for autoresume point
		if "AUTORESUME" in self.settings \
			and os.path.exists(self.settings["autoresume_path"]+"copy_files_to_image"):
				print "Resume point detected, skipping target path setup operation..."
		else:
			if "netboot2/packages" in self.settings:
				if type(self.settings["netboot2/packages"]) == types.StringType:
					loopy=[self.settings["netboot2/packages"]]
				else:
					loopy=self.settings["netboot2/packages"]

			for x in loopy:
				if "netboot2/packages/"+x+"/files" in self.settings:
				    if type(self.settings["netboot2/packages/"+x+"/files"]) == types.ListType:
					    myfiles.extend(self.settings["netboot2/packages/"+x+"/files"])
				    else:
					    myfiles.append(self.settings["netboot2/packages/"+x+"/files"])

			if "netboot2/extra_files" in self.settings:
				if type(self.settings["netboot2/extra_files"]) == types.ListType:
					myfiles.extend(self.settings["netboot2/extra_files"])
				else:
					myfiles.append(self.settings["netboot2/extra_files"])

			try:
				cmd("/bin/bash "+self.settings["controller_file"]+\
					" image " + list_bashify(myfiles),env=self.env)
			except CatalystError:
				self.unbind()
				raise CatalystError,"Failed to copy files to image!"

			touch(self.settings["autoresume_path"]+"copy_files_to_image")

	def setup_overlay(self):
		if "AUTORESUME" in self.settings \
		and os.path.exists(self.settings["autoresume_path"]+"setup_overlay"):
			print "Resume point detected, skipping setup_overlay operation..."
		else:
			if "netboot2/overlay" in self.settings:
				for x in self.settings["netboot2/overlay"]:
					if os.path.exists(x):
						cmd("rsync -a "+x+"/ "+\
							self.settings["chroot_path"] + self.settings["merge_path"], "netboot2/overlay: "+x+" copy failed.",env=self.env)
				touch(self.settings["autoresume_path"]+"setup_overlay")

	def move_kernels(self):
		# we're done, move the kernels to builds/*
		# no auto resume here as we always want the
		# freshest images moved
		try:
			cmd("/bin/bash "+self.settings["controller_file"]+\
				" final",env=self.env)
			print ">>> Netboot Build Finished!"
		except CatalystError:
			self.unbind()
			raise CatalystError,"Failed to move kernel images!"

	def remove(self):
		if "AUTORESUME" in self.settings \
			and os.path.exists(self.settings["autoresume_path"]+"remove"):
			print "Resume point detected, skipping remove operation..."
		else:
			if self.settings["spec_prefix"]+"/rm" in self.settings:
				for x in self.settings[self.settings["spec_prefix"]+"/rm"]:
					# we're going to shell out for all these cleaning operations,
					# so we get easy glob handling
					print "netboot2: removing " + x
					os.system("rm -rf " + self.settings["chroot_path"] + self.settings["merge_path"] + x)

	def empty(self):
		if "AUTORESUME" in self.settings \
			and os.path.exists(self.settings["autoresume_path"]+"empty"):
			print "Resume point detected, skipping empty operation..."
		else:
			if "netboot2/empty" in self.settings:
				if type(self.settings["netboot2/empty"])==types.StringType:
					self.settings["netboot2/empty"]=self.settings["netboot2/empty"].split()
				for x in self.settings["netboot2/empty"]:
					myemp=self.settings["chroot_path"] + self.settings["merge_path"] + x
					if not os.path.isdir(myemp):
						print x,"not a directory or does not exist, skipping 'empty' operation."
						continue
					print "Emptying directory", x
					# stat the dir, delete the dir, recreate the dir and set
					# the proper perms and ownership
					mystat=os.stat(myemp)
					shutil.rmtree(myemp)
					os.makedirs(myemp,0755)
					os.chown(myemp,mystat[ST_UID],mystat[ST_GID])
					os.chmod(myemp,mystat[ST_MODE])
		touch(self.settings["autoresume_path"]+"empty")

	def set_action_sequence(self):
	    self.settings["action_sequence"]=["unpack","unpack_snapshot","config_profile_link",
	    				"setup_confdir","portage_overlay","bind","chroot_setup",\
					"setup_environment","build_packages","root_overlay",\
					"copy_files_to_image","setup_overlay","build_kernel","move_kernels",\
					"remove","empty","unbind","clean","clear_autoresume"]

def register(foo):
	foo.update({"netboot2":netboot2_target})
	return foo