chg: ``send`` accept input from STDIN #48

Open
bgallet wants to merge 7 commits from bgallet/0k-charms:send into master
  1. 30
      apache/actions/relations/publish-dir/check-external-https
  2. 1
      apache/actions/relations/web-proxy/check-external-https
  3. 5
      docker-host/src/bin/send
  4. 48
      mongo/actions/upgrade
  5. 5
      mongo/hooks/init
  6. 6
      mongo/lib/common
  7. 3
      mongo/upgrade/build/6.0.19/Dockerfile
  8. 3
      mongo/upgrade/build/7.0.15/Dockerfile
  9. 144
      odoo-tecnativa/actions/load
  10. 91
      odoo-tecnativa/actions/save
  11. 213
      odoo-tecnativa/lib/common
  12. 0
      odoo-tecnativa/resources/opt/odoo/common/entrypoint.d/20-postgres-wait

30
apache/actions/relations/publish-dir/check-external-https

@ -0,0 +1,30 @@
#!/bin/bash
# compose: no-hooks
## Load action gets a first argument a FILE/DIRECTORY/URL holding the necessary files.
##
##
if [ -z "$SERVICE_DATASTORE" ]; then
echo "This script is meant to be run through 'compose' to work properly." >&2
exit 1
fi
usage="$exname [-h|--help]"
while [ "$1" ]; do
case "$1" in
"--help"|"-h")
print_usage
exit 0
;;
*)
err "Unexpected argument '$1'."
exit 1
;;
esac
shift
done
url=$(relation-get url)
curl -Isf "$url" --max-time 5 >/dev/null

1
apache/actions/relations/web-proxy/check-external-https

@ -0,0 +1 @@
../publish-dir/check-external-https

5
docker-host/src/bin/send

@ -66,6 +66,11 @@ Options:
"
# Accept input from stdin
if [[ -p /dev/stdin ]]; then
message=$(cat)
fi
while [ "$#" -gt 0 ]; do
arg="$1"
shift

48
mongo/actions/upgrade

@ -79,31 +79,45 @@ mongo:image:version() {
}
mongo:container:version() {
local container="$1"
if ! out=$(docker exec -i "$container" mongo --version); then
local container="$1" version
if ! out=$(docker exec "$container" mongod --version); then
err "Couldn't infer container's '$container' mongod's version."
exit 1
return 1
fi
out=${out%%$'\n'*}
out=${out%%$'\r'*}
image_version=${out#db version v}
echo "$image_version"
version=${out#db version v}
echo "$version"
}
mongo:container:mongo () {
local container="$1"
docker exec -i "$container" mongo 2>&1
}
mongo:container:fix_compat() {
local container="$1" image_version="$2"
out=$(echo "db.adminCommand( { setFeatureCompatibilityVersion: \"${image_version%.*}\" } )" |
mongo:container:mongo "$container" 2>&1)
if [[ "$out" == *"\"ok\" : 1"* ]]; then
local container="$1" image_version="$2" version
if ! version=$(mongo:container:version "$container"); then
err "Couldn't get container's mongod version"
return 1
fi
## No need of confirmation for version < 7.0.0
if version_gt 7.0.0 "$version"; then
mongo_cmd="db.adminCommand( { setFeatureCompatibilityVersion: \"${image_version%.*}\" } )"
else
mongo_cmd="db.adminCommand( { setFeatureCompatibilityVersion: \"${image_version%.*}\", confirm: true } )"
fi
out=$(echo "$mongo_cmd" | mongo:container:mongo "$container" 2>&1)
if [[ "$out" == *"\"ok\" : 1"* ]] || [[ "$out" == *"ok: 1"* ]]; then
debug "Feature Compatibility set to ${WHITE}${image_version%.*}${NORMAL}"
else
err "Failed to set feature compatibility version to ${WHITE}${image_version%.*}${NORMAL}:"
echo "$out" | prefix " | " >&2
printf "%s" "$out" | prefix " | " >&2
return 1
fi
}
@ -202,13 +216,18 @@ if [ -n "$containers" ]; then
#err "Running container(s) for $DARKYELLOW$SERVICE_NAME$NORMAL are still running:"
for container in $containers; do
debug "Stopping container $container"
docker stop "$container" >/dev/null || {
docker:container:stop "$container" >/dev/null || {
err "Failed to stop container '$container'."
exit 1
}
docker rm "$container" > /dev/null || {
err "Couldn't delete container '$container'."
}
docker rm "$container" > /dev/null
## Check that the container is actually removed
if [ -n "$(docker ps -q -f name="$container")" ]; then
err "Failed to remove container '$container'."
exit 1
fi
container_stopped+=("$container")
done
fi
@ -339,6 +358,7 @@ if [ -z "$unexpected_stop" -a -z "$expected_stop" ]; then
docker stop "$migration_container"
fi
if [ -n "$failed" ]; then
echo "${WHITE}Failing migration logs:${NORMAL}" >&2
cat "$MIGRATION_TMPDIR/migration.log" | prefix " ${GRAY}|${NORMAL} " >&2

5
mongo/hooks/init

@ -93,9 +93,10 @@ ensure_db_docker_running
cmd="rs.initiate({ _id: 'rs01', members: [ { _id: 0, host: '$SERVICE_NAME:27017' } ]})"
debug "${WHITE}running:$NORMAL $cmd"
out=$(ddb <<<"$cmd")
out=$(ddb 2>&1 <<<"$cmd") || true
if [[ "$out" == *"\"codeName\" : \"AlreadyInitialized\""* ]]; then
if [[ "$out" == *"\"codeName\" : \"AlreadyInitialized\""* ]] ||
[[ "$out" == *"MongoServerError: already initialized"* ]]; then
info "ReplicaSet already initialized."
elif [[ "$out" == *"\"ok\" : 1"* ]]; then
info "ReplicaSet initialized. "

6
mongo/lib/common

@ -46,7 +46,11 @@ _set_db_params() {
}
ddb() { dcmd mongo --quiet "$@"; }
ddb() {
local eval_code
eval_code="$(cat - )"
dcmd mongo --quiet "$@" --eval "$eval_code" </dev/null
}
djson() {

3
mongo/upgrade/build/6.0.19/Dockerfile

@ -1,4 +1,5 @@
FROM mongo:6.0.19
ENTRYPOINT [ "mongod", "--config", "/etc/mongod.conf" ]
RUN ln -sf /usr/bin/mongosh /usr/bin/mongo
ENTRYPOINT [ "mongod", "--config", "/etc/mongod.conf" ]

3
mongo/upgrade/build/7.0.15/Dockerfile

@ -1,4 +1,5 @@
FROM mongo:7.0.15
ENTRYPOINT [ "mongod", "--config", "/etc/mongod.conf" ]
RUN ln -sf /usr/bin/mongosh /usr/bin/mongo
ENTRYPOINT [ "mongod", "--config", "/etc/mongod.conf" ]

144
odoo-tecnativa/actions/load

@ -64,147 +64,15 @@ version:lt() { ! version:ge "$@"; }
if [ -z "$dbname" ]; then
##
## Fetch default dbname in relation to postgres-database
##
## XXXvlab: can't get real config here
if ! read-0 ts _ _ < <(get_service_relation "$SERVICE_NAME" "postgres-database"); then
err "Couldn't find relation ${DARKCYAN}postgres-database${NORMAL}."
exit 1
fi
relation_file=$(get_relation_data_dir "$SERVICE_NAME" "$ts" "postgres-database") || {
err "Failed to find relation file"
exit 1
}
postgres_config=$(cat "$relation_file"/data) || exit 2
dbname="$(e "$postgres_config" | shyaml get-value dbname)" || {
err "Couldn't retrieve information of ${DARKCYAN}postgres-database${NORMAL}'s relation."
dbname=$(relation:get "$SERVICE_NAME:postgres-database" dbname) || {
err "Couldn't retrieve information of" \
"${DARKYELLOW}$SERVICE_NAME${NORMAL}-->${DARKCYAN}postgres-database${NORMAL}."
exit 1
}
fi
set -e
curl_opts=()
## Ensure odoo is launched
service_def=$(get_compose_service_def "$SERVICE_NAME")
## XXXvlab: should be moved to lib
CONFIG=$SERVICE_CONFIGSTORE/etc/odoo-server.conf
ADMIN_PASSWORD=$(echo "$service_def" | shyaml -q get-value options.admin-password) || {
if [ -e "$CONFIG" ]; then
ADMIN_PASSWORD=$(grep ^admin_passwd "$CONFIG" | sed -r 's/^admin_passwd\s+=\s+(.+)$/\1/g')
fi
if [ -z "$ADMIN_PASSWORD" ]; then
err "Could not find 'admin-password' in $SERVICE_NAME service definition nor in config file."
exit 1
fi
}
containers="$(get_running_containers_for_service "$SERVICE_NAME")"
if [ -z "$containers" ]; then
err "No containers running for service $DARKYELLOW$SERVICE_NAME$NORMAL."
exit 1
fi
if [ "$(echo "$containers" | wc -l)" -gt 1 ]; then
err "More than 1 container running for service $DARKYELLOW$SERVICE_NAME$NORMAL."
echo " Please contact administrator to fix this issue." >&2
exit 1
fi
container="$(echo "$containers" | head -n 1)"
odoo_version=$(docker exec "$container" python -c 'import odoo.release; print(odoo.release.version)') || {
err "Failed to get odoo version."
exit 1
}
if version:lt "$odoo_version" "16.0" && [ -n "$neutralize" ]; then
err "Option \`\`--neutralize\`\` (or \`\`-n\`\`)" \
"is only available for odoo ${WHITE}16.0${NORMAL}+."
echo " Service ${DARKYELLOW}$SERVICE_NAME${NORMAL} is running odoo ${WHITE}$odoo_version${NORMAL}" >&2
exit 1
fi
if [ -n "$neutralize" ]; then
curl_opts+=(-F "neutralize_database=true")
fi
container_network_ip=$(get_healthy_container_ip_for_service "$SERVICE_NAME" 8069 4) || {
err "Please ensure that $DARKYELLOW$service$NORMAL is running before using '$exname'."
exit 1
}
container_ip=${container_network_ip##*:}
container_network=${container_network_ip%%:*}
. "$CHARM_PATH/lib/common"
set -e
DEFAULT_CURL_IMAGE=${DEFAULT_CURL_IMAGE:-docker.0k.io/curl}
check_input() {
local chars
read -n 2 -r chars
if [ "$chars" != "PK" ]; then
err "Unexpected input not matching ZIP signature. Invalid dump."
echo " First chars: '$chars'"
exit 1
fi
{
printf "%s" "$chars"
cat
}
}
## Beware that we are not on the host, so we need to create the
## fifo in a place we can share with the curl container.
export TMPDIR=/var/cache/compose
settmpdir RESTORE_TMPDIR
mkfifo "$RESTORE_TMPDIR/fifo"
## Using fifo because if using ``@-`` curl will load all the database
## in memory before sending it, which is NOT desirable as the size of
## the database can be greater than available memory. Using fifo, we
## force the data to be streamed.
cmd=(
docker run -i --rm --network "$container_network"
-v "$RESTORE_TMPDIR/fifo:/tmp/restore/fifo"
"$DEFAULT_CURL_IMAGE"
-sS
-X POST
-F "master_pwd=${ADMIN_PASSWORD}"
-F "name=${dbname}"
"${curl_opts[@]}"
-F "backup_file=@/tmp/restore/fifo"
http://${container_ip}:8069/web/database/restore
)
## XXXvlab: contains password, left only for advanced debug
#echo "COMMAND: ${cmd[@]}" >&2
"${cmd[@]}" > "$RESTORE_TMPDIR/out" &
pid=$!
check_input > "$RESTORE_TMPDIR/fifo"
wait "$pid" || {
die "Posting to odoo restore API through curl was unsuccessfull."
}
out=$(cat "$RESTORE_TMPDIR/out")
if [[ "$out" == *"<html>"* ]]; then
errmsg=$(echo "$out" | grep "alert-danger")
errmsg=${errmsg#*>}
errmsg=${errmsg%%<*}
if [ "$errmsg" ]; then
errmsg=$(echo "$errmsg" | recode html..utf8)
die "$errmsg"
fi
die "Unexpected output. Restore probably failed."
fi >&2
info "Restored stdin dump to odoo '$dbname'."
odoo:load "$dbname" "$neutralize"

91
odoo-tecnativa/actions/save

@ -56,94 +56,15 @@ done
if [ -z "$dbname" ]; then
##
## Fetch default dbname in relation to postgres-database
##
## XXXvlab: can't get real config here
if ! read-0 ts _ _ < <(get_service_relation "$SERVICE_NAME" "postgres-database"); then
err "Couldn't find relation ${DARKCYAN}postgres-database${NORMAL}."
exit 1
fi
relation_file=$(get_relation_data_dir "$SERVICE_NAME" "$ts" "postgres-database") || {
err "Failed to find relation file"
exit 1
}
postgres_config=$(cat "$relation_file"/data) || exit 2
dbname="$(e "$postgres_config" | shyaml get-value dbname)" || {
err "Couldn't retrieve information of ${DARKCYAN}mysql-database${NORMAL}'s relation."
dbname=$(relation:get "$SERVICE_NAME:postgres-database" dbname) || {
err "Couldn't retrieve information of" \
"${DARKYELLOW}$SERVICE_NAME${NORMAL}-->${DARKCYAN}postgres-database${NORMAL}."
exit 1
}
fi
set -e
. $CHARM_PATH/lib/common
## Ensure odoo is launched
service_def=$(get_compose_service_def "$SERVICE_NAME")
## XXXvlab: should be moved to lib
CONFIG=$SERVICE_CONFIGSTORE/etc/odoo-server.conf
ADMIN_PASSWORD=$(echo "$service_def" | shyaml -q get-value options.admin-password) || {
if [ -e "$CONFIG" ]; then
ADMIN_PASSWORD=$(grep ^admin_passwd "$CONFIG" | sed -r 's/^admin_passwd\s+=\s+(.+)$/\1/g')
fi
if [ -z "$ADMIN_PASSWORD" ]; then
err "Could not find 'admin-password' in $SERVICE_NAME service definition nor in config file."
exit 1
fi
}
set -e
container_network_ip=$(get_healthy_container_ip_for_service "$SERVICE_NAME" 8069 4) || {
err "Please ensure that $DARKYELLOW$SERVICE_NAME$NORMAL is running before using '$exname'."
exit 1
}
container_ip=${container_network_ip##*:}
container_network=${container_network_ip%%:*}
DEFAULT_CURL_IMAGE=${DEFAULT_CURL_IMAGE:-docker.0k.io/curl}
check_output() {
local chars
read -n 2 -r chars
if [ "$chars" != "PK" ]; then
out=$(cat)
errmsg=$(echo "$out" | grep "alert-danger")
errmsg=${errmsg#*>}
errmsg=${errmsg%%<*}
if [ -n "$errmsg" ]; then
errmsg=$(echo "$errmsg" | recode html..utf8)
die "$errmsg"
fi
err "Unexpected output not matching ZIP signature. Dump probably failed."
exit 1
fi
{
echo -n "$chars"
cat
}
}
cmd=(
docker run --rm --network "$container_network"
"$DEFAULT_CURL_IMAGE"
-sS
-X POST
-F "master_pwd=${ADMIN_PASSWORD}"
-F "name=${dbname}"
-F "backup_format=zip"
http://${container_ip}:8069/web/database/backup
)
## XXXvlab: contains password, left only for advanced debug
#debug "${cmd[@]}"
set -o pipefail
"${cmd[@]}" | check_output &&
info "Requested odoo '$dbname' dump and outputted result to stdout."
odoo:save "$dbname"

213
odoo-tecnativa/lib/common

@ -38,3 +38,216 @@ sql() {
ddb "${dbname:-$DBNAME}"
)
}
odoo:get-admin-password() {
local service_def config admin_password
service_def=$(get_compose_service_def "$SERVICE_NAME")
config=$SERVICE_CONFIGSTORE/etc/odoo-server.conf
admin_password=$(echo "$service_def" | shyaml -q get-value options.admin-password) || {
if [ -e "$config" ]; then
admin_password=$(grep ^admin_passwd "$config" | sed -r 's/^admin_passwd\s+=\s+(.+)$/\1/g')
fi
if [ -z "$admin_password" ]; then
err "Could not find 'admin-password' in $SERVICE_NAME service definition nor in config file."
return 1
fi
}
echo "$admin_password"
}
## XXXvlab: to include in `compose-core`
get_running_container_for_service() {
local containers service="$1"
containers=($(get_running_containers_for_service "$service"))
if [ "${#containers[@]}" == 0 ]; then
err "No containers running for service $DARKYELLOW$service$NORMAL."
return 1
fi
if [ "${#containers[@]}" -gt 1 ]; then
err "More than 1 container running for service $DARKYELLOW$service$NORMAL."
echo " Please contact administrator to fix this issue." >&2
return 1
fi
echo "${containers[0]}"
}
odoo:save() {
local ADMIN_PASSWORD container_ip container_network_ip container_network cmd dbname="$1"
ADMIN_PASSWORD=$(odoo:get-admin-password) || {
err "Couldn't retrieve admin password for $SERVICE_NAME."
return 1
}
container_network_ip=$(get_healthy_container_ip_for_service "$SERVICE_NAME" 8069 4) || {
err "Please ensure that $DARKYELLOW$SERVICE_NAME$NORMAL is running before using '$exname'."
return 1
}
container_ip=${container_network_ip##*:}
container_network=${container_network_ip%%:*}
DEFAULT_CURL_IMAGE=${DEFAULT_CURL_IMAGE:-docker.0k.io/curl}
check_output() {
local chars
read -n 2 -r chars
if [ "$chars" != "PK" ]; then
out=$(cat)
errmsg=$(echo "$out" | grep "alert-danger")
errmsg=${errmsg#*>}
errmsg=${errmsg%%<*}
if [ -n "$errmsg" ]; then
errmsg=$(echo "$errmsg" | recode html..utf8)
die "$errmsg"
fi
err "Unexpected output not matching ZIP signature. Dump probably failed."
return 1
fi
{
echo -n "$chars"
cat
}
}
cmd=(
docker run --rm --network "$container_network"
"$DEFAULT_CURL_IMAGE"
-sS
-X POST
-F "master_pwd=${ADMIN_PASSWORD}"
-F "name=${dbname}"
-F "backup_format=zip"
http://${container_ip}:8069/web/database/backup
)
## XXXvlab: contains password, left only for advanced debug
#debug "${cmd[@]}"
set -o pipefail
"${cmd[@]}" | check_output &&
info "Requested odoo '$dbname' dump and outputted result to stdout."
}
odoo:version() {
local odoo_version container
container="$(get_running_container_for_service "$SERVICE_NAME")" || {
err "Couldn't find running container for service ${DARKYELLOW}$SERVICE_NAME${NORMAL}."
return 1
}
odoo_version=$(docker exec "$container" python -c 'import odoo.release; print(odoo.release.version)') || {
err "Failed to get odoo version from ${DARKYELLOW}$SERVICE_NAME${NORMAL}."
return 1
}
echo "$odoo_version"
}
odoo:load() {
local ADMIN_PASSWORD container_ip container_network_ip container_network cmd \
dbname="$1" neutralize="$2" DEFAULT_CURL_IMAGE RESTORE_TMPDIR
curl_opts=()
ADMIN_PASSWORD=$(odoo:get-admin-password) || {
err "Couldn't retrieve admin password for ${DARKYELLOW}$SERVICE_NAME${NORMAL}."
return 1
}
odoo_version=$(odoo:version) || return 1
if version:lt "$odoo_version" "16.0" && [ -n "$neutralize" ]; then
err "Option \`\`--neutralize\`\` (or \`\`-n\`\`)" \
"is only available for odoo ${WHITE}16.0${NORMAL}+."
echo " Service ${DARKYELLOW}$SERVICE_NAME${NORMAL} is running odoo ${WHITE}$odoo_version${NORMAL}" >&2
return 1
fi
if [ -n "$neutralize" ]; then
curl_opts+=(-F "neutralize_database=true")
fi
container_network_ip=$(get_healthy_container_ip_for_service "$SERVICE_NAME" 8069 4) || {
err "Please ensure that $DARKYELLOW$service$NORMAL is running before using '$exname'."
return 1
}
container_ip=${container_network_ip##*:}
container_network=${container_network_ip%%:*}
DEFAULT_CURL_IMAGE=${DEFAULT_CURL_IMAGE:-docker.0k.io/curl}
check_input() {
local chars
read -n 2 -r chars
if [ "$chars" != "PK" ]; then
err "Unexpected input not matching ZIP signature. Invalid dump."
echo " First chars: '$chars'"
return 1
fi
{
printf "%s" "$chars"
cat
}
}
## Beware that we are not on the host, so we need to create the
## fifo in a place we can share with the curl container.
export TMPDIR=/var/cache/compose
settmpdir RESTORE_TMPDIR
mkfifo "$RESTORE_TMPDIR/fifo"
## Using fifo because if using ``@-`` curl will load all the database
## in memory before sending it, which is NOT desirable as the size of
## the database can be greater than available memory. Using fifo, we
## force the data to be streamed.
cmd=(
docker run -i --rm --network "$container_network"
-v "$RESTORE_TMPDIR/fifo:/tmp/restore/fifo"
"$DEFAULT_CURL_IMAGE"
-sS
-X POST
-F "master_pwd=${ADMIN_PASSWORD}"
-F "name=${dbname}"
"${curl_opts[@]}"
-F "backup_file=@/tmp/restore/fifo"
http://${container_ip}:8069/web/database/restore
)
## XXXvlab: contains password, left only for advanced debug
#echo "COMMAND: ${cmd[@]}" >&2
"${cmd[@]}" > "$RESTORE_TMPDIR/out" &
pid=$!
check_input > "$RESTORE_TMPDIR/fifo"
wait "$pid" || {
die "Posting to odoo restore API through curl was unsuccessfull."
}
out=$(cat "$RESTORE_TMPDIR/out")
if [[ "$out" == *"<html>"* ]]; then
errmsg=$(echo "$out" | grep "alert-danger")
errmsg=${errmsg#*>}
errmsg=${errmsg%%<*}
if [ "$errmsg" ]; then
errmsg=$(echo "$errmsg" | recode html..utf8)
die "$errmsg"
fi
die "Unexpected output. Restore probably failed."
fi >&2
info "Restored stdin dump to odoo '$dbname'."
}

0
odoo-tecnativa/resources/opt/odoo/common/entrypoint.d/20-postgres-wait

Loading…
Cancel
Save