name: keepalived
adopt-info: keepalived
summary: High availability VRRP and load-balancing for Linux
description: |
  Keepalived provides simple and robust loadbalancing and high-availability
  to Linux based infrastructures using VRRP and the well-known Linux Virtual
  Server (IPVS) kernel module.

grade: stable
confinement: classic

base: core20

# linux-libc-dev for 5.08, 4.18 and 3.10 are no longer available via
#    http://security.ubuntu.com/ubuntu/pool/main/l/linux/
# or
#    http://ports.ubuntu.com/pool/main/l/linux/
# and will disappear for other versions is due course.
#
# To find the relevant files, go to launchpad.net/ubuntu/CODENAME/ARCH/linux-libc-dev
#   where CODENAME is, for example cosmic for Ubuntu 18.10, and ARCH is each of amd64,
#   ppc64el, arm64, armhf and s390x.
# Select the most recent version, and click on link under Version column (RHS). Under
#   Downloadable files is a link to the .deb file. That is what we need in DEB_URL.
#
# We should probably update the locations of the files to use this more difficult way
#   of finding them once the Ubuntu version that provides them goes EOL.

apps:
  daemon:
    daemon: forking
    command: bin/keepalived-wrapper
  keepalived:
    command: bin/keepalived-wrapper
  "508":        # Fedora 33 and Ubuntu 20.10
    command: usr/sbin/keepalived-508
  "504":        # Ubuntu 20.04
    command: usr/sbin/keepalived-504
  "418":        # RHEL 8
    command: usr/sbin/keepalived-418
  "415":        # Ubuntu 18.04
    command: usr/sbin/keepalived-415
  "313":        # RHEL 7 - kernel 3.10 but launchpad no longer has 3.10 packages
    command: usr/sbin/keepalived-313
  genhash:
    command: usr/bin/genhash

parts:
  keepalived:
    plugin: autotools
    source: .
    source-type: git
    autotools-configure-parameters:
      - --prefix=/usr
      - --with-samples-dir='$(docdir)'
      - --enable-bfd
      - --enable-dbus
      - --enable-json
      - --enable-regex
      - --enable-snmp
      - --enable-snmp-rfc
      - --disable-libipset-dynamic
      - --disable-systemd
    override-build: |
      snapcraftctl build
      VER=
      if [ -f lib/git-commit.h ]; then
        COMMIT=`grep GIT_COMMIT lib/git-commit.h | wc -l || true`
        if [ $COMMIT -eq 1 ]; then
          VER=$(grep GIT_COMMIT lib/git-commit.h | cut -d'"' -f2)
        fi
      fi
      if [ -z $VER ]; then
        VER=`grep "^AC_INIT" configure.ac | tr '[' ']' | cut -d']' -f4`
      fi
      snapcraftctl set-version $VER
      echo -n "Build kernel:"
      printf " %d.%d.%d\n" $(printf "%6.6x" $(grep LINUX_VERSION_CODE /usr/include/linux/version.h | sed -e "s/.*CODE //") | sed -e "s/\(..\)\(..\)/0x\1 0x\2 0x/")
      KERNEL_VER=$(printf "%d%2.2d" $(printf "%6.6x" $(grep LINUX_VERSION_CODE /usr/include/linux/version.h | sed -e "s/.*CODE //") | sed -e "s/\(..\)\(..\)../0x\1 0x\2/"))
      rm ${SNAPCRAFT_PART_INSTALL}/usr/bin/genhash
      ln -s ../sbin/keepalived-$KERNEL_VER ${SNAPCRAFT_PART_INSTALL}/usr/bin/genhash
    build-packages:
      - libxtables-dev
      - libip4tc-dev
      - libip6tc-dev
      - libipset-dev
      - libglib2.0-dev
      - libmagic-dev
      - libmnl-dev
      - libnftnl-dev
      - libnl-3-dev
      - libnl-genl-3-dev
      - libnfnetlink-dev
      - libpcre2-dev
      - libsnmp-dev
      - libssl-dev
      - libkmod-dev
    stage-packages:
      - libnfnetlink0
      - libipset13
      - libglib2.0-0
      - libmagic1
      - libmnl0
      - libnftnl11
      - libnl-3-200
      - libnl-genl-3-200
      - libpcre2-8-0
      - libsnmp35
    organize:
      'usr/sbin/keepalived': usr/sbin/keepalived-504

  linux-headers-508:
    plugin: nil
    build-packages:
      - dpkg
      - dpkg-dev
      - wget
    after:
      - keepalived
    override-pull: |
      KERNEL_VER="5.8.0"
      ARCH=$(dpkg-architecture -q DEB_BUILD_ARCH)
      if [ $ARCH = amd64 ]; then
        DEB_URL="http://launchpadlibrarian.net/548300974/linux-libc-dev_5.8.0-63.71_amd64.deb"
      elif [ $ARCH = ppc64el ]; then
        DEB_URL="http://launchpadlibrarian.net/548289994/linux-libc-dev_5.8.0-63.71_ppc64el.deb"
      elif [ $ARCH = arm64 ]; then
        DEB_URL="http://launchpadlibrarian.net/548341043/linux-libc-dev_5.8.0-63.71_arm64.deb"
      elif [ $ARCH = armhf ]; then
        DEB_URL="http://launchpadlibrarian.net/548325871/linux-libc-dev_5.8.0-63.71_armhf.deb"
      elif [ $ARCH = s390x ]; then
        DEB_URL="http://launchpadlibrarian.net/548279675/linux-libc-dev_5.8.0-63.71_s390x.deb"
      fi
      wget --quiet ${DEB_URL} -O ${SNAPCRAFT_STAGE}/linux-libc-dev.deb
      echo -n "Unpacking kernel headers..."
      dpkg -x ${SNAPCRAFT_STAGE}/linux-libc-dev.deb ${SNAPCRAFT_STAGE}/keepalived-linux-libc-dev
      printf " %d.%d.%d\n" $(printf "%6.6x" $(grep LINUX_VERSION_CODE ${SNAPCRAFT_STAGE}/keepalived-linux-libc-dev/usr/include/linux/version.h | sed -e "s/.*CODE //") | sed -e "s/\(..\)\(..\)/0x\1 0x\2 0x/")
      rm -f ${SNAPCRAFT_STAGE}/linux-libc-dev.deb
    override-build: |
      snapcraftctl build
      # Move header from the part to the host
      ARCH_DIR=$(ls -d ${SNAPCRAFT_STAGE}/keepalived-linux-libc-dev/usr/include/*/asm | sed -e "s:/asm$::" -e "s:.*/::")
      for d in linux asm-generic $ARCH_DIR/asm; do
        dest=/usr/include/$d
        rm -rf $dest
        mv ${SNAPCRAFT_STAGE}/keepalived-linux-libc-dev/usr/include/$d ${dest%/*}
      done
      rm -rf ${SNAPCRAFT_STAGE}/keepalived-linux-libc-dev
      echo -n "Build kernel:"
      printf " %d.%d.%d\n" $(printf "%6.6x" $(grep LINUX_VERSION_CODE /usr/include/linux/version.h | sed -e "s/.*CODE //") | sed -e "s/\(..\)\(..\)/0x\1 0x\2 0x/")
    stage:
      - -*
    prime:
      - -*

  keepalived-508:
    plugin: autotools
    source: .
    source-type: git
    after:
      - linux-headers-508
    autotools-configure-parameters:
      - --prefix=/usr
      - --with-samples-dir='$(docdir)'
      - --enable-bfd
      - --enable-dbus
      - --enable-json
      - --enable-regex
      - --enable-snmp
      - --enable-snmp-rfc
      - --disable-libipset-dynamic
    organize:
      'usr/sbin/keepalived': usr/sbin/keepalived-508
      'usr/etc/dbus-1/system.d/org.keepalived.Vrrp1.conf': etc/dbus-1/system.d/org.keepalived.Vrrp1.conf
    stage:
      - usr/sbin/keepalived-508
      - etc/dbus-1/system.d/org.keepalived.Vrrp1.conf
      - usr/share/dbus-1/interfaces/org.keepalived.Vrrp1.Instance.xml
      - usr/share/dbus-1/interfaces/org.keepalived.Vrrp1.Vrrp.xml
    prime:
      - usr/sbin/keepalived-508
      - etc/dbus-1/system.d/org.keepalived.Vrrp1.conf
      - usr/share/dbus-1/interfaces/org.keepalived.Vrrp1.Instance.xml
      - usr/share/dbus-1/interfaces/org.keepalived.Vrrp1.Vrrp.xml

  linux-headers-418:
    plugin: nil
    build-packages:
      - dpkg
      - dpkg-dev
      - wget
    after:
      - keepalived-508
    override-pull: |
      KERNEL_VER="4.18.0"
      ARCH=$(dpkg-architecture -q DEB_BUILD_ARCH)
      if [ $ARCH = amd64 ] || [ $ARCH = ppc64el ] ||  [ $ARCH = arm64 ]; then
        DEB_URL="http://launchpadlibrarian.net/431512728/linux-libc-dev_4.18.0-26.27_amd64.deb"
      elif [ $ARCH = armhf ]; then
        DEB_URL="http://launchpadlibrarian.net/431525966/linux-libc-dev_4.18.0-26.27_armhf.deb"
      elif [ $ARCH = s390x ]; then
        DEB_URL="http://launchpadlibrarian.net/431485536/linux-libc-dev_4.18.0-26.27_s390x.deb"
      fi
      wget --quiet ${DEB_URL} -O ${SNAPCRAFT_STAGE}/linux-libc-dev.deb
      echo -n "Unpacking kernel headers..."
      dpkg -x ${SNAPCRAFT_STAGE}/linux-libc-dev.deb ${SNAPCRAFT_STAGE}/keepalived-linux-libc-dev
      printf " %d.%d.%d\n" $(printf "%6.6x" $(grep LINUX_VERSION_CODE ${SNAPCRAFT_STAGE}/keepalived-linux-libc-dev/usr/include/linux/version.h | sed -e "s/.*CODE //") | sed -e "s/\(..\)\(..\)/0x\1 0x\2 0x/")
      rm -f ${SNAPCRAFT_STAGE}/linux-libc-dev.deb
    override-build: |
      snapcraftctl build
      # Move header from the part to the host
      ARCH_DIR=$(ls -d ${SNAPCRAFT_STAGE}/keepalived-linux-libc-dev/usr/include/*/asm | sed -e "s:/asm$::" -e "s:.*/::")
      for d in linux asm-generic $ARCH_DIR/asm; do
        dest=/usr/include/$d
        rm -rf $dest
        mv ${SNAPCRAFT_STAGE}/keepalived-linux-libc-dev/usr/include/$d ${dest%/*}
      done
      rm -rf ${SNAPCRAFT_STAGE}/keepalived-linux-libc-dev
      echo -n "Build kernel:"
      printf " %d.%d.%d\n" $(printf "%6.6x" $(grep LINUX_VERSION_CODE /usr/include/linux/version.h | sed -e "s/.*CODE //") | sed -e "s/\(..\)\(..\)/0x\1 0x\2 0x/")
    stage:
      - -*
    prime:
      - -*

  keepalived-418:
    plugin: autotools
    source: .
    source-type: git
    after:
      - linux-headers-418
    autotools-configure-parameters:
      - --prefix=/usr
      - --with-samples-dir='$(docdir)'
      - --enable-bfd
      - --enable-dbus
      - --enable-json
      - --enable-regex
      - --enable-snmp
      - --enable-snmp-rfc
      - --disable-libipset-dynamic
    organize:
      'usr/sbin/keepalived': usr/sbin/keepalived-418
    stage:
      - usr/sbin/keepalived-418
    prime:
      - usr/sbin/keepalived-418

  linux-headers-415:
    plugin: nil
    build-packages:
      - dpkg
      - dpkg-dev
      - wget
    after:
      - keepalived-418
    override-pull: |
      KERNEL_VER="4.15.0"
      ARCH=$(dpkg-architecture -q DEB_BUILD_ARCH)
      if [ $ARCH = amd64 ]; then
        ARCHIVE_URL="http://security.ubuntu.com/ubuntu/pool/main/l/linux/"
      else
        ARCHIVE_URL="http://ports.ubuntu.com/pool/main/l/linux/"
      fi
      DEB_SUFFIX=$(wget ${ARCHIVE_URL} -O - | grep linux-libc-dev | cut -d'_' -f2-3 | cut -d'"' -f1 | grep ${ARCH} | grep ${KERNEL_VER} | tail -n1)
      wget --quiet ${ARCHIVE_URL}/linux-libc-dev_${DEB_SUFFIX} -O ${SNAPCRAFT_STAGE}/linux-libc-dev.deb
      echo -n "Unpacking kernel headers..."
      dpkg -x ${SNAPCRAFT_STAGE}/linux-libc-dev.deb ${SNAPCRAFT_STAGE}/keepalived-linux-libc-dev
      printf " %d.%d.%d\n" $(printf "%6.6x" $(grep LINUX_VERSION_CODE ${SNAPCRAFT_STAGE}/keepalived-linux-libc-dev/usr/include/linux/version.h | sed -e "s/.*CODE //") | sed -e "s/\(..\)\(..\)/0x\1 0x\2 0x/")
      rm -f ${SNAPCRAFT_STAGE}/linux-libc-dev.deb
    override-build: |
      snapcraftctl build
      # Move header from the part to the host
      ARCH_DIR=$(ls -d ${SNAPCRAFT_STAGE}/keepalived-linux-libc-dev/usr/include/*/asm | sed -e "s:/asm$::" -e "s:.*/::")
      for d in linux asm-generic $ARCH_DIR/asm; do
        dest=/usr/include/$d
        rm -rf $dest
        mv ${SNAPCRAFT_STAGE}/keepalived-linux-libc-dev/usr/include/$d ${dest%/*}
      done
      rm -rf ${SNAPCRAFT_STAGE}/keepalived-linux-libc-dev
      echo -n "Build kernel:"
      printf " %d.%d.%d\n" $(printf "%6.6x" $(grep LINUX_VERSION_CODE /usr/include/linux/version.h | sed -e "s/.*CODE //") | sed -e "s/\(..\)\(..\)/0x\1 0x\2 0x/")
    stage:
      - -*
    prime:
      - -*

  keepalived-415:
    plugin: autotools
    source: .
    source-type: git
    after:
      - linux-headers-415
    autotools-configure-parameters:
      - --prefix=/usr
      - --with-samples-dir='$(docdir)'
      - --enable-bfd
      - --enable-dbus
      - --enable-json
      - --enable-regex
      - --enable-snmp
      - --enable-snmp-rfc
      - --disable-libipset-dynamic
    organize:
      'usr/sbin/keepalived': usr/sbin/keepalived-415
    stage:
      - usr/sbin/keepalived-415
    prime:
      - usr/sbin/keepalived-415

  linux-headers-313:
    plugin: nil
    build-packages:
      - dpkg
      - dpkg-dev
      - wget
    after:
      - keepalived-415
    override-pull: |
      ARCH=$(dpkg-architecture -q DEB_BUILD_ARCH)
      if [ $ARCH = amd64 ] || [ $ARCH = ppc64el ] ||  [ $ARCH = arm64 ]; then
        DEB_URL="http://launchpadlibrarian.net/422971061/linux-libc-dev_3.13.0-170.220_amd64.deb"
      elif [ $ARCH = armhf ]; then
        DEB_URL="http://launchpadlibrarian.net/422971427/linux-libc-dev_3.13.0-170.220_i386.deb"
      fi
      if [ $ARCH != s390x ]; then
        # s390x was not supported on Trusty, so there are no kernel headers for it.
        # Just let it build with the previous headers.
        wget --quiet ${DEB_URL} -O ${SNAPCRAFT_STAGE}/linux-libc-dev.deb
        echo -n "Unpacking kernel headers..."
        dpkg -x ${SNAPCRAFT_STAGE}/linux-libc-dev.deb ${SNAPCRAFT_STAGE}/keepalived-linux-libc-dev
        printf " %d.%d.%d\n" $(printf "%6.6x" $(grep LINUX_VERSION_CODE ${SNAPCRAFT_STAGE}/keepalived-linux-libc-dev/usr/include/linux/version.h | sed -e "s/.*CODE //") | sed -e "s/\(..\)\(..\)/0x\1 0x\2 0x/")
        rm -f ${SNAPCRAFT_STAGE}/linux-libc-dev.deb
      fi
    override-build: |
      snapcraftctl build
      ARCH=$(dpkg-architecture -q DEB_BUILD_ARCH)
      if [ $ARCH != s390x ]; then
        # Move header from the part to the host
        ARCH_DIR=$(ls -d ${SNAPCRAFT_STAGE}/keepalived-linux-libc-dev/usr/include/*/asm | sed -e "s:/asm$::" -e "s:.*/::")
        for d in linux asm-generic $ARCH_DIR/asm; do
          dest=/usr/include/$d
          rm -rf $dest
          mv ${SNAPCRAFT_STAGE}/keepalived-linux-libc-dev/usr/include/$d ${dest%/*}
        done
      fi
      rm -rf ${SNAPCRAFT_STAGE}/keepalived-linux-libc-dev
      echo -n "Build kernel:"
      printf " %d.%d.%d\n" $(printf "%6.6x" $(grep LINUX_VERSION_CODE /usr/include/linux/version.h | sed -e "s/.*CODE //") | sed -e "s/\(..\)\(..\)/0x\1 0x\2 0x/")
    stage:
      - -*
    prime:
      - -*

  keepalived-313:
    plugin: autotools
    source: .
    source-type: git
    after:
      - linux-headers-313
    autotools-configure-parameters:
      - --prefix=/usr
      - --with-samples-dir='$(docdir)'
      - --enable-bfd
      - --enable-dbus
      - --enable-json
      - --enable-regex
      - --enable-snmp
      - --enable-snmp-rfc
      - --disable-libipset-dynamic
    organize:
      'usr/sbin/keepalived': usr/sbin/keepalived-313
    stage:
      - usr/sbin/keepalived-313
    prime:
      - usr/sbin/keepalived-313

  keepalived-wrapper:
    plugin: dump
    source: snap-tools
    organize:
      'keepalived-wrapper': bin/
