Create a gist now

Instantly share code, notes, and snippets.

Creates an Odoo upgrade request and uploads the database dump using SFTP.
#!/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; }
# REQUIRED PARAMETERS:
CONTRACT=M123456-abcdef
AIM=test
TARGET=8.0
EMAIL=john.doe@example.com
DUMPFILE=db_name.dump
SSH_KEYS=/path/to/your/authorized_keys
# END of REQUIRED PARAMETERS
MAX_SFTP_TRIES=100
BASE_URL="https://upgrade.odoo.com"
# check public key file is valid:
ssh-keygen -l -f $SSH_KEYS 1>/dev/null
if [ ! -f $DUMPFILE ]; then
echo "dump file '$DUMPFILE' 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
# Creating the request:
CREATE_URL="${BASE_URL}/database/v1/create"
URL_PARAMS="contract=${CONTRACT}&aim=${AIM}&target=${TARGET}&email=${EMAIL}&filename=${DUMPFILE}"
curl -sS "${CREATE_URL}?${URL_PARAMS}" > create_result.json
# check for failures
failures=$(cat create_result.json | jq -r '.failures[]')
if [ "$failures" != "" ]; then
echo $failures | jq -r '.'
exit 3
else
echo "create: ok"
fi
# Requesting an SFTP access:
REQUEST_SFTP_ACCESS_URL="${BASE_URL}/database/v1/request_sftp_access"
KEY=$(cat create_result.json | jq -r '.request.key')
REQUEST_ID=$(cat create_result.json | jq -r '.request.id')
URL_PARAMS="key=${KEY}&request=${REQUEST_ID}"
curl -sS "${REQUEST_SFTP_ACCESS_URL}?${URL_PARAMS}" -F ssh_keys=@${SSH_KEYS} > request_sftp_result.json
# check for failures
failures=$(cat request_sftp_result.json | jq -r '.failures[]')
if [ "$failures" != "" ]; then
echo $failures | jq -r '.'
exit 3
else
echo "request_sftp_access: ok"
fi
# Uploading the dump file:
SSH_OPTIONS="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
SFTP_PORT=$(cat request_sftp_result.json | jq -r '.request.sftp_port')
SFTP_USER=$(cat request_sftp_result.json | jq -r '.request.sftp_user')
SFTP_SERVER=$(cat request_sftp_result.json | jq -r '.request.hostname')
SFTP_FILENAME=$(cat request_sftp_result.json | jq -r '.request.shared_file')
SFTP_CMD="sftp -b batch.sftp -P ${SFTP_PORT} ${SSH_OPTIONS} ${SFTP_USER}@${SFTP_SERVER}"
echo "put -a \"$DUMPFILE\" \"$SFTP_FILENAME\"" > batch.sftp
echo "bye" >> batch.sftp
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
# Asking to process the database
PROCESS_URL="${BASE_URL}/database/v1/process"
URL_PARAMS="key=${KEY}&request=${REQUEST_ID}"
curl -sS "${PROCESS_URL}?${URL_PARAMS}" > process_result.json
# check for failures
failures=$(cat process_result.json | jq -r '.failures[]')
if [ "$failures" != "" ]; then
echo $failures | jq -r '.'
exit 3
else
echo "ask to process: ok"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment