Chef検証(4) レシピ実行

# 稼働確認

## Cookbook の用意

- 以下のCoockbookを事前にダウンロードし、Chef Workstationに配置します。
    - https://supermarket.chef.io/cookbooks/chef-client
    - https://supermarket.chef.io/cookbooks/cron
    - https://supermarket.chef.io/cookbooks/logrotate
    - https://supermarket.chef.io/cookbooks/ohai
   
- tar -tzf コマンドで解凍ファイルの確認。
- tar -xzf コマンドで解凍実行。

```
inohana@ino-ubuntu:~$ ls -la ~/chef-repo/cookbooks/
total 180
drwxrwxr-x 3 inohana inohana  4096 Mar 10 04:47 .
drwxrwxr-x 5 inohana inohana  4096 Mar 10 04:44 ..
-rw-r--r-- 1 inohana inohana 36478 Mar 10  2017 chef-client20170224-18693-kljzie.tar.gz
drwxrwxr-x 4 inohana inohana  4096 Mar 10 04:43 chef-repo
-rw-r--r-- 1 inohana inohana 51079 Mar 10  2017 compat_resource20161220-68836-19c0oql.tar.gz
-rw-r--r-- 1 inohana inohana  7783 Mar 10  2017 cron20170307-2625-1nxvs0i.tar.gz
-rw-r--r-- 1 inohana inohana 14512 Mar 10  2017 logrotate.tgz
-rw-r--r-- 1 inohana inohana 53706 Mar 10  2017 windows20161123-51892-1tz3m2.tar.gz

inohana@ino-ubuntu:~/chef-repo/cookbooks$ tar -tzf chef-client20170224-18693-kljzie.tar.gz
chef-client/.foodcritic
chef-client/README.md
chef-client/CHANGELOG.md
chef-client/CONTRIBUTING.md
chef-client/MAINTAINERS.md
(略)
chef-client/templates/freebsd
chef-client/templates/freebsd/chef-client.erb
chef-client/templates/freebsd/chef.erb
chef-client/templates/windows
chef-client/templates/windows/client.service.rb.erb

inohana@ino-ubuntu:~/chef-repo/cookbooks$ tar -xzf chef-client20170224-18693-kljzie.tar.gz

```

## Chef Server への Cookbook Upload

- ダウンロードしたchef-client CookbookをChef ServerにUpload します。

```
inohana@ino-ubuntu:~/chef-repo$ knife cookbook upload chef-client --include-dependencies
Uploading chef-client    [7.2.0]
Uploading cron           [4.0.0]
Uploading logrotate      [2.1.0]
Uploading windows        [2.1.1]
Uploading compat_resource [12.16.3]
Uploading ohai           [5.0.0]
Uploaded 6 cookbooks.
inohana@ino-ubuntu:~/chef-repo$ knife cookbook list
chef-client       7.2.0
compat_resource   12.16.3
cron              4.0.0
logrotate         2.1.0
ohai              5.0.0
windows           2.1.1
```

## レシピを実行

