Skip to content

Instantly share code, notes, and snippets.

@mikhailov
Last active October 15, 2017 09:37
Show Gist options
  • Save mikhailov/8104790 to your computer and use it in GitHub Desktop.
Save mikhailov/8104790 to your computer and use it in GitHub Desktop.
Vagrantless (draft), tested on OS X only.
# Here is an example what Vagrant produces to create new VM
/usr/bin/VBoxManage list hostonlyifs
/usr/bin/VBoxManage import -n ~/.vagrant.d/boxes/ubuntu1204/virtualbox/box.ovf
/usr/bin/VBoxManage import ~/.vagrant.d/boxes/ubuntu1204/virtualbox/box.ovf --vsys 0 --vmname 'test_1383128976_1388771771625_2328' --vsys 0 --unit 10 --disk '~/VirtualBox \VMs/test_1383128976_1388771771625_2328/box-disk1.vmdk'
/usr/bin/VBoxManage list vms
/usr/bin/VBoxManage showvminfo c613294b-5258-45ba-a1af-cd908cb11ddf
/usr/bin/VBoxManage modifyvm c613294b-5258-45ba-a1af-cd908cb11ddf --macaddress1 080027D0180B
/usr/bin/VBoxManage list hostonlyifs
/usr/bin/VBoxManage modifyvm c613294b-5258-45ba-a1af-cd908cb11ddf --name test2_default_1388771785554_78815
/usr/bin/VBoxManage sharedfolder add c613294b-5258-45ba-a1af-cd908cb11ddf --name /vagrant --hostpath ~/Desktop/test
/usr/bin/VBoxManage setextradata c613294b-5258-45ba-a1af-cd908cb11ddf 'VBoxInternal2/SharedFoldersEnableSymlinksCreate//vagrant' 1
/usr/bin/VBoxManage modifyvm c613294b-5258-45ba-a1af-cd908cb11ddf --nic2 none --nic3 none --nic4 none --nic5 none --nic6 none --nic7 none --nic8 none
/usr/bin/VBoxManage modifyvm c613294b-5258-45ba-a1af-cd908cb11ddf --nic1 nat
/usr/bin/VBoxManage modifyvm c613294b-5258-45ba-a1af-cd908cb11ddf --natpf1 'ssh,tcp,127.0.0.1,2222,,22'
/usr/bin/VBoxManage storagectl c613294b-5258-45ba-a1af-cd908cb11ddf --name 'SATA Controller' --hostiocache on
/usr/bin/VBoxManage modifyvm c613294b-5258-45ba-a1af-cd908cb11ddf --rtcuseutc on
/usr/bin/VBoxManage modifyvm c613294b-5258-45ba-a1af-cd908cb11ddf --natdnsproxy1 on
/usr/bin/VBoxManage startvm c613294b-5258-45ba-a1af-cd908cb11ddf --type headless
# This the bash script to handle up-and-runnig VM created through Vagrant
#!/usr/bin/env bash
# PRIVATE: error level message
error_message () {
echo "$(tput setaf 1)ERROR: $1$(tput sgr0)"
}
# PRIVATE: info level message
info_message () {
echo "$(tput setaf 2)INFO: $1$(tput sgr0)"
}
if [ -z "$1" ]; then
echo "Vagrantless syntax:
Vagrantless up <uuid>
Vagrantless stop <uuid>
Vagrantless status [<uuid>]
Vagrantless ssh <uuid>
"
exit 1
else
vagrantVMs=$(find .vagrant -type f -name 'id' | while read line; do cat "${line}"; echo; done)
fi
if [ ! -z "$2" ]; then
vm=$(VBoxManage list vms | grep -F "$vagrantVMs" | grep -F "$2")
runningvm=$(VBoxManage list runningvms | grep -F "$vagrantVMs" | grep -F "$2")
if [ "$vm" ]; then
vmname=$(echo $vm | awk '{ print $1 }' | sed "s/\"//g")
fi
fi
################################
# ./Vagrantless up VM
################################
if [ "$1" == "up" ]; then
if [ ! -z "$2" ]; then
if [ "$runningvm" ]; then
error_message "This VM is running already. Do nothing."
elif [ "$vm" ]; then
info_message "This VM is available. It's going to start..."
VBoxManage startvm $vmname --type headless
else
error_message "This VM is not available yet."
fi
else
error_message "Please specify correct VM first."
fi
fi
################################
# ./Vagrantless stop VM
################################
if [ "$1" == "stop" ]; then
if [ ! -z "$2" ]; then
if [ "$runningvm" ]; then
info_message "This VM is available. Let's stop it..."
VBoxManage controlvm $vmname poweroff
else
error_message "This VM is not running or not available."
fi
else
error_message "Please specify correct VM first."
fi
fi
################################
# ./Vagrantless status [VM]
################################
if [ "$1" == "status" ]; then
if [ ! -z "$2" ]; then
if [ "$vm" ]; then
info_message "This VM is available."
VBoxManage showvminfo $vname | grep State
else
error_message "Please specify correct VM first."
fi
fi
fi
################################
# ./Vagrantless ssh VM
################################
if [ "$1" == "ssh" ]; then
if [ ! -z "$2" ]; then
if [ "$runningvm" ]; then
info_message "This VM is available. Let's SSH it."
# Hard-coded IP address
IP=192.168.1.7
ssh -oStrictHostKeyChecking=no root@$IP
else
error_message "Please specify correct VM first."
fi
else
error_message "Please specify correct VM first."
fi
fi
echo ""
echo "$(tput setaf 2)---- Available VMs:$(tput setaf 4)"
VBoxManage list vms | grep -F "$vagrantVMs" | sed -n 's/\(.*\)\(_\)\(.*\)\(_\)\(.*\)\ /\3 /p'
echo "$(tput setaf 2)---- Running VMs:$(tput setaf 7)"
VBoxManage list runningvms | grep -F "$vagrantVMs" | sed -n 's/\(.*\)\(_\)\(.*\)\(_\)\(.*\)\ /\3 /p'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment