OpsWorksで動かすルールをvagrant+chef soloを使ってローカルでも試したい!
と思いませんか?
OpsWorksそれ自体はお金はかからないとはいえ、OpsWorksから起動されるec2と付随するいくつかのサービスにはお金がかかります。それを vagrant を使ってローカルで試行錯誤できればお金はかかりません。
OpsWorksはchefで作られていることは周知の事実です。
http://docs.aws.amazon.com/opsworks/latest/userguide/welcome.html にも以下の通り書かれています。
AWS OpsWorks uses its own Chef recipes to configure the instances on a stack. Chef Solo runs on every instance, and AWS OpsWorks sends commands to each instance to run recipes that set up, configure, and shut down the instance. You can extend these actions with your own custom recipes. You can also run your custom recipes on a specific instance or on all instances in your stack.
というわけで、ちょっとやってみましょう。
chefむけにかかれたルールはgitで管理するのが、よくある楽な方法です。そのため、一番ラクなのは「全てのシステム記述を一つのgitに書く」こと。
全てのシステム記述を同じgitに書いていれば、chef soloをopsworksの代わりに使うことでほぼテストはローカルでできる。
と、言いたいところですが、OpsWorksのGUIで設定する部分と、chef soloむけの設定には差異があるので、ちょっとしたコツが必要になります。
- ec2の代わりとしてvagrantの中でUbuntuを動作するようにする。
- OpsWorks代わりのchef solo環境をlocalに用意する
- 設定(site-coolbooks)はbitbucketに配置する
ここでのポイントは、手元につくったChef SoloはあくまでもOpsWorks代わりをさせるためであり、手元のChefSoloには最低限の設定ですませること。
ちなみに、bitbucketは無料でも秘密レポジトリを作れるので、OpsWorksむけにはgithubよりも楽だと思います。
これから書くことはこんなかんじ。OpsWorksの進化はきっととまらないので、今の時点での私のおすすめってことで!
手元のChefSoloとOpsWorksの両方で共通で使う設定(site-cookbooks)は
bitbucketに配置させる。
共通で使う設定 であるため、可能な限りこのsite-cookbooksに詰め込むことになる。
knife cookbook create dnsbalance -o dnsbalance
** Creating cookbook dnsbalance
** Creating README for cookbook: dnsbalance
** Creating CHANGELOG for cookbook: dnsbalance
** Creating metadata for cookbook: dnsbalance
$ tree
.
└── dnsbalance
└── dnsbalance
├── CHANGELOG.md
├── README.md
├── attributes
├── definitions
├── files
│ └── default
├── libraries
├── metadata.rb
├── providers
├── recipes
│ └── default.rb
├── resources
└── templates
└── default
12 directories, 4 files
dnsbalance/dnsbalanceと、二段に深くなったディレクトリ構成だが、これはそういうもんだと諦めることにする。
共通で使う設定 であるため、可能な限りこのsite-cookbooksに詰め込むと
$ tree
.
└── dnsblance
├── .git/config (ここは略)
└── dnsbalance
├── CHANGELOG.md
├── README.md
├── attributes
│ └── default.rb
├── metadata.rb
├── recipes
│ └── default.rb
└── templates
└── default
├── dns_balance.sbin.erb
├── init.erb
├── ssh-config.erb
├── td-agent.conf.erb
└── td-agent.conf.erb~
という具合になる。このレポジトリをbitbucketに追加する。
追加の仕方はいろいろだが、
git remote add origin ssh://git@bitbucket.org/ar1/chef-cookbook-dnsbalance.git
といった具合。このときの .git/configを抜粋すると
[remote "origin"]
url = ssh://git@bitbucket.org/ar1/chef-cookbook-dnsbalance.git
fetch = +refs/heads/*:refs/remotes/origin/*
親レポジトリ(OpsWorksの代理として動作するレポジトリ)のディレクトリに移動。
そこのトップで、
submoduleとして、追加する。
git submodule add git@bitbucket.org:ar1/chef-cookbook-dnsbalance.git site-cookbooks
その結果
$ tree
.
├── Berksfile
├── Berksfile.lock
├── cookbooks
├── data_bags
├── nodes
│ └── vagrant-ubuntu.json
├── roles
└── site-cookbooks ((ここより下が追加されている))
└── dnsbalance
├── CHANGELOG.md
├── README.md
├── attributes
│ └── default.rb
├── metadata.rb
├── recipes
│ └── default.rb
└── templates
└── default
├── dns_balance.sbin.erb
├── init.erb
├── ssh-config.erb
└── td-agent.conf.erb
10 directories, 12 files
という具合になる。