compile_gpdb_remote_windows.bash 3.0 KB
Newer Older
1 2 3
#!/bin/bash
#shellcheck disable=2153,2087,2035,2140

4 5 6 7 8 9 10 11 12
set -eo pipefail

ROOT_DIR=$(pwd)

# Get ssh private key from REMOTE_KEY, which is assumed to
# be encode in base64. We can't pass the key content directly
# since newline doesn't work well for env variable.
function setup_ssh_keys() {
    # Setup ssh keys
13
    echo -n "${REMOTE_KEY}" | base64 -d > ~/remote.key
14 15 16 17 18 19 20
    chmod 400 ~/remote.key

    eval "$(ssh-agent -s)"
    ssh-add ~/remote.key

    # Scan for target server's public key, append port number
    mkdir -p ~/.ssh
21
    ssh-keyscan -p "${REMOTE_PORT}" "${REMOTE_HOST}" > ~/.ssh/known_hosts
22 23 24 25 26 27 28 29 30 31 32
}

# All the variables in this funciton are GLOBAL environment variables.
# We set up the runtime vars here.
function remote_setup() {
    # Get a session id for different commit builds.
    SESSION_ID=$(date +%Y%m%d%H%M%S.%N)

    WORK_DIR="C:\\Users\\buildbot\\${SESSION_ID}"
    
    # Get git information from local repo(concourse gpdb_src input)
33 34 35 36 37 38
    pushd gpdb_src
        GIT_URI=$(git config --get remote.origin.url)
        GIT_COMMIT=$(git rev-parse HEAD)
        GIT_TAG=$(git describe --tags --abbrev=0 | grep -E -o '[0-9]\.[0-9]+\.[0-9]+')
        GPDB_VERSION=$(./getversion --short)
    popd
39 40 41 42 43 44 45 46
}

# Since we're cloning in a different machine, maybe there's 
# new commit pushed to the same repo. We need to reset to the
# same commit to current concourse build.
function remote_clone() {
    # Connect to remote windows server powershell environment and execute
    # the specified commands
47 48 49 50
    ssh -A -T -p "${REMOTE_PORT}" "${REMOTE_USER}"@"${REMOTE_HOST}" <<- EOF
    mkdir "${WORK_DIR}"
    cd "${WORK_DIR}"
    git clone "${GIT_URI}" gpdb_src
51
    cd gpdb_src
52
    git reset --hard "${GIT_COMMIT}"
53
    git submodule update --init --recursive
54 55 56 57 58 59
EOF
}

function remote_compile() {
    # Connect to remote windows server powershell environment and execute
    # the specified commands
60 61 62 63
    ssh -T -p "${REMOTE_PORT}" "${REMOTE_USER}"@"${REMOTE_HOST}" <<- EOF
    cd "${WORK_DIR}\gpdb_src"
    set WORK_DIR=${WORK_DIR}
    set GPDB_VERSION=${GIT_TAG}
64 65 66 67 68
    concourse\scripts\compile_gpdb_remote_windows.bat
EOF
}

function download() {
69 70 71 72 73 74
    pushd "$ROOT_DIR/gpdb_artifacts/"
        scp -P "${REMOTE_PORT}" -q "${REMOTE_USER}"@"${REMOTE_HOST}":"${WORK_DIR}/*.msi" ./
        scp -P "${REMOTE_PORT}" -q "${REMOTE_USER}"@"${REMOTE_HOST}":"${WORK_DIR}/*.exe" ./
        echo "${GPDB_VERSION}" > version
        tar cvzf greenplum-clients-x86_64.tar.gz *
    popd
75 76 77 78 79 80 81 82
}

# Since we are cloning and building on remote machine,
# files won't be deleted as concourse container destroys.
# We have to clean everything for success build.
function cleanup() {
    # Connect to remote windows server powershell environment and execute
    # the specified commands
83 84
    ssh -T -p "${REMOTE_PORT}" "${REMOTE_USER}"@"${REMOTE_HOST}" <<- EOF
    rmdir /S /Q "${WORK_DIR}"
85 86 87 88 89
EOF
}

function _main() {

90
    if [[ -z "${REMOTE_PORT}" ]]; then
91 92 93 94 95 96 97 98 99 100 101 102
        REMOTE_PORT=22
    fi

    time setup_ssh_keys
    time remote_setup
    trap cleanup EXIT
    time remote_clone
    time remote_compile
    time download
}

_main "$@"