LoginSignup
17
18

More than 5 years have passed since last update.

Packer in Atlas

Last updated at Posted at 2015-05-19

Packer を使えば Vagrant Box や Docker イメージ、AMIを簡単に作成することができますよね。

最近だと1からTemplateを書かなくても、誰かが作成したイイものがあるので調子に乗って色々作ってしまって・・・
気がついたらマシンのSSDを食い潰していたなんてことも、きっとあるでしょう。

作成した Vagrant Box や Docker イメージを公開する場合はアップロードしなくてはいけませんが、アップロードにワクワクするのは初めだけです。だんだん待ち時間が退屈になってきます。

あー、リモートで Packer を実行しておいたら自動で配布可能な状態まで持っていてくれないかなぁ

そんな願望を叶えてくれる(かもしれない)のが、Atlas です。

Atlas は VagrantやPackerを開発している Hashicorp が提供しているサービスで、Vagrant Box のホスティングや検索、凄すぎて鼻血が出てしまう Vagrant Share などを提供していた Vagrant Cloud の代替となる、新しいプロダクトです。

Hashicorp社の新サービスATLASと周辺ツールのまとめ

この Atlas を使うと、Packer をリモートで実行し、構築された Vagrant Box や Docker イメージ、AMIを保管、公開することができます。

Atlas アカウント

過去に Vagrant Cloud を利用していた場合は、そのアカウントでログインできます。
利用したことがなければアカウントを作成しましょう。

アカウント作成後、Personal Settings ページで Token を作成します。

スクリーンショット 2015-05-15 21.27.18.png

生成したトークンを環境変数に設定します。

.bashrc
export ATLAS_TOKEN=VZCJUQMKVWqjP99oys-mx6h2PicLbpzBiBtAxHV_DvRDTMtsWEQdidA8YCydSH1Ctmo

Packer Template の準備

今回は 株式会社時雨堂 さんが公開している shiguredo/packer-templates を使って、CentOS 7 の Vagrant Box を作ってみます。

template.json を基に以下のような編集を行います。


Push

このビルド設定の名前を Atlasアカウント名/ビルド設定名 の形式で定義します。

template.json
"push": {
  "name": "niwashun/centos70-build-configuration"
}

※ビルド名に centos-7.0 のようにドットが含まれていると、この後で実行する packer push がうまくいかないみたいです

Builders

元々記載されているISOファイルが無くなってしまっているみたいなので変更します。

template.json
"builders": [
  {
    "type": "virtualbox-iso",
    ...
    "iso_checksum": "154ba47b7a37e52e0100310c3aeb8f9d9daf4806",
    "iso_checksum_type": "sha1",
    "iso_url": "http://ftp.iij.ad.jp/pub/linux/centos/7.0.1406/isos/x86_64/CentOS-7.0-1406-x86_64-DVD.iso",
    ...
  },
  {
    "type": "vmware-iso",
    ...
    "iso_checksum": "154ba47b7a37e52e0100310c3aeb8f9d9daf4806",
    "iso_checksum_type": "sha1",
    "iso_url": "http://ftp.iij.ad.jp/pub/linux/centos/7.0.1406/isos/x86_64/CentOS-7.0-1406-x86_64-DVD.iso",
    ...
  }
]   

Variables

必須ではないですが、Atlas アカウント名 と Vagrant Box名を変数として定義しておきます。

template.json
"variables": {
  "atlas_username": "niwashun",
  "atlas_name": "centos-7.0",
}

Post-Processors

VMware と VirtualBox 用の Atlas post-processor を定義します。
variables を定義していない場合は artifact を適宜書き換えてください。

template.json
"post-processors": [
  {
    "type": "atlas",
    "only": ["vmware-iso"],
    "artifact": "{{user `atlas_username`}}/{{user `atlas_name`}}",
    "artifact_type": "vagrant.box",
    "metadata": {
      "provider": "vmware_desktop",
      "version": "0.0.1"
    }
  },
  {
    "type": "atlas",
    "only": ["virtualbox-iso"],
    "artifact": "{{user `atlas_username`}}/{{user `atlas_name`}}",
    "artifact_type": "vagrant.box",
    "metadata": {
      "provider": "virtualbox",
      "version": "0.0.1"
    }
  }
]

最終的にはこんな感じ。

Push コマンドを実行

いよいよリモートでビルドを実行します。編集した template.json と同じディレクトリで packer push を実行します。

command-line
$ packer push -create template.json

成功すると、Push successful to 'niwashun/centos70-build-configuration' というメッセージが表示されます。

では、Atlas を見てみましょう。

スクリーンショット 2015-05-15 22.21.00.png

template.json で指定したビルド名から、Builder毎の状況が確認できます。

スクリーンショット 2015-05-15 22.17.44.png

何度か packer push していると、その履歴もココに出力されます。

スクリーンショット 2015-05-15 22.18.02.png

virtualbox-iso の右端をクリックすると、詳細なビルド状況を確認することができます。

スクリーンショット 2015-05-15 22.18.41.png

ビルドが完了するとステータスが finishederror に変わります。

スクリーンショット 2015-05-16 12.27.50.png

スクリーンショット 2015-05-16 12.28.22.png

スクリーンショット 2015-05-18 15.03.26.png

$ vagrant init niwashun/centos-7.0
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

構築直後の Box ファイルは Private になっています。
この状態で試してみたい場合は vagrant login が必要です。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'niwashun/centos-7.0' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
The box 'niwashun/centos-7.0' could not be found or
could not be accessed in the remote catalog. If this is a private
box on Vagrant Cloud, please verify you're logged in via
`vagrant login`. Also, please double-check the name. The expanded
URL and error message are shown below:

URL: ["https://vagrantcloud.com/niwashun/centos-7.0"]
Error: The requested URL returned error: 404 Not Found

vagrant login してみましょう。

$ vagrant login
In a moment we'll ask for your username and password to Vagrant Cloud.
After authenticating, we will store an access token locally. Your
login details will be transmitted over a secure connection, and are
never stored on disk locally.

If you don't have a Vagrant Cloud account, sign up at vagrantcloud.com

Username or Email: niwashun
Password (will be hidden): 
You're now logged in!

これで vagrant up できるようになりま・・・

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'niwashun/centos-7.0' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'niwashun/centos-7.0'
    default: URL: https://vagrantcloud.com/niwashun/centos-7.0
==> default: Adding box 'niwashun/centos-7.0' (v0.0.1) for provider: virtualbox
    default: Downloading: https://vagrantcloud.com/niwashun/boxes/centos-7.0/versions/0.0.1/providers/virtualbox.box
The "metadata.json" file for the box 'niwashun/centos-7.0' was not found.
Boxes require this file in order for Vagrant to determine the
provider it was made for. If you made the box, please add a
"metadata.json" file to it. If someone else made the box, please
notify the box creator that the box is corrupt. Documentation for
box file format can be found at the URL below:

http://docs.vagrantup.com/v2/boxes/format.html

・・・!
で、できねぇッ!?

Missing metadata.json (Packer (push) -> ATLAS (build) -> Vagrant (download)) #2090

ぐふぉ

参考

Packer in Atlas: Automate the Building and Managing of Vagrant Boxes and Machine Images

17
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
18