顯示具有 Vagrant 標籤的文章。 顯示所有文章
顯示具有 Vagrant 標籤的文章。 顯示所有文章

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:~$

2011年10月30日 星期日

執行lucid32.box 使用 jRuby + vagrant在Windows XP

image

最近朋友有在Windows環境中使用vagrant的需求,
而筆者有時要用windows xp來使用vagrant,
故寫了本篇,便是介紹如何安裝vagrant及其環境。

安裝 Ruby
  • 實驗使用cygwin+ruby,但最後有相容性臭蟲問題而成殘念。
/usr/lib/ruby/gems/1.8/gems/virtualbox-0.9.2/lib/virtualbox/com/mscom_interface.rb:28:in `initialize': failed to create WIN32OLE object from `VirtualBox.VirtualBox' (WIN32OLERuntimeError)
    HRESULT error code:0x80004005
C:\Documents and Settings\vagrant>set PATH=C:\jruby-1.6.5\bin;%PATH%
C:\Documents and Settings\vagrant>gem install vagrant
Fetching: archive-tar-minitar-0.5.2.gem (100%)
Fetching: erubis-2.7.0.gem (100%)
Fetching: json-1.5.4-java.gem (100%)
Fetching: net-ssh-2.1.4.gem (100%)
Fetching: net-scp-1.0.4.gem (100%)
Fetching: i18n-0.6.0.gem (100%)
Fetching: thor-0.14.6.gem (100%)
Fetching: ffi-1.0.10-java.gem (100%)
Fetching: virtualbox-0.9.2.gem (100%)
Fetching: vagrant-0.8.7.gem (100%)
Successfully installed archive-tar-minitar-0.5.2
Successfully installed erubis-2.7.0
Successfully installed json-1.5.4-java
Successfully installed net-ssh-2.1.4
Successfully installed net-scp-1.0.4
Successfully installed i18n-0.6.0
Successfully installed thor-0.14.6
Successfully installed ffi-1.0.10-java
Successfully installed virtualbox-0.9.2
Successfully installed vagrant-0.8.7
10 gems installed
C:\Documents and Settings\vagrant>cd SRC
C:\Documents and Settings\vagrant\SRC>cd vagrantTest
C:\Documents and Settings\vagrant\SRC\vagrantTest>vagrant up
JRuby limited openssl loaded. http://jruby.org/openssl
gem install jruby-openssl for full support.
[default] Importing base box 'lucid32'...
[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.0
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] -- web: 80 => 8080 (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] Forcing shutdown of VM...
[default] Destroying VM and associated drives...
[default] Destroying unused networking interface...
LoadError: OpenSSL::BN requires the jruby-openssl gem
出現OpenSSL錯誤,在安裝下表的jruby-openssl後,此問題已排除。
C:\Documents and Settings\vagrant\SRC\vagrantTest>gem install jruby-openssl
Fetching: bouncy-castle-java-1.5.0146.1.gem (100%)
Fetching: jruby-openssl-0.7.4.gem (100%)
Successfully installed bouncy-castle-java-1.5.0146.1
Successfully installed jruby-openssl-0.7.4
2 gems installed

啟動lucid32版本
此版本由http://files.vagrantup.com/lucid32.box下載:
(使用vagrant add lucid32 下載,
便已發生無法正在驗證的問題,
筆者直接將box檔案用tar xvf lucid32.box解壓縮至.vagrant.d\boxes\lucid32\目錄中)
C:\Documents and Settings\vagrant\SRC\vagrantTest>vagrant up
[default] Importing base box 'lucid32'...
[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.0
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] -- web: 80 => 8080 (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] Failed to connect to VM!
Failed to connect to VM via SSH. Please verify the VM successfully booted
by looking at the VirtualBox GUI.
由最後發現啟動lucid32已成功,但SSH連線失敗,對照本文一開始的左圖(vagrantTest_1319904659, Ubuntu)
接著使用vagrant ssh 命令查看相訊息。
C:\Documents and Settings\vagrant\SRC\vagrantTest>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
(註:在Mac OS X 中,因terminal環境有ssh 指令,所以可直接啟動ssh連線至虛擬機器)

使用PuTTY 


啟動PuTTY
(筆者愛用Pietty,由PuTTY改版以支援中文)

Auto-login username
在Connection->Data中,Auto-login username一欄,輸入使用者名稱 vagrant,如下:
image

輸入私密金鑰
在Connection->SSH->Auth中,Private key file for authentication一欄,輸入私密金鑰ppk所在路徑及檔名,如下:
C:\jruby-1.6.5\lib\ruby\gems\1.8\gems\vagrant-0.8.7\keys\vagrant.ppk
image

主機名稱及Port
使用主機名稱localhost 及port 2222 來連接,卻無法使用ssh 來連接 lucid32,有未知的問題存在,網上此問題討論。
image
筆者使用另一個朋友所製作gold.box, 使用主機名稱localhost 及port 2200 來連接成功,如下圖:
image image
如欲按儲存以保留設定,輸入 Saved sessions 名稱: <自訂名>。

結論
就筆者使用vagrant在windows來說,設定環境很多小問題,需要花時間去調整。整體而言,在Mac OS X 環境,設定vagrant就比windows 容易許多。
記得使用完要關機,在啟動目錄中,輸入vagrant halt,便會關閉虛擬機。一點小經驗,望有所助益。
如有網友成功直接在cygwin 中成功設定vagrant 環境,請不吝分享。

附註
  • 請先行安裝VirtualBox
  • 為求方便使用,可將jruby安裝路徑(如:C:\jruby-1.6.5\bin)加入系統環境變數
  • 請確定有將C:\Program Files\Oracle\VirtualBox 路徑加入至系統環境變數中。(與cygwin 環境整合使用)
  • 若有使用Python,也可將環境變數加入,以便在Windows命令提示字元終端中使用。
setup_globalvarsetup_globalvar_selectpathsetup_globalvar_edit  


  • 成功關閉gold vm 的訊息


C:\Documents and Settings\vagrant\SRC\gold>vagrant halt
[default] Attempting graceful shutdown of linux...

C:\Documents and Settings\vagrant\SRC\gold>