github.com/rigado/snapd@v2.42.5-go-mod+incompatible/cmd/snap-confine/spread-tests/spread-prepare.sh (about)

     1  #!/bin/bash
     2  # This script is started by spread to prepare the execution environment
     3  set -xue
     4  
     5  # Sanity check, are we in the top-level directory of the tree?
     6  test -f configure.ac || ( echo 'this script must be executed from the top-level of the tree' && exit 1)
     7  
     8  # Record where the top level directory is
     9  top_dir=$(pwd)
    10  
    11  # Record the current distribution release data to know what to do
    12  # shellcheck disable=SC1091
    13  {
    14      release_ID="$( . /etc/os-release && echo "${ID:-linux}" )"
    15      release_VERSION_ID="$( . /etc/os-release && echo "${VERSION_ID:-}" )"
    16  }
    17  
    18  
    19  build_debian_or_ubuntu_package() {
    20      local pkg_version
    21      local distro_packaging_git_branch
    22      local distro_packaging_git
    23      local distro_archive
    24      local distro_codename
    25      local sbuild_createchroot_extra=""
    26      pkg_version="$(cat "$top_dir/VERSION")"
    27      
    28      if [ ! -f "$top_dir/spread-tests/distros/$release_ID.$release_VERSION_ID" ] || \
    29         [ ! -f "$top_dir/spread-tests/distros/$release_ID.common" ]; then
    30          echo "Distribution: $release_ID (release $release_VERSION_ID) is not supported"
    31          echo "please read this script and create new files in spread-test/distros"
    32          exit 1
    33      fi
    34  
    35      # source the distro specific vars
    36      # shellcheck disable=SC1090
    37      {
    38          . "$top_dir/spread-tests/distros/$release_ID.$release_VERSION_ID"
    39          . "$top_dir/spread-tests/distros/$release_ID.common"
    40      }
    41  
    42      # sanity check, ensure that essential variables were defined
    43      test -n "$distro_packaging_git_branch"
    44      test -n "$distro_packaging_git"
    45      test -n "$distro_archive"
    46      test -n "$distro_codename"
    47  
    48      # Create a scratch space 
    49      scratch_dir="$(mktemp -d)"
    50      trap 'rm -rf "$scratch_dir"' EXIT
    51  
    52      # Do everything in the scratch directory
    53      cd "$scratch_dir"
    54  
    55      # Fetch the current Ubuntu packaging for the appropriate release
    56      git clone -b "$distro_packaging_git_branch" "$distro_packaging_git" distro-packaging
    57  
    58      # Install all the build dependencies declared by the package.
    59      eatmydata apt-get install --quiet -y gdebi-core
    60      gdebi --quiet --apt-line ./distro-packaging/debian/control | xargs -r apt-get install --quiet -y
    61  
    62      # Generate a new upstream tarball from the current state of the tree
    63      ( cd "$top_dir" && spread-tests/release.sh )
    64  
    65      # Prepare the .orig tarball and unpackaged source tree
    66      cp "$top_dir/snap-confine-$pkg_version.tar.gz" "snap-confine_$pkg_version.orig.tar.gz"
    67      tar -zxf "snap-confine_$pkg_version.orig.tar.gz" 
    68  
    69      # Apply the debian directory from downstream packaging to form a complete source package
    70      mv "distro-packaging/debian" "snap-confine-$pkg_version/debian"
    71      rm -rf distro-packaging
    72  
    73      # Add an automatically-generated changelog entry
    74      # The --controlmaint takes the maintainer details from debian/control
    75      ( cd "snap-confine-$pkg_version" && dch --controlmaint --newversion "${pkg_version}-1" "Automatic CI build")
    76  
    77      # Build an unsigned source package
    78      ( cd "snap-confine-$pkg_version" && dpkg-buildpackage -uc -us -S )
    79  
    80      # Copy source package files to the top-level directory (this helps for
    81      # interactive debugging since the package is available right there)
    82      cp ./*.dsc ./*.debian.tar.* ./*.orig.tar.gz "$top_dir/"
    83  
    84      # Ensure that we have a sbuild chroot ready
    85      if ! schroot -l | grep "chroot:${distro_codename}-.*-sbuild"; then
    86          sbuild-createchroot \
    87              --include=eatmydata \
    88              "--make-sbuild-tarball=/var/lib/sbuild/${distro_codename}-amd64.tar.gz" \
    89              "$sbuild_createchroot_extra" \
    90              "$distro_codename" "$(mktemp -d)" \
    91              "$distro_archive"
    92      fi
    93  
    94      # Build a binary package in a clean chroot.
    95      # NOTE: nocheck is because the package still includes old unit tests that
    96      # are deeply integrated into how ubuntu apparmor denials are logged. This
    97      # should be removed once those test are migrated to spread testes.
    98      DEB_BUILD_OPTIONS=nocheck sbuild \
    99          --arch-all \
   100          --dist="$distro_codename" \
   101          --batch \
   102          "snap-confine_${pkg_version}-1.dsc"
   103  
   104      # Copy all binary packages to the top-level directory
   105      cp ./*.deb "$top_dir/"
   106  }
   107  
   108  
   109  # Apply tweaks
   110  case "$release_ID" in
   111      ubuntu)
   112          # apt update is hanging on security.ubuntu.com with IPv6.
   113          sysctl -w net.ipv6.conf.all.disable_ipv6=1
   114          trap "sysctl -w net.ipv6.conf.all.disable_ipv6=0" EXIT
   115          ;;
   116  esac
   117  
   118  # Install all the build dependencies
   119  case "$release_ID" in
   120      ubuntu|debian)
   121          # treat APT_PROXY as a location of apt-cacher-ng to use
   122          if [ -n "${APT_PROXY:-}" ]; then
   123              printf 'Acquire::http::Proxy "%s";\n' "$APT_PROXY" > /etc/apt/apt.conf.d/00proxy
   124          fi
   125          # cope with unexpected /etc/apt/apt.conf.d/95cloud-init-proxy that may be in the image
   126          rm -f /etc/apt/apt.conf.d/95cloud-init-proxy || :
   127          # trusty support is under development right now
   128          # we special-case the release until we have officially landed
   129          if [ "$release_ID" = "ubuntu" ] && [ "$release_VERSION_ID" = "14.04" ]; then
   130              add-apt-repository ppa:thomas-voss/trusty
   131          fi
   132          apt-get update
   133          apt-get dist-upgrade -y
   134          if [ "$release_ID" = "ubuntu" ] && [ "$release_VERSION_ID" = "14.04" ]; then
   135              apt-get install -y systemd
   136              # starting systemd manually is working around
   137              # systemd not running as PID 1 on trusty systems.
   138              service systemd start
   139          fi
   140          # On Debian and derivatives we need the following things:
   141          # - sbuild -- to build the binary package with extra hygiene
   142          # - devscripts -- to modify the changelog automatically
   143          # - git -- to clone native downstream packaging
   144          apt-get install --quiet -y sbuild devscripts git
   145          # XXX: Taken from https://wiki.debian.org/sbuild
   146          mkdir -p /root/.gnupg
   147          # NOTE: We cannot use sbuild-update --keygen as virtual machines lack
   148          # the necessary entropy to generate keys before the spread timeout
   149          # kicks in. Instead we just copy pre-made, insecure keys from the
   150          # source repository.
   151          mkdir -p /var/lib/sbuild/apt-keys/
   152          cp -a "$top_dir/spread-tests/data/apt-keys/"* /var/lib/sbuild/apt-keys/
   153          sbuild-adduser "$LOGNAME"
   154          ;;
   155      *)
   156          echo "unsupported distribution: $release_ID"
   157          echo "patch spread-prepare to teach it about how to install build dependencies"
   158          exit 1
   159          ;;
   160  esac
   161  
   162  # Build and install the native package using downstream packaging and the fresh upstream tarball
   163  case "$release_ID" in
   164      ubuntu|debian)
   165          build_debian_or_ubuntu_package "$release_ID" "$release_VERSION_ID"
   166          # Install the freshly-built packages
   167          dpkg -i snap-confine_*.deb || apt-get -f install -y
   168          dpkg -i ubuntu-core-launcher_*.deb || apt-get -f install -y
   169          # Install snapd (testes require it)
   170          apt-get install -y snapd
   171          ;;
   172      *)
   173          echo "unsupported distribution: $release_ID"
   174          exit 1
   175          ;;
   176  esac
   177  
   178  # Install the core snap
   179  snap list | grep -q ubuntu-core || snap install ubuntu-core