~debfactory-devs/debfactory/devel

2 by janito
Added schroot_build.py
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
#
88 by Joao Pinto
Debian schroots can be built with -p=debian
4
#   (C) Copyright 2006-2010, GetDeb Team - https://launchpad.net/~getdeb
2 by janito
Added schroot_build.py
5
#    This program is free software: you can redistribute it and/or modify
6
#    it under the terms of the GNU General Public License as published by
7
#    the Free Software Foundation, either version 3 of the License, or
8
#    (at your option) any later version.
9
#
10
#    This program is distributed in the hope that it will be useful,
11
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
#    GNU General Public License for more details.
14
#
15
#    You should have received a copy of the GNU General Public License
16
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
#
4 by janito
Removed system config files copy already managed by the schroot config scripts
18
"""
118 by Joao Pinto
build_schroot.py:
19
chroot build script
88 by Joao Pinto
Debian schroots can be built with -p=debian
20
21
This script automates the creation of a file based schroot image
7 by janito
Automated the /etc/schroot/schroot.conf entry append.
22
23
 The script performs the following actions:
24
	- Check/install the pre-required packages (fakeroot, schroot, etc)
25
	- Install a base system into the specified directory using debootstrap
26
	- Copy required system config files into the chroot
27
	- Customize the configuration (eg. point the repositories to apt-cacher)
28
	- Append the schroot configuration entry to /etc/schroot/schoot.conf
4 by janito
Removed system config files copy already managed by the schroot config scripts
29
"""
30
import os
31
import sys
32
import shutil
33
import commands
88 by Joao Pinto
Debian schroots can be built with -p=debian
34
import glob
87.1.3 by Joao Pinto
Added ABS setup documentation
35
from optparse import OptionParser
88 by Joao Pinto
Debian schroots can be built with -p=debian
36
2 by janito
Added schroot_build.py
37
available_archs = ("i386", "amd64")
38
4 by janito
Removed system config files copy already managed by the schroot config scripts
39
apt_mirror = 'localhost:3142'
40
87.1.3 by Joao Pinto
Added ABS setup documentation
41
def command_line_parser():
42
	""" 
43
	Returns an option parser object with the available 
44
	command line parameters
45
	"""        
46
	parser = OptionParser()
118 by Joao Pinto
build_schroot.py:
47
	parser.add_option("-c", "--chroot-base-dir",
87.1.3 by Joao Pinto
Added ABS setup documentation
48
	    action = "store", type="string", dest="chroot_dir", \
124 by Joao Pinto
Switch to "aufs" schroot directories
49
	    help = "Base directory to install the chroot directory into", \
87.1.3 by Joao Pinto
Added ABS setup documentation
50
	    default = '', \
51
	    )                        
52
	parser.add_option("-d", "--dist", \
53
	    action = "store", type="string", dest="dist", \
54
	    help = "Target distribution release for debootstrap")
55
	parser.add_option("-a", "--arch", \
56
	    action = "store", type="string", dest="arch", \
57
	    help = "Target architecture for debootstrap")
118 by Joao Pinto
build_schroot.py:
58
	parser.add_option("-s", "--sbuild", \
87.1.3 by Joao Pinto
Added ABS setup documentation
59
	    action = "store_true", dest="sbuild", default=False, \
60
	    help = "Configure for sbuild")            
61
	parser.add_option("-p", "--provider", \
62
	    action = "store", type="string", dest="provider", \
63
	    help = "Distribution provider ([ubuntu]| debian)", \
64
	    default='ubuntu')
65
	
66
	return parser
7.1.1 by Christoph Korn
Added options for the script:
67
2 by janito
Added schroot_build.py
68
def check_and_install_package(package):
88 by Joao Pinto
Debian schroots can be built with -p=debian
69
	""" 
70
	check if a package is installed, if not present the option to install it
71
	"""
72
	is_installed = int(commands.getoutput('dpkg -l '+package+' 2>&1 | grep -c "^ii"'))
73
	if is_installed:
74
		print "Package "+package+" is installed."
75
	else:
7 by janito
Automated the /etc/schroot/schroot.conf entry append.
76
		print "Package "+package+" is required but not installed."
2 by janito
Added schroot_build.py
77
		install = ""
78
		while install not in ("y", "n"):
79
			install = raw_input('Install it now ? (y/n)')
80
			if install == "n":
7 by janito
Automated the /etc/schroot/schroot.conf entry append.
81
				print "Exiting on missing package."
2 by janito
Added schroot_build.py
82
				sys.exit(1);
83
		os.system('sudo apt-get install -y '+package)
84
85
def get_host_release():
7 by janito
Automated the /etc/schroot/schroot.conf entry append.
86
	"""
88 by Joao Pinto
Debian schroots can be built with -p=debian
87
	Get the system release (lsb_release -cs)
7 by janito
Automated the /etc/schroot/schroot.conf entry append.
88
	"""
88 by Joao Pinto
Debian schroots can be built with -p=debian
89
	release = commands.getoutput('lsb_release -cs').strip("\r\n")
2 by janito
Added schroot_build.py
90
	return release
91
7 by janito
Automated the /etc/schroot/schroot.conf entry append.
92
def check_create_dir(name):
2 by janito
Added schroot_build.py
93
	"""
7 by janito
Automated the /etc/schroot/schroot.conf entry append.
94
	Check if a given directory exists, create it if not
2 by janito
Added schroot_build.py
95
	"""
96
	if os.path.exists(name):
97
		print name+" was found"
98
	else:
99
		print "Creating directory "+name
100
		os.mkdir(name)
101
102
def copy_to_chroot(fname, chrootdir):
124 by Joao Pinto
Switch to "aufs" schroot directories
103
	print "/"+fname+" -> "+os.path.join(chrootdir, fname)
104
	shutil.copyfile("/"+fname, os.path.join(chrootdir, fname))
2 by janito
Added schroot_build.py
105
106
# Check requirements
107
def check_requirements():
87.1.3 by Joao Pinto
Added ABS setup documentation
108
	global options
2 by janito
Added schroot_build.py
109
	"""
110
	Check script requirements
111
	"""
4 by janito
Removed system config files copy already managed by the schroot config scripts
112
	print "Checking system requirements"
2 by janito
Added schroot_build.py
113
	# Check for dhcroot and debootstrap
114
	check_and_install_package("schroot")
115
	check_and_install_package("debootstrap")
4 by janito
Removed system config files copy already managed by the schroot config scripts
116
	check_and_install_package("apt-cacher-ng")
87.1.3 by Joao Pinto
Added ABS setup documentation
117
	if options.sbuild:
7.1.1 by Christoph Korn
Added options for the script:
118
		check_and_install_package("sbuild")
2 by janito
Added schroot_build.py
119
	print
120
121
def check_base_dir():
87.1.3 by Joao Pinto
Added ABS setup documentation
122
	global options
123
	if options.chroot_dir:
124
		check_create_dir(options.chroot_dir)
125
		return options.chroot_dir
2 by janito
Added schroot_build.py
126
	base_dir = "/home/schroot"
7 by janito
Automated the /etc/schroot/schroot.conf entry append.
127
	new_base_dir = raw_input("Please enter the chroot install base directory\n["+base_dir+"]: ")
2 by janito
Added schroot_build.py
128
	basedir = new_base_dir or base_dir
7 by janito
Automated the /etc/schroot/schroot.conf entry append.
129
	check_create_dir(basedir)
2 by janito
Added schroot_build.py
130
	return basedir
131
132
def check_chroot_release(basedir):
87.1.3 by Joao Pinto
Added ABS setup documentation
133
	global options, available_release
88 by Joao Pinto
Debian schroots can be built with -p=debian
134
	scripts_dir = '/usr/share/debootstrap/scripts/'
87.1.3 by Joao Pinto
Added ABS setup documentation
135
	default_release = options.dist or get_host_release()
88 by Joao Pinto
Debian schroots can be built with -p=debian
136
	available_releases = [x.replace(scripts_dir,'') \
137
						 for x in glob.glob(scripts_dir+"*") if '.' not in x]
138
	print 'Available releases (fount at %s):\n %s' % (scripts_dir, ','.join(available_releases))
87.1.3 by Joao Pinto
Added ABS setup documentation
139
	selected_release = options.dist
140
	while selected_release not in available_releases:
141
		selected_release = raw_input("Please enter the chroot release version ["+default_release+"]: ")
142
		selected_release = selected_release or default_release
143
	default_arch = options.arch or "i386"
144
	selected_arch = options.arch
145
	while selected_arch not in available_archs:
146
		selected_arch = raw_input("Please enter the chroot architecture, options are: "+','.join(available_archs)+"\n["+default_arch+"]: ")
147
		selected_arch = selected_arch or default_arch
2 by janito
Added schroot_build.py
148
118 by Joao Pinto
build_schroot.py:
149
	chrootdir = os.path.join(basedir, selected_release + "-" + selected_arch)
7 by janito
Automated the /etc/schroot/schroot.conf entry append.
150
	check_create_dir(chrootdir)
87.1.3 by Joao Pinto
Added ABS setup documentation
151
	return (selected_release, selected_arch, chrootdir)
2 by janito
Added schroot_build.py
152
153
def debootstrap(release, arch, chrootdir):
87.1.3 by Joao Pinto
Added ABS setup documentation
154
	global options
88 by Joao Pinto
Debian schroots can be built with -p=debian
155
	print "Installing base system for "+release+" ["+arch+"] into "+ chrootdir	
156
	return os.system("debootstrap --variant=buildd --arch "+arch+" "+release+\
87.1.3 by Joao Pinto
Added ABS setup documentation
157
		" "+chrootdir+" http://"+apt_mirror+"/"+options.provider+"/");
2 by janito
Added schroot_build.py
158
159
def chroot_config_files_update(chrootdir, release, arch):
90 by joao.pinto at getdeb
Commit last merge
160
    """
161
    Updates config files fromt he chrootdir environment
162
    """
163
    default_sources="""
4 by janito
Removed system config files copy already managed by the schroot config scripts
164
deb http://mirror/ubuntu/ release main restricted
5 by janito
Force noninteractive install for console-setup
165
deb-src http://mirror/ubuntu/ release main restricted
4 by janito
Removed system config files copy already managed by the schroot config scripts
166
deb http://mirror/ubuntu/ release-updates main restricted
5 by janito
Force noninteractive install for console-setup
167
deb-src http://mirror/ubuntu/ release-updates main restricted
4 by janito
Removed system config files copy already managed by the schroot config scripts
168
169
deb http://mirror/ubuntu/ release universe
5 by janito
Force noninteractive install for console-setup
170
deb-src http://mirror/ubuntu/ release universe
4 by janito
Removed system config files copy already managed by the schroot config scripts
171
deb http://mirror/ubuntu/ release-updates universe
5 by janito
Force noninteractive install for console-setup
172
deb-src http://mirror/ubuntu/ release-updates universe
4 by janito
Removed system config files copy already managed by the schroot config scripts
173
174
deb http://mirror/ubuntu/ release multiverse
5 by janito
Force noninteractive install for console-setup
175
deb-src http://mirror/ubuntu/ release multiverse
4 by janito
Removed system config files copy already managed by the schroot config scripts
176
deb http://mirror/ubuntu/ release-updates multiverse
5 by janito
Force noninteractive install for console-setup
177
deb-src http://mirror/ubuntu/ release-updates multiverse
4 by janito
Removed system config files copy already managed by the schroot config scripts
178
179
deb http://mirror/ubuntu release-security main restricted
5 by janito
Force noninteractive install for console-setup
180
deb-src http://mirror/ubuntu release-security main restricted
4 by janito
Removed system config files copy already managed by the schroot config scripts
181
deb http://mirror/ubuntu release-security universe
5 by janito
Force noninteractive install for console-setup
182
deb-src http://mirror/ubuntu release-security universe
4 by janito
Removed system config files copy already managed by the schroot config scripts
183
deb http://mirror/ubuntu release-security multiverse
5 by janito
Force noninteractive install for console-setup
184
deb-src http://mirror/ubuntu release-security multiverse
4 by janito
Removed system config files copy already managed by the schroot config scripts
185
	"""
90 by joao.pinto at getdeb
Commit last merge
186
    apt_mirror = "localhost:3142" 
187
    default_sources = default_sources.replace("mirror", apt_mirror)
188
    default_sources = default_sources.replace("release", release)
189
    if options.provider == 'ubuntu':
190
    	FILE = open(chrootdir+"/etc/apt/sources.list","w")
191
    	FILE.writelines(default_sources)
192
    	FILE.close()
193
    copy_to_chroot("etc/resolv.conf", chrootdir);
194
    copy_to_chroot("etc/hosts", chrootdir);
195
    copy_to_chroot("etc/sudoers", chrootdir);
196
    os.system("echo "+release+"."+arch+" > "+chrootdir+"/etc/debian_chroot")
147 by Christoph Korn
Disable daemons in chroot
197
    # Disable daemons in chroot:
198
    # http://bazaar.launchpad.net/~ubuntu-dev/ubuntu-dev-tools/trunk/view/head:/mk-sbuild#L765
199
    os.system("echo "+release+"."+arch+" > "+chrootdir+"/etc/debian_chroot")
200
    os.system("echo '#!/bin/sh' > "+chrootdir+"/usr/sbin/policy-rc.d")
201
    os.system("echo 'while true; do' >> "+chrootdir+"/usr/sbin/policy-rc.d")
202
    os.system("echo '    case \"$1\" in' >> "+chrootdir+"/usr/sbin/policy-rc.d")
203
    os.system("echo '      -*) shift ;;' >> "+chrootdir+"/usr/sbin/policy-rc.d")
204
    os.system("echo '      makedev) exit 0;;' >> "+chrootdir+"/usr/sbin/policy-rc.d")
205
    os.system("echo '      x11-common) exit 0;;' >> "+chrootdir+"/usr/sbin/policy-rc.d")
206
    os.system("echo '      *) exit 101;;' >> "+chrootdir+"/usr/sbin/policy-rc.d")
207
    os.system("echo '    esac' >> "+chrootdir+"/usr/sbin/policy-rc.d")
208
    os.system("echo 'done' >> "+chrootdir+"/usr/sbin/policy-rc.d")
209
    os.system("chmod a+x "+chrootdir+"/usr/sbin/policy-rc.d")
2 by janito
Added schroot_build.py
210
211
def chroot_postinstall_update(chrootdir, release, arch):
118 by Joao Pinto
build_schroot.py:
212
    global options
213
    """
214
    Perform post-install update actions"
215
    """
216
    print "Post install actions for the chroot image"
217
    print "Installing some support tools"
218
    os.system("chroot "+chrootdir+" apt-get -y update")
219
    os.system("chroot "+chrootdir+" apt-get -y --force-yes install gnupg apt-utils")
220
    os.system("chroot "+chrootdir+" apt-get -y update")
221
    os.system("chroot "+chrootdir+" locale-gen "+lang)	
222
    os.system("chroot "+chrootdir+" apt-get -y --no-install-recommends install wget dh-make fakeroot cdbs sudo")
223
    os.system("chroot "+chrootdir+" apt-get -y --no-install-recommends install devscripts vim")
224
    os.system("mount --bind /proc "+chrootdir +"/proc")
225
    os.system("chroot "+chrootdir+" sudo DEBIAN_FRONTEND=noninteractive apt-get -y --no-install-recommends install console-setup")
226
    os.system("umount "+chrootdir +"/proc")
227
    # We need to install build-essential for hardy, it is not contained on the buildd variant
228
    os.system("chroot "+chrootdir+" apt-get -y --no-install-recommends install build-essential")
229
    os.system("chroot "+chrootdir+" apt-get -y --no-install-recommends upgrade")
230
    os.system("chroot "+chrootdir+" apt-get clean")
124 by Joao Pinto
Switch to "aufs" schroot directories
231
    os.system("du -sh "+chrootdir)
137 by Christoph Korn
build_schroot.py: The admin group is lpadmin.
232
    schroot_conf = ""
118 by Joao Pinto
build_schroot.py:
233
    schroot_conf += "["+release+"-"+arch+"]"+"\n"
124 by Joao Pinto
Switch to "aufs" schroot directories
234
    schroot_conf += "type=directory\n"
235
    schroot_conf += "directory="+chrootdir+"\n"
236
    schroot_conf += "union-type=aufs\n"
137 by Christoph Korn
build_schroot.py: The admin group is lpadmin.
237
    schroot_conf += "groups=lpadmin\n"
118 by Joao Pinto
build_schroot.py:
238
    if options.sbuild == 1:
239
    	schroot_conf += "root-groups=sbuild\n"
240
    if arch=="i386":
241
    	schroot_conf += "personality=linux32\n"
242
    config_fn = "/etc/schroot/chroot.d/" + release + "-" + arch         
243
    config_file = open(config_fn, 'w')
244
    config_file.writelines(schroot_conf)
245
    config_file.close()
246
    print ""
247
    print "You can now use your schroot with:\n\tschroot -c "+release+"-"+arch+" -p"
2 by janito
Added schroot_build.py
248
87.1.3 by Joao Pinto
Added ABS setup documentation
249
250
print 
251
print "###### schroot build helper script"
252
print
253
254
# We need root
255
if os.getuid() != 0 :
256
	print 'This script needs to be run with root privileges !'
257
	sys.exit(2)
258
259
lang = os.environ['LANG']
260
os.putenv('LANG', 'C') # We rely on system commands output
261
262
(options, args) = command_line_parser().parse_args()
263
7 by janito
Automated the /etc/schroot/schroot.conf entry append.
264
my_release = get_host_release()
2 by janito
Added schroot_build.py
265
check_requirements()
266
basedir = check_base_dir()
267
(release, arch, chrootdir) = check_chroot_release(basedir)
88 by Joao Pinto
Debian schroots can be built with -p=debian
268
rc = debootstrap(release, arch, chrootdir)
269
if rc != 0:
270
	print "Debootstrap failed!"
271
	sys.exit(1)
2 by janito
Added schroot_build.py
272
chroot_config_files_update(chrootdir, release, arch)
273
chroot_postinstall_update(chrootdir, release, arch)
274
4 by janito
Removed system config files copy already managed by the schroot config scripts
275
print 
276
print "schroot for "+release+" successfully created."
2 by janito
Added schroot_build.py
277