itamae 生活 2日目

昨日 に引き続き今日も itamae

Today's Goal

  • itamae の BestPractice の沿った構成で Nginx x Unicorn x Rails の構成を作る
  • Ruby は rbenv 環境下で入れたい

Premise

  • itamae が global に install されている

Let's Try

BestPractice をみながらディレクトリ構築

ディレクトリ構成

ココ にある通り

.
├── cookbooks
│   └── nginx
│       ├── default.rb
│       ├── files
│       │   └── etc
│       │       └── nginx
│       │           └── conf.d
│       │               └── static.conf
│       └── templates
│           └── etc
│               └── nginx
│                   └── conf.d
│                       └── dynamic.conf
└── roles
    └── web.rb

手でペチペチ作るのもなんだなぁと思ってたら、それっぽいのがあった

itamae help

$ itamae -h
Commands:
  itamae destroy [cookbook|role] [NAME]   # Undo role or cookbook (short-cut alias: "d")
  itamae docker RECIPE [RECIPE...]        # Create Docker image
  itamae generate [cookbook|role] [NAME]  # Initialize role or cookbook (short-cut alias: "g")
  itamae help [COMMAND]                   # Describe available commands or one specific command
  itamae init NAME                        # Create a new project
  itamae local RECIPE [RECIPE...]         # Run Itamae locally
  itamae ssh RECIPE [RECIPE...]           # Run Itamae via ssh
  itamae version                          # Print version

itamae inititamae gemenate が使えそう

itamae init

$ itamae init itamae-labo
      create
      create  Gemfile
      create  cookbooks/.keep
      create  roles/.keep
         run  bundle install from "."
Could not locate Gemfile or .bundle/ directory

$ cd itamae-labo
$ ls -la
total 8
drwxr-xr-x   5 kawakubox  staff   170 12 10 00:51 .
drwxr-xr-x  47 kawakubox  staff  1598 12 10 00:51 ..
-rw-r--r--   1 kawakubox  staff    63 12 10 00:51 Gemfile
drwxr-xr-x   3 kawakubox  staff   102 12 10 00:51 cookbooks
drwxr-xr-x   3 kawakubox  staff   102 12 10 00:51 roles

ルートディレクトリと Gemfile, 空ディレクトリ(cookbook, roles)作ってくれる。

cookbook

ミドルウェアやライブラリを取り回しやすい単位でまとめたもの

role

cookbook の利用者。 アプリサーバ、DBサーバなどで構成がガラッと変わるのでそれぞれ app, db といった role を用意してあげる

itamae generate

create cookbook

nginx の cookbook 作ってみる

$ itamae g cookbook nginx
      create
      create  default.rb
      create  files/.keep
      create  templates/.keep

g は generate のエイリアスrails っぽい

create role

続いて role を作る

$ itamae g role web
      create
      create  default.rb
      create  files/.keep
      create  templates/.keep

Recipe for nginx

昨日のまんまなので省略

Role for web

include_recipe '../../cookbooks/nginx/default.rb'

Execution

$ itamae ssh -i ~/.ssh/hogepiyo.pem -h xx.xx.xx.xx -u ec2-user roles/web/default.rb

 INFO : Starting Itamae...
 INFO : Recipe: /../itamae-labo/roles/web/default.rb
 INFO :   Recipe: /../itamae-labo/cookbooks/nginx/default.rb
 INFO :     package[nginx] installed will change from 'false' to 'true'
 INFO :     service[nginx] enabled will change from 'false' to 'true'
 INFO :     service[nginx] running will change from 'false' to 'true'

※動かす前に既に入っていた nginx を削除してから実行した


眠くなったので今日はここまで

Summary

BesstPractice の構成に則って作るには、 itamae init, itamae generate を使うのがよい