Vagrant - ghdrako/doc_snipets GitHub Wiki
Vagrant:
- Abstract virtualization
- Supervisor to run virtual machine
- Work with VirtualBox(For Vagrant VirtualBox is provider) Vagrant acts as a wrapper for VirtualBox
- Writen in Ruby
Providers:
- VirtualBox,
- VMWare(Fusion and Workstation),
- Docker,
- Hyper-V,
- AWS
vagrant --version
vagrant up --provider=vmware_fusion
vagrant up --provider=aws
By default Vagrant stores boxes VMs in current user's home directory in the ~/.vagrant.d/
folder.
The location of the Vagrant home directory can be redefined with the VAGRANT_HOME
enviroment variable.
export VAGRANT_HOME=/soe/shared/directory
In windows i EnvirometVariables dialog box.
vagrant -h
vagrant box -h
vagrant box add -h
mkdir ubuntu
cd ubuntu
vagrant init hashicorp/precise32
lub vagrant init hashicorp/precise32 --minimal Vagrantfile bedzie maly
vagrant up
vagrant ssh -- uwaga dziala ale w consoli bashowej np Gita,CygWin,MinGW
vagrant box list -- lista zainstalowanych VM
vagrant box remove box/name -- kasowanie VM
vagrant ssh-config --informacje do polaczenia z ssh
vboxmanage -version
vboxmanage list runningvms -- lista dzialajacych VM -- dziala w CMD bash nie widzi ale vagranta widzi ?
Ctrl+V zaznacza blok w Vim
Vagrantfile - informacje jak startowac VM - mozna odblokowac GUI wtedy bedzie widoczne okno VirtualBoxa przydane bo widac wiecej komunikatow o bledach
vagrant reload
-- restart VMa np. po zmianach w Vagrantfile
Default VM user i haslo vagrant
katalog w ktorym jest Vagrantfile jest wspoldzielony z VM, z poziomu VMa jest to katalog vagrant
cd /vagrant/
vagrant suspend -- hibernate VM wykonujemy w katalogu gdzie Vagrant file aby bylo wiadomo o ktory VM chodzi
vagrant resume --odhibernovanie VM
vagrant halt -- shutdown VM
boxmanage list vms - wyswietla wszystkie VM jakie ma VirtualBox
vagrant destroy -- skasowanie VMa dziala w cmd windowsa a w git bashu nie ????
vagrant destroy -f -- gdy sa problemy z tym pierwszym np nie moze sie polaczyc z TTY
vagrant status - czy maszyna jest uruchomina
vagrant provision - uruchamia provisioning
vagrant --help
dodac do Vagrant file
config.vm.box_download_insecure = true
lub uruchomic
vagrant --insecure up
#lista zainstalowanych pluginow:
vagrant plugin list
# Installing a plugin from a known gem source
$ vagrant plugin install my-plugin
# Installing a plugin from a local file source
$ vagrant plugin install /path/to/my-plugin.gem
#update all plugins
vagrant plugin update
#update a specific plugin
vagrant plugin update [<name>]
#uninstall plugin
vagrant plugin uninstall <name> [<name2> <name3> ...]
vagrant plugin install vagrant-vbguest
# gdy problem z certyfikatami dla ssl
vagrant plugin install vagrant-vbguest --plugin-source http://rubygems.org
vagrant plugin install vagrant-vbguest
Guest additions provide better driver support, port forwarding, and host-only networking. They help your VM run faster and have more options available.
agrant up # Creates a VM using the Vagrantfile as a guide
vagrant destroy # Destroys the running VM
vagrant status # Checks the running status of a VM
vagrant ssh # Accesses the VM over Secure Shell
mkdir ubuntu
cd ubuntu
vagrant init hashicorp/precise32
#lub
vagrant init hashicorp/precise32 --minimal # Vagrantfile bedzie maly
# vagrant init stworzy plik z wieloma ustawieniami caly zakomentowany
vagrant up
vagrant ssh -- uwaga dziala ale w consoli bashowej np Gita,CygWin,MinGW
Every VM start from box. Box is a file from each VM is created
- vbox - is VM starting point
- packer - narzedzie do tworzenia swoich box-ow
- https://vagrantcloud.com - tool to discover and distribute vboxes
- http://www.vagrantbox.es
np hashicorp/precise32 - vbox provided by hashicorp contain 32-b ubuntu 12.04 that has Chef/Puppet preinstalled.
vagrant box add precise32 http://files.vagrantup.com/precise32.box - sciagnie box do lokalnego repozytorim box-ow
vagrant box -h # info o opcjach zarzadzania swoimi prywatnymi boxami
vagrant box list
vagrant box repackage # jesli chemy wyeksportowac na inny komputer vboxas np. jesli nie mamy dostepu do internetu
vagrant package # packages a currently running VirtualBox environment into a re-usable box
Example using vagrant package
- init and up the existing box first
-
vagrant package --output vagrant_example.box --base NAME
# Instead of packaging a VirtualBox machine that Vagrant manages, this will package a VirtualBox machine that VirtualBox manages. NAME should be the name or UUID of the machine from the VirtualBox GUI.
Every VM start from box. Box is a file from each VM is created.Are use as tampletes to current project.Boxes are compressed.
vagrant box add hashicorp/precise32 -- sciagniecie boxa do lokalnego repozytorium
vagrant box list -- lista zainstalowanych VM(box-ow)
vagrant box remove box/name -- Usuniecie boxa z repozytorim
When you “add” a box, it essentially downloads a pre-built Virtual Machine Disk (VMDK) from the internet/network and stores it. The VMDK’s get downloaded to a hidden sub-directory in your home directory.
Boxes exist in %userprofile%/.vagrant.d/boxes
cd ~/.vagrant.d/boxes
cd ~/.vagrant.d/boxes/precise32/virtualbox
ls
Vagrantfile box-disk1.vmdk box.ovf metadata.json
- VMDK
- Vagrantfile describing the box
- Open Virtualization Format (OVF) file for VirtualBox (so VirtualBox can import it)
- metadata JSON file telling Vagrant the provider (in this case VirtualBox)
The files inside the .vagrant.d/boxes
directory are the initial VM files that are copied over the first time you vagrant up
.
By default Vagrant uses ~/.vagrant.d
. Fortunately, vagrant provides an environment variable called VAGRANT_HOME
by which you can set vagrant home. Just do the following to change the vagrant home (this only works in the current session)
export VAGRANT_HOME=/path/to/vagrant
To make it permanent, add this to your ~/.bash_profile
(for login shell).
VAGRANT_HOME
has to be set and that the property "machinefolder" in VirtualBox has to be set
through "VBoxManage setproperty machinefolder "
cd /C/"Program Files"/Oracle/VirtualBox
VBoxManage setproperty machinefolder /d/data/vagrant
It might be useful to permanently set this on a Windows box by executing
setx VAGRANT_HOME "/d/.vagrant.d/"
You have to run it from path where setx app is located. Most common path is: C:\Windows\System32
Base boxes can be downloaded manually:
curl [URL] -o [FILENAME]
wget -O [FILENAME] [URL]
- create VM from existing box
- install additional software, configure
-
vagrant package
--output moj_pierwszy_box.box -
vagrant box add [NAME] [ADDRESS]
ADDRESS: Vendor/Name http://... file://... A local filename vagrant box add moj_pierwszy_box moj_pierwszy_box.box vagrant box list
- W katalogu projektu mozna go uzyc
vagrant init -m moj_pierwszy_box
Vagrant up
- https://atlas.hashicorp.com/boxes/search
- https://vagrantcloud.com - tool to discover and distribute vboxes
- http://www.vagrantbox.es
Most important boxes:
- Hashicorp: http://atlas.hashicorp.com/hasicorp
- Puppetlabs:
- Chef:
- Ubuntu:
VM user i haslo: vagrant
By default gest VM is configured as ssh deamon is avaliable on port 2222
vagrant ssh-config --informacje do polaczenia z ssh
--check the path to the host's private key
ssh -p 2222 [email protected]
vagrant ssh - authorized by RSA keys
~/.vagrant.d/insecure_private_key
/home/vagrant/.ssh/authorized_keys
To change RSA key pair used by the guest,generate a new pair keys
ssh-keygen -t rsa -C [email protected] -f johnny
#Host
cp johnny.pub project/
vagrant up
#Guest
cat /vagrant/johnny.pub > ~/.ssh/authorized_keys
#Host
in vagrantfile
config.ssh.private_key_path = "/path/to/the/keyjohnny/"
vagrant reload -- stop guest OS and boot it again
katalog w ktorym jest Vagrantfile jest wspoldzielony z VM, z poziomu VMa jest to katalog vagrant
cd /vagrant/
config.vm.synced_folder [HOST-PATH], [GUEST-PATH]
in WIndows
config.vm.synced_folder "c:\\dir\\on\\host\\windows", "/dir/on/guest"
Sync folders:
- shared folders
config.vm.synced.folder ".","/vagrant" [parameters]
-
rsync drawback
- one way synchronization copy files to cloud providers
config.vm.synced.folder ".","/vagrant", type "rsync", rsync__exclude: ".git/"
To monior change in host folder use rsync_auto set to true
- NFS,SMB
- NFS:
config.vm.network "private_network", type "dhcp"
config.vm.synced.folder ".","/vagrant", type "nfs" - nfs dziala tylko z private network.
Is not default avaliable on Windows host, Vagrant ignore type: nfs. We can change this by install vagrant-winnfsd plugin.
- SMB:
config.vm.synced.folder ".","/vagrant", type "smb"
To use SMB you need administrator privileges.
Vagrant will provision the virtual machine only once on the first run,
any subsequent provisioning must be executed with the --provisio
n flag
either vagrant up --provision
or vagrant reload --provisio
.
The provisioning will re-run also if you destroy the VM and rebuild it with vagrant destroy
and vagrant up
.
vagrant reload --provision
Vagrant.configure("2") do |config|
...
config.vm.provision "NAME OF PROVISIONER" ...
end
- shell,
- puppet,
- ansible,
- chef-solo
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise32"
config.vm.provision :shell, path: "bootstrap.sh"
end
vagrant up --provision - boot system and run provisioner even if were already applied
vagrant up --np-provision - turn off provisioner even if is first time vagrant up executed
vagrant up --provision-with x,y,z
vagrant provision --run only provision
vagrant reload --provision
By default Vagrant execute sell script as a root. Using privileged: false change this, Script execute as vagrant user. If root right are needed use sudo
config.vm.provision "shell", inline: "echo Hello"
config.vm.provision "shell", path: "manifests/puppet.sh"
config.vm.provision "shell", inline: <<-SHELL
sudo apt-get update
sudo apt-get install -y apache2
SHELL , privileged: false
config.vm.provision "shell" do |s|
s.path "provision/setup.sh"
end
Vagrant has two types of shell provisioning,
- inline - you can write shell commands in the Vagrantfile itself
- external - load and run a shell script from a file (relative to Vagrantfile) or even from a URL.
Installation process of MySQL will prompt you for the root password, but Vagrant needs to automate the installation and somehow fill in the password automatically.For this we need to install a tool called debconf-utils. we can use this tool to tell the MySQL installation process to stop prompting for a password and use the password from the command line instead. Then we can go ahead and install MySQL without getting the root password prompts.
provision/setup.sh (as root):
apt-get update
apt-get install debconf-utils -y > /dev/null
debconf-set-selections <<< "mysql-server mysql-server/root_password password 1234"
debconf-set-selections <<< "mysql-server mysql-server/root_password_again password 1234"
apt-get install mysql-server -y > /dev/null
```
Example (execute with privileged: false)
```
sudo apt-get update
sudo apt-get install -y apache2 php php-mbstring php-zip phpunit unzip libapache2-mod-php
debconf-set-selections <<< "mysql-server mysql-server/root_password admin"
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again admin"
sudo apt-get install mysql-server
sudo mysql start
mysql -u root -padmin < /vagrant/createUser.sql
curl -Ss https://getcomposer.org/installer | php
sudo mv composer.phar /usr/bin/composer
sudo chown -R vagrant:vagrant /var/www
composer global require laravel/installer
cd /var/www
composer create-project --prefer-dist laravel/laravel myProject
chmod -R 777 /var/www/myProject/storage
sudo sed -i 's/DocumentRoot.*/DocumentRoot \/var\/www\/myProject\/public/' /etc/apache2/site-avaliable/000-default.conf
sudo apachectl restart
sed -i '/mysql/{n;n;n;n;s/'\''DB_DATABASE'\'', '\''.*'\''/'\''DB_DATABASE'\'','\''myproject'\''/g}' /var/www/myProject/config/database.php
sed -i '/mysql/{n;n;n;n;s/'\''DB_USERNAME'\'', '\''.*'\''/'\''DB_USERNAME'\'','\''myproject'\''/g}' /var/www/myProject/config/database.php
sed -i '/mysql/{n;n;n;n;s/'\''DB_PASSWORD'\'', '\''.*'\''/'\''DB_PASSWORD'\'','\''mypassword'\''/g}' /var/www/myProject/config/database.php
```
createUser.sql:
```
CREATE DATABASE myproject;
CREATE USER 'myproject@localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILAGES ON myproject.* TO 'myproject@localhost';
```
FLUSH PRIVILEGES:
```
config.vm.provision "shell" do |s|
s.path "provision/setup.sh"
end
```
### Networking
Three way to communicate with VM
1. Port forwarding
```
config.vm.network :forwarded_port, guest: 80, host: 8080, host_ip: "127.0.0.1", auto_correct: true -- host_ip # forwardowanie portu tylko dla lokalnej maszyny/hosta, bez tego z dowolnej co moze byc niebezpieczne
- Private network You can communicate with multiple guest. Can be configured manually or by VirtualBox build-in DHCP server
config.vm.network "private_network", ip: "10.20.30.40"
config.vm.network "private_network", type: "dhcp"
All guest machinnes running on host can communicate withch each other because they share IP address space.IP are private the traffic from remote machines is not routed to or from the guests.When boot the machine with private network, Vagrant create a new network interface in the host's configuration.
ifconfig
The interface are not removed even you shout down VM's. To remove interface from the host:
VBoxManage hostonlyif remove vboxnet0
VBoxManage hostonlyif remove vboxnet1
!!! In Windows VBoxManage is not in the path, so we need full path
- Public network Guest with public network is avaliable for remote access.
config.vm.network "public_network"
config.vm.network "public_network", ip "192.168.0.17" bez DHCP
To remove the interfaces from the host, use the following commands:
# Host OS
$ VBoxManage hostonlyif remove vboxnet0
$ VBoxManage hostonlyif remove vboxnet1
$ VBoxManage hostonlyif remove vboxnet2
■ Note on Windows, VBoxManage.exe is not in the path, so you need to prepend the full path. here is an example:
# Host OS
$ /c/Program\ Files/Oracle/VirtualBox/VBoxManage.exe hostonlyif remove vboxnet0
PuTTYGen
przycisk Load %userprofile%/.vagrant.d/insecure_public_key
- ten klucz nie dziala bo VM podmienia na losowo wygenerowany
u mnie zmienione na D:\data\vagrant
<katalog_vm>/\.vagrant\machines\default\virtualbox\private_key
Vagrant 1.7+ default to using a different SSH key for each host.To revert to the earlier behavior of using the same SSH key
config.ssh.insert_key = false
vagrant ssh-config
vagrant global-status # lista maszyn wirtualnych zarzadzanych przez Vagranta z ich statusem
vagrant global-status --prune # czysci smieci z cache dodatkowo dluzej dziala
vagrant destroy <id VM-a> -
Vagrantfile:
VAGRANTFILE_API_VERSION = "2" -- okresla format pliku konfiguracyjnego - teraz jest 2 a w przyszlosci bedzie 3
git
global gitignore
- file *.box
- directory /.vagrant/
in project directory:
git init
echo .vagrant > .gitignore
echo ".box" >> .gitignore
git add -A
git commit -m "Initial version"
git tag -a v0.1.0 -m "Release 0.1.0"
git tag - check avaliable versions
git log --oneline --decorate - liprint history with tag list
git status
vagrant status
- running -
vagrant up
- poweroff -
vagrant halt
- saved -
vagrant suspend
- zamrozenievagrant resume
- odmrozenie - not created - in two situation befor
vagrant up
and aftervagrant destroy
- aborted
- Stage 1
- Download and instal the box in the system $AGRANT_HOME/.vagrant.d/boxes if not exist
- Directory to store downloaded boxes is the same as URI, but each slash(/) replaced with -VAGRANTSLASH-
- Stage 2
- Importing the base box into the project
- to directory ~/VirtualBox VMs/
- ls ~/VirtualBox\ VMs/
- Name of the direcory is name of project's directory with timestamps. Imported box is decompressed.
- Stage 3 - Booting the system
In project directory is .vagrant
directory.It's place where Vagrant stores ID and some other informationa
about the guest OS
v.gui = true
v.memory = 4096 Verify memory in Guest OS: free m or cat /proc/meminfo or sudo lshw
v.cpu = 2
config.vm.hostname = "abc"
config.vm.post_up_message = " The application is avaliable at http://xxx:8765"
config.ssh.forward_x11 = true - wyswietalnie okien aplikacji z VMa np Xming-iem
sudo apt-get install x11-apps -y
sudo apt-get install firefox -y
In windows you need in VM set DISPLAY variable
export DISPLAY="192.168.0.100:0.0"
xeyes
checksum veryfication in box-es
config.vm.box_download_checsum_type = "sha256"
config.vm.box_download_checsum = "01234567890"
config.vm.box = "http://..../apache-v1.0.0.box"
Calculating checsum in windows openssl dgst -sha256 filename.box
vagrant up --debug
Vagrant.configure("2") do |config|
config.vm.provision "shell", inline: "echo Hello"
config.vm.define "web" do |web|
web.vm.box = "chef/centos-6.5-i386"
end
config.vm.define "db" do |db|
db.vm.box = "ubuntu/trusty32"
end
end
vagrant ssh web or vagrant ssh db
vagrant up web or vagrant up db
private network can be used to make a private network between multiple machines and the host.
Specifing primary machine to use when specific machine is not specified in eg. vagrant up
config.vm.define "web", primary: true do |web|
vagrant reload -- restart VMa np. po zmianach w Vagrantfile
config.vm.define "web"
config.vm.define "db"
config.vm.define "db_follower", autostart: false
vagrant up start only web and db
vagrant up db_follower - manualy force start "db_follower" machine
Ctrl+V
zaznacza blok w Vim
Vagrantfile - informacje jak startowac VM - mozna odblokowac GUI wtedy bedzie widoczne okno VirtualBoxa przydane bo widac wiecej komunikatow o bledach
vagrant suspend # hibernate VM wykonujemy w katalogu gdzie Vagrant file aby bylo wiadomo o ktory VM chodzi
vagrant resume # odhibernovanie VM
vagrant halt # shutdown VM
vagrant destroy
vagrant -h --help
vagrant status - czy maszyna wirtualna jest uruchomina
vbox - is VM starting point
packer - narzedzie do tworzenia swoich box-ow
np hashicorp/precise32 - vbox provided by hashicorp contain 32-b ubuntu 12.04 that has Chef/Puppet preinstalled.
vagrant box add precise32 http://files.vagrantup.com/precise32.box - sciagnie box do lokalnego repozytorim box-ow
vagrant box -h info o opcjach zarzadzania swoimi prywatnymi boxami
vagrant box list
vagrant box repackage -- jesli chemy wyeksportowac na inny komputer vboxas
np. jesli nie mamy dostepu do internetu
vagrant package - packages a currently running VirtualBox environment into a re-usable box
np.
- init and up the existing box first
-
vagrant package --output vagrant_example.box --base NAME
# Instead of packaging a VirtualBox machine that Vagrant manages, this will package a VirtualBox machine that VirtualBox manages. NAME should be the name or UUID of the machine from the VirtualBox GUI.
- Veewee - narzedzie do budowy VMow od podstaw z instalek ISO
- Packer - jak wyzej bardziej dobra dokumentacja
- chef and puppet - tools for provisioning software packages and system configuration
vagrant init ubuntu/trusty64 --minimal
For Virtual Box provider, the logs are stored at ~/VirtualBox VMs/vagrant_box_name/Logs
- Make the Box as Small as possible,
sudo apt-get clean # remove APT cache
sudo dd if=/dev/zero of=/EMPTY bs=1M # “zero out” the drive (this is for Ubuntu)
sudo rm -f /EMPTY
cat /dev/null > ~/.bash_history && history -c && exit #clear the Bash History and exit the VM
- Repackage the VM into a New Vagrant Box
vagrant package --output mynew.box
- Add the Box into Your Vagrant Install
vagrant box add mynewbox mynew.box
- delete and remove the Vagrant file we built this box from.
vagrant destroy
rm Vagrantfile
- Initialize Your New Vagrant Box
vagrant init mynewbox
W powyzszy sposob stworzyłem boxa precise64java w ktorym zainstalowana jest oracle java 7 w ponizszy sposob:
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer
sudo apt-get install oracle-java7-set-default
install apache
echo "ServerName localhost" | sudo tee /etc/apache2/conf.d/fqdn
But on Ubuntu 14.04:
echo "ServerName localhost" | sudo tee /etc/apache2/conf-available/fqdn.conf
sudo a2enconf fqdn