Last active
October 1, 2021 09:40
-
-
Save olilau/6343c51b8f4452d7ce66a962f09cf674 to your computer and use it in GitHub Desktop.
Creates an Odoo upgrade request and uploads the database dump using SFTP.
This file contains 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 | |
VERSION="0.3" | |
MAX_CMD_TRIES=30 | |
MAX_SFTP_TRIES=100 | |
# check sftp has the "put -a" command: | |
if man sftp | grep ' put.*local-path.*remote-path' | grep -q afPpr ; | |
then | |
SFTP_PUT="put -a" | |
else | |
SFTP_PUT="put" | |
fi | |
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; } | |
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 [ "$SAAS_CONTRACT_PRIVATE_KEY" = "" ]; then | |
SAAS_CONTRACT_PRIVATE_KEY_PARAM="" | |
else | |
SAAS_CONTRACT_PRIVATE_KEY_PARAM="&saas_contract_private_key=$SAAS_CONTRACT_PRIVATE_KEY" | |
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}$SAAS_CONTRACT_PRIVATE_KEY_PARAM" | |
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 '.' | |
reason=$(cat "${WORK_DIR}/create_result.json" | jq -r '.failures[].reason') | |
if [ "$counter" -le "0" ]; then | |
echo "Tried too many times. Exiting" | |
exit 4 | |
fi | |
if [ "$reason" = "CONTRACT:INVALID" ]; then | |
exit 5 | |
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 "$SFTP_PUT \"$DUMP\" \"$SFTP_FILENAME\"" > "${WORK_DIR}/batch.sftp" | |
echo "bye" >> "${WORK_DIR}/batch.sftp" | |
echo "Uploading dump file using:" | |
echo " $SFTP_CMD" | |
counter=$MAX_SFTP_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}" | |
counter=$MAX_CMD_TRIES | |
while true; | |
do | |
echo "Asking to launch the process" | |
curl $CURL_OPTS -sS "${PROCESS_URL}?${URL_PARAMS}" > "${WORK_DIR}/process_result.json" | |
failures=$(cat "${WORK_DIR}/process_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 "ask to process: ok" | |
break | |
fi | |
done | |
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