Chefにゅうもん2
嫁が起きない。
半日以上は寝てるってばよ。
前回はNode側でvimをインストールしてみた。
今回は自分のマシンからvagrant up
した仮想マシンをいじくってみよう。
vagrant init
まずVagrantboxで適当なboxファイルを探しきて、Vagrantfile
を作る。
$ vagrant box add centos6 https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box $ mkdir ~/vms/centos6 && cd ~/vms/centos6 $ vagrant init centos6
Vagrantfile
こんな感じで
# -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure(2) do |config| config.vbguest.auto_update = false config.vm.box = "centos6" config.vm.network "private_network", ip: '192.168.33.10' end
Gemfile
# Gemfileはこんな感じで $ cat Gemfile source 'https://rubygems.org' gem 'chef' gem 'knife-solo' gem 'berkshelf' # bundle execって打つのめんどくさいから--binstubsオプションつける # 自分はrbhev使ってるからこれ参考にしたよ # http://qiita.com/naoty_k/items/9000280b3c3a0e74a618 $ bundle install --binstubs
Chefリポジトリの作成
$ bundle exec knife solo init . Creating kitchen... Creating knife.rb in kitchen... Creating cupboards... Setting up Berkshelf...
なんか色々ファイルが出来た。
centos6 ├── Berksfile # 誰かが公開しているCookBookをBundlerみたいに管理する為の設定ファイル ├── Gemfile # Gemの依存関係を管理するファイル ├── Gemfile.lock # 依存gemのversionと取得先 ├── Vagrantfile # 仮想マシン起動の為の設定情報 ├── cookbooks # 誰かが公開しているcookbookを置いとく場所 ├── data_bags # cookbook内で利用したいデータを格納する場所らしい ├── environments # 開発用とか本番用とか環境によって設定を分けたい時に使うデータを格納する場所 ├── nodes # 開発用とか本番用とか環境によって設定を分けたい時に使うデータを格納する場所 ├── roles # Nodeの状態を定義しておくもの置き場 └── site-cookbooks # 自分で作ったcookbookはここに置いとく
Chef Soloのインストール
# 仮想マシン起動 vagrant up # SSHの接続設定 vagrant ssh-config --host cos6 >> ~/.ssh/config # Chef Soloインストール knife solo bootstrap cos6
途中でWARNING: Local cookbook_path '/Users/nob/vms/centos6/cookbooks' does not exist
みたいな警告が出るけど、適用するcookbookは今のところ未設定なのでムシ!!
Cookbookを作成してrun_listに追加してみる
$ knife cookbook create dstat -o site-cookbooks $ tree site-cookbooks/dstat # なんかいっぱい出来た site-cookbooks/dstat ├── CHANGELOG.md ├── README.md ├── attributes ├── definitions ├── files │ └── default ├── libraries ├── metadata.rb ├── providers ├── recipes │ └── default.rb ├── resources └── templates └── default # こんな感じで「dstatをインストールするレシピ」を書く $ cat site-cookbooks/dstat/recipes/default.rb package "dstat" do action :install end # run_list(適用するレシピ集)に作成したdstatを追加する $ knife node run_list add cos6 dstat -z cos6: run_list: recipe[dstat]
プロビジョニングの実行
# プロビジョニングしてみる $ knife solo cook cos6 Running Chef on cos6... Checking Chef version... Installing Berkshelf cookbooks to 'cookbooks'... DEPRECATED: Your Berksfile contains a site location pointing to the Opscode Community Site (site :opscode). Site locations have been replaced by the source location. Change this to: 'source "https://supermarket.chef.io"' to remove this warning. For more information visit https://github.com/berkshelf/berkshelf/wiki/deprecated-locations Resolving cookbook dependencies... Uploading the kitchen... WARNING: Local cookbook_path '/Users/nob/vms/centos6/cookbooks' does not exist Generating solo config... Running Chef... Starting Chef Client, version 12.0.3 Compiling Cookbooks... Converging 1 resources Recipe: dstat::default * yum_package[dstat] action install - install version 0.7.0-1.el6 of package dstat Running handlers: Running handlers complete Chef Client finished, 1/1 resources updated in 4.7559155 seconds
DEKITA!!
起きない嫁を起こします。
参考
Chefにゅうもん1
やっときたか< Kindle for Mac http://t.co/U8E2XqU8bc
— Dan Kogai (@dankogai) February 13, 2015
やったー( ◜◡^)っ
ということで最近Chefをいじってんだけど、よく解かんないまま進んでた感があるので一旦整理。
言葉の意味
結構チンプンカンプンだった ┐(★゚д゚)┌㍗??
なまえ | いみ |
---|---|
レシピ | さーばー設定の手順 |
クックブック | レシピに必要なデータやファイルをまとめるもの |
リポジトリ | Chefの実行に必要なものをまとめる君 |
ノード | Chefでいじいじする対象(Guest OSとは呼ばない) |
knife | クックブックを作成する為のコマンドツール。 |
絵で書くと多分こんな感じ
冪等性
何回実行しても、実行後の状態が変わらないってことを保証する。
# Guest側OSで実行 # vim knife cookbook create vim -o /var/chef/cookbooks # vimをインストールしてね(♥ω♥) cat <<... > /var/chef/cookbooks/vim/recipes/default.rb package "vim" do action :install end ... # おねがいします。 chef-solo -o vim {:config_missing=>true} [2015-02-13T16:34:57+00:00] WARN: ***************************************** [2015-02-13T16:34:57+00:00] WARN: Did not find config file: /etc/chef/solo.rb, using command line options. [2015-02-13T16:34:57+00:00] WARN: ***************************************** Starting Chef Client, version 12.0.3 [2015-02-13T16:34:58+00:00] WARN: Run List override has been provided. [2015-02-13T16:34:58+00:00] WARN: Original Run List: [] [2015-02-13T16:34:58+00:00] WARN: Overridden Run List: [recipe[vim]] Compiling Cookbooks... Converging 1 resources Recipe: vim::default * yum_package[vim] action install - install version 7.2.411-1.8.el6 of package vim-enhanced Running handlers: Running handlers complete # vimがinstall出来た!! Chef Client finished, 1/1 resources updated in 24.999607308 seconds vim ~ VIM - Vi IMproved ~ ~ version 7.2.411 ~ by Bram Moolenaar et al. ~ Modified by <bugzilla@redhat.com> ~ Vim is open source and freely distributable ~ ~ Become a registered Vim user! ~ type :help register<Enter> for information ~ ~ type :q<Enter> to exit ~ type :help<Enter> or <F1> for on-line help ~ type :help version7<Enter> for version info # もっかいおねがいします。 chef-solo -o vim {:config_missing=>true} [2015-02-13T16:38:30+00:00] WARN: ***************************************** [2015-02-13T16:38:30+00:00] WARN: Did not find config file: /etc/chef/solo.rb, using command line options. [2015-02-13T16:38:30+00:00] WARN: ***************************************** Starting Chef Client, version 12.0.3 [2015-02-13T16:38:32+00:00] WARN: Run List override has been provided. [2015-02-13T16:38:32+00:00] WARN: Original Run List: [] [2015-02-13T16:38:32+00:00] WARN: Overridden Run List: [recipe[vim]] Compiling Cookbooks... Converging 1 resources Recipe: vim::default * yum_package[vim] action install (up to date) Running handlers: Running handlers complete # エラーにならずにsuceecss!installはskipされた Chef Client finished, 0/1 resources updated in 2.555000426 seconds
眠い。続きはまた明日だの。
CentOS7にZsh入れるとこまで
Windowsでこの作業やったら泣きそうになった(:.;゚;Д;゚;.:)
後でまとめよう。
Chefリポジトリを作成する。
$ mkdir -p vms/chef-repo && cd vms/chef-repo
ChefとKnife-Zeroのインストール
Windowsではwindows-pr
とかないとエラー出まくって嫌になった。
berkshelfが依存してるdep-selector-libgecode
ってgemが強敵。
cat <<... > Gemfile source 'https://rubygems.org' gem 'chef' gem 'knife-zero' gem 'berkshelf' if RUBY_PLATFORM.match(/mswin(?!ce)|mingw|cygwin|bccwin/) gem 'rdp-ruby-wmi' gem 'ruby-wmi' gem 'windows-api' gem 'windows-pr' gem 'win32-eventlog' gem 'win32-process' gem 'win32-service' end ... bundle install
.chef/knife.rb
常にローカルモードで動くように。
mkdir ~/.chef echo 'local_mode true' > ~/.chef/knife.rb
GuestOSにChef Clientをインストールする
bundle exec knife zero bootstrap 10.0.0.10 --sudo
したら
/Users/nob/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/chef-12.0.3/lib/chef/data_bag_item.rb:161: warning: circular argument reference - data_bag
みたいな警告が出るから調べてみたら、ChefのBugだった。
どーすんのよ?って調べてたら暫定対応っぽいパッチ書いてる目からビーム出すおじさんが居た。
/Users/nob/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/chef-12.0.3/lib/chef/data_bag_item.rb
をここを参考に下記のような感じで直しちゃう。
# 161行目の def destroy(data_bag=data_bag, databag_item=name) # を def destroy(data_bag=data_bag(), databag_item=name) # に修正する
気を取り直して、、
# Chef Clientをインストールする bundle exec knife zero bootstrap 10.0.0.10 bundle exec knife node list WARN: No cookbooks directory found at or above current directory. Assuming /Users/nob/vms/chef-repo. cos7 # 無事インストール出来たのでsnapshotとっとく vagrant snapshot take v1_chef_client_installed
レシピを適用する
cat <<... > Berksfile source "https://supermarket.chef.io" cookbook 'zsh' ... bundle exec berks vendor cookbooks bundle exec knife node run_list add localhost zsh # ??? 下記のコマンドは通らない、Guest OS側のネットワーク系の設定不足だと思う bundle exec knife zero chef_client 'name:cos7' --attribute ipaddress
問題解決した?
# → nodeの情報見てみる $ knife node show cos7 Node Name: cos7 Environment: _default FQDN: cos7 IP: 10.0.2.15 Run List: recipe[zsh] Roles: Recipes: Platform: centos 7.0.1406 Tags: # vagrant sshして… $ ip a # enp0s3のIPが設定されてた。 # 10.0.0.10じゃないとssh通らないから、そっちのIP使って欲しい。 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:ea:9b:b5 brd ff:ff:ff:ff:ff:ff inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3 valid_lft 82613sec preferred_lft 82613sec inet6 fe80::a00:27ff:feea:9bb5/64 scope link valid_lft forever preferred_lft forever 3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:dc:fc:d5 brd ff:ff:ff:ff:ff:ff inet 10.0.0.10/24 brd 10.0.0.255 scope global enp0s8 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fedc:fcd5/64 scope link valid_lft forever preferred_lft forever
nodes/cos7.json
の
"ipaddress": "10.0.2.15", "macaddress": "08:00:27:EA:9B:B5", "ip6address": "fe80::a00:27ff:fedc:fcd5", // を "ipaddress": "10.0.0.10", "macaddress": "08:00:27:dc:fc:d5", "ip6address": "fe80::a00:27ff:fedc:fcd5",
に書き換えちゃう。
したら
$ knife node show cos7 Node Name: cos7 Environment: _default FQDN: cos7 IP: 10.0.0.10 Run List: recipe[zsh] Roles: Recipes: zsh, zsh::default Platform: centos 7.0.1406 Tags:
になってzshのインストールもさくっと出来た。
でも、、、
毎回この設定書き換わるから、この対処法は間違ってる。
とりあえず、今からプラバンdeなにか作ります。
VagrantでCentOS7の仮想マシンを作成する。
勤め先の開発環境をvagrantとChefを使って作ろうと思う。
Install Vagrant(For OSX)
事前にHomebrewとHomebrew Caskがインストールされていること。
brew cask install vagrant brew cask install virtualbox
Install Vagrant plug-in
vagrant plugin install vagrant-vbguest vagrant plugin install vagrant-vbox-snapshot vagrant plugin install sahara
仮想マシンを作成
vagrant init
vagrant box add centos7 https://f0fff3908f081cb6461b407be80daf97f07ac418.googledrive.com/host/0BwtuV7VyVTSkUG1PM3pCeDJ4dVE/centos7.box md -p vms/centos7 && cd vms/centos7 vagrant init centos7
Vagrantfileの設定
Vagrantfile
ってのが作られる。
仮想マシンの設定ファイルとのことだ。
設定項目は多々あれど、とりあえずこんな感じ
# -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure(2) do |config| Encoding.default_external = 'UTF-8' config.vbguest.auto_update = false config.vm.define :centos7, primary: true do |node| node.vm.box = "centos7" node.vm.hostname = 'cos7' node.vm.network "private_network", ip: '10.0.0.10' node.ssh.forward_agent = true node.vm.provision :shell, :path => "provision/bootstrap.sh" config.vm.provider :virtualbox do |vb| vb.name = "cos7" vb.gui = false vb.memory = 1024 vb.cpus = 2 vb.customize ["modifyvm", :id, "--largepages", "on"] vb.customize ["modifyvm", :id, "--accelerate3d", "off"] vb.customize ["modifyvm", :id, "--accelerate2dvideo", "off"] vb.customize ["modifyvm", :id, "--natdnsproxy1", "off"] vb.customize ["modifyvm", :id, "--natdnshostresolver1", "off"] end end end
Vagrant 起動時に1回だけ実行するスクリプトを設定する | Z BLOG
を参考にしてprovision/bootstrap.sh
はこんな感じde書いた。
proxyの設定と、SELINUXとfirewallを無効にしてる。
#!/usr/bin/env bash # Proxy環境化の場合はtrueに設定すること ON_PROXY=true HTTP_PROXY="xxxx(host):xx(port)" HTTPS_PROXY="$HTTP_PROXY" HOST_ID="10.0.0.1" test -f /etc/bootstrapped && exit if $ON_PROXY ; then echo "Setting HTTP Proxy for /etc/environment" cat <<... >> /etc/environment http_proxy="http://$HTTP_PROXY/" https_proxy="https://$HTTPS_PROXY/" ... echo "Setting HTTP Proxy for /etc/yum.conf" cat <<... >> /etc/yum.conf proxy=http://$HTTP_PROXY ... echo "Setting HTTP Proxy for /etc/wgetrc" cat <<... >> /etc/wgetrc http_proxy=http://$HTTP_PROXY https_proxy=http://$HTTPS_PROXY ftp_proxy=http://$HTTP_PROXY ... echo "Setting HTTP Proxy for cURL in Vagrant user directory" if test -d /home/vagrant; then cat <<... >> /home/vagrant/.curlrc proxy = "http://$HTTP_PROXY" ... fi fi echo "Disable SELINUX & firewall" sudo systemctl stop firewalld sudo systemctl disable firewalld sudo sed -i -e s/SELINUX=enforcing/SELINUX=disabled/ /etc/selinux/config sudo setenforce 0 sudo systemctl restart network date > /etc/bootstrapped
vagrant up
最初はこんなエラーだった。
vagrant up # 省略 Failed to mount folders in Linux guest. This is usually because the "vboxsf" file system is not available. Please verify that the guest additions are properly installed in the guest and can work properly. The command attempted was: mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` vagrant /vagrant mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` vagrant /vagrant The error output from the last command was: /sbin/mount.vboxsf: mounting failed with the error: No such device
ほんで、vagrant
とvirtualbox
を最新にして
下記のpluginもインストールした。
vagrant plugin install vagrant-vbguest
したらエラーが警告に変わった。
vagrant up # 途中は省略 ( ´థ౪థ) GuestAdditions versions on your host (4.3.20) and guest (4.3.14) do not match. # 途中は省略 ( ◉◞౪◟◉) Installing Virtualbox Guest Additions 4.3.20 - guest version is 4.3.14 # 途中は省略 ( ◜◡^)っ✂╰⋃╯ An error occurred during installation of VirtualBox Guest Additions 4.3.20. Some functionality may not work as intended. In most cases it is OK that the "Window System drivers" installation failed.
GuestAdditionsのバージョンが違うらしい。
このままだと共有フォルダのマウントが出来ないので下記の解決方法を参考にした。
Vagrant can't mount shared folder in VirtualBox 4.3.10
vagrant ssh # /usr/lib配下にVBoxGuestAdditionsへのシンボリックリンクを貼る [vagrant@localhost ~]$ sudo ln -s /opt/VBoxGuestAdditions-4.3.20/lib/VBoxGuestAdditions /usr/lib/VBoxGuestAdditions [vagrant@localhost ~]$ exit # reloadすれば正常に起動する筈 vagrant reload
snapshotとっとく
正常に動作してめでたいのでsnapshotをとっておこう。
vagrant snapshot take v0_init
[参考]
Vagrant のネットワーク周りのあれこれ
Vagrant 起動時に1回だけ実行するスクリプトを設定する | Z BLOG
Vagrant can't mount shared folder in VirtualBox 4.3.10
Scalatraプロジェクト構成
嫁が起きない。
まぁいいや。
前回までで環境構築は終わったと思われ。
後は、作ったプロジェクトをimportして終わり。
テンプレートから作ったプロジェクトはこんな感じになってる。
プロジェクト構成
${PROJECT_ROOT} │ ├── README.md ├── project │ ├── build.properties │ ├── build.scala │ └── plugins.sbt ├── sbt └── src ├── main │ ├── resources │ │ └── logback.xml │ ├── scala │ │ ├── ScalatraBootstrap.scala │ │ └── com │ │ └── nyasu1111 │ │ └── sample │ │ ├── SampleServlet.scala │ │ └── SampleStack.scala │ └── webapp │ └── WEB-INF │ ├── templates │ │ ├── layouts │ │ │ └── default.jade │ │ └── views │ │ └── hello-scalate.jade │ └── web.xml └── test └── scala └── com └── nyasu1111 └── sample └── SampleServletSpec.scala
サーバー起動時の動き
container:start
すると
ScalatraBootstrap#init
が呼ばれる。 initメソッドに
context.mount(new SampleServlet, "/*")
って書いてある。バインディング先を指定してるんだろう。
2. ScalatraBootstrap#init
でインスタンス化されたSampleServlet
が呼び出される。
3. SampleServlet
のtrait
である、SampleStack#createTemplateEngine
を呼び出して、レイアウト系の何かを登録するみたい。
シンプル(๑˃̵ᴗ˂̵)و
Scalatra環境構築-2-
きゅうりのキューちゃんの汁ってうまいよね。
ご飯にかけて食べると相当いいと思う。
自分はIntelliJ IDEA使ってるんで、sbt-ideaプラグインをインストールします。
${PROJECT_ROOT}/project/plugins.sbt
に
resolvers += "Sonatype snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/" addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.7.0-SNAPSHOT")
を追記する。
SNAPSHOTがきもかったら、
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")
で。
ほんで
$ sbt > gen-idea # なんか色々
で終わりー。
Scalatra環境構築-1-
とか
を読んで写経したりしてたんだけど、飽き始めてきた( ・ὢ・ )( ・ὢ・ )
実際になんか作ってみよう( ・ὢ・ )
Playよりとっつきやすそうな、scalatraを使って見ようと思う。
プロジェクトの作成まで
scalatraのチュートリアルに沿ってやってみる。
1:: giter8
をinstallする。
giter8
はテンプレートからプロジェクトを作成するコマンドラインツールです。
$ brew install giter8
2:: scalatra
プロジェクトを作成する。
$ g8 scalatra/scalatra-sbt organization [com.example]: com.nyasu1111 name [My Scalatra Web App]: sample version [0.1.0-SNAPSHOT]: servlet_name [MyScalatraServlet]: SampleServlet package [com.example.app]: com.nyasu1111.sample scala_version [2.11.1]: sbt_version [0.13.5]: scalatra_version [2.3.0]: Template applied in ./sample
3:: buildする。
$ cd sample $ sbt # なんか色々ダウンロードする。jettyっていっぱい書いてあるよ。 # 長い・・・ # ⁽⁽◝( •௰• )◜⁾⁾≡₍₍◞( •௰• )◟₎₎⁽⁽◝( •௰• )◜⁾⁾≡₍₍◞( •௰• )◟₎₎ # 終わった! [info] Done updating.
4:: Hello, world!
> container:start # ⁽⁽◝( •௰• )◜⁾⁾≡₍₍◞( •௰• )◟₎₎⁽⁽◝( •௰• )◜⁾⁾≡₍₍◞( •௰• )◟₎₎ [success] Total time: 16 s, completed 2015/01/24 13:49:15 > browse
5:: 自動コンパイルモードon/off
> ~ ;copy-resources;aux-compile 1. Waiting for source changes... (press enter to interrupt) Enter押すと自動コンパイルモード終わり
次はプロジェクト構成と環境構築。