Last active
January 6, 2023 13:01
-
-
Save hedzr/a24592879ac90239be6c8b1746feebd4 to your computer and use it in GitHub Desktop.
run a vagrant vm more than easy - chs post at https://hedzr.com/devops/linux/run-virtualbox-vm-from-command-line/
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
__vms_reg() { | |
# hash -d | grep -qE '^vhost.ub20a=' || hash -d vhost.ub20a="$HOME/work/ops.work/ub20a.local" | |
hash -d | grep -qE '^vhost.hello=' || hash -d vhost.hello="$HOME/hack/work/hello-1-cxx" | |
# Or modified tabbed entries in ~/.vagrant.vhosts. A sample entry is: | |
# ub20a $HOME/work/ops.work/ub20a.local | |
if [ -f "$HOME/.vagrant.vhosts" ]; then | |
local name1 name2 | |
while IFS=$'\t' read -r name1 name2; do | |
if [[ "$name1" != "" ]]; then | |
dbg ">> loading pair: $name1 -> $(eval echo -e $name2)" | |
# hash -d | grep -qE '^vhost\.'$name1'=' || | |
hash -d vhost.$name1="$(eval echo -e $name2)" | |
fi | |
done <"$HOME/.vagrant.vhosts" | |
fi | |
} | |
vagrant_run() { | |
__vms_reg | |
if [[ $# -eq 0 ]]; then | |
cat <<-"EOF" | |
Usage: | |
$ vagrant_run <vagrant-vhost-folder-short-name> [vhost-name] [up | halt | destroy...] | |
$ vagrant_run <vhost-name> [up | halt] | |
$ vagrant_run ls|list | |
For example: | |
$ vagrant_run ub20a | |
~ub20a is a hash -d name which points to somewhere who owns a Vagrantfile. | |
$ vagrant_run ub20a halt | |
$ vagrant_run ub20a up | |
$ vagrant_run redis-va redis01 | |
~refis-va is a hash -d name which points to somewhere who owns a Vagrantfile. | |
There are multiple vhosts defined in the Vagrantfile (via `nodes = Hash.new\n...`) | |
$ vagrant_run redis-va redis01 up | |
$ vagrant_run redis-va redis01 halt | |
$ vagrant_run ls | |
$ vagrant_run ubuntu20testvm up # a virtualbox vm | |
Known VMs: | |
- ub20a : | |
- redis-va : k8s, ops, study and develop | |
EOF | |
else | |
local name="$1" | |
# echo "\$# = $#" | |
[[ $# -ge 1 ]] && shift | |
if hash -d | grep -qE 'vhost.'${name}'='; then | |
if [ -d ~vhost.$name ]; then | |
echo "entering " ~vhost.$name " ..." | |
pushd ~vhost.$name >/dev/null | |
if [ -f Vagrantfile ]; then | |
local cmd="${1:-up}" | |
# echo "\$# = $#" | |
[[ $# -ge 1 ]] && shift | |
[ -f /tmp/vagrant-commands ] || vagrant | grep -E '^ [a-z][^ ]+[ ]+.*$' | awk '{print $1}' >/tmp/vagrant-commands | |
local commands=($(cat /tmp/vagrant-commands)) | |
if [[ " ${commands[*]} " =~ " ${cmd} " ]]; then | |
# whatever you want to do when array contains value | |
local vhost_name='' | |
echo "running cmd=${cmd}..." | |
# echo "FOUND: vhost name: $vhost_name, cmd: $cmd" | |
else | |
local vhost_name="$cmd" | |
cmd="${1:-up}" | |
[[ $# -ge 1 ]] && shift | |
fi | |
echo "vhost name: $vhost_name, cmd: $cmd, args: $@" | |
# return | |
case $cmd in | |
up) | |
if [[ $# -eq 0 ]]; then | |
# For a vagrant vhost definition file (Vagrantfile), if there is | |
# multiple vhosts which are defined, vagrant_run will fail to | |
# bring all vhosts up. | |
# That is, you can only up one vhost once via vagrant_run, just like: | |
# $ vagrant_run redis-va redis01 up | |
# $ vagrant_run redis-va k8mm001 up | |
# And these are wrong calls: | |
# $ vagrant_run redis-va | |
# $ vagrant_run redis-va up | |
# | |
grep -qE 'nodes = Hash\.new' Vagrantfile && { | |
echo -e "Wrong command 'vagrant_run $name up'!!\nYou MUST specify a vhost name: 'vagrant_run $name <vhost_name> [up|...]'" 1>&2 && | |
return | |
} | |
fi | |
local nodes=($(grep -E '^\t+".*?" => .*"ip"=>' Vagrantfile | awk '{print $1}' | xargs echo)) | |
if [[ "${nodes[@]}" =~ " $vhost_name " ]]; then | |
echo "running ..." | |
else | |
echo "FOUND: vhost name: '$vhost_name', cmd: $cmd, args: $@" | |
echo "Known Nodes: '${nodes[@]}'" | |
vagrant $cmd "$@" | |
return | |
fi | |
# return | |
vagrant $cmd $vhost_name "$@" | |
;; | |
*) | |
echo "FOUND: vhost name: $vhost_name, cmd: $cmd, args: $@" | |
;; | |
esac | |
# return | |
vagrant $cmd $vhost_name "$@" | |
fi | |
popd >/dev/null | |
else | |
: | |
fi | |
else | |
local cmd="${1:-up}" | |
[[ $# -ge 1 ]] && shift | |
local n="$(echo $(VBoxManage list vms | grep -o $name))" | |
if [[ "$n" == "$name" ]]; then | |
case $cmd in | |
up | run) | |
VBoxManage startvm $name --type headless | |
;; | |
halt | stop | poweroff | shutdown) | |
VBoxManage controlvm $name poweroff | |
;; | |
esac | |
else | |
case $name in | |
ls | list) | |
echo "VMs in virtualbox" | |
echo | |
VBoxManage list vms | |
echo | |
echo "VMs in hash folder" | |
echo | |
hash -d | grep -E 'vhost\.(.*)=' | sed -re "s#${HOME//\//\\/}#~#" | |
;; | |
*) | |
echo "Error: Unknown <vagrant-vhost-folder-short-name> or <vhost-name>: $name" 1>&2 | |
echo 1>&2 | |
echo "You may create the hash link for a vagrant vhost:" 1>&2 | |
echo " $ hash -d vhost.ub20a=\"\$HOME/work/ops.work/ub20a.local\"" 1>&2 | |
;; | |
esac | |
fi | |
fi | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment