Skip to content

Instantly share code, notes, and snippets.

@drankard
Last active August 29, 2015 14:13
Show Gist options
  • Save drankard/bef6295e75671f373483 to your computer and use it in GitHub Desktop.
Save drankard/bef6295e75671f373483 to your computer and use it in GitHub Desktop.
A little helper managing lxc containers
#!/bin/bash
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
AMOUNT=5
NAME="xymon"
case "$1" in
create)
for i in $(seq 1 $AMOUNT);
do
echo "Creating $NAME-$i"
lxc-clone -s -o template -n "$NAME-$i" > /dev/null
lxc-start -n "$NAME-$i" -d
done
echo "Please wait, booting..."
sleep 15
echo "sudo lxc-ls --fancy -F ipv4,name"
sed -i "/$NAME/d" hosts
lxc-ls --fancy -F ipv4,name | column -t | grep "$NAME" >> hosts
lxc-ls --fancy -F ipv4,name | column -t | grep "$NAME"
;;
destroy)
for i in $(seq 1 $AMOUNT);
do
echo "Destroying $NAME-$i"
lxc-stop -n "$NAME-$i"
lxc-destroy -n $NAME-$i
done
sed -i "/$NAME/d" hosts
;;
start)
for i in $(seq 1 $AMOUNT);
do
echo "Starting $NAME-$i"
lxc-start -n "$NAME-$i" -d
done
echo "Please wait, booting..."
sleep 15
sed -i "/$NAME/d" hosts
lxc-ls --fancy -F ipv4,name | column -t | grep $NAME
lxc-ls --fancy -F ipv4,name | column -t | grep $NAME >> hosts
;;
stop)
for i in $(seq 1 $AMOUNT);
do
echo "Shutting down $NAME-$i"
lxc-stop -n "$NAME-$i"
done
sed -i "/$NAME/d" hosts
;;
status)
lxc-ls --fancy -F ipv4,name | column -t | grep $NAME
;;
*)
echo "Usage: $0 {create|destroy|start|stop|status}"
exit 1
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment