#!/bin/bash
set -eux

HOSTNAME=$(hostname)
JENKINS_HOME="/var/lib/jenkins"
KEY="staging-juju-rsa"

export PATH=/usr/local/bin:$JENKINS_HOME/Bin:$PATH


as_jenkins() {
    local_command="$@"
    su -l jenkins -c "$local_command"
}


update_branch() {
    local_branch=$1
    local_dir="$(basename $local_branch | cut -d ':' -f2)"
    local_path="$JENKINS_HOME/$local_dir"
    if [[ -d $local_path ]]; then
        as_jenkins bzr pull -d $local_path
    else
        as_jenkins bzr branch $local_branch $local_path
    fi
}


update_known_hosts() {
    local_host="$1"
    local_key="$(ssh-keyscan -H $local_host)"
    if ! grep "$local_key" $JENKINS_HOME/.ssh/known_hosts; then
        echo "$local_key" >> $JENKINS_HOME/.ssh/known_hosts
    fi
}


status-set maintenance "Updating scripts" || true

set +x
LP_KEY=$(config-get lp-key)
if [[ "$LP_KEY" == "" ]]; then
    status-set blocked "Waiting for lp-key to be set" || true
    exit 0
fi
set -x

status-set maintenance "Updating bzr" || true
set +x
echo "$LP_KEY" > $JENKINS_HOME/.ssh/lp_rsa
set -x
cp $JENKINS_HOME/.ssh/lp_rsa $JENKINS_HOME/.ssh/id_rsa
chown -R jenkins:jenkins $JENKINS_HOME/.ssh
chmod 600 $JENKINS_HOME/.ssh/*rsa
update_known_hosts bazaar.launchpad.net
update_known_hosts git.launchpad.net
update_known_hosts upload.launchpad.net
update_known_hosts ppa.launchpad.net
chmod 600 $JENKINS_HOME/.ssh/known_hosts
as_jenkins bzr --no-aliases launchpad-login juju-qa-bot

status-set maintenance "Updating branches" || true
if [[ -d $JENKINS_HOME/ci-director ]]; then
    update_branch lp:ci-director
fi
update_branch lp:workspace-runner
update_branch lp:juju-release-tools
update_branch lp:juju-ci-tools
update_branch lp:juju-ci-tools/repository
update_branch lp:~juju-qa/+junk/cloud-city

status-set maintenance "Updating permissions" || true
chown -R jenkins $JENKINS_HOME/cloud-city
chmod -R go-w $JENKINS_HOME/cloud-city
chmod 700 $JENKINS_HOME/cloud-city
chmod 700 $JENKINS_HOME/cloud-city/gnupg
chmod 600 $JENKINS_HOME/cloud-city/$KEY

status-set maintenance "Updating ssh" || true
if [[ ! -f $JENKINS_HOME/.ssh/config ]]; then
cat << EOC > $JENKINS_HOME/.ssh/config
Host 10.* 192.168.*
  StrictHostKeyChecking no
  UserKnownHostsFile /dev/null
  User ubuntu
  IdentityFile $JENKINS_HOME/cloud-city/$KEY
EOC
fi
if [[ ! -f $JENKINS_HOME/.ssh/id_rsa ]]; then
    as_jenkins ln -s $JENKINS_HOME/cloud-city/$KEY $JENKINS_HOME/.ssh/id_rsa
fi
if [[ ! -f $JENKINS_HOME/.ssh/id_rsa.pub ]]; then
    as_jenkins ln -s $JENKINS_HOME/cloud-city/$KEY.pub $JENKINS_HOME/.ssh/id_rsa.pub
fi
chown -R jenkins:jenkins $JENKINS_HOME/.ssh
as_jenkins ssh-import-id lp:juju-qa-bot || true

status-set maintenance "Updating git" || true
github_username=$(sed -r '/github_user/!d; s/.*"(.*)"/\1/; s/%20/ /' $JENKINS_HOME/cloud-city/juju-bot.txt)
github_password=$(sed -r '/github_password/!d; s/.*"(.*)"/\1/; s/%20/ /' $JENKINS_HOME/cloud-city/juju-bot.txt)
cat << EOC > $JENKINS_HOME/.gitconfig
[user]
        name = jenkins
        email = jenkins@jenkins
[credential]
        helper = cache
[credential "https://github.com"]
        username = $github_username
        password = $github_password
[url "https://"]
        insteadOf = http://
EOC
sudo chown jenkins:jenkins $JENKINS_HOME/.gitconfig

S3_PROXY=$(config-get s3-proxy)
if [[ $S3_PROXY == ""  ]]; then
    # Remove s3 proxy changes from .boto and juju-qa.s3cfg.
    rm $JENKINS_HOME/.boto || true
    (cd cloud-city && as_jenkins bzr revert bzr revert juju-qa.s3cfg || true)
else
    S3_PROXY_HOST=$(echo "$S3_PROXY" | cut -d ':' -f 1)
    S3_PROXY_PORT=$(echo "$S3_PROXY" | cut -d ':' -f 2)
    # Place a .boto file for python scripts
    cat << EOC > $JENKINS_HOME/.boto
[Boto]
proxy = $S3_PROXY_HOST
proxy_port = $S3_PROXY_PORT
EOC
    chown jenkins:jenkins $JENKINS_HOME/.boto
    # Modify cloud-city/juju-qa.s3cfg to use a proxy.
    sed -i -r "s/(proxy_host =).*/\1 $S3_PROXY_HOST/; s/(proxy_port =).*/\1 $S3_PROXY_PORT/;" \
        $JENKINS_HOME/cloud-city/juju-qa.s3cfg
fi

status-set maintenance "Updating dependencies from branches" || true
if [[ $(uname) == "Linux" ]]; then
    as_jenkins make -C $JENKINS_HOME/juju-ci-tools install-deps
    as_jenkins make -C $JENKINS_HOME/workspace-runner install
fi

NOW=$(date +%Y-%m-%dT%H:%M)
status-set active "$HOSTNAME updated $NOW" || true