- エラー。
    - どうやらVer12.5以前のChef Clientで起こる不具合のようです。
        - https://github.com/chef-cookbooks/iptables/issues/49
    - Chef Clientを[Ciscoのサイト](http://www.cisco.com/c/en/us/td/docs/switches/datacenter/nexus9000/sw/7-x/programmability/guide/b_Cisco_Nexus_9000_Series_NX-OS_Programmability_Guide_7x/b_Cisco_Nexus_9000_Series_NX-OS_Programmability_Guide_7x_chapter_01110.html)からダウンロードすると古いバージョンを入れさせられるようです。。。
    - 新しいChef Clientは[Chefのサイト](https://downloads.chef.io/chef/stable/12.19.33#nexus)で入手しましょう。ただし、最新Ver(現時点で12.19.36)ではCisco NX-OSが提供されていません。。。

```
inohana@ino-ubuntu:~/chef-repo$ knife ssh name:chefn9k "sudo chef-client -o 'recipe[chef-client::delete_validation]'" -x chefuser01 -p 2222
chefuser01@chefn9k's password:
chefn9k [2017-03-10T03:07:40+00:00] WARN: Please install an English UTF-8 locale for Chef to use, falling back to C locale and disabling UTF-8 support.
chefn9k Starting Chef Client, version 12.4.1
chefn9k [2017-03-10T03:07:42+00:00] WARN: Run List override has been provided.
chefn9k [2017-03-10T03:07:42+00:00] WARN: Original Run List: []
chefn9k [2017-03-10T03:07:42+00:00] WARN: Overridden Run List: [recipe[chef-client::delete_validation]]
chefn9k resolving cookbooks for run list: ["chef-client::delete_validation"]
chefn9k Synchronizing Cookbooks:
chefn9k   - chef-client
chefn9k   - cron
chefn9k   - logrotate
chefn9k   - compat_resource
chefn9k   - windows
chefn9k   - ohai
chefn9k Compiling Cookbooks...
chefn9k
chefn9k ================================================================================
chefn9k Recipe Compile Error in /var/chef/cache/cookbooks/cron/resources/d.rb
chefn9k ================================================================================
chefn9k
chefn9k NoMethodError
chefn9k -------------
chefn9k undefined method `property' for #<Class:0x000000051ae668>
chefn9k
chefn9k Cookbook Trace:
chefn9k ---------------
chefn9k   /var/chef/cache/cookbooks/cron/resources/d.rb:20:in `class_from_file'
chefn9k   /var/chef/cache/cookbooks/compat_resource/files/lib/chef_compat/monkeypatches/chef/resource/lwrp_base.rb:50:in `build_from_file'
chefn9k
chefn9k Relevant File Content:
chefn9k ----------------------
chefn9k /var/chef/cache/cookbooks/cron/resources/d.rb:
chefn9k
chefn9k  13:  # Unless required by applicable law or agreed to in writing, software
chefn9k  14:  # distributed under the License is distributed on an "AS IS" BASIS,
chefn9k  15:  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
chefn9k  16:  # See the License for the specific language governing permissions and
chefn9k  17:  # limitations under the License.
chefn9k  18:  #
chefn9k  19:
chefn9k  20>> property :name, String, name_property: true
chefn9k  21:  property :cookbook, String, default: 'cron'
chefn9k  22:
chefn9k  23:  property :predefined_value, [String], callbacks: { 'should be a valid predefined value' => ->(spec) { validate_predefined_value(spec) } }
chefn9k  24:  property :minute, [Integer, String], default: '*', callbacks: { 'should be a valid minute spec' => ->(spec) { validate_numeric(spec, 0, 59) } }
chefn9k  25:  property :hour, [Integer, String], default: '*', callbacks: { 'should be a valid hour spec' => ->(spec) { validate_numeric(spec, 0, 23) } }
chefn9k  26:  property :day, [Integer, String], default: '*', callbacks: { 'should be a valid day spec' => ->(spec) { validate_numeric(spec, 1, 31) } }
chefn9k  27:  property :month, [Integer, String], default: '*', callbacks: { 'should be a valid month spec' => ->(spec) { validate_month(spec) } }
chefn9k  28:  property :weekday, [Integer, String], default: '*', callbacks: { 'should be a valid weekday spec' => ->(spec) { validate_dow(spec) } }
chefn9k  29:
chefn9k
chefn9k
chefn9k Running handlers:
chefn9k [2017-03-10T03:07:47+00:00] ERROR: Running exception handlers
chefn9k Running handlers complete
chefn9k [2017-03-10T03:07:47+00:00] ERROR: Exception handlers complete
chefn9k Chef Client failed. 0 resources updated in 5.952580274 seconds
chefn9k [2017-03-10T03:07:47+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
chefn9k [2017-03-10T03:07:47+00:00] ERROR: undefined method `property' for #<Class:0x000000051ae668>
chefn9k [2017-03-10T03:07:47+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
```

### Chef Clientのリプレイス

- SCPでファイルをGuestshellに配置。
- rpmコマンドでChef Client Installするもエラー発生。
    - ディスクサイズが足りない模様orz

```
[guestshell@chefn9k ssh]$ rpm -qa | grep chef
chef-12.4.1.cisco+20150826204615-1.el7.x86_64

[guestshell@chefn9k ~]$ chvrf management scp inohana@172.22.150.100:/home/inohana/chef-12.19.33-1.nexus7.x86_64.rpm .
inohana@172.22.150.100's password:
chef-12.19.33-1.nexus7.x86_64.rpm                                                                            100%   35MB

[guestshell@chefn9k ~]$ rpm -U chef-12.19.33-1.nexus7.x86_64.rpm
warning: chef-12.19.33-1.nexus7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 56e74822: NOKEY
error: can't create transaction lock on /var/lib/rpm/.rpm.lock (Permission denied)
[guestshell@chefn9k ~]$ sudo rpm -U chef-12.19.33-1.nexus7.x86_64.rpm
warning: chef-12.19.33-1.nexus7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 56e74822: NOKEY
        installing package chef-12.19.33-1.nexus7.x86_64 needs 125MB on the / filesystem

# 125MB不足。

[guestshell@chefn9k ~]$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop8      388M  333M   36M  91% /
none            8.9G  971M  7.9G  11% /cisco
none            2.0G  4.0K  2.0G   1% /cisco/core
none            5.0M  1.8M  3.3M  36% /etc/shared
none            300M  380K  300M   1% /var/nginx_local
none            200M   41M  160M  21% /var/run/netns
none            2.0G  4.0K  2.0G   1% /volatile
/dev/sda4       7.5G  4.1G  3.4G  55% /bootflash
/dev            7.9G     0  7.9G   0% /dev
none            8.9G  971M  7.9G  11% /cisco
none            2.0G  4.0K  2.0G   1% /cisco/core
none            5.0M  1.8M  3.3M  36% /etc/shared
none            300M  380K  300M   1% /var/nginx_local
none            200M   41M  160M  21% /var/run/netns
none            2.0G  4.0K  2.0G   1% /volatile
none            2.0G  4.0K  2.0G   1% /cisco/core
tmpfs           7.9G     0  7.9G   0% /dev/shm
tmpfs           7.9G  8.1M  7.9G   1% /run
tmpfs           7.9G  8.0K  7.9G   1% /sys/fs/cgroup
none            7.9G   42M  7.8G   1% /var/volatile

#　古いバージョンのChef ClientをUninstallしてから実行。

[guestshell@chefn9k ~]$ sudo rpm -e chef-12.4.1.cisco+20150826204615-1.el7.x86_64

# 成功。

[guestshell@chefn9k ~]$ sudo rpm -U chef-12.19.33-1.nexus7.x86_64.rpm
warning: chef-12.19.33-1.nexus7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 56e74822: NOKEY
Thank you for installing Chef!

[guestshell@chefn9k ~]$ rpm -qa | grep chef
chef-12.19.33-1.nexus7.x86_64

```

### もう一度レシピを実行。

```
inohana@ino-ubuntu:~/chef-repo$ knife ssh name:chefn9k "sudo chef-client -o 'recipe[chef-client::delete_validation]'" -x chefuser01 -p 2222

chefn9k [2017-03-10T04:03:06+00:00] WARN: Please install an English UTF-8 locale for Chef to use, falling back to C locale and disabling UTF-8 support.
chefn9k [2017-03-10T04:03:07+00:00] INFO: Forking chef instance to converge...
chefn9k Starting Chef Client, version 12.19.33
chefn9k [2017-03-10T04:03:07+00:00] INFO: *** Chef 12.19.33 ***
chefn9k [2017-03-10T04:03:07+00:00] INFO: Platform: x86_64-linux
chefn9k [2017-03-10T04:03:07+00:00] INFO: Chef-client pid: 3220
chefn9k [2017-03-10T04:03:09+00:00] WARN: Run List override has been provided.
chefn9k [2017-03-10T04:03:09+00:00] WARN: Original Run List: []
chefn9k [2017-03-10T04:03:09+00:00] WARN: Overridden Run List: [recipe[chef-client::delete_validation]]
chefn9k [2017-03-10T04:03:09+00:00] INFO: Run List is [recipe[chef-client::delete_validation]]
chefn9k [2017-03-10T04:03:09+00:00] INFO: Run List expands to [chef-client::delete_validation]
chefn9k [2017-03-10T04:03:09+00:00] INFO: Starting Chef Run for chefn9k
chefn9k [2017-03-10T04:03:09+00:00] INFO: Running start handlers
chefn9k [2017-03-10T04:03:09+00:00] INFO: Start handlers complete.
chefn9k [2017-03-10T04:03:09+00:00] INFO: HTTP Request Returned 404 Not Found:
chefn9k [2017-03-10T04:03:09+00:00] INFO: HTTP Request Returned 404 Not Found:
chefn9k [2017-03-10T04:03:09+00:00] INFO: Error while reporting run start to Data Collector. URL: https://ino-ubuntu.iselab.local/organizations/ise/data-collector Exception: 404 -- 404 "Not Found"  (This is normal if you do not have Chef Automate)
chefn9k resolving cookbooks for run list: ["chef-client::delete_validation"]
chefn9k [2017-03-10T04:03:10+00:00] INFO: Loading cookbooks [chef-client@7.2.0, cron@4.0.0, logrotate@2.1.0, compat_resource@12.16.3, windows@2.1.1, ohai@5.0.0]
chefn9k [2017-03-10T04:03:10+00:00] INFO: Skipping removal of obsoleted cookbooks from the cache
chefn9k Synchronizing Cookbooks:
chefn9k   - cron (4.0.0)
chefn9k   - chef-client (7.2.0)
chefn9k   - logrotate (2.1.0)
chefn9k   - compat_resource (12.16.3)
chefn9k   - windows (2.1.1)
chefn9k   - ohai (5.0.0)
chefn9k Installing Cookbook Gems:
chefn9k Compiling Cookbooks...
chefn9k Converging 1 resources
chefn9k Recipe: chef-client::delete_validation
chefn9k   * file[/etc/chef/validation.pem] action delete[2017-03-10T04:03:10+00:00] INFO: Processing file[/etc/chef/validation.pem] action delete (chef-client::delete_validation line 35)
chefn9k  (up to date)
chefn9k [2017-03-10T04:03:10+00:00] WARN: Skipping final node save because override_runlist was given
chefn9k [2017-03-10T04:03:10+00:00] INFO: Chef Run complete in 1.556623256 seconds
chefn9k [2017-03-10T04:03:10+00:00] INFO: Skipping removal of unused files from the cache
chefn9k
chefn9k Running handlers:
chefn9k [2017-03-10T04:03:10+00:00] INFO: Running report handlers
chefn9k Running handlers complete
chefn9k [2017-03-10T04:03:10+00:00] INFO: Report handlers complete
chefn9k
chefn9k Deprecated features used!
chefn9k   Default value "*" is invalid for property minute of resource cron_d. In Chef 13 this will become an error: undefined method `validate_numeric' for #<Class:0x0000000265aac0>. at 1 location:
chefn9k     - /var/chef/cache/cookbooks/cron/resources/d.rb:24:in `class_from_file'
chefn9k    See https://docs.chef.io/deprecations_custom_resource_cleanups.html for further details.
chefn9k   Default value "*" is invalid for property hour of resource cron_d. In Chef 13 this will become an error: undefined method `validate_numeric' for #<Class:0x0000000265aac0>. at 1 location:
chefn9k     - /var/chef/cache/cookbooks/cron/resources/d.rb:25:in `class_from_file'
chefn9k    See https://docs.chef.io/deprecations_custom_resource_cleanups.html for further details.
chefn9k   Default value "*" is invalid for property day of resource cron_d. In Chef 13 this will become an error: undefined method `validate_numeric' for #<Class:0x0000000265aac0>. at 1 location:
chefn9k     - /var/chef/cache/cookbooks/cron/resources/d.rb:26:in `class_from_file'
chefn9k    See https://docs.chef.io/deprecations_custom_resource_cleanups.html for further details.
chefn9k   Default value "*" is invalid for property month of resource cron_d. In Chef 13 this will become an error: undefined method `validate_month' for #<Class:0x0000000265aac0>. at 1 location:
chefn9k     - /var/chef/cache/cookbooks/cron/resources/d.rb:27:in `class_from_file'
chefn9k    See https://docs.chef.io/deprecations_custom_resource_cleanups.html for further details.
chefn9k   Default value "*" is invalid for property weekday of resource cron_d. In Chef 13 this will become an error: undefined method `validate_dow' for #<Class:0x0000000265aac0>. at 1 location:
chefn9k     - /var/chef/cache/cookbooks/cron/resources/d.rb:28:in `class_from_file'
chefn9k    See https://docs.chef.io/deprecations_custom_resource_cleanups.html for further details.
chefn9k
chefn9k Chef Client finished, 0/1 resources updated in 02 seconds
```