Chef Workstation

Chef Workstation
– chef
– knife-solo
– knife configure

[vagrant@localhost chef]$ gem list

*** LOCAL GEMS ***

actioncable (5.0.0.1)
actionmailer (5.0.0.1)
actionpack (5.0.0.1)
actionview (5.0.0.1)
activejob (5.0.0.1)
activemodel (5.0.0.1)
activerecord (5.0.0.1)
activesupport (5.0.0.1)
addressable (2.4.0)
airbrussh (1.3.0)
arel (7.1.4)
backports (3.6.8)
bigdecimal (default: 1.2.8)
builder (3.2.2)
bundler (1.13.6)
byebug (9.0.6)
capistrano (3.11.0)
chef (12.16.42)
chef-config (12.16.42)
chef-zero (5.1.0)
chunky_png (1.3.8)
coffee-rails (4.2.1)
coffee-script (2.4.1)
coffee-script-source (1.10.0)
colorator (1.1.0)
compass (1.0.3)
compass-core (1.0.3)
compass-import-once (1.0.5)
concurrent-ruby (1.0.2)
debug_inspector (0.0.2)
did_you_mean (1.0.0)
diff-lcs (1.2.5)
erubis (2.7.0)
execjs (2.7.0)
ffi (1.9.14)
ffi-yajl (2.3.0)
forwardable-extended (2.6.0)
fuzzyurl (0.9.0)
globalid (0.3.7)
haml (4.0.7)
hashie (3.4.6)
highline (1.7.8)
httparty (0.13.7)
i18n (0.7.0)
iniparse (1.4.2)
io-console (default: 0.4.5)
ipaddress (0.8.3)
jbuilder (2.6.0)
jekyll (3.3.0)
jekyll-feed (0.8.0)
jekyll-sass-converter (1.4.0)
jekyll-watch (1.5.0)
jquery-rails (4.2.1)
json (default: 1.8.3)
knife-solo (0.6.0)
kramdown (1.12.0)
libv8 (3.16.14.15 x86_64-linux)
libyajl2 (1.2.0)
liquid (3.0.6)
listen (3.0.8)
loofah (2.0.3)
lunchy (0.10.4)
mail (2.6.4)
mercenary (0.3.6)
method_source (0.8.2)
mime-types (3.1)
mime-types-data (3.2016.0521)
mini_portile2 (2.1.0)
minima (2.0.0)
minitest (5.9.1, 5.8.3)
mixlib-archive (0.2.0)
mixlib-authentication (1.4.1)
mixlib-cli (1.7.0)
mixlib-config (2.2.4)
mixlib-log (1.7.1)
mixlib-shellout (2.2.7)
multi_json (1.12.1)
multi_xml (0.5.5)
mustache (1.0.3)
net-scp (1.2.1)
net-sftp (2.1.2)
net-ssh (3.2.0)
net-ssh-gateway (1.2.0)
net-ssh-multi (1.2.1)
net-telnet (0.1.1)
nio4r (1.2.1)
nokogiri (1.6.8.1)
ohai (8.21.0)
pathutil (0.14.0)
pg (0.19.0)
plist (3.2.0)
power_assert (0.2.6)
proxifier (1.0.3)
psych (default: 2.0.17)
puma (3.6.0)
rack (2.0.1, 1.6.5, 1.6.4)
rack-protection (1.5.3)
rack-test (0.6.3)
rails (5.0.0.1)
rails-dom-testing (2.0.1)
rails-html-sanitizer (1.0.3)
railties (5.0.0.1)
rake (11.3.0, 10.4.2)
rb-fsevent (0.9.8)
rb-inotify (0.9.7)
rdoc (default: 4.2.1)
ref (2.0.0)
rouge (1.11.1)
rspec (3.5.0)
rspec-core (3.5.4)
rspec-expectations (3.5.0)
rspec-its (1.2.0)
rspec-mocks (3.5.0)
rspec-support (3.5.0)
rspec_junit_formatter (0.2.3)
rubygems-update (2.6.8)
safe_yaml (1.0.4)
sass (3.4.22)
sass-rails (5.0.6)
serverspec (2.37.2)
sfl (2.3)
sinatra (1.4.7)
sinatra-contrib (1.4.7)
specinfra (2.66.0)
spring (2.0.0)
spring-watcher-listen (2.0.1)
sprockets (3.7.0)
sprockets-rails (3.2.0)
sshkit (1.17.0)
syslog-logger (1.6.8)
systemu (2.6.5)
test-unit (3.1.5)
therubyracer (0.12.2)
thor (0.19.1)
thread_safe (0.3.5)
tilt (2.0.5, 2.0.2)
turbolinks (5.0.1)
turbolinks-source (5.0.0)
tzinfo (1.2.2)
uglifier (3.0.3)
uuidtools (2.1.5)
web-console (3.4.0)
websocket-driver (0.6.4)
websocket-extensions (0.1.2)
wmi-lite (1.0.0)

gem listでknife-soloとchefが入っています。

Chef

概要
– インフラをコードで記述するためのツール
– https://www.chef.io/

Ship software faster, safer and better
Chef is the leader in Continuous Automation software, an innovator in application automation and one of the founders of the DevOps movement. Chef works with more than a thousand of the most innovative companies around the world to deliver their vision of digital transformation, providing the practices and platform to deliver software at speed.

Surviving and thriving with digital transformation
Continuous change is inevitable, and so continuous automation is necessary. The digital leaders of the near-future, both individual and corporate, will be those who are able to provide automation for effortless infrastructure, compliance at velocity, and delivery of any app, anywhere.

Unix, Ruby

chef 概念 / y用語

Chef server: Chefサーバーでは送られてきたcookbookを元に、複数あるnodeサーバーに対して一気に指示を送り、cookbook通りの構成が瞬時に整う
Node
Workstation:「インフラの構成をこうしたい」というコードが書かれた「cookbook」を用意

Chef template

chef-repo > site-cookbooks > hello > recipes > default.rb

template "index.html" do 
	path "/var/www/html/index.html"
	source "index.html.erb"
	mode 0644
end

chef-repo > site-cookbooks > hello > templates > default > index.html.erb

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	hello
</body>
</html>

MinGW mysys-opensshとmsys-rsyncのインストール

mysys-opensshとmsys-rsyncをインストールし、windowsの環境変数に、C:\MinGW\bin;C:\MinGW\msys\1.0\binを追加。C:\MinGW\msys\1.0\etc\fstabにc: /cygdrive/cを追加して、再度\chef\chef-repo> knife solo cook 192.168.33.10 –ssh-user vagrantを実行。

resolving cookbooks for run list: ["hello"]
Synchronizing Cookbooks:
  - hello (0.1.0)
Installing Cookbook Gems:
Compiling Cookbooks...
Converging 1 resources
Recipe: hello::default
  * log[Hello world] action write


Running handlers:
Running handlers complete
Chef Client finished, 1/1 resources updated in 46 seconds

出来ました!長かった。。。

workstationからvimをインストール
chef-repo > site-cookbooks > hello > recipes > default.rb

package "vim-enhanced" do 
 action :install 
end
Running Chef: sudo chef-solo -c ~/chef-solo/solo.rb -j ~/chef-solo/dna.json
Starting Chef Client, version 12.16.42
resolving cookbooks for run list: ["hello"]
Synchronizing Cookbooks:
  - hello (0.1.0)
Installing Cookbook Gems:
Compiling Cookbooks...
Converging 1 resources
Recipe: hello::default
  * yum_package[vim-enhanced] action install
    - install version 7.4.629-5.el6 of package vim-enhanced

Running handlers:
Running handlers complete
Chef Client finished, 1/1 resources updated in 02 minutes 02 seconds

a

iptable: stop

service "iptables" do
	action :stop
end
[vagrant@localhost ~]$ sudo service iptables status
iptables: ファイアウォールが稼働していません。

getting started Chef

Chefはインフラをコードで記述する技術で、workstationからchef serverにcookbookを送って、nodeの管理を行います。
Chef

workstationにchefのインストール

gem install chef
gem install knife-solo

knife-configureで設定していきます。

knife configure

1. Chefのリポジトリ(cookbook)を作成…workstation
2. Nodeをchefに対応する
3. cookbookを作る
4. cookbookをnodeに反映

リポジトリ作成

> knife solo init chef-repo
> cd chef-repo

nodeをchef対応にする

> knife solo prepare 192.168.33.10 --ssh-user vagrant
Bootstrapping Chef...
vagrant@192.168.33.10's password:
Thank you for installing Chef!
Generating node config 'nodes/192.168.33.10.json'...

workstationよりcookbookの作成

\chef-repo> knife cookbook create hello -o site-cookbooks

chef-repo > site-cookbooks > hello > recipes > default.rb

#
# Cookbook Name:: hello
# Recipe:: default
#
# Copyright 2016, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#
log "hello world"

chef-repo > nodes > 192.168.33.10.json

{
  "run_list": [
  	"recipe[hello]"
  ],
  "automatic": {
    "ipaddress": "192.168.33.10"
  }
}
\chef\chef-repo> knife solo cook 192.168.33.10 --ssh-user vagrant
ERROR: RuntimeError: Failed to launch command ["rsync", "-rL", "--chmod=ugo=rwX", "--rsh=ssh vag
--exclude=revision-deploys", "--exclude=.git", "--exclude=.hg", "--exclude=.svn", "--exclude=.bz
/gems/knife-solo-0.6.0/lib/knife-solo/resources/patch_cookbooks/", ":~/chef-solo/cookbooks-1"]

rsyncコマンドがないとのエラー表示