summaryrefslogtreecommitdiff
path: root/integration/scripts/functions_qemu
blob: 635b1451cc4ef3b5a0774e4b5787ad71bffccca9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash
#
# Additional functions that would mostly just pertain to a Ubuntu + Qemu setup
#

function build_vm() {
    exclaim "Actually building the image, this can take up to 15 minutes"
    rm -rf ~/.cache/image-create

    local datastore_type=$1
    local guest_os=$2
    local guest_release=$3
    local dev_mode=$4
    local guest_username=$5
    local image_output=$6

    local elementes="base vm"
    local trove_elements_path=${PATH_TROVE}/integration/scripts/files/elements
    local GUEST_IMAGETYPE=${GUEST_IMAGETYPE:-"qcow2"}
    local GUEST_IMAGESIZE=${GUEST_IMAGESIZE:-4}
    local GUEST_CACHEDIR=${GUEST_CACHEDIR:-"$HOME/.cache/image-create"}
    local working_dir=$(dirname ${image_output})

    export GUEST_USERNAME=${guest_username}

    # In dev mode, the trove guest agent needs to download trove code from
    # trove-taskmanager host during service initialization.
    if [[ "${dev_mode}" == "true" ]]; then
        host_ip=$(ip route get 8.8.8.8 | head -1 | awk '{print $7}')
        export CONTROLLER_IP=${CONTROLLER_IP:-${host_ip}}
        export PATH_TROVE=${PATH_TROVE}
        export ESCAPED_PATH_TROVE=$(echo ${PATH_TROVE} | sed 's/\//\\\//g')
        export GUEST_LOGDIR=${GUEST_LOGDIR:-"/var/log/trove/"}
        export ESCAPED_GUEST_LOGDIR=$(echo ${GUEST_LOGDIR} | sed 's/\//\\\//g')
        export TROVESTACK_SCRIPTS=${TROVESTACK_SCRIPTS}
        export HOST_SCP_USERNAME=${HOST_SCP_USERNAME:-$(whoami)}
        export HOST_USERNAME=${HOST_SCP_USERNAME}
        export SSH_DIR=${SSH_DIR:-"$HOME/.ssh"}
        export DEST=${DEST:-'/opt/stack'}
        export TROVE_BRANCH=${TROVE_BRANCH:-'master'}
        manage_ssh_keys
    fi

    # For system-wide installs, DIB will automatically find the elements, so we only check local path
    if [ "${DIB_LOCAL_ELEMENTS_PATH}" ]; then
        export ELEMENTS_PATH=${trove_elements_path}:${DIB_LOCAL_ELEMENTS_PATH}
    else
        export ELEMENTS_PATH=${trove_elements_path}
    fi

    export DIB_RELEASE=${guest_release}
    export DIB_CLOUD_INIT_DATASOURCES="ConfigDrive"

    # https://cloud-images.ubuntu.com/releases is more stable than the daily
    # builds(https://cloud-images.ubuntu.com/xenial/current/),
    # e.g. sometimes SHA256SUMS file is missing in the daily builds
    declare -A releasemapping=( ["xenial"]="16.04" ["bionic"]="18.04")
    export DIB_CLOUD_IMAGES="https://cloud-images.ubuntu.com/releases/${DIB_RELEASE}/release/"
    export BASE_IMAGE_FILE="ubuntu-${releasemapping[${DIB_RELEASE}]}-server-cloudimg-amd64-root.tar.gz"

    TEMP=$(mktemp -d ${working_dir}/diskimage-create.XXXXXXX)
    pushd $TEMP > /dev/null

    elementes="$elementes ${guest_os}"

    if [[ "${dev_mode}" == "false" ]]; then
        elementes="$elementes pip-and-virtualenv"
        elementes="$elementes pip-cache"
        elementes="$elementes guest-agent"
    else
        elementes="$elementes ${guest_os}-guest"
        elementes="$elementes ${guest_os}-${guest_release}-guest"
    fi

    elementes="$elementes ${guest_os}-${datastore_type}"
    elementes="$elementes ${guest_os}-${guest_release}-${datastore_type}"

    # Build the image
    disk-image-create -x \
      -a amd64 \
      -o ${image_output} \
      -t ${GUEST_IMAGETYPE} \
      --image-size ${GUEST_IMAGESIZE} \
      --image-cache ${GUEST_CACHEDIR} \
      $elementes

    # out of $TEMP
    popd > /dev/null
    sudo rm -rf $TEMP

    exclaim "Image ${image_output} was built successfully."
}

function build_guest_image() {
    exclaim "Params for build_guest_image function: $@"

    local datastore_type=${1:-"mysql"}
    local guest_os=${2:-"ubuntu"}
    local guest_release=${3:-"xenial"}
    local dev_mode=${4:-"true"}
    local guest_username=${5:-"ubuntu"}
    local output=$6

    VALID_SERVICES='mysql percona mariadb redis cassandra couchbase mongodb postgresql couchdb vertica db2 pxc'
    if ! [[ " $VALID_SERVICES " =~ " $datastore_type " ]]; then
        exclaim "You did not pass in a valid datastore type. Valid types are:" $VALID_SERVICES
        exit 1
    fi

    build_vm ${datastore_type} ${guest_os} ${guest_release} ${dev_mode} ${guest_username} ${output}
}

function clean_instances() {
    LIST=`virsh -q list|awk '{print $1}'`
    for i in $LIST; do sudo virsh destroy $i; done
}

# In dev mode, guest agent needs to ssh into the controller to download code.
function manage_ssh_keys() {
    if [ -d ${SSH_DIR} ]; then
        echo "${SSH_DIR} already exists"
    else
        echo "Creating ${SSH_DIR} for ${HOST_SCP_USERNAME}"
        sudo -Hiu ${HOST_SCP_USERNAME} mkdir -m go-w -p ${SSH_DIR}
    fi

    if [ ! -f ${SSH_DIR}/id_rsa.pub ]; then
         /usr/bin/ssh-keygen -f ${SSH_DIR}/id_rsa -q -N ""
    fi

    cat ${SSH_DIR}/id_rsa.pub >> ${SSH_DIR}/authorized_keys
    sort ${SSH_DIR}/authorized_keys | uniq > ${SSH_DIR}/authorized_keys.uniq
    mv ${SSH_DIR}/authorized_keys.uniq ${SSH_DIR}/authorized_keys
    chmod 600 ${SSH_DIR}/authorized_keys
}