2014年3月2日 星期日

解放開發環境建置之原罪 - Packer / Brief Introduction of Packer

Packer - 幫您打包好不同虛擬機器 image 的好幫手!


什麼是 Packer ?

近年虛擬化技術隨著雲端技術大鳴大放,程式開發者也受益於虛擬化技術,使得原本的開發環境能夠透過虛擬機器建立標準化開發環境並能夠藉由虛擬機器 image 相互交流,有Packer 這套工具可幫助使用者快速且大量的製作運用在不同平台上(Virtualbox, VMWare, Amazon EC2, DigitalOcean, Docker, Google Compute Engine, Openstack, QEMU)的虛擬機器 image。只要在設定擋上寫好設定,接下來的安裝步驟通通可以交給 Packer 來幫你處理了。是不是很方便呢!


為什麼要用 Packer ?

大家一定有過如此的經驗,在開發專案時,我們可能要在不同的環境中進行測試。縱然有虛擬化技術幫我們省掉不少麻煩。但不同的環境在製作的過程中還是會有許多瑣碎事。比如說安裝作業系統時必須手動輸入許多設定(語系、網路、磁碟分割、使用者帳號等)。這些過程不僅無趣且耗費許多時間,而透過 Packer (救星),這些瑣碎事都能解決!


Packer 與 Vagrant 有何分別?

這裡假設大家已了解何為 Vagrant。若不知道,請看「解放開發環境建置之原罪 - Vargrant」介紹。根據同時是 Packer 與 Vagrant 開發者的 Mitchell Hashimoto:
First off, Packer does not require or even encourage Vagrant usage. Packer stands on its own as a useful tool for its own purposes: namely, building machine images for whatever environment you require. 
However, Packer is very useful alongside Vagrant, if you want. Packer can create Vagrant boxes. So if you wanted Vagrant boxes, then Packer can do this for you. This is the ONLY way Packer helps you use Vagrant. It does not REPLACE any part of Vagrant.
從 Mitch 自述中我們了解到 Packer 主要目的是建立不同平台所需的虛擬機器 image,不僅如此,它也可以建立 Vagrant 所使用的 box。對於 Vagrant 使用者來說,這也是 Packer 唯一且幫助最大的地方。簡言之,兩者不重複,也不衝突,可發揮 1 + 1 > 2 的綜效。

捲起袖子,來實作吧!

本文中範例運行環境為 Debian。請大家到 Packer 官方網站下載(連結) packer 吧!載完後解壓縮,可發現裡面是已經 compile 好的執行檔。接著請輸入:./packer
$ ./packer
usage: packer [--version] [--help]  []

Available commands are:
build       build image(s) from template
fix         fixes templates from old versions of packer
inspect     see components of a template
validate    check that a template is valid

Globally recognized options:
-machine-readable    Machine-readable output format.
看到這基本上我們已經安心一大半了,代表我們可以開始玩 Packer 了!

如前述,Packer 可以自動幫我們解決作業系統安裝過程中的所有瑣碎設定與操作。這真是造福了不少工程師啊!這一切的核心就在一個設定檔中 config.json。(註:Packer 吃的設定檔是以 json 為格式。因此,設定檔如何命名都隨使用者自訂。只要是 *.json 就可以了) 以下我們就來看一個可以運行的 config.json 包含了哪些設定。
{
    "builders": [{
        "vm_name": "packer-test",
        "type": "virtualbox-iso",
        "boot_command": [
            "<esc><wait>",
            "auto url=http://192.168.56.1/preseed.cfg",
            "<enter><wait>"
     ],
        "guest_os_type": "Debian_64",
        "ssh_username": "vagrant",
        "ssh_password": "vagrant",
        "shutdown_command": "echo vagrant|sudo -S /sbin/shutdown -hP now",
        "iso_checksum": "db79d463072da26b91c14e08b5a77a77bec9476ad1e5b0d2241228e4d59233f12c74477e77d427e407e1f45da4d2414c76367554352f57480fc7c00dde164028",
        "iso_checksum_type": "sha512",
        "iso_urls": [
            "http://ftp.jaist.ac.jp/debian-cd/7.4.0/amd64/iso-cd/debian-7.4.0-amd64-netinst.iso",
            "http://cdimage.debian.org/debian-cd/7.4.0/amd64/iso-cd/debian-7.4.0-amd64-netinst.iso"
     ]
    }]
}


細講 config.json

* builders : 在 builders 中可以同時設定許多台虛擬機器安裝的細節。本文為求精簡易懂,因此只專注在單一虛擬機器設定上。

* vm_name : 設定虛擬機器的名稱。

* type : 設定虛擬機器運作的平台。選項有很多,可依照需求選擇不同平台,不同平台的設定值請參考官方手冊。本文中,我們挑選 Virtualbox 為我們運行虛擬機器的平台,因此我們的值就填上 virtualbox-iso。

* boot_command : 在虛擬機器初次啟動後,準備進入作業系統安裝時,我們可以利用 boot_command 輸入一些我們想輸入的指令。

* 首先是 <esc><wait>。由於本文挑選 Debian 為安裝在虛擬機器上之作業系統,因此在出現安裝選單 (INSTALL MENU) 後,按 ESC 鍵,並等待<wait>一秒鐘。

* 接著是 auto url=http://192.168.56.1/preseed.cfg;,此行指令將會自動被輸入在畫面中,意思是接下來自動安裝所需要的細節都會參考 192.168.56.1 (需架設 Web server 在 80 port) 上 preseed.cfg 這個檔案。至於 192.168.56.1 這個 IP Address 是隨手打上的,使用者要自行找到機器並放上自己設定的 preseed.cfg 才會正常運作。如果說 config.json 是 Packer 的核心,那 preseed.cfg 就是核心中的核心。preseed.cfg 的設定細節可參考官方文件自行依需求調整。

* 最後是 <enter><wait>,也就是按 ENTER 的意思。之後所有的安裝,都會參考 preseed.cfg 中的設定而自動進行。

* guest_os_type : 設定被安裝在虛擬機器上的作業系統類型。若不知道可以輸入 VBoxManage list ostypes 指令來查看有哪些類型可選。在此,我們選的是 Debian_64

* ssh_username, ssh_password : 作業系統安裝完成後,進行 SSH 連線的使用者帳號及密碼設定。

* shutdown_command : 關機指令,作業系統安裝完成後關機的意思。

* iso_( checksum | checksum_type | urls ): 分別指定要從何處下載作業系統安裝檔、checksum 類型以及 checksum hash 值。


注入靈魂的時刻來臨!

設定好一切後,我們只要輸入指令 ./packer build config.json ,packer 就會依照我們的設定,幫我們自動建立虛擬機器了!
virtualbox-iso output will be in this color.

==> virtualbox-iso: Downloading or copying Guest additions checksums
virtualbox-iso: Downloading or copying: http://download.virtualbox.org/virtualbox/4.3.6/SHA256SUMS
==> virtualbox-iso: Downloading or copying Guest additions
virtualbox-iso: Downloading or copying: http://download.virtualbox.org/virtualbox/4.3.6/VBoxGuestAdditions_4.3.6.iso
==> virtualbox-iso: Downloading or copying ISO
virtualbox-iso: Downloading or copying: http://ftp.jaist.ac.jp/debian-cd/7.4.0/amd64/iso-cd/debian-7.4.0-amd64-netinst.iso
==> virtualbox-iso: Creating virtual machine...
==> virtualbox-iso: Creating hard drive...
==> virtualbox-iso: Creating forwarded port mapping for SSH (host port 4391)
==> virtualbox-iso: Starting the virtual machine...
==> virtualbox-iso: Waiting 10s for boot...
==> virtualbox-iso: Typing the boot command...
==> virtualbox-iso: Waiting for SSH to become available...
==> virtualbox-iso: Connected to SSH!
==> virtualbox-iso: Uploading VirtualBox version info (4.3.6)
==> virtualbox-iso: Uploading VirtualBox guest additions ISO...
==> virtualbox-iso: Gracefully halting virtual machine...
virtualbox-iso:
virtualbox-iso: We trust you have received the usual lecture from the local System
virtualbox-iso: Administrator. It usually boils down to these three things:
virtualbox-iso:
virtualbox-iso: #1) Respect the privacy of others.
virtualbox-iso: #2) Think before you type.
virtualbox-iso: #3) With great power comes great responsibility.
virtualbox-iso:
virtualbox-iso: [sudo] password for vagrant: 
virtualbox-iso: Broadcast message from root@debian (Thu Feb 27 13:46:43 2014):
virtualbox-iso:
virtualbox-iso: The system is going down for system halt NOW!
==> virtualbox-iso: Preparing to export machine...
virtualbox-iso: Deleting forwarded port mapping for SSH (host port 4391)
==> virtualbox-iso: Exporting virtual machine...
==> virtualbox-iso: Unregistering and deleting virtual machine...
Build 'virtualbox-iso' finished.

* 看到上面的訊息代表 Packer 已經依照設定成功建立了一個名為 packer-test 的虛擬機器了。當然,Packer 還可以做很多事,例如:provision 以及產生給 Vagrant 用的 box 檔。這些精彩的功能,我們後續會以其他篇文章來探討!也希望各位能享受 Packer 帶給我們的便利。


參考資料:

http://www.packer.io/


沒有留言: