source: ProjectBuilder/devel/pb-modules/lib/ProjectBuilder/VE.pm@ 1517

Last change on this file since 1517 was 1517, checked in by Bruno Cornec, 12 years ago

r4765@localhost: bruno | 2012-05-10 17:08:00 +0200

  • pb.conf: centos-5.6 moved python-libs into a separate package. List it or otherwise we can't run yum. Fix the mirror servers for centos-4,5 to just get the latest, and add mirror servers for 6.
  • pb.conf.pod: document addrepo, looked here and didn't find it, accidentally found it in code before adding it myself. Fix pod glitch at the end (missing =over/=back)
  • Distribution.pm: move the code to apply ftp_proxy/http_proxy from the conf files to a sub function since it's now needed in two places. Log the proxy and verify that everything we tried to install succeeded since yum will silently tolerate failing to install things, but rpmbuild will complain about missing dependencies. Tolerate an error from testing install of an rpm in pb_distro_setuprepo_gen.
  • Env.pm: Give an example of multi-line filters and an example of a transform.
  • VE.pm: If the verpmtype isn't defined, error out cleanly. Otherwise we're just going to fail later with lots of noisy output.
File size: 9.7 KB
Line 
1#!/usr/bin/perl -w
2#
3# Common functions for virtual environment
4#
5# $Id$
6#
7
8package ProjectBuilder::VE;
9
10use strict;
11use Data::Dumper;
12use Carp 'confess';
13use English;
14use ProjectBuilder::Version;
15use ProjectBuilder::Base;
16use ProjectBuilder::Conf;
17use ProjectBuilder::Distribution;
18
19# Global vars
20# Inherit from the "Exporter" module which handles exporting functions.
21
22use vars qw($VERSION $REVISION @ISA @EXPORT);
23use Exporter;
24
25# Export, by default, all the functions into the namespace of
26# any code which uses this module.
27
28our @ISA = qw(Exporter);
29our @EXPORT = qw(pb_ve_launch);
30
31($VERSION,$REVISION) = pb_version_init();
32
33=pod
34
35=head1 NAME
36
37ProjectBuilder::VE, part of the project-builder.org - module dealing with Virtual Environment
38
39=head1 DESCRIPTION
40
41This modules provides functions to deal with Virtual Environements (VE), aka chroot.
42
43=head1 SYNOPSIS
44
45 use ProjectBuilder::VE;
46
47 #
48 # Return information on the running distro
49 #
50 my $pbos = pb_ve_launch();
51
52=head1 USAGE
53
54=over 4
55
56=item B<pb_ve_launch>
57
58This function launch a VE, creating it if necessary using multiple external potential tools.
59
60=cut
61
62sub pb_ve_launch {
63
64my $v = shift || undef;
65my $pbforce = shift || 0; # By default do not rebuild VE
66my $pbsnap = shift || 0; # By default do not snap VE
67
68# Get distro context
69my $pbos = pb_distro_get_context($v);
70
71# Get VE context
72my ($ptr,$vepath) = pb_conf_get("vetype","vepath");
73my $vetype = $ptr->{$ENV{'PBPROJ'}};
74
75confess "No vetype defined for $ENV{PBPROJ}" unless (defined $vetype);
76pb_log(1, "Using vetype $vetype for $ENV{PBPROJ}\n");
77
78if (($vetype eq "chroot") || ($vetype eq "schroot")) {
79
80 # We need to avoid umask propagation to the VE
81 umask 0022;
82
83 # We can probably only get those params now we have the distro context
84 my ($rbsb4pi,$rbspi,$vesnap,$oscodename,$osmindep,$verebuild,$rbsmirrorsrv) = pb_conf_get_if("rbsb4pi","rbspi","vesnap","oscodename","osmindep","verebuild","rbsmirrorsrv");
85
86 # Architecture consistency
87 my $arch = pb_get_arch();
88 if ($arch ne $pbos->{'arch'}) {
89 die "Unable to launch a VE of architecture $pbos->{'arch'} on a $arch platform" unless (($pbos->{'arch'} =~ /i?86/o) && ($arch eq "x86_64"));
90 }
91
92 # If we are already root (from pbmkbm e.g.) don't use sudo, just call the command
93 my $sudocmd="";
94 $sudocmd ="sudo " if ($EFFECTIVE_USER_ID != 0);
95
96 my $root = pb_path_expand($vepath->{$ENV{PBPROJ}});
97 if (((defined $verebuild) && ($verebuild->{$ENV{'PBPROJ'}} =~ /true/i)) || ($pbforce == 1)) {
98 my ($verpmtype,$vedebtype) = pb_conf_get("verpmtype","vedebtype");
99 my ($rbsopt1) = pb_conf_get_if("rbsopt");
100
101 # We have to rebuild the chroot
102 if ($pbos->{'type'} eq "rpm") {
103
104 # Which tool is used
105 my $verpmstyle = $verpmtype->{$ENV{'PBPROJ'}};
106 die "No verpmtype defined for $ENV{PBPROJ}" unless (defined $verpmstyle);
107
108 # Get potential rbs option
109 my $rbsopt = "";
110 if (defined $rbsopt1) {
111 if (defined $rbsopt1->{$verpmstyle}) {
112 $rbsopt = $rbsopt1->{$verpmstyle};
113 } elsif (defined $rbsopt1->{$ENV{'PBPROJ'}}) {
114 $rbsopt = $rbsopt1->{$ENV{'PBPROJ'}};
115 } else {
116 $rbsopt = "";
117 }
118 }
119
120 my $postinstall = pb_ve_get_postinstall($pbos,$rbspi,$verpmstyle);
121 if ($verpmstyle eq "rinse") {
122 # Need to reshape the mirrors generated with local before-post-install script
123 my $b4post = "--before-post-install ";
124 my $postparam = pb_distro_get_param($pbos,$rbsb4pi);
125 if ($postparam eq "") {
126 $b4post = "";
127 } else {
128 $b4post .= $postparam;
129 }
130
131 # Need to reshape the package list for pb
132 my $addpkgs;
133 $postparam = "";
134 $postparam .= pb_distro_get_param($pbos,$osmindep);
135 if ($postparam eq "") {
136 $addpkgs = "";
137 } else {
138 my $pkgfile = "$ENV{'PBTMP'}/addpkgs.lis";
139 open(PKG,"> $pkgfile") || die "Unable to create $pkgfile";
140 foreach my $p (split(/,/,$postparam)) {
141 print PKG "$p\n";
142 }
143 close(PKG);
144 $addpkgs = "--add-pkg-list $pkgfile";
145 }
146
147 my $rinseverb = "";
148 $rinseverb = "--verbose" if ($pbdebug gt 0);
149 my ($rbsconf) = pb_conf_get("rbsconf");
150
151 my $command = pb_check_req("rinse",0);
152 pb_system("$sudocmd $command --directory \"$root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}\" --arch \"$pbos->{'arch'}\" --distribution \"$pbos->{'name'}-$pbos->{'version'}\" --config \"$rbsconf->{$ENV{'PBPROJ'}}\" $b4post $postinstall $rbsopt $addpkgs $rinseverb","Creating the rinse VE for $pbos->{'name'}-$pbos->{'version'} ($pbos->{'arch'})", "verbose");
153 } elsif ($verpmstyle eq "rpmbootstrap") {
154 my $rbsverb = "";
155 foreach my $i (1..$pbdebug) {
156 $rbsverb .= " -v";
157 }
158 my $addpkgs = "";
159 my $postparam = "";
160 $postparam .= pb_distro_get_param($pbos,$osmindep);
161 if ($postparam eq "") {
162 $addpkgs = "";
163 } else {
164 $addpkgs = "-a $postparam";
165 }
166 my $command = pb_check_req("rpmbootstrap",0);
167 pb_system("$sudocmd $command $rbsopt $postinstall $addpkgs $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'} $rbsverb","Creating the rpmbootstrap VE for $pbos->{'name'}-$pbos->{'version'} ($pbos->{'arch'})", "verbose");
168 } elsif ($verpmstyle eq "mock") {
169 my ($rbsconf) = pb_conf_get("rbsconf");
170 my $command = pb_check_req("mock",0);
171 pb_system("$sudocmd $command --init --resultdir=\"/tmp\" --configdir=\"$rbsconf->{$ENV{'PBPROJ'}}\" -r $v $rbsopt","Creating the mock VE for $pbos->{'name'}-$pbos->{'version'} ($pbos->{'arch'})");
172 # Once setup we need to install some packages, the pb account, ...
173 pb_system("$sudocmd $command --install --configdir=\"$rbsconf->{$ENV{'PBPROJ'}}\" -r $v su","Configuring the mock VE");
174 } else {
175 die "Unknown verpmtype type $verpmstyle. Report to dev team";
176 }
177 } elsif ($pbos->{'type'} eq "deb") {
178 my $vedebstyle = $vedebtype->{$ENV{'PBPROJ'}};
179
180 my $codename = pb_distro_get_param($pbos,$oscodename);
181 my $postparam = "";
182 my $addpkgs;
183 $postparam .= pb_distro_get_param($pbos,$osmindep);
184 if ($postparam eq "") {
185 $addpkgs = "";
186 } else {
187 $addpkgs = "--include $postparam";
188 }
189 my $debmir = "";
190 $debmir .= pb_distro_get_param($pbos,$rbsmirrorsrv);
191
192 # Get potential rbs option
193 my $rbsopt = "";
194 if (defined $rbsopt1) {
195 if (defined $rbsopt1->{$vedebstyle}) {
196 $rbsopt = $rbsopt1->{$vedebstyle};
197 } elsif (defined $rbsopt1->{$ENV{'PBPROJ'}}) {
198 $rbsopt = $rbsopt1->{$ENV{'PBPROJ'}};
199 } else {
200 $rbsopt = "";
201 }
202 }
203
204 # debootstrap works with amd64 not x86_64
205 my $debarch = $pbos->{'arch'};
206 $debarch = "amd64" if ($pbos->{'arch'} eq "x86_64");
207 if ($vedebstyle eq "debootstrap") {
208 my $dbsverb = "";
209 $dbsverb = "--verbose" if ($pbdebug gt 0);
210
211 # Some perl modules are in Universe on Ubuntu
212 $rbsopt .= " --components=main,universe" if ($pbos->{'name'} eq "ubuntu");
213
214 pb_system("$sudocmd mkdir -p $root/$pbos->{name}/$pbos->{version}/$pbos->{arch} ; $sudocmd /usr/sbin/debootstrap $dbsverb $rbsopt --arch=$debarch $addpkgs $codename \"$root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}\" $debmir","Creating the debootstrap VE for $pbos->{'name'}-$pbos->{'version'} ($pbos->{'arch'})", "verbose");
215 # debootstrap doesn't create an /etc/hosts file
216 if (! -f "$root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}/etc/hosts" ) {
217 pb_system("$sudocmd cp /etc/hosts $root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}/etc/hosts");
218 }
219 } else {
220 die "Unknown vedebtype type $vedebstyle. Report to dev team";
221 }
222 } elsif ($pbos->{'type'} eq "ebuild") {
223 die "Please teach the dev team how to build gentoo chroot";
224 } else {
225 die "Unknown distribution type $pbos->{'type'}. Report to dev team";
226 }
227 }
228
229 # Fix modes to allow access to the VE for pb user
230 pb_system("$sudocmd chmod 755 $root/$pbos->{'name'} $root/$pbos->{'name'}/$pbos->{'version'} $root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}","Fixing permissions");
231
232 # Test if an existing snapshot exists and use it if appropriate
233 # And also use it of no local extracted VE is present
234 if ((-f "$root/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}.tar.gz") &&
235 (((defined $vesnap->{$v}) && ($vesnap->{$v} =~ /true/i)) ||
236 ((defined $vesnap->{$ENV{'PBPROJ'}}) && ($vesnap->{$ENV{'PBPROJ'}} =~ /true/i)) ||
237 ($pbsnap eq 1) ||
238 (! -d "$root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}"))) {
239 pb_system("$sudocmd rm -rf $root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'} ; $sudocmd mkdir -p $root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'} ; $sudocmd tar xz -C $root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'} -f $root/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}.tar.gz","Extracting snapshot of $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}.tar.gz under $root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}");
240 }
241 # Nothing more to do for VE. No real launch
242} else {
243 die "VE of type $vetype not supported. Report to the dev team";
244}
245}
246
247#
248# Return the postinstall line if needed
249#
250
251sub pb_ve_get_postinstall {
252
253my $pbos = shift;
254my $rbspi = shift;
255my $vestyle = shift;
256my $post = "";
257
258# Do we have a local post-install script
259if ($vestyle eq "rinse") {
260 $post = "--post-install ";
261} elsif ($vestyle eq "rpmbootstrap") {
262 $post = "-s ";
263}
264
265my $postparam = pb_distro_get_param($pbos,$rbspi);
266if ($postparam eq "") {
267 $post = "";
268} else {
269 $post .= $postparam;
270}
271return($post);
272}
273
274
275=head1 WEB SITES
276
277The main Web site of the project is available at L<http://www.project-builder.org/>. Bug reports should be filled using the trac instance of the project at L<http://trac.project-builder.org/>.
278
279=head1 USER MAILING LIST
280
281None exists for the moment.
282
283=head1 AUTHORS
284
285The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
286
287=head1 COPYRIGHT
288
289Project-Builder.org is distributed under the GPL v2.0 license
290described in the file C<COPYING> included with the distribution.
291
292=cut
293
294
2951;
Note: See TracBrowser for help on using the repository browser.