knife-soloが失敗して、成功するまでの長い道のり


#失敗メモです
久しぶりにchefでサーバーを構築しようとknife-soloをやったら、エラーの嵐でやっと解決できたので、その過程を残します。だれかの役に立てればと思います。
##実行環境
OSX : 10.10.2(Yosemite)
ruby : ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-darwin13.0]
chef : 12.0.0.rc.0
gem  :  2.4.5

##やりたいこと
単純に```knife solo```をコマンドで実行してchef-soloの準備をしたかった。
##gemの準備
任意のディレクトリでbundlerを初期化

```bash
bundle init
```

続いて生成されたgemfileにgemを指定。knife-solo、berkshelfとサーバー構築のテストを行いたかったのでserverspecも追加。

```ruby:gemfile
# A sample Gemfile
source "https://rubygems.org"

# gem "rails"
gem "knife-solo"
gem "berkshelf"
gem "serverspec"

```

bundlerをインストール。

```bash
bundle install --path=vendor/bundle
```

エラーなくインストールできたので、```knife solo init .```を実行。



```bash
knife solo init .
ffi-yajl/json_gem is deprecated, these monkeypatches will be dropped shortly
/Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:128:in `require': cannot load such file -- chef/encrypted_data_bag_item/check_encrypted (LoadError)
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:128:in `rescue in require'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:39:in `require'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-12.0.0.rc.0/lib/chef/knife/data_bag_secret_options.rb:21:in `<top (required)>'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/lib/chef/knife/core/subcommand_loader.rb:34:in `load'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/lib/chef/knife/core/subcommand_loader.rb:34:in `block in load_commands'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/lib/chef/knife/core/subcommand_loader.rb:34:in `each'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/lib/chef/knife/core/subcommand_loader.rb:34:in `load_commands'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/lib/chef/knife.rb:120:in `load_commands'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/lib/chef/knife.rb:168:in `run'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/lib/chef/application/knife.rb:139:in `run'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/bin/knife:25:in `<top (required)>'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/bin/knife:23:in `load'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/bin/knife:23:in `<main>'
```

エラーで動かなかった。なぜじゃ。(；一_一)




##ffi-yajl/json_gem is deprecated　に注目
エラー文言```ffi-yajl/json_gem is deprecated```に注目
これでGoogleに検索かけると以下のページがヒット

[[妥協]WindowsのCygwinでgem install knife-soloがffi-yajlのエラーでこける件](http://qiita.com/yositani2002/items/a7a119b8a83b6b3998e0)

もっと調べてstackoverflowがヒット

[YAJL internal error: attempted use of non-string object as key](http://stackoverflow.com/questions/24991354/yajl-internal-error-attempted-use-of-non-string-object-as-key)

stackoverflowにかかれていた通り、ffi-yajlというgemをバージョン1.2にしてバージョン1.3.1は消すようにした。

```bash
gem install ffi-yajl -v 1.2
gem uninstall ffi-yajl -v 1.3.1
```

実行してみる。

```
sudo knife solo init .
/Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:128:in `require': cannot load such file -- chef/encrypted_data_bag_item/check_encrypted (LoadError)
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:128:in `rescue in require'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:39:in `require'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-12.0.0.rc.0/lib/chef/knife/data_bag_secret_options.rb:21:in `<top (required)>'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/lib/chef/knife/core/subcommand_loader.rb:34:in `load'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/lib/chef/knife/core/subcommand_loader.rb:34:in `block in load_commands'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/lib/chef/knife/core/subcommand_loader.rb:34:in `each'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/lib/chef/knife/core/subcommand_loader.rb:34:in `load_commands'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/lib/chef/knife.rb:120:in `load_commands'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/lib/chef/knife.rb:168:in `run'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/lib/chef/application/knife.rb:139:in `run'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/bin/knife:25:in `<top (required)>'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/bin/knife:23:in `load'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/bin/knife:23:in `<main>'

```

以前エラーが出る。
```cannot load such file -- chef/encrypted_data_bag_item/check_encrypted (LoadError)```
が解消されない。
(ffi-yajlとは別の問題だった！！)





##cannot load such file -- chef/encrypted_data_bag_item/check_encrypted (LoadError)を潰す
最初パーミッションの問題と思って、lsしてみると、読み込もうとしているファイルの所有者がrootだった。
chownでユーザーを変える。
変えたが同じエラーになった。

残念。

調べるとこんなページが。
[Certain knife operations result in exception on `chef/encrypted_data_bag_item/check_encrypted` #238](https://github.com/chef/chef-dk/issues/238)
→chef DKのバージョンを上げてくれって書かれていた。
→chef DKって何？？
→Chef Development Kitらしい。
→[Chef Development Kit](https://downloads.chef.io/chef-dk/mac/#/)でダウンロードする。


無事にインストールする。バージョンを確認

```bash
chef --version
Chef Development Kit Version: 0.4.0
```

が、エラーは解消されず。

```bash
knife solo init .
/Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:128:in `require': cannot load such file -- chef/encrypted_data_bag_item/check_encrypted (LoadError)
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:128:in `rescue in require'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:39:in `require'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-12.0.0.rc.0/lib/chef/knife/data_bag_secret_options.rb:21:in `<top (required)>'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/lib/chef/knife/core/subcommand_loader.rb:34:in `load'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/lib/chef/knife/core/subcommand_loader.rb:34:in `block in load_commands'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/lib/chef/knife/core/subcommand_loader.rb:34:in `each'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/lib/chef/knife/core/subcommand_loader.rb:34:in `load_commands'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/lib/chef/knife.rb:120:in `load_commands'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/lib/chef/knife.rb:168:in `run'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/lib/chef/application/knife.rb:139:in `run'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/bin/knife:25:in `<top (required)>'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/bin/knife:23:in `load'
	from /Users/satoutakeshi/.rbenv/versions/2.1.3/bin/knife:23:in `<main>'

```

##FATAL: Cannot find sub command for: 'solo init .'のエラー

いろいろ探してパスが間違っている気がしたので、


http://berkshelf.com/

上記からパスの指定を実行

```bash
PATH=$HOME/.chefdk/gem/ruby/2.1.0/bin:/opt/chefdk/bin:$PATH
```

そして再度```knife solo init```

```bash
knife solo init .
FATAL: Cannot find sub command for: 'solo init .'
Available subcommands: (for details, knife SUB-COMMAND --help)

** BOOTSTRAP COMMANDS **
knife bootstrap FQDN (options)

** CLIENT COMMANDS **
knife client bulk delete REGEX (options)
knife client create CLIENT (options)
knife client delete CLIENT (options)
knife client edit CLIENT (options)
knife client list (options)
knife client reregister CLIENT (options)
knife client show CLIENT (options)

** CONFIGURE COMMANDS **
knife configure (options)
knife configure client DIRECTORY

** COOKBOOK COMMANDS **
knife cookbook bulk delete REGEX (options)
knife cookbook create COOKBOOK (options)
knife cookbook delete COOKBOOK VERSION (options)
knife cookbook download COOKBOOK [VERSION] (options)
knife cookbook list (options)
knife cookbook metadata COOKBOOK (options)
knife cookbook metadata from FILE (options)
knife cookbook show COOKBOOK [VERSION] [PART] [FILENAME] (options)
knife cookbook test [COOKBOOKS...] (options)
knife cookbook upload [COOKBOOKS...] (options)

** COOKBOOK SITE COMMANDS **
knife cookbook site download COOKBOOK [VERSION] (options)
knife cookbook site install COOKBOOK [VERSION] (options)
knife cookbook site list (options)
knife cookbook site search QUERY (options)
knife cookbook site share COOKBOOK [CATEGORY] (options)
knife cookbook site show COOKBOOK [VERSION] (options)
knife cookbook site unshare COOKBOOK

** DATA BAG COMMANDS **
knife data bag create BAG [ITEM] (options)
knife data bag delete BAG [ITEM] (options)
knife data bag edit BAG ITEM (options)
knife data bag from file BAG FILE|FOLDER [FILE|FOLDER..] (options)
knife data bag list (options)
knife data bag show BAG [ITEM] (options)

** DECRYPT COMMANDS **
knife decrypt VAULT ITEM [VALUES] (options)

** ENCRYPT COMMANDS **
knife encrypt create VAULT ITEM VALUES (options)
knife encrypt delete VAULT ITEM (options)
knife encrypt remove VAULT ITEM VALUES (options)
knife encrypt rotate keys VAULT ITEM (options)
knife encrypt update VAULT ITEM VALUES (options)

** ENVIRONMENT COMMANDS **
knife environment compare [ENVIRONMENT..] (options)
knife environment create ENVIRONMENT (options)
knife environment delete ENVIRONMENT (options)
knife environment edit ENVIRONMENT (options)
knife environment from file FILE [FILE..] (options)
knife environment list (options)
knife environment show ENVIRONMENT (options)

** EXEC COMMANDS **
knife exec [SCRIPT] (options)

** HELP COMMANDS **
knife help [list|TOPIC]

** INDEX COMMANDS **
knife index rebuild (options)

** KNIFE COMMANDS **
Usage: /opt/chefdk/bin/knife (options)

** NODE COMMANDS **
knife node bulk delete REGEX (options)
knife node create NODE (options)
knife node delete NODE (options)
knife node edit NODE (options)
knife node environment set NODE ENVIRONMENT
knife node from file FILE (options)
knife node list (options)
knife node run_list add [NODE] [ENTRY[,ENTRY]] (options)
knife node run_list remove [NODE] [ENTRIES] (options)
knife node run_list set NODE ENTRIES (options)
knife node show NODE (options)

** PATH-BASED COMMANDS **
knife delete [PATTERN1 ... PATTERNn]
knife deps PATTERN1 [PATTERNn]
knife diff PATTERNS
knife download PATTERNS
knife edit [PATTERN1 ... PATTERNn]
knife list [-dfR1p] [PATTERN1 ... PATTERNn]
knife show [PATTERN1 ... PATTERNn]
knife upload PATTERNS
knife xargs [COMMAND]

** RAW COMMANDS **
knife raw REQUEST_PATH

** RECIPE COMMANDS **
knife recipe list [PATTERN]

** ROLE COMMANDS **
knife role bulk delete REGEX (options)
knife role create ROLE (options)
knife role delete ROLE (options)
knife role edit ROLE (options)
knife role from file FILE [FILE..] (options)
knife role list (options)
knife role show ROLE (options)

** SEARCH COMMANDS **
knife search INDEX QUERY (options)

** SERVE COMMANDS **
Usage: /opt/chefdk/bin/knife (options)

** SPORK COMMANDS **
knife spork bump COOKBOOK [major|minor|patch|manual]
knife spork check COOKBOOK (options)
knife spork data bag create BAG [ITEM] (options)
knife spork data bag delete BAG [ITEM] (options)
knife spork data bag edit BAG ITEM (options)
knife spork data bag from file BAG FILE|FOLDER [FILE|FOLDER..] (options)
knife spork environment check ENVIRONMENT (options)
knife spork environment create ENVIRONMENT (options)
knife spork environment delete ENVIRONMENT (options)
knife spork environment edit ENVIRONMENT (options)
knife spork environment from file FILENAME (options)
knife spork info
knife spork node create NODE (options)
knife spork node delete NODE (options)
knife spork node edit NODE (options)
knife spork node from file FILE (options)
knife spork node run_list add [NODE] [ENTRY[,ENTRY]] (options)
knife spork node run_list add [NODE] [ENTRY[,ENTRY]] (options)
knife spork node run_list set NODE ENTRIES (options)
knife spork omni COOKBOOK (options)
knife spork promote ENVIRONMENT COOKBOOK (options)
knife spork role create ROLE (options)
knife spork role delete ROLENAME (options)
knife spork role edit ROLENAME (options)
knife spork role from file FILENAME (options)
knife spork upload [COOKBOOKS...] (options)

** SSH COMMANDS **
knife ssh QUERY COMMAND (options)

** SSL COMMANDS **
knife ssl check [URL] (options)
knife ssl fetch [URL] (options)

** STATUS COMMANDS **
knife status QUERY (options)

** TAG COMMANDS **
knife tag create NODE TAG ...
knife tag delete NODE TAG ...
knife tag list NODE

** USER COMMANDS **
knife user create USER (options)
knife user delete USER (options)
knife user edit USER (options)
knife user list (options)
knife user reregister USER (options)
knife user show USER (options)

** VAULT COMMANDS **
knife vault create VAULT ITEM VALUES (options)
knife vault decrypt VAULT ITEM [VALUES] (options)
knife vault delete VAULT ITEM (options)
knife vault download VAULT ITEM PATH (options)
knife vault edit VAULT ITEM (options)
knife vault refresh VAULT ITEM
knife vault remove VAULT ITEM VALUES (options)
knife vault rotate all keys
knife vault rotate keys VAULT ITEM (options)
knife vault show VAULT ITEM [VALUES] (options)
knife vault update VAULT ITEM VALUES (options)
```

上手く言ったと思ったらエラーが変わっただけでした。。
よく読む。
→「knifeコマンドに'solo'なんてオプションないよ！」
→そんなばかな。。

同じようなエラーを持つ方の記事を発見

[knife soloが動かない](http://jsapachehtml.hatenablog.com/entry/2014/05/25/083527)

```bash
gem list chef
*** LOCAL GEMS ***

chef (12.0.0.rc.0, 11.16.4, 11.16.2)
chef-zero (3.2.1, 2.2.1)
```
確かに自分も複数バージョンが入っていた。

##古いchefを消す

[How to uninstall chef-client and the whole Chef package](http://stackoverflow.com/questions/17115245/how-to-uninstall-chef-client-and-the-whole-chef-package)
も参考にしてchefを消す作業。


```bash
sudo rm -rf /opt/chefdk/
gem which chef
/Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-12.0.0.rc.0/lib/chef.rb
```

シンボリックリンクを貼り直す。

```bash
satoutashinoAir:chef satoutakeshi$ sudo ln -sf /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-12.0.0.rc.0/bin/chef-apply /usr/bin/chef-apply
satoutashinoAir:chef satoutakeshi$ sudo ln -sf /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-12.0.0.rc.0/bin/chef-client /usr/bin/chef-client
satoutashinoAir:chef satoutakeshi$ sudo ln -sf /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-12.0.0.rc.0/bin/chef-service-manager /usr/bin/chef-service-manager
satoutashinoAir:chef satoutakeshi$ sudo ln -sf /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-12.0.0.rc.0/bin/chef-shell /usr/bin/chef-shell
satoutashinoAir:chef satoutakeshi$ sudo ln -sf /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-12.0.0.rc.0/bin/chef-solo /usr/bin/chef-solo
satoutashinoAir:chef satoutakeshi$ sudo ln -sf /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-12.0.0.rc.0/bin/chef-windows-service /usr/bin/chef-windows-service
satoutashinoAir:chef satoutakeshi$ sudo ln -sf /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-12.0.0.rc.0/bin/knife
satoutashinoAir:chef satoutakeshi$ sudo ln -sf /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-12.0.0.rc.0/bin/knife /usr/bin/knife
satoutashinoAir:chef satoutakeshi$ sudo ln -sf /Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-12.0.0.rc.0/bin/shef /usr/bin/shef
```

そして、```knife solo init .```を実行！

```bash
knife solo init .
-bash: /opt/chefdk/bin/knife: No such file or directory
```

しかしだめ。
エラー解消されず。しかも、コマンドが認識されなくなってしまった。

##やっとエラー解消
コマンドが認識されなくたった時の対処法を載せてくれている方がいた！
[installしたはずなのにコマンドが見つからない時の対処法φ(・・*)](http://qiita.com/hitomi_/items/3bd9fc32cc5600b535bb)

まず、knifeコマンドの場所を探す

```bash
locate knife | grep bin
satoutashinoAir:chef satoutakeshi$ locate knife | grep bin
/Users/satoutakeshi/.rbenv/versions/2.1.3/bin/knife
/Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.2/bin/knife
/Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-11.16.4/bin/knife
/Users/satoutakeshi/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/chef-12.0.0.rc.0/bin/knife
/Users/satoutakeshi/vagrant/unibo/vagrant-rails-chef/vendor/bundler/ruby/2.1.0/bin/knife
/Users/satoutakeshi/vagrant/unibo/vagrant-rails-chef/vendor/bundler/ruby/2.1.0/gems/chef-11.16.4/bin/knife
```

/Users/satoutakeshi/.rbenv/versions/2.1.3/bin/knife
がknifeコマンドの格納場所。
.bash_profileにここのパスを追加する。

```bash
vim ~/.bash_profile 

```

で以下を追加。

```vim
% setenv PATH /Users/satoutakeshi/.rbenv/versions/2.1.3/bin:$PATH
```

.bash_profileを再読み込み

```bash
source ~/.bash_profile 
```

そして、knife soloを実行！
そのまえに、最初の設定をしてなかったので、する。


```
knife configure
knife solo init .

```

行けた！
長かった。