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

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

r4374@localhost: bruno | 2011-10-25 12:10:59 +0200

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