Vagrantは、仮想開発環境の作成と構成に使用されるソフトウェアツールです。これは、VirtualBoxやVMwareのような他の仮想化ソフトウェアよりもラッパーとして機能します。バゲージ自体はコマンドラインユーティリティです。
Vagrantfile
ボックスは、Rubyで書かれた特別なVagrantfile
設定を使用して設定されますが、bash、ChefまたはPuppetで書かれた追加の設定スクリプトを含むことがあります。チーム内の開発者は、共通のソースからVagrant設定ファイルをダウンロードし、同じ開発環境をローカルで再作成することができます。
バージョン | ダウンロード | 変更ログ | 発売日 |
---|---|---|---|
1.8.5 | ダウンロード | 変更ログ | 2016-07-18 |
1.8.4 | ダウンロード | 変更ログ | 2016年6月13日 |
1.8.3 | ダウンロード | 変更ログ | 2016年6月10日 |
1.8.1 | ダウンロード | 変更ログ | 2015-12-24 |
1.8.0 | ダウンロード | 変更ログ | 2015-12-21 |
1.7.4 | ダウンロード | 変更ログ | 2015-07-17 |
1.7.3 | ダウンロード | 変更ログ | 2015-07-10 |
1.7.2 | ダウンロード | 変更ログ | 2015-01-06 |
1.7.1 | ダウンロード | 変更ログ | 2014-12-12 |
1.7.0 | ダウンロード | 変更ログ | 2014年12月10日〜10日 |
1.6.5 | ダウンロード | 変更ログ | 2014-09-04 |
1.6.4 | ダウンロード | 変更ログ | 2014-09-02 |
1.6.3 | ダウンロード | 変更ログ | 2014-05-29 |
1.6.2 | ダウンロード | 変更ログ | 2014-05-12 |
1.6.1 | ダウンロード | 変更ログ | 2014-05-08 |
1.6.0 | ダウンロード | 変更ログ | 2014-05-06 |
ホストマシン(Windows / Linux / OSX)で、空のdir my_project
作成します。
次のようにVagrantfile
という名前のファイルを作成します。
Vagrant.configure("2") do |config|
config.vm.box = "gbarbieru/xenial" #An Ubuntu 16.04 based image
config.vm.hostname = "my_project"
config.vm.network :private_network, ip: "172.16.123.10"
end
あなたのマシンを実行してください:
host$ vagrant up
host$ vagrant ssh
virtual$ cd /vagrant
完了!
ノート:
putty
からssh
したいです。これは、ホスト名127.0.0.1
とポート2222
ssh-ingすることで可能です。ユーザー名はvagrant
、パスワードはvagrant
です。 WindowsプラットフォームでVagrantを使用するには、まず仮想化ソフトウェアとsshコマンドラインツールをインストールする必要があります。この例では、フリーウェアのVirtualBoxとCygwinを使用します。
公式のダウンロードページから最新のVirtualBoxバージョンをダウンロードし 、ダウンロードしたファイルを実行します。インストール中に一時的にネットワーク接続が失われることに注意してください。
また、Oracleに追加のドライバをインストールさせる必要があります。
cygwin.comから入手して、「パッケージの選択」ページが表示されるまでセットアップを実行してください。
また、Windows PATH環境変数にC:\cygwin64\bin
フォルダを追加する必要があります。あなたがどこからでもssh
コマンドを呼び出すことができるように。
vagrantup.comからVagrantをダウンロードし、セットアップガイドに従ってインストールしてください。その後、コンピュータを再起動する必要があります。
仮想化技術VTx / VTdを有効にする必要があります。 (あなたは、セキュリティオプションのBIOSでそれを見つけることができます)
正しくインストールされたすべてがハードドライブのどこかに空のフォルダを作成するかどうかをテストするには、コマンドラインを開いて作成したフォルダに移動します。
cd c:/path/to/your/folder
入力してください
vagrant init hashicorp/precise64
vagrant up
これにより、Ubuntu 12.04 LTS 64ビットを実行しているVirtualBoxが作成され起動されます。32ビット版をvagrant init hashicorp/precise32
使用します。別の箱が必要な場合は、hashicorpのウェブサイトで詳細を見つけることができます。
また、 Vagrantfile
vagrant init
コマンドは、現在のフォルダにVagrantfile
設定ファイルを作成します。今すぐあなたを他の人に送ることができます。その人がvagrant up
電話を呼び出すと、同じ正確な仮想マシンが自分のPCに作成されます。
これらの2つのコマンドが正常に実行された後にsshをテストするには、同じフォルダで次のコマンドを実行します。
vagrant ssh
すべてがうまくいけば、 vagrant
ユーザーとしてログインしている仮想マシンの内部に入ります。
あなたはVMを停止することができます
vagrant halt
削除する
vagrant destroy
インストール方法については、 vagrantbox.esページを参照してください。
この例では、Vagrantを使用してカスタムLAMPプロジェクト開発環境を作成します。
まず、 Virtual BoxとVagrantをインストールする必要があります。
次に、ホームディレクトリにvagrant
フォルダを作成し、端末を開き、現在のディレクトリを新しいvagrant
ディレクトリに変更します。ここで、 vagrant init
実行しvagrant init
。 Vagrantfile
ファイルは内部に作成され、次のようになります。
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
config.vm.box = "base"
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
#
# View the documentation for the provider you are using for more
# information on available options.
# Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
# such as FTP and Heroku are also available. See the documentation at
# https://docs.vagrantup.com/v2/push/atlas.html for more information.
# config.push.define "atlas" do |push|
# push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
# end
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# sudo apt-get update
# sudo apt-get install -y apache2
# SHELL
end
上記のファイルを編集する次の行の追加またはコメント解除:
config.vm.box = "hashicorp/precise64"
config.vm.network :forwarded_port, guest: 80, host: 8080
config.vm.network :private_network, ip: "192.168.33.10"
config.ssh.forward_agent = true
if Vagrant::Util::Platform.windows?
config.vm.synced_folder "./", "/vagrant"
else
config.vm.synced_folder "./", "/vagrant", type: "nfs"
end
# https://stefanwrobel.com/how-to-make-vagrant-performance-not-suck
config.vm.provider "virtualbox" do |v|
host = RbConfig::CONFIG['host_os']
# Give VM 1/4 system memory & access to all cpu cores on the host
if host =~ /darwin/
cpus = `sysctl -n hw.ncpu`.to_i
# sysctl returns Bytes and we need to convert to MB
mem = `sysctl -n hw.memsize`.to_i / 1024 / 1024 / 4
elsif host =~ /linux/
cpus = `nproc`.to_i
# meminfo shows KB and we need to convert to MB
mem = `grep 'MemTotal' /proc/meminfo | sed -e 's/MemTotal://' -e 's/ kB//'`.to_i / 1024 / 4
else # sorry Windows folks, I can't help you
cpus = 2
mem = 1024
end
v.customize ["modifyvm", :id, "--memory", mem]
v.customize ["modifyvm", :id, "--cpus", cpus]
end
目的のドメインを迷惑メールVMにリダイレクトするためのhosts
ファイルを編集します。 Linuxの場合/etc/hosts
、Windows C:\Windows\System32\Drivers\etc\hosts
場合は/etc/hosts
になりC:\Windows\System32\Drivers\etc\hosts
。この行を追加します:
192.168.33.10 vagrantServer.com
もちろん、 vagrantServer.com
を任意の名前に置き換えることができます。
これでbootstrap.sh
ファイルを作成します( vagrant
ディレクトリ内)。このスクリプトは、VMが最初から生成されるたびに実行されます。コメントを注意深く読んでください:
#!/usr/bin/env bash
# .ssh/authorized_keys (you will need to create a `.ssh` directory inside the `vagrant` one and add a file named `authorized_keys` with the public keys of the users who have access to the repository and may use this environment).
# You also will have to grant access to those public keys from the Github account, Bitbucket, or whatever you're using.
cat /vagrant/config/authorized_keys >> /home/vagrant/.ssh/authorized_keys
if ! [ -d /root/.ssh ]; then
mkdir /root/.ssh
fi
cp /vagrant/config/authorized_keys /root/.ssh
chmod 600 /root/.ssh/authorized_keys
# Install packages
apt-get update
apt-get install -y python-software-properties
add-apt-repository ppa:ondrej/php5 -y
apt-get update
apt-get install -y curl nano apache2 php5 php5-mysql php5-curl php5-gd php5-intl php5-mcrypt git
# Apache2 run with user vagrant
APACHEUSR=`grep -c 'APACHE_RUN_USER=www-data' /etc/apache2/envvars`
APACHEGRP=`grep -c 'APACHE_RUN_GROUP=www-data' /etc/apache2/envvars`
if [ APACHEUSR ]; then
sed -i 's/APACHE_RUN_USER=www-data/APACHE_RUN_USER=vagrant/' /etc/apache2/envvars
fi
if [ APACHEGRP ]; then
sed -i 's/APACHE_RUN_GROUP=www-data/APACHE_RUN_GROUP=vagrant/' /etc/apache2/envvars
fi
sudo chown -R vagrant:www-data /var/lock/apache2
# Set user/password to mysql previously to installation
# Replace rootMysqlPassword with your desired MySQL root password
debconf-set-selections <<< 'mysql-server mysql-server/root_password password rootMysqlPassword'
debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password rootMysqlPassword'
# Install mysql
apt-get update
apt-get install -y mysql-server mysql-client
# Link /vagrant (sync_folder) to apache directory (/var/www)
if ! [ -L /var/www ]; then
rm -rf /var/www
ln -fs /vagrant /var/www
fi
# Install composer
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
# Composer example. Uncoment to istall phpunit
#composer global require "phpunit/phpunit=3.7.*" --prefer-source
# Create mysql database (replace "vagrantDB" with any desired database name, and "rootMysqlPassword" with the password set above)
mysql -u root -prootMysqlPassword -v -e "CREATE USER 'developer'@'%' IDENTIFIED BY 'dev';
CREATE SCHEMA vagrantDB;
GRANT ALL ON vagrantDB TO 'developer'@'%';"
# Uncomment to set default database fixtures based on `/vagrant/config/vagrantDBFixtures.sql` file.
#mysql -u root -prootMysqlPassword -v vagrantDB < /vagrant/config/vagrantDBFixtures.sql
###################################################
################ THIS IS OPTIONAL #################
###################################################
# Install nodejs
curl -sL https://deb.nodesource.com/setup | sudo bash -
apt-get install -y nodejs
# Install npm packages
npm install -g npm
npm install -g bower
npm install -g forever
npm install -g gulp
# Set accepted license before install java
echo debconf shared/accepted-oracle-license-v1-1 select true | sudo debconf-set-selections
echo debconf shared/accepted-oracle-license-v1-1 seen true | sudo debconf-set-selections
# Install java7
apt-get install -y oracle-java7-installer oracle-java7-set-default
###################################################
################### END OPTIONAL ##################
###################################################
# Generate ssh key without passphrase
ssh-keygen -f /root/.ssh/id_rsa -t rsa -N ""
# Add bitbucket and github to known hosts
touch /root/.ssh/known_hosts
ssh-keyscan -H bitbucket.org >> /root/.ssh/known_hosts
ssh-keyscan -H github.com >> /root/.ssh/known_hosts
# Source: https://gist.github.com/winhamwr/7122947
# Sleep until we can successfully SSH into Bitbucket.
# Uses doublinng backoff while waiting
# with_backoff() adapted from http://stackoverflow.com/a/8351489
# Retries a command a configurable number of times with backoff.
#
# The retry count is given by ATTEMPTS (default 5), the initial backoff
# timeout is given by TIMEOUT in seconds (default 1.)
#
# Successive backoffs double the timeout.
#generatedKey="`cat /root/.ssh/id_rsa.pub`"
echo -n "Generate a SSH key (https://help.github.com/articles/generating-ssh-keys/)
and add it to your Bitbucket account (Profile -> SHH keys) to continue. "
with_backoff() {
local max_attempts=${ATTEMPTS-5}
local timeout=${TIMEOUT-1}
local attempt=0
local exitCode=0
while [ $attempt -lt $max_attempts ]
do
set +e
"$@"
exitCode=$?
set -e
if [ $exitCode -eq 0 ]
then
break
fi
echo "Failure! Retrying in $timeout.." 1>&2
sleep $timeout
attempt=$(( attempt + 1 ))
timeout=$(( timeout * 2 ))
done
if [ $exitCode -ne 0 ]
then
echo "You've failed me for the last time! ($@)" 1>&2
fi
return $exitCode
}
ATTEMPTS=${ATTEMPTS:-5}
export ATTEMPTS
with_backoff ssh -T git@bitbucket.org;
# Clone repositories (replace "yourProjectName" and "yourProjectRepository" with your project data)
cd /var/www
rm -rf yourProjectName/
git clone yourProjectRepository
# Add server names to /etc/hosts (replace "vagrantServer.com" with the domain set above)
echo -e '\n127.0.0.1 vagrantServer.com' >> /etc/hosts
# Enable apache modes
a2enmod rewrite
# Copy sites-available file (you need to add the Apache configuration file for the desired domain in `config/sites-available`. Replace "vagrantServer.conf" with the desired name)
cp /vagrant/config/sites-available/vagrantServer.conf /etc/apache2/sites-available/
# Remove html from document root
sed -i 's/\/var\/www\/html/\/var\/www/g' /etc/apache2/sites-available/*
service apache2 restart
# Enable sites (replace "vagrantServer.conf" with the above file name)
a2ensite vagrantServer.conf
# Install ruby, compass and sass (Optional)
apt-get install -y rubygems
gem install compass
npm install -g sass
# Pull the repo
cd /var/www/yourProjectName
git pull --all
上記のファイルをチェックして保存したら、端末にもう一度アクセスし、カレントディレクトリを以前作成したvagrant
に変更して、 vagrant
を入力vagrant up
ます。 VMが作成され、ブーストラップファイルがVMから実行されるので、必要なものはすべてコピー/インストールされます。終了したら、ブラウザを開いてvagrantServer.com
(または名前を付けた名前)に移動し、 vagrantServer.com
VMから配信されたコンテンツを表示する必要があります。
またを通して、あなたのプロジェクトファイルを編集することができますvagrant/yourProjectName
ディレクトリと内のすべてのファイルvagrant
のディレクトリは、あなたのホストと浮浪者VM間で共有と同期されます。
すべてのフォルダを両方向に同期させるには、これをあなたのVagrantfile
挿入します
config.vm.synced_folder "my-project1", "/home/vagrant/my-project1"
すべてのフォルダを両方向に同期させるには、これをあなたのVagrantファイルに挿入します:
config.vm.synced_folder "my-project1", "/home/vagrant/my-project1", type: "rsync",
:rsync__exclude => ['my-project1/mini_project2/target,my-project1/mini_project2/target,my-project1/mini_project3/target']
すべてのターゲットフォルダは同期から除外されます。
同期はのみで発生vagrant up
と上vagrant reload
。
ホストからゲストへのすべての変更を同期させるには、次のものを使用する必要があります。
vagrant rsync-auto
Amazon AWSにアップロードするローカルの迷惑メールボックスがあります。まず、 .box
ファイルを作成する必要があります。
vagrant package --base my-virtual-machine
この手順は、画像のサイズに応じてしばらく時間がかかります。次に、 .box
ファイルから.vmdk
イメージを取得する必要があります。
gunzip -S .box package.box
tar xf package
この手順を実行すると、4つの新しいファイル( package
、 box-disk1.vmdk
、 Vagrantfile
、およびbox.ovf
ます。今、AWSにアップロードする。 AWSアカウントを持っていると仮定して、 S3のバケットを作成してAmazonのサーバーにイメージを保存します。あなたは、次のステップのためにAmazonのEC2 CLIが必要になります (私が知る限りコンソールでこれを行うことはできません):
ec2-import-instance box-disk1_1.vmdk -f VMDK -t t2.micro -a x86_64 -b <S3-bucket-name> -o $AWS_ACCESS_KEY -w $AWS_SECRET_KEY -p Linux
このコマンドのこの結果はしばらく時間がかかります。大きな画像ファイルをS3にアップロードしていますが、コマンド自体がより早く返されます。 ec2-describe-conversion-tasks
コマンドを使用すると、インポートの進行状況を確認できます。
それが完了すると、AWSコンソールで実行中のボックスのインスタンスが表示されます。ただし、パブリックIPアドレスがなく、関連付けられた.pem
ファイルがないため、アクセスできないことがあります。したがって、次のステップはインスタンスからAMIを作成することです。 AMIを作成するには、インスタンスを停止する(終了しないでください!)インスタンスを右クリックし、「 Image
- >「 Create Image
。これにはしばらく時間がかかります。進行状況は、コンソールのAMIビューで確認できます。終了したら、 .pem
キーファイルを添付したAMIを使用してインスタンスを起動すると、 ssh
でログインできssh
。