summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormischa <mischa@rx.high5.nl>2020-01-03 12:34:05 +0100
committermischa <mischa@rx.high5.nl>2020-01-03 12:34:05 +0100
commitb242f91dab291935677710fb1594fdfee5fdfba9 (patch)
tree5d6823287fc834165cd1e14c4bb52d75933461f8
parentafac5c8d3cf7812dda4920f73a4dd98a3bce8da4 (diff)
add remove script
-rwxr-xr-xremove.sh88
1 files changed, 88 insertions, 0 deletions
diff --git a/remove.sh b/remove.sh
new file mode 100755
index 0000000..9b6e30f
--- /dev/null
+++ b/remove.sh
@@ -0,0 +1,88 @@
+main () {
+ CONF_FILE="$PWD/_deploy.conf"
+ [ -f "$CONF_FILE" ] && . "$CONF_FILE"
+
+ file=${VMS}/$1
+
+ if [ -f "$file" ]; then
+ echo "reading $file"
+ . "$file"
+ backup_image "$file"
+ remove_user "$file"
+ move_file "$file"
+ else
+ echo "ERROR file doesn't exist: ${file}"
+ fi
+}
+
+check_instance() {
+ # Check if the instance name exists, otherwise return filename as VM.
+ # Takes vm*.txt and instance
+ # prints either filename or instance variable
+ if test -z "$2"
+ then echo "$1" | sed "s@^$VMS@@;s@^/@@;s/\\.txt$//"
+ else echo "$2"
+ fi
+}
+
+check_owner() {
+ # Check if the owner name exists, otherwise returns username.
+ # Takes username and owner
+ # prints either owner or username
+ if test -z "$2"
+ then echo "$1"
+ else echo "$2"
+ fi
+}
+
+check_format() {
+ # Check if the image format exists, otherwise returns img.
+ # Takes format
+ # prints either format or img
+ if test -z "$1"
+ then echo "${FORMAT}"
+ else echo "$1"
+ fi
+}
+
+backup_image() {
+ filename=$1
+ _instance=$(check_instance "$filename" "$instance")
+ _format=$(check_format "$format")
+ if [ -f "${IMAGES}/${_instance}.${_format}" ]; then
+ mv ${IMAGES}/${_instance}.${_format} ${IMAGES}/${_instance}.${_format}-backup
+ echo "vmm(4)/vmd(8) files moved: ${IMAGES}/${_instance}.${_format} ${IMAGES}/${_instance}.${_format}-backup"
+ else
+ echo "ERROR vmm(4)/vmd(8) files moved: ${IMAGES}/${_instance}.${_format} doesn't exist"
+ fi
+}
+
+remove_user() {
+ _owner=$(check_owner "$username" "$owner")
+ if [ -n "$_owner" ]; then
+ if grep -e "^${_owner}:" /etc/passwd > /dev/null; then
+ userdel -r "$_owner"
+ echo "userdel(8) removal: $_owner"
+ else
+ echo "ERROR userdel(8) removal: $_owner doesn't exist"
+ fi
+
+ if grep -e "^${_owner}:" /etc/group > /dev/null; then
+ groupdel "$_owner"
+ echo "groupdel(8) removal: $_owner"
+ else
+ echo "ERROR groupdel(8) removal: $_owner doesn't exist"
+ fi
+ fi
+}
+
+move_file() {
+ filename=$1
+ cp ${filename} ${filename}-backup
+ echo "cp(1): ${filename} ${filename}-backup"
+ mv ${filename} ${filename}.free
+ echo "mv(1): ${filename} ${filename}.free"
+
+}
+
+main "$@"