Changeset 1900 in ProjectBuilder for devel/pb-modules/lib/ProjectBuilder/VE.pm


Ignore:
Timestamp:
Sep 30, 2014, 10:08:03 AM (10 years ago)
Author:
Bruno Cornec
Message:
  • Add support for docker phase 1: newve working
  • Add separate pb_ve_snap function
  • Add separate pb_ve_docker functions to interact with the registry and the repository
  • Add a dockerregistry conf parameter
File:
1 edited

Legend:

Unmodified
Added
Removed
  • devel/pb-modules/lib/ProjectBuilder/VE.pm

    r1646 r1900  
    3131 
    3232our @ISA = qw(Exporter);
    33 our @EXPORT = qw(pb_ve_launch);
     33our @EXPORT = qw(pb_ve_launch pb_ve_snap);
    3434
    3535($VERSION,$REVISION) = pb_version_init();
     
    4343=head1 DESCRIPTION
    4444
    45 This modules provides functions to deal with Virtual Environements (VE), aka chroot.
     45This modules provides functions to deal with Virtual Environements (VE), aka chroot/containers.
    4646
    4747=head1 SYNOPSIS
     
    6060=item B<pb_ve_launch>
    6161
    62 This function launch a VE, creating it if necessary using multiple external potential tools.
     62This function launches a VE, creating it if necessary using multiple external potential tools.
    6363
    6464=cut
     
    6969my $pbforce = shift || 0;       # By default do not rebuild VE
    7070my $locsnap = shift || 0;       # By default do not snap VE
     71my $vetype = shift || undef;        # By default no image
     72my $pbimage = shift || undef;       # By default no image
     73
     74my $dockerregistry = undef;
     75my $docrepo = undef;            # By default no repository for docker available
    7176
    7277# Get distro context
     
    7479
    7580# Get VE context
    76 my ($ptr,$vepath) = pb_conf_get("vetype","vepath");
    77 my $vetype = $ptr->{$ENV{'PBPROJ'}};
    78 
     81if (not defined $vetype) {
     82    my ($ptr) = pb_conf_get("vetype");
     83    $vetype = $ptr->{$ENV{'PBPROJ'}};
     84}
    7985confess "No vetype defined for $ENV{PBPROJ}" unless (defined $vetype);
    8086pb_log(1, "Using vetype $vetype for $ENV{PBPROJ}\n");
    81 
    82 if (($vetype eq "chroot") || ($vetype eq "schroot")) {
     87my ($vepath) = pb_conf_get("vepath");
     88
     89if (($vetype eq "chroot") || ($vetype eq "schroot") || ($vetype eq "docker")) {
    8390
    8491    # We need to avoid umask propagation to the VE
     
    8794    # We can probably only get those params now we have the distro context
    8895    my ($rbsb4pi,$rbspi,$vesnap,$oscodename,$osmindep,$verebuild,$rbsmirrorsrv) = pb_conf_get_if("rbsb4pi","rbspi","vesnap","oscodename","osmindep","verebuild","rbsmirrorsrv");
     96    if ($vetype eq "docker") {
     97        # Check acces to registry
     98        ($dockerregistry) = pb_conf_get("dockerregistry");
     99        if ((defined $dockerregistry) && (defined $dockerregistry->{$ENV{'PBPROJ'}})) {
     100            pb_ve_docker_registry($dockerregistry->{$ENV{'PBPROJ'}});
     101        } else {
     102            die "When using docker you need to declare a dockerregistry parameter. Read the man page"
     103        }
     104    }
    89105
    90106    # Architecture consistency
     
    98114    if ($EFFECTIVE_USER_ID != 0) {
    99115        $sudocmd ="sudo ";
    100         foreach my $proxy (qw/http_proxy ftp_proxy/) {
     116    foreach my $proxy (qw/http_proxy ftp_proxy/) {
    101117            if (defined $ENV{$proxy}) {
    102118                open(CMD,"sudo sh -c 'echo \$$proxy' |") or die "can't run sudo sh?: $!";
     
    113129
    114130    my $root = pb_path_expand($vepath->{$ENV{PBPROJ}});
    115     if (((defined $verebuild) && ($verebuild->{$ENV{'PBPROJ'}} =~ /true/i)) || ($pbforce == 1)) {
     131   
     132    if (((((defined $verebuild) && ($verebuild->{$ENV{'PBPROJ'}} =~ /true/i)) || ($pbforce == 1)) && ($vetype ne "docker"))
     133        # For docker we may have a reference image that we'll use
     134        || (((not defined $pbimage) || ($pbimage eq "")) && ($vetype eq "docker"))) {
     135
    116136        my ($verpmtype,$vedebtype) = pb_conf_get("verpmtype","vedebtype");
    117137        my ($rbsopt1) = pb_conf_get_if("rbsopt");
     
    250270
    251271    # Test if an existing snapshot exists and use it if appropriate
    252     # And also use it of no local extracted VE is present
     272    # And also use it if no local extracted VE is present
    253273    if ((-f "$root/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}.tar.gz") &&
    254274    (((defined $vesnap->{$v}) && ($vesnap->{$v} =~ /true/i)) ||
     
    266286    pb_system("$sudocmd $command 755 $root/$pbos->{'name'} $root/$pbos->{'name'}/$pbos->{'version'} $root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}","Fixing permissions");
    267287
     288    # If docker, create the image and remove the now temp dir except if we had one already
     289    if ($vetype eq "docker") {
     290        if ((not defined $pbimage) || ($pbimage eq "")) {
     291            # Snaphot the VE to serve as an input for docker
     292            pb_ve_snap($pbos,$root);
     293            # Create the docker image from the previous bootstrap
     294            my $cmd1 = pb_check_req("docker",0);
     295            $docrepo = pb_ve_docker_repo($dockerregistry->{$ENV{'PBPROJ'}});
     296            pb_system("$cmd1 import - $docrepo:$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'} < $root/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}.tar.gz");
     297            pb_system("$cmd1 push $docrepo:$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}");
     298        #} else {
     299        }
     300    }
     301
    268302    # Nothing more to do for VE. No real launch
    269303} else {
     
    299333}
    300334
     335# Snapshot the VE
     336sub pb_ve_snap {
     337
     338my $pbos = shift;
     339my $root = shift;
     340my $tpdir = "$root/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}";
     341pb_system("sudo tar cz -C $tpdir -f $root/$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}.tar.gz .","Creating a snapshot of $tpdir");
     342}
     343
     344# Returns the docker registry to interact with
     345sub pb_ve_docker_registry {
     346
     347my $dockerreg = shift || undef;
     348my $wget = pb_check_req("wget",0);
     349my ($scheme, $account, $host, $port, $path) = pb_get_uri($dockerreg);
     350my $docreg = $scheme."://";
     351$docreg .= $account."@" if ((defined $account) && ($account ne ""));
     352$docreg .= $host;
     353$docreg .= ":$port" if ((defined $port) && ($port ne ""));
     354open(FD,"$wget $docreg -q -O -|") || die "Unable to talk to the docker registry $docreg";
     355my $found = undef;
     356while (<FD>) {
     357    $found = 1 if (/docker-registry/);
     358}
     359close(FD);
     360die "No correct docker-registry answering at $docreg. Please check your configuration" if (not defined $found);
     361#
     362return($docreg);
     363}
     364
     365# Returns the docker repository to interact with
     366sub pb_ve_docker_repo {
     367
     368my $dockerreg = shift || undef;
     369my $docrepo = "";
     370my ($scheme, $account, $host, $port, $path) = pb_get_uri($dockerreg);
     371$docrepo .= $host;
     372$docrepo .= ":$port" if ((defined $port) && ($port ne ""));
     373$docrepo .= "$path";
     374return($docrepo);
     375}
    301376
    302377=head1 WEB SITES
Note: See TracChangeset for help on using the changeset viewer.