DEB

From Unix Wiki
Jump to: navigation, search

Automatic backport script

Very simple script inspired by following article: Using pbuilder to backport Debian packages

Script is in early stage of development. Main functions already working.

It expects a list of packages as arguments to backport.

auto-backport.sh:

#!/bin/bash

listOfPackages="$@"
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

variables() {
  # First, change the target mirrorSite to something that suites your region. 
  mirrorSite="http://ftp.debian.org/debian/"
  # Thereafter, you will want to point "distribution" at your target
  # of choice as demonstrated below.
  distribution="wheezy"
  # Specify distribution to get sources
  sourceDistribution="sid"
  # pbuilder will mount the specified path, in this instance, the path to 
  # successfully backported packages, to its respective location within
  # the chroot environment pbuilder operates within.
  bindmounts="/var/cache/pbuilder/result"
  # Specify hooks directory
  hookDir="/etc/pbuilder/hook.d"
  # Specify Working directory. Don't store any information here. Auto flush enabled.
  workDir="/root/src"
  # Architecture
  #arch="i386"
  arch="amd64"
}

checkId() {
  id=`id -u`
  if [ "${id}" != "0" ]
    then
      echo "This script should be executed under root account."
      exit 1
  fi
}

checkPackages() {
  packages="pbuilder fakeroot devscripts apt-utils"
  for package in ${packages}
    do
      dpkg -l ${package} 1>/dev/null 2>/dev/null
      if [ "$?" != '0' ]
        then
          echo "${package} is NOT present. Installing package:"
          apt-get install ${package} || echo "Something wrong..." && exit 1
      fi
    done
}

createSourcesList() {
  echo "deb-src ${mirrorSite} sid main contrib non-free" > /etc/apt/sources.list.d/sid-sources.list
  apt-get update
}

baseImage() {
  tar tf /var/cache/pbuilder/base.tgz 1>/dev/null 2>/dev/null
  if [ "$?" == 0 ]
    then
      pbuilder --update --distribution ${distribution} --mirror ${mirrorSite} \
        --bindmounts ${bindmounts} --hookdir "${hookDir}"
    else
      pbuilder --create --distribution ${distribution} --mirror ${mirrorSite} \
        --bindmounts ${bindmounts} --hookdir "${hookDir}"
  fi
}

pbuilderHooks() {
  mkdir -p ${hookDir}
  echo -e "#!/bin/sh

echo 'APT::Get::AllowUnauthenticated "true";' > /etc/apt/apt.conf.d/80allowunauth

# May be necessary if base image does not have apt-utils already.
apt-get -y install apt-utils

# We want this to be up to date for _every_ run for magic to happen!

( cd /var/cache/pbuilder/result
  /usr/bin/apt-ftparchive packages . > /var/cache/pbuilder/result/Packages
)

cat<<EOF >/var/cache/pbuilder/result/Release
Archive: stable
Component: main
Origin: pbuilder
Label: pbuilder
Architecture: ${arch}
EOF

cat<<EOF >/etc/apt/sources.list
deb file:///var/cache/pbuilder/result ./
deb ${mirrorSite} ${distribution} main contrib non-free
EOF

cat<<EOF >/etc/apt/preferences
Package: *
Pin: release o=pbuilder
Pin-Priority: 701

Package: *
Pin: release a=${distribution}
Pin-Priority: 600
EOF

apt-get update" > ${hookDir}/D01apt-ftparchive
  chmod +x ${hookDir}/D01apt-ftparchive
}

getSourceFiles() {
  mkdir -p "${workDir}"
  rm -rf ${workDir}/*
  cd "${workDir}"
  apt-get source ${listOfPackages}
}

pbuilderBuild() {
  for dsc in *.dsc
    do
      pbuilder --build --bindmounts ${bindmounts} --hookdir "${hookDir}" ${dsc}
    done
}

checkId
checkPackages
variables
createSourcesList
pbuilderHooks
baseImage
getSourceFiles
pbuilderBuild

exit 0