-
-
Save nvn-odoo/4b787c50aa4c10f8486037aa8f81fd11 to your computer and use it in GitHub Desktop.
Creates an Odoo upgrade request and uploads the database dump using SFTP.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
command -v curl >/dev/null 2>&1 || { echo >&2 "I require curl but it's not installed. Aborting."; exit 1; } | |
command -v jq >/dev/null 2>&1 || { echo >&2 "I require jq but it's not installed. Aborting."; exit 1; } | |
MAX_CMD_TRIES=100 | |
BASE_WORK_DIR=${HOME}/odoo_upgrade_request | |
if [ ! -d "$BASE_WORK_DIR" ]; then | |
echo "Creating the working directory: $BASE_WORK_DIR" | |
mkdir "$BASE_WORK_DIR" | |
fi | |
#BASE_URL="https://myupgrade.com" | |
if [ "$BASE_URL" = "" ]; then | |
BASE_URL="https://upgrade.odoo.com" | |
else | |
CURL_OPTS="-k" | |
fi | |
if [ "$CONTRACT" = "" ]; then | |
echo "Missing parameter: CONTRACT" >&2 | |
exit 1 | |
fi | |
if [ "$EMAIL" = "" ]; then | |
echo "Missing parameter: EMAIL" >&2 | |
exit 1 | |
fi | |
if [ "$AIM" = "" ]; then | |
echo "Missing parameter: AIM" >&2 | |
exit 1 | |
fi | |
if [ "$TARGET" = "" ]; then | |
echo "Missing parameter: TARGET" >&2 | |
exit 1 | |
fi | |
if [ "$DUMP" = "" ]; then | |
echo "Missing parameter: DUMP" >&2 | |
exit 1 | |
fi | |
DB=$(basename "$DUMP") | |
WORK_DIR=${BASE_WORK_DIR}/${DB:?} | |
if [ ! -d "$WORK_DIR" ]; then | |
mkdir "$WORK_DIR" | |
fi | |
if [ "$SSH_KEYS" = "" ]; then | |
SSH_KEYS=${HOME}/.ssh/id_rsa.pub | |
fi | |
echo "Using '$SSH_KEYS' as SSH keys" | |
if [ ! -f "$DUMP" ]; then | |
echo "dump file '$DUMP' does not exist. Aborting" >&2 | |
exit 2 | |
fi | |
if [ ! -f "$SSH_KEYS" ]; then | |
echo "ssh key file '$SSH_KEYS' does not exist. Aborting" >&2 | |
exit 2 | |
fi | |
# check public key file is valid: | |
ssh-keygen -l -f "$SSH_KEYS" 1>/dev/null | |
CREATE_URL="${BASE_URL}/database/v1/create" | |
URL_PARAMS="contract=${CONTRACT}&aim=${AIM}&target=${TARGET}&email=${EMAIL}&filename=${DUMP}" | |
counter=$MAX_CMD_TRIES | |
while true; | |
do | |
echo "Creating the request" | |
curl $CURL_OPTS -sS "${CREATE_URL}?${URL_PARAMS}" > "${WORK_DIR}/create_result.json" | |
failures=$(cat "${WORK_DIR}/create_result.json" | jq -r '.failures[]') | |
if [ "$failures" != "" ]; then | |
echo $failures | jq -r '.' | |
if [ "$counter" -le "0" ]; then | |
echo "Tried too many times. Exiting" | |
exit 4 | |
fi | |
echo "trying again (# of tries left: $counter)"; | |
sleep 3 | |
counter=$((counter-1)); | |
else | |
echo "create: ok" | |
break | |
fi | |
done | |
# Requesting an SFTP access: | |
REQUEST_SFTP_ACCESS_URL="${BASE_URL}/database/v1/request_sftp_access" | |
KEY=$(cat "${WORK_DIR}/create_result.json" | jq -r '.request.key') | |
REQUEST_ID=$(cat "${WORK_DIR}/create_result.json" | jq -r '.request.id') | |
URL_PARAMS="key=${KEY}&request=${REQUEST_ID}" | |
counter=$MAX_CMD_TRIES | |
while true; | |
do | |
echo "Requesting SFTP access" | |
curl $CURL_OPTS -sS "${REQUEST_SFTP_ACCESS_URL}?${URL_PARAMS}" -F ssh_keys=@"${SSH_KEYS}" > "${WORK_DIR}/request_sftp_result.json" | |
failures=$(cat "${WORK_DIR}/request_sftp_result.json" | jq -r '.failures[]') | |
if [ "$failures" != "" ]; then | |
echo $failures | jq -r '.' | |
if [ "$counter" -le "0" ]; then | |
echo "Tried too many times. Exiting" | |
exit 4 | |
fi | |
echo "trying again (# of tries left: $counter)"; | |
sleep 3 | |
counter=$((counter-1)); | |
else | |
echo "request_sftp_access: ok" | |
break | |
fi | |
done | |
# Uploading the dump file: | |
SSH_OPTIONS="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" | |
SFTP_PORT=$(cat "${WORK_DIR}/request_sftp_result.json" | jq -r '.request.sftp_port') | |
SFTP_USER=$(cat "${WORK_DIR}/request_sftp_result.json" | jq -r '.request.sftp_user') | |
SFTP_SERVER=$(cat "${WORK_DIR}/request_sftp_result.json" | jq -r '.request.hostname') | |
SFTP_FILENAME=$(cat "${WORK_DIR}/request_sftp_result.json" | jq -r '.request.shared_file') | |
SFTP_CMD="sftp -b ${WORK_DIR}/batch.sftp -P ${SFTP_PORT} ${SSH_OPTIONS} ${SFTP_USER}@${SFTP_SERVER}" | |
echo "put -a \"$DUMP\" \"$SFTP_FILENAME\"" > "${WORK_DIR}/batch.sftp" | |
echo "bye" >> "${WORK_DIR}/batch.sftp" | |
echo "Uploading dump file using:" | |
echo " $SFTP_CMD" | |
counter=$MAX_CMD_TRIES | |
until $SFTP_CMD | |
do | |
if [ "$counter" -le "0" ]; then | |
echo "Tried too many times. Exiting" | |
exit 4 | |
fi | |
echo "trying again (# of tries left: $counter)"; | |
sleep 3 | |
counter=$((counter-1)); | |
done | |
if [ "$NO_PROCESS" != "" ]; then | |
echo "no process -> exiting" | |
echo "request id: ${REQUEST_ID}" | |
exit 0 | |
fi | |
# Asking to process the database | |
PROCESS_URL="${BASE_URL}/database/v1/process" | |
URL_PARAMS="key=${KEY}&request=${REQUEST_ID}" | |
curl $CURL_OPTS -sS "${PROCESS_URL}?${URL_PARAMS}" > "${WORK_DIR}/process_result.json" | |
# check for failures | |
failures=$(cat "${WORK_DIR}/process_result.json" | jq -r '.failures[]') | |
if [ "$failures" != "" ]; then | |
echo $failures | jq -r '.' | |
exit 3 | |
else | |
echo "ask to process: ok" | |
fi | |
echo "command to periodicaly check the upgrade request status:" | |
key=$(cat ${WORK_DIR}/create_result.json | jq -r '.request.key') | |
request=$(cat ${WORK_DIR}/create_result.json | jq -r '.request.id') | |
echo "curl $CURL_OPTS -Ss \"${BASE_URL}/database/v1/status?key=${key}&request=${request}\" | jq -r '.request.state'" | |
echo | |
echo "alternatively, you can connect to this status page and periodicaly refresh the page:" | |
cat ${WORK_DIR}/create_result.json | jq -r '.request.status_url' | |
echo | |
echo "once 'done', download the upgraded dump with:" | |
echo "wget \"$(cat ${WORK_DIR}/create_result.json | jq -r '.request.upgraded_dump_url')\" --continue --tries=0 -O \"upgraded_$(echo $DB | sed 's/.dump/.zip/g')\"" | |
echo | |
echo "You can get the complete status info with:" | |
echo "curl $CURL_OPTS -Ss \"${BASE_URL}/database/v1/status?key=${key}&request=${request}\" | jq -r '.request'" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment