2011年11月3日 星期四

Vagrant+virtualbox (2): 實戰封裝自制package.box

image

續上篇執行lucid32.box 使用 jRuby + vagrant在Windows XP
Vagrantfile 範例
Vagrant::Config.run do |config|
  config.vm.box = "gold"
  # config.vm.forward_port("web", 80, 8080)
end


添加所需應用程式及設定,如安裝django套件:
sudo easy_install pip
sudo pip install django
使用此檔案執行 vagrant up後,使用 putty (參考執行lucid32.box 使用 jRuby + vagrant在Windows XP


若要附加自己的說明檔案,請使用—include
vagrant package --vagrantfile .vagrant --include README.txt


若要附加自己的說明檔案,請使用--
vagrant package --vagrantfile .vagrant --


執行結果
C:\Documents and Settings\vagrant\SRC\vagrant\gold>vagrant package --vagrantfile .vagrant --
[default] Attempting graceful shutdown of linux...
[default] Clearing any previously set forwarded ports...
[default] Cleaning previously set shared folders...
[default] Creating temporary directory for export...
[default] Exporting VM...
Progress: 0%Progress: 1%Progress: 4%Progress: 9%Progress: 10%Progress: 14%Progre
ss: 19%Progress: 24%Progress: 28%Progress: 34%Progress: 39%Progress: 42%Progress
: 44%Progress: 49%Progress: 54%Progress: 69%
[default] Compressing package to: C:/Documents and Settings/vagrant/SRC/vagrant/gold/package.box[default] Packaging additional file: .vagrant


加入本機檔案package.box 為mygold box
vagrant box add mygold package.box


執行結果
C:\Documents and Settings\vagrant\SRC\vagrant\gold>vagrant box add mygold package.box[vagrant] Downloading with Vagrant::Downloaders::File...
[vagrant] Copying box to temporary location...
[vagrant] Extracting box...
[vagrant] Verifying box...
[vagrant] Cleaning up downloaded box...

在新增之後,看看目前有的boxes:
C:\Documents and Settings\vagrant\SRC\vagrant\mygold>vagrant box list
gold
lucid32
mygold


修改新的Vagrantfile 來使用 mygold
Vagrant::Config.run do |config|
  config.vm.box = "mygold"
  # config.vm.forward_port("web", 80, 8080)
end

執行vagrant up 啟動
C:\Documents and Settings\vagrant\SRC\vagrant\mygold>vagrant up
There is a syntax error in the following Vagrantfile. The syntax error
message is reproduced below for convenience:
C:/Documents and Settings/vagrant/.vagrant.d/boxes/mygold/include/_Vagrantfile:1: syntax error, unexpected ':'
{"active":{"default":"f5a2c996-a70b-4c2c-8928-a85127a26863"}}

發生錯誤,移除include/_Vagrantfile ,再執行一次,啟動成功。
C:\Documents and Settings\vagrant\SRC\vagrant\mygold>vagrant up
[default] Importing base box 'mygold'...
[default] The guest additions on this VM do not match the install version of
VirtualBox! This may cause things such as forwarded ports, shared
folders, and more to not work properly. If any of those things fail on
this machine, please update the guest additions and repackage the
box.
Guest Additions Version: 4.1.2
VirtualBox Version: 4.1.4
[default] Matching MAC address for NAT networking...
[default] Clearing any previously set forwarded ports...
[default] Forwarding ports...
[default] -- ssh: 22 => 2222 (adapter 1)
[default] Creating shared folders metadata...
[default] Running any VM customizations...
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Mounting shared folders...
[default] -- v-root: /vagrant
C:\Documents and Settings\vagrant\SRC\vagrant\mygold>vagrant ssh
`vagrant ssh` isn't available on the Windows platform. The
vagrant.ppk file for use with Putty is available at:
  C:/jruby-1.6.5/lib/ruby/gems/1.8/gems/vagrant-0.8.7/keys/vagrant.ppk
To use this create a new Putty session for `vagrant@localhost`
on port `2222`, in the Connection>SSH>Auth
configuration section navigate to the vagrant.ppk file,
select it, save the session for later use, and connect.
For a more detailed guide please consult:
http://vagrantup.com/docs/getting-started/setup/windows.html
C:\Documents and Settings\vagrant\SRC\vagrant\mygold>

使用 putty連入,檢查之前在gold box所產生的測試用檔案[my.box],OK。
image


停止啟動mygold box

vagrant halt


刪除啟動後的mygold box
C:\Documents and Settings\vagrant\SRC\vagrant\mygold>vagrant destroy
[default] Forcing shutdown of VM...
[default] Destroying VM and associated drives...


結論
利用封裝已安裝及設定好的軟體的Box,可大大減少啟動後所需的安裝時間。
且可使用不同版本的gold box 來進行軟體自動化測試,並方便管理。


附註
多人共用mygold.box
可將編寫腳本檔案 mygold_update.bat 中加入如下設定:
@echo off
REM 刪除已存在的mygold_xxxx, 視情況決定是否執行destory, 或由使用者自行決定
vagrant destroy
REM 移除舊的mygold
vagrant box remove mygold
REM 加入新的mygold
vagrant box add mygold http://yourwebserver:port/mygold.box
註:請記得將所產生的package.box檔案更改並放到自行架設的web server 上。

可與ssh 搭配使用
vagrant ssh 等於
ssh -i <vagrant private key> vagrant@localhost –p <vagrant port>
(ex: ssh -i /cygdrive/c/jruby-1.6.5/lib/ruby/gems/1.8/gems/vagrant-0.8.7/keys/vagrant vagrant@localhost -p 2222)

下列為使用cygwin + ssh + vagrant key
$ ssh -i /cygdrive/c/jruby-1.6.5/lib/ruby/gems/1.8/gems/vagrant-0.8.7/keys/vagrant vagrant@localhost -p 2222
Could not create directory '/home/vagrant/.ssh'.
The authenticity of host '[localhost]:2222 ([127.0.0.1]:2222)' can't be established.
ECDSA key fingerprint is 36:28:64:97:6c:a4:dd:2a:4e:1a:0e:db:3a:db:d4:66.
Are you sure you want to continue connecting (yes/no)? yes
Failed to add the host to the list of known hosts (/home/vagrant/.ssh/known_hosts).
Welcome to Ubuntu 11.04 (GNU/Linux 2.6.38-8-server x86_64)
* Documentation:  http://www.ubuntu.com/server/doc
  System information as of Thu Nov  3 01:22:58 CST 2011
  System load:  0.0               Processes:           71
  Usage of /:   3.1% of 38.15GB   Users logged in:     0
  Memory usage: 6%                IP address for eth0: 10.0.2.15
  Swap usage:   0%
  Graph this data and manage this system at https://landscape.canonical.com/
New release 'oneiric' available.
Run 'do-release-upgrade' to upgrade to it.
Last login: Thu Nov  3 00:49:19 2011 from 10.0.2.2
vagrant@vagrant-ubuntu-natty:~$

沒有留言: