【Ottoのビルド】 Ottoを試して理解する(その2)
前回、Windows10でのOtto(0.2)によるProvisionに失敗しましたが、GitHub上のブランチではすでに解決済みであることを確認しました。 なので、修正済みバイナリを自分でビルドしてみたいと思います。
やりたいこと
Github上のOtto Bug Fix Branchをビルドし、WindowsでProvisionを成功させる。 OttoのビルドはCentOS 7.1 (VM)上で行う。 OttoはGoで書かれているので、VM上にGoの環境を整える。
ビルド環境
GuestOS: CentOS 7.1 (VirtualBox)
HostOS: Windows10
- Vagrant 1.8.1
- VirtualBox 5.0.12
- TeraTerm 4.89
VMの準備
VM用のフォルダをHost上で準備し、初期化します(HostOS)。
vagrant init
Vagrantfileのポイント
Vagrangfileをこんな感じで修正しました。
- Box
config.vm.box = "bento/centos-7.1"
* Provisioning(Shellscript)
config.vm.provision "shell", inline: <<-SHELL sudo yum -y install git sudo yum -y install zip sudo yum -y install gcc cd /usr/local/src wget https://storage.googleapis.com/golang/go1.5.3.linux-amd64.tar.gz tar -C /usr/local -xzf go1.5.3.linux-amd64.tar.gz grep -q /usr/local/go/bin /home/vagrant/.bashrc || echo 'export PATH=$PATH:/usr/local/go/bin' >> /home/vagrant/.bashrc grep -q /home/vagrant/work/bin /home/vagrant/.bashrc || echo 'export PATH=$PATH:/home/vagrant/work/bin' >> /home/vagrant/.bashrc grep -q GOPATH /home/vagrant/.bashrc || echo 'GOPATH=/home/vagrant/work' >> /home/vagrant/.bashrc grep -q GOROOT_BOOTSTRAP /home/vagrant/.bashrc || echo 'GOROOT_BOOTSTRAP=/home/vagrant/go1.4' >> /home/vagrant/.bashrc SHELL
Vagrantfileの準備ができたら、さっそく起動!
vagrant up
Go 1.4のダウンロードやビルドは、この後マニュアルでやりました。
Goの環境変数設定
Getting Started - The Go Programming Language
- GOROOT: デフォルト(usr/local/go)以外の場所にインストールするときに設定必要
- GOROOT_BOOTSTRAP: GoをビルドするためのGo1.4の置き場。今回はhome下に設定。
- GOPATH: Goのワーキングディレクトリ
なお、/usr/local/go/bin
と/home/vagrant/work/bin
にパスを通しています。
/usr/local/go/bin
はGoのパス、/home/vagrant/work/bin
は依存するGo Package Binaryへのパスです。
VMへのSSH接続
Windowsではsshコマンドがないので、そのままではvagrant ssh
コマンドが利用できません。
いろいろ解決手段はありますが、ここではTeraTermを使ってssh接続しています。
- IP:127.0.0.1
- Port:2222
なお、vagrant upデフォルトのユーザ名およびパスワードは、vagrantになっています。
Goのクロスコンパイル事前準備
クロスコンパイルとは、Linux上でWindowsのバイナリをコンパイルしたりすることです。 Goはクロスコンパイルに対応していますが、準備が必要です。
Go1.5をコンパイルするのにGo1.4が必要!
を参考に、以下のことをやります。
まずはGo1.4をhome下でビルドする。
cd ~ git clone -b release-branch.go1.4 https://go.googlesource.com/go $HOME/go1.4 cd $HOME/go1.4/src ./make.bash
次に、GOROOT_BOOTSTRAPを設定(Vagrangfileで設定済み)し、Windowsでのクロスコンパイル向けビルドを行う。
$ export GOROOT_BOOTSTRAP=/home/vagrant/go1.4 $ cd $(go env GOROOT) $ sudo -E GOOS=windows GOARCH=amd64 ./make.bash
最後にこのメッセージがでたら成功です。
--- Installed Go for windows/amd64 in /usr/local/go Installed commands in /usr/local/go/bin
ただし、Ottoのクロスコンパイルに関しては、この作業が不要だったかもしれません。 上記クロスコンパイル準備していない32bit Windowsやその他のプラットフォームのバイナリもコンパイル可能でした。
Ottoのコンパイル
ここまできたら、Ottoをコンパイルすることができます。
本家のガイドに従いつつ、やっていきましょう。
ディレクトリ作成
$GOPATH/src/github.com/hashicorp
を作成し、cd
します。
今回、GOPATHは/home/vagrant/work
にしました。
git clone
本来ならこうです。
git clone https://github.com/castaneai/otto.git
今回は、Windows用修正ブランチをビルドするので、こうします。 (このブランチは、masterにマージされ、そのうちなくなるかもしれません。)
git clone -b fix-fails-provisioning-on-windows https://github.com/castaneai/otto.git
Build!!
make updatedeps
で、依存するパッケージのアップデートを行います。 その後、
make bin
うまくいけば、こんな感じです。
$ make bin find . -type f -name '.DS_Store' -delete go generate ./... ==> Getting dependencies... ==> Removing old directory... ==> Building... Number of parallel builds: 1 --> windows/amd64: github.com/hashicorp/otto --> linux/386: github.com/hashicorp/otto --> linux/amd64: github.com/hashicorp/otto --> linux/arm: github.com/hashicorp/otto --> darwin/386: github.com/hashicorp/otto --> darwin/amd64: github.com/hashicorp/otto --> windows/386: github.com/hashicorp/otto ==> Packaging... --> windows_amd64 adding: otto.exe (deflated 73%) --> linux_386 adding: otto (deflated 69%) --> linux_amd64 adding: otto (deflated 73%) --> linux_arm adding: otto (deflated 70%) --> darwin_386 adding: otto (deflated 69%) --> darwin_amd64 adding: otto (deflated 73%) --> windows_386 adding: otto.exe (deflated 70%) ==> Results: total 20M
pkg下にバイナリができているので、HostOSとのシェアフォルダにコピーします。
cp pkg/windows_amd64.zip /vagrant/
これで、Vagrantfileがあるフォルダにwindows_amd64.zipがコピーされます。
ようやく、WindowsでOttoによるProvisioningができます! 今日はここまで!