Job-Specification

---
key: test-container-deploy
name: Test Deploy to Container
context:
  tasks:
    ubuntu-lts:
      key: ubuntu-lts
      name: Ubuntu 16.04 LTS
      traits:
        LXD: true
        ruby-install: true
      scripts:
        test:
          body: |
            #!/usr/bin/env bash
            set -eux
            lxc exec ${CONTAINER_NAME} -- curl -s -i --fail http://localhost
          start_when:
            deployed to container:
              script_key: deploy-to-container
        bundle:
          body: |
            #!/usr/bin/env bash
            set -eux
            export PATH=~/.rubies/$RUBY/bin:$PATH

            cd deploy/container-test

            rm -f .bundle/config

            if [ ! -d ~/.rubies/$RUBY ]; then
              ruby-install --no-install-deps $RUBY_ENGINE $RUBY_VERSION
            fi

            if [ ! -f ~/.rubies/$RUBY/bin/bundle ]; then
              gem install bundler
            fi

            bundle install
          timeout: 20 Minutes
          exclusive_executor_resource: ruby-install_{{$RUBY}}
        cache-container:
          body: |
            #!/usr/bin/env bash
            set -eux
            source deploy/container-test/bin/shared-container-cache.sh

            lxc info "$CONTAINER_NAME"

            # cleanup before caching:
            lxc exec "$CONTAINER_NAME" -- /bin/bash -c '\
              systemctl stop leihs* && dropdb leihs && systemctl stop postgresql && rm -rf /tmp/leihs /leihs'

            save_container_to_image_cache
            lxc image info "$CACHED_IMAGE_NAME" || true
            remove_cached_image_from_lxd || true
          timeout: 1 Hour
          start_when:
            test is ok:
              states:
              - passed
              script_key: test
          ignore_abort: true
          exclusive_executor_resource: lxc_image_caching_{{LXC_IMAGE_CACHE_NAME}}_{{LXC_CACHED_BASE_IMAGE}}
        create-container:
          body: |
            #!/usr/bin/env bash
            set -eux
            source deploy/container-test/bin/shared-container-cache.sh

            select_cached_or_base_image
            lxc image list
            lxc launch --ephemeral ${SELECTED_LXC_IMAGE} ${CONTAINER_NAME}
            lxc list
          start_when:
            bundled:
              script_key: bundle
          exclusive_executor_resource: lxc_image_caching_{{LXC_IMAGE_CACHE_NAME}}_{{LXC_CACHED_BASE_IMAGE}}
        destroy-container:
          body: |
            #!/usr/bin/env bash
            set -eux
            lxc list
            lxc info "$CONTAINER_NAME"

            # enable for debugging; keeps the container running for a while
            # sleep 1000

            lxc delete --force "$CONTAINER_NAME"
            lxc list
          timeout: 1 Hour
          start_when:
            test failed:
              states:
              - aborted
              - passed
              - failed
              - skipped
              script_key: test
            test is ok and tried to cache container:
              states:
              - aborted
              - passed
              - failed
              - skipped
              script_key: cache-container
          ignore_abort: true
        prepare-container:
          body: |
            #!/usr/bin/env bash
            set -eux
            lxc exec ${CONTAINER_NAME} -- ifconfig
            git repack -a
            git submodule foreach --recursive 'git repack -a'
            # the next command will not work if we invoke it too quickly after
            # booting the machine
            sleep 30
            # cleanup in case base image already has leihs code
            lxc exec $CONTAINER_NAME -- rm -rf /tmp/leihs /leihs
            tar cf - ${CIDER_CI_WORKING_DIR} \
              | lxc exec ${CONTAINER_NAME} -- tar xvf - -C /tmp
            lxc exec ${CONTAINER_NAME} -- \
              /tmp/${CIDER_CI_WORKING_DIR}/deploy/container-test/bin/install-dependencies
          timeout: 10 Minutes
          start_when:
            container has been created:
              script_key: create-container
        deploy-to-container:
          body: |
            #!/usr/bin/env bash
            set -eux
            lxc exec ${CONTAINER_NAME} -- /bin/bash -c "set -eux && cd /tmp/${CIDER_CI_WORKING_DIR}/deploy && container-test/bin/deploy"
          timeout: 1 hour
          start_when:
            container has been prepared:
              script_key: prepare-container
      git_options:
        submodules:
          include_match: "^.*$"
      environment_variables:
        RUBY: "{{RUBY_ENGINE}}-{{RUBY_VERSION}}"
        RUBY_ENGINE: ruby
        RUBY_VERSION: 2.2.4
        CONTAINER_NAME: leihs-deploy-test-{{CIDER_CI_TRIAL_ID}}
        LXC_BASE_IMAGE: ubuntu:16.04
        LXC_CACHED_BASE_IMAGE: leihs-ubuntu-lts
    debian-stretch:
      key: debian-stretch
      name: Debian 9 (stretch)
      traits:
        LXD: true
        ruby-install: true
      scripts:
        test:
          body: |
            #!/usr/bin/env bash
            set -eux
            lxc exec ${CONTAINER_NAME} -- curl -s -i --fail http://localhost
          start_when:
            deployed to container:
              script_key: deploy-to-container
        bundle:
          body: |
            #!/usr/bin/env bash
            set -eux
            export PATH=~/.rubies/$RUBY/bin:$PATH

            cd deploy/container-test

            rm -f .bundle/config

            if [ ! -d ~/.rubies/$RUBY ]; then
              ruby-install --no-install-deps $RUBY_ENGINE $RUBY_VERSION
            fi

            if [ ! -f ~/.rubies/$RUBY/bin/bundle ]; then
              gem install bundler
            fi

            bundle install
          timeout: 20 Minutes
          exclusive_executor_resource: ruby-install_{{$RUBY}}
        cache-container:
          body: |
            #!/usr/bin/env bash
            set -eux
            source deploy/container-test/bin/shared-container-cache.sh

            lxc info "$CONTAINER_NAME"

            # cleanup before caching:
            lxc exec "$CONTAINER_NAME" -- /bin/bash -c '\
              systemctl stop leihs* && dropdb leihs && systemctl stop postgresql && rm -rf /tmp/leihs /leihs'

            save_container_to_image_cache
            lxc image info "$CACHED_IMAGE_NAME" || true
            remove_cached_image_from_lxd || true
          timeout: 1 Hour
          start_when:
            test is ok:
              states:
              - passed
              script_key: test
          ignore_abort: true
          exclusive_executor_resource: lxc_image_caching_{{LXC_IMAGE_CACHE_NAME}}_{{LXC_CACHED_BASE_IMAGE}}
        create-container:
          body: |
            #!/usr/bin/env bash
            set -eux
            source deploy/container-test/bin/shared-container-cache.sh

            select_cached_or_base_image
            lxc image list
            lxc launch --ephemeral ${SELECTED_LXC_IMAGE} ${CONTAINER_NAME}
            lxc list
          start_when:
            bundled:
              script_key: bundle
          exclusive_executor_resource: lxc_image_caching_{{LXC_IMAGE_CACHE_NAME}}_{{LXC_CACHED_BASE_IMAGE}}
        destroy-container:
          body: |
            #!/usr/bin/env bash
            set -eux
            lxc list
            lxc info "$CONTAINER_NAME"

            # enable for debugging; keeps the container running for a while
            # sleep 1000

            lxc delete --force "$CONTAINER_NAME"
            lxc list
          timeout: 1 Hour
          start_when:
            test failed:
              states:
              - aborted
              - passed
              - failed
              - skipped
              script_key: test
            test is ok and tried to cache container:
              states:
              - aborted
              - passed
              - failed
              - skipped
              script_key: cache-container
          ignore_abort: true
        prepare-container:
          body: |
            #!/usr/bin/env bash
            set -eux
            lxc exec ${CONTAINER_NAME} -- ifconfig
            git repack -a
            git submodule foreach --recursive 'git repack -a'
            # the next command will not work if we invoke it too quickly after
            # booting the machine
            sleep 30
            # cleanup in case base image already has leihs code
            lxc exec $CONTAINER_NAME -- rm -rf /tmp/leihs /leihs
            tar cf - ${CIDER_CI_WORKING_DIR} \
              | lxc exec ${CONTAINER_NAME} -- tar xvf - -C /tmp
            lxc exec ${CONTAINER_NAME} -- \
              /tmp/${CIDER_CI_WORKING_DIR}/deploy/container-test/bin/install-dependencies
          timeout: 10 Minutes
          start_when:
            container has been created:
              script_key: create-container
            container has been configured:
              script_key: configure-container
        configure-container:
          body: |
            #!/usr/bin/env bash
            set -eux

            lxc file push \
              ${CIDER_CI_WORKING_DIR}/deploy/container-test/debian-apt-sources.txt \
              ${CONTAINER_NAME}/etc/apt/sources.list
          timeout: 1 Minute
          start_when:
            container has been created:
              script_key: create-container
        deploy-to-container:
          body: |
            #!/usr/bin/env bash
            set -eux
            lxc exec ${CONTAINER_NAME} -- /bin/bash -c "set -eux && cd /tmp/${CIDER_CI_WORKING_DIR}/deploy && container-test/bin/deploy"
          timeout: 1 hour
          start_when:
            container has been prepared:
              script_key: prepare-container
      git_options:
        submodules:
          include_match: "^.*$"
      environment_variables:
        RUBY: "{{RUBY_ENGINE}}-{{RUBY_VERSION}}"
        RUBY_ENGINE: ruby
        RUBY_VERSION: 2.2.4
        CONTAINER_NAME: leihs-deploy-test-{{CIDER_CI_TRIAL_ID}}
        LXC_BASE_IMAGE: images:debian/stretch/amd64
        LXC_CACHED_BASE_IMAGE: leihs-debian9
  task_defaults:
    traits:
      g2016: true
      ci-g2016-02: true
    scripts:
      show-log-if-failed:
        body: |
          #!/usr/bin/env bash
          lxc exec ${CONTAINER_NAME} -- /bin/bash -c "journalctl -xe"
        start_when:
          deploy has failed:
            states:
            - failed
            script_key: deploy-to-container
        ignore_abort: true
        ignore_state: true
    max_trials: 1
    environment_variables:
      LXC_IMAGE_CACHE_NAME: leihs
      LXC_IMAGE_CACHE_KEY_CMD: echo "$(date -uI)_$(cd deploy && git log -n1 --format=%t)"
run_when:
  always:
    type: branch
    exclude_match: "^.*no-ci.*$"
    include_match: "^.*$"