#!/bin/bash #================================================= # PACKAGE_CHECK HELPERS #================================================= # Start an LXC and execute a command in it # # usage: ynh_lxc_pc_exec --name=name --command=command # | arg: -n, --name= - name of the LXC # | arg: -c, --command= - command to execute # # Requires YunoHost version *.*.* or higher. ynh_lxc_pc_exec () { # Declare an array to define the options of this helper. local legacy_args=nc local -A args_array=([n]=name= [c]=command=) local name local command # Manage arguments with getopts ynh_handle_getopts_args "$@" _ynh_lxc_start_and_wait $name start_timer # Execute the command given in argument in the container and log its results. lxc exec $name --env PACKAGE_CHECK_EXEC=1 -t -- /bin/bash -c "$command" | tee -a "$complete_log" $current_test_log # Store the return code of the command local returncode=${PIPESTATUS[0]} log_debug "Return code: $returncode" stop_timer 1 # Return the exit code of the ssh command return $returncode } # Create a witness in an LXC container # # usage: ynh_lxc_pc_witness_file_create --name=name --witness=witness --type=type # | arg: -n, --name= - name of the LXC # | arg: -w, --witness= - witness to create # | arg: -t, --type= - type of witness, can be file or directory # # Requires YunoHost version *.*.* or higher. ynh_lxc_pc_witness_file_create () { # Declare an array to define the options of this helper. local legacy_args=nwt local -A args_array=([n]=name= [w]=witness= [t]=type=) local name local witness local type # Manage arguments with getopts ynh_handle_getopts_args "$@" [ "$type" = "file" ] && local action="touch" || local action="mkdir -p" ynh_lxc_run_inside --name=$name --command="$action $witness" } ynh_lxc_pc_witness_files_set () { # Create files to check if the remove script does not remove them accidentally log_debug "Create witness files..." # Nginx conf ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" --type=file ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" --type=file # /etc ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/etc/witnessfile" --type=file # /opt directory ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/opt/witnessdir" --type=directory # /var/www directory ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/var/www/witnessdir" --type=directory # /home/yunohost.app/ ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/home/yunohost.app/witnessdir" --type=directory # /var/log ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/var/log/witnessfile" --type=file # Config fpm ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" --type=file # Config logrotate ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/etc/logrotate.d/witnessfile" --type=file # Config systemd ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/etc/systemd/system/witnessfile.service" --type=file # Database ynh_lxc_run_inside --name=$LXC_NAME --command="mysqladmin --wait status > /dev/null 2>&1" echo "CREATE DATABASE witnessdb" | ynh_lxc_run_inside --name=$LXC_NAME --command="mysql --wait > /dev/null 2>&1" } ynh_lxc_pc_witness_file_check () { if ynh_lxc_run_inside --name=$LXC_NAME --command="test ! -e \"$1\"" then log_error "The file $1 is missing ! Something gone wrong !" SET_RESULT "failure" witness fi } ynh_lxc_pc_witness_files_check () { # Check all the witness files, to verify if them still here # Nginx conf ynh_lxc_pc_witness_file_check "/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" ynh_lxc_pc_witness_file_check "/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" # /etc ynh_lxc_pc_witness_file_check "/etc/witnessfile" # /opt directory ynh_lxc_pc_witness_file_check "/opt/witnessdir" # /var/www directory ynh_lxc_pc_witness_file_check "/var/www/witnessdir" # /home/yunohost.app/ ynh_lxc_pc_witness_file_check "/home/yunohost.app/witnessdir" # /var/log ynh_lxc_pc_witness_file_check "/var/log/witnessfile" # Config fpm ynh_lxc_pc_witness_file_check "/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" # Config logrotate ynh_lxc_pc_witness_file_check "/etc/logrotate.d/witnessfile" # Config systemd ynh_lxc_pc_witness_file_check "/etc/systemd/system/witnessfile.service" # Database if ! ynh_lxc_run_inside --name=$LXC_NAME --command="mysqlshow witnessdb > /dev/null 2>&1" then log_error "The database witnessdb is missing ! Something gone wrong !" SET_RESULT "failure" witness return 1 fi } ynh_lxc_pc_create () { log_info "Launching new LXC $LXC_NAME ..." # Check if we can launch container from YunoHost remote image if lxc remote list | grep -q "yunohost" && lxc image list yunohost:$LXC_BASE | grep -q -w $LXC_BASE; then lxc launch yunohost:$LXC_BASE $LXC_NAME \ -c security.nesting=true \ -c security.privileged=true \ -c limits.memory=80% \ -c limits.cpu.allowance=80% \ >>/proc/self/fd/3 # Check if we can launch container from a local image elif lxc image list $LXC_BASE | grep -q -w $LXC_BASE; then lxc launch $LXC_BASE $LXC_NAME \ -c security.nesting=true \ -c security.privileged=true \ -c limits.memory=80% \ -c limits.cpu.allowance=80% \ >>/proc/self/fd/3 else log_critical "Can't find base image $LXC_BASE, run ./package_check.sh --rebuild" fi pipestatus="${PIPESTATUS[0]}" location=$(lxc list --format json | jq -e --arg LXC_NAME $LXC_NAME '.[] | select(.name==$LXC_NAME) | .location' | tr -d '"') [[ "$location" != "none" ]] && log_info "... on $location" [[ "$pipestatus" -eq 0 ]] || exit 1 _ynh_lxc_start_and_wait $LXC_NAME ynh_lxc_pc_witness_files_set lxc snapshot $LXC_NAME snap0 } ynh_lxc_pc_snapshot_create () { # Create a temporary snapshot local snapname=$1 start_timer # Check all the witness files, to verify if them still here ynh_lxc_pc_witness_files_check >&2 # Remove swap files to avoid killing the CI with huge snapshots. ynh_lxc_swapfiles_clean ynh_lxc_stop $LXC_NAME # Check if the snapshot already exist if ! ynh_lxc_snapshot_exists "$snapname" then log_info "(Creating snapshot $snapname ...)" lxc snapshot $LXC_NAME $snapname fi _ynh_lxc_start_and_wait $LXC_NAME stop_timer 1 }