Last active
March 3, 2021 20:55
-
-
Save shvchk/4e375a7c0a43b7f6e25fc9cdb24e75d6 to your computer and use it in GitHub Desktop.
Anbox environments management
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 | |
script_name=$(basename "$0") | |
envs="/var/snap/anbox" | |
def_env="${envs}/common" | |
current_env=$(readlink -f "$def_env") | |
clean=false | |
show_help() { | |
cat << EOF | |
Usage: $script_name [options] NAME | |
NAME is an environment name, corresponding to existing '${def_env}-NAME' directory | |
Options: | |
-c, --clean Clean Anbox log files from old environment before unplugging | |
-h, --help Show this help | |
Example: | |
You have 'whatsapp' environment plugged ('${def_env}-whatsapp'), | |
and wish to clean its logs, unplug and plug 'signal' environment instead: | |
$script_name --clean signal | |
This will stop Anbox service, clean logs from the old environment ('${def_env}-whatsapp'), remove link to old environment from '${envs}', create link to new environment and start Anbox. | |
Since it requires root privileges, use 'sudo $script_name ...' to launch this script if it's in root's PATH, otherwise use 'pkexec $script_name ...' or 'sudo env PATH=\$PATH $script_name ...'. | |
EOF | |
} | |
opts=( "$@" ) | |
if [[ ${#opts[@]} -eq 0 ]]; then | |
show_help | |
exit 1 | |
fi | |
if [[ "${opts[-1]}" != "-h" && "${opts[-1]}" != "--help" ]]; then | |
env="${opts[-1]}" | |
unset 'opts[-1]' | |
fi | |
for i in "${opts[@]}"; do | |
case $i in | |
-c | --clean ) | |
clean=true | |
;; | |
-h | --help ) | |
show_help | |
exit 0 | |
;; | |
* ) | |
echo "Unknown option: $i" | |
;; | |
esac | |
done | |
if [[ ! -d "${def_env}-${env}" ]]; then | |
echo "Can't find "${def_env}-${env}" environment, exiting" | |
exit 1 | |
fi | |
if [[ -L "$def_env" && -d "$def_env" && "$current_env" == "${def_env}-${env}" ]]; then | |
echo "'$env' environment already plugged, no need to do anything" | |
exit 0 | |
fi | |
if [[ ! -L "$def_env" && -d "$def_env" ]]; then | |
echo "Existing '${def_env}' directory found, rename it?" | |
echo "If you set its name to e.g. 'default', it will be renamed to '${def_env}-default'" | |
echo "and will be accessible with this script like this: '$script_name default'" | |
echo "To cancel and exit without changes, press Ctrl+C" | |
def_env_name="default" | |
read -e -i "$def_env_name" -p "Environment name for '${def_env}': " input | |
def_env_name="${input:-$def_env_name}" | |
echo "Stopping Anbox" | |
snap stop anbox | |
echo "Renaming '${def_env}' to '${def_env}-${def_env_name}'" | |
mv "${def_env}" "${def_env}-${def_env_name}" | |
fi | |
echo "Stopping Anbox" | |
snap stop anbox | |
if [[ "$clean" == true ]]; then | |
echo "Cleaning logs" | |
rm ${def_env}/logs/* 2> /dev/null | |
rm ${def_env}/data/system.log* 2> /dev/null | |
fi | |
if [[ -L "$def_env" && -d "$def_env" ]]; then | |
echo "Removing link to an old environment" | |
rm "$def_env" | |
fi | |
if [[ ! -e "$def_env" ]]; then | |
echo "Creating link to a new environment" | |
ln -s "${def_env}-${env}" "$def_env" | |
else | |
echo "Old '$def_env' environment still exists, something went wrong" | |
fi | |
echo "Starting Anbox" | |
snap start anbox |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment