ChefDK + Test-Kitchen + Vagrant + RedHat/CentOS でJenkinsサーバを構築

以下を参考に、ChefDK上のTest-KitchenでJenkinsサーバを起動させてみた

- [RedHat Linux RPM packages for Jenkins](http://devopspy.com/linux/install-java-8-on-centos-rhel-7/)
- [Vagrantとchefを使ってCentOS6.4にCI(Jenkins)サーバを構築](https://qiita.com/amasok23/items/2483f5f3e9fe371d1d92)

#環境
- macOS : 10.13.4
- ChefDK : 3.0.36
- Vagrant : 2.1.2
- VirtualBox : 5.2.12
- CentOS : 7.4.1708


#クックブック作成から

```bash


$ chef generate cookbook jenkins
...
$ cd jenkins

$ vi .kitchen.yml                       # ネットワーク構成
...

$ vi recipes/default.rb                 # インストールレシピ
...

$ vi test/smoke/default/default_test.rb # オプション。InSpecテスト
...



```
##.kitchen.yml
test-kitchen設定

```yml:.kitchen.yml
---
driver:
  name: vagrant

provisioner:
  name: chef_zero
  # You may wish to disable always updating cookbooks in CI or other testing environments.
  # For example:
  #   always_update_cookbooks: <%= !ENV['CI'] %>
  always_update_cookbooks: true

verifier:
  name: inspec

platforms:
  #- name: ubuntu-16.04
  - name: centos-7

suites:
  #- name: default
  - name: jenkins
    driver:                                               # 追記
      network:                                            # 追記
        - ["private_network", {ip: "192.168.33.10"}]      # 追記 (以下でも可)
        - ["forwarded_port", {guest: 8080, host: 58080}]  # 追記
      - recipe[jenkins::default]
    verifier:
      inspec_tests:
        - test/smoke/default
    attributes:
```
主に、private_network のアドレスでの接続設定、もしくは 127.0.0.1:<ポート> での接続設定を追記

##recipes/default.rb
Jenkins導入起動レシピ

```ruby:
#
# Cookbook:: jenkins
# Recipe:: default
#
# Copyright:: 2018, The Authors, All Rights Reserved.

service 'firewalld' do
  action [:disable, :stop]
end

package 'java-1.8.0-openjdk' # 2.54 (2017-04) and newer: Java 8

remote_file '/etc/yum.repos.d/jenkins.repo' do
  source 'https://pkg.jenkins.io/redhat-stable/jenkins.repo'
end

execute 'rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key' do
  not_if 'rpm -qi gpg-pubkey-d50582e6-4a3feef6'
end

package 'jenkins'

service 'jenkins' do
  action [:enable, :start]
end

```
公式の導入手順準拠

##test/smoke/default/default_test.rb
InSpecのテスト

```ruby:test/smoke/default/default_test.rb


# # encoding: utf-8

# Inspec test for recipe jenkins::default

# The Inspec reference, with examples and extensive documentation, can be
# found at http://inspec.io/docs/reference/resources/

describe package('java-1.8.0-openjdk') do
  it { should be_installed }
  its('version') { should cmp >= '1.8.0' }
end

describe package('jenkins') do
  it { should be_installed }
  its('version') { should cmp >= '2.54' }
end

```
無くても。

#vm起動、Chef導入、Jenkins導入起動、InSpecテスト

```
$ kitchen test -d never
-----> Starting Kitchen (v1.21.2)
-----> Cleaning up any prior instances of <jenkins-centos-7>
-----> Destroying <jenkins-centos-7>...
       Finished destroying <jenkins-centos-7> (0m0.00s).
-----> Testing <jenkins-centos-7>
-----> Creating <jenkins-centos-7>...
       Bringing machine 'default' up with 'virtualbox' provider...
       ==> default: Importing base box 'bento/centos-7'...
==> default: Matching MAC address for NAT networking...
       ==> default: Checking if box 'bento/centos-7' is up to date...
       ==> default: Setting the name of the VM: jenkins-centos-7_default_1530402680277_51812
       ==> default: Clearing any previously set network interfaces...
       ==> default: Preparing network interfaces based on configuration...
           default: Adapter 1: nat
           default: Adapter 2: hostonly
       ==> default: Forwarding ports...
           default: 8080 (guest) => 58080 (host) (adapter 1)
           default: 22 (guest) => 2222 (host) (adapter 1)
       ==> default: Booting VM...
       ==> default: Waiting for machine to boot. This may take a few minutes...
           default: SSH address: 127.0.0.1:2222
           default: SSH username: vagrant
           default: SSH auth method: private key
           default: Warning: Connection reset. Retrying...
           default: Warning: Remote connection disconnect. Retrying...
           default: Warning: Connection reset. Retrying...
           default: Warning: Remote connection disconnect. Retrying...
           default: Warning: Connection reset. Retrying...
           default: Warning: Remote connection disconnect. Retrying...
           default: Warning: Connection reset. Retrying...
           default: Warning: Remote connection disconnect. Retrying...
           default: Warning: Connection reset. Retrying...
           default: Warning: Remote connection disconnect. Retrying...
           default: Warning: Connection reset. Retrying...
           default: Warning: Remote connection disconnect. Retrying...
           default: Warning: Connection reset. Retrying...
           default: Warning: Remote connection disconnect. Retrying...
           default: Warning: Connection reset. Retrying...
           default: Warning: Remote connection disconnect. Retrying...
           default: Warning: Connection reset. Retrying...
           default: Warning: Remote connection disconnect. Retrying...
           default: Warning: Connection reset. Retrying...
           default: Warning: Remote connection disconnect. Retrying...
           default: Warning: Connection reset. Retrying...
           default: Warning: Remote connection disconnect. Retrying...
           default: Warning: Connection reset. Retrying...
           default: Warning: Remote connection disconnect. Retrying...
           default: Warning: Connection reset. Retrying...
           default: Warning: Remote connection disconnect. Retrying...
           default: Warning: Connection reset. Retrying...
           default: Warning: Remote connection disconnect. Retrying...
           default: Warning: Connection reset. Retrying...
           default: Warning: Remote connection disconnect. Retrying...
           default: Warning: Connection reset. Retrying...
           default: Warning: Remote connection disconnect. Retrying...
           default: Warning: Connection reset. Retrying...
           default: Warning: Remote connection disconnect. Retrying...
           default: Warning: Connection reset. Retrying...
           default: Warning: Remote connection disconnect. Retrying...
           default: 
           default: Vagrant insecure key detected. Vagrant will automatically replace
           default: this with a newly generated keypair for better security.
           default: 
           default: Inserting generated public key within guest...
           default: Removing insecure key from the guest if it's present...
           default: Key inserted! Disconnecting and reconnecting using new SSH key...
       ==> default: Machine booted and ready!
       ==> default: Checking for guest additions in VM...
       ==> default: Setting hostname...
       ==> default: Configuring and enabling network interfaces...
           default: SSH address: 127.0.0.1:2222
           default: SSH username: vagrant
           default: SSH auth method: private key
       ==> default: Mounting shared folders...
           default: /tmp/omnibus/cache => /Users/aa220269/.kitchen/cache
       ==> default: Machine not provisioned because `--no-provision` is specified.
       [SSH] Established
       Vagrant instance <jenkins-centos-7> created.
       Finished creating <jenkins-centos-7> (1m16.07s).
-----> Converging <jenkins-centos-7>...
       Preparing files for transfer
       Preparing dna.json
       Resolving cookbook dependencies with Berkshelf 7.0.2...
       Removing non-cookbook files before transfer
       Preparing validation.pem
       Preparing client.rb
-----> Installing Chef Omnibus (install only if missing)
       Downloading https://omnitruck.chef.io/install.sh to file /tmp/install.sh
       Trying wget...
       Download complete.
       el 7 x86_64
       Getting information for chef stable  for el...
       downloading https://omnitruck.chef.io/stable/chef/metadata?v=&p=el&pv=7&m=x86_64
         to file /tmp/install.sh.3522/metadata.txt
       trying wget...
       sha1	bf0bd662c5b46d81418c943ac4bc6b84c095aa9c
       sha256	abdb676a8d55fc9807b4eabe5313c54d3946977d7ae3eecd7303cd388b04042a
       url	https://packages.chef.io/files/stable/chef/14.2.0/el/7/chef-14.2.0-1.el7.x86_64.rpm
       version	14.2.0
       downloaded metadata file looks valid...
       /tmp/omnibus/cache/chef-14.2.0-1.el7.x86_64.rpm exists
       Comparing checksum with sha256sum...
       
       WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
       
       You are installing an omnibus package without a version pin.  If you are installing
       on production servers via an automated process this is DANGEROUS and you will
       be upgraded without warning on new releases, even to new major releases.
       Letting the version float is only appropriate in desktop, test, development or
       CI/CD environments.
       
       WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
       
       Installing chef 
       installing with rpm...
       警告: /tmp/omnibus/cache/chef-14.2.0-1.el7.x86_64.rpm: ヘッダー V4 DSA/SHA1 Signature、鍵 ID 83ef826a: NOKEY
       準備しています...              ################################# [100%]
       更新中 / インストール中...
          1:chef-14.2.0-1.el7                ################################# [100%]
       Thank you for installing Chef!
       Transferring files to <jenkins-centos-7>
       Starting Chef Client, version 14.2.0
       Creating a new client identity for jenkins-centos-7 using the validator key.
       resolving cookbooks for run list: ["jenkins::default"]
       Synchronizing Cookbooks:
         - jenkins (0.1.0)
       Installing Cookbook Gems:
       Compiling Cookbooks...
       Converging 6 resources
       Recipe: jenkins::default
         * service[firewalld] action disable (up to date)
         * service[firewalld] action stop (up to date)
         * yum_package[java-1.8.0-openjdk] action install
           - install version 1:1.8.0.171-8.b10.el7_5.x86_64 of package java-1.8.0-openjdk
         * remote_file[/etc/yum.repos.d/jenkins.repo] action create
           - create new file /etc/yum.repos.d/jenkins.repo
           - update content in file /etc/yum.repos.d/jenkins.repo from none to 9d8369
           --- /etc/yum.repos.d/jenkins.repo	2018-06-30 23:52:55.569968119 +0000
           +++ /etc/yum.repos.d/.chef-jenkins20180630-3642-1qy4qvq.repo	2018-06-30 23:52:55.567967227 +0000
           @@ -1 +1,5 @@
           +[jenkins]
           +name=Jenkins-stable
           +baseurl=http://pkg.jenkins.io/redhat-stable
           +gpgcheck=1
           - restore selinux security context
         * execute[rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key] action run
           - execute rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
         * yum_package[jenkins] action install
           - install version 0:2.121.1-1.1.noarch of package jenkins
         * service[jenkins] action enable (up to date)
         * service[jenkins] action start
           - start service service[jenkins]
       
       Running handlers:
       Running handlers complete
       Chef Client finished, 5/8 resources updated in 01 minutes 00 seconds
       Downloading files from <jenkins-centos-7>
       Finished converging <jenkins-centos-7> (1m16.45s).
-----> Setting up <jenkins-centos-7>...
       Finished setting up <jenkins-centos-7> (0m0.00s).
-----> Verifying <jenkins-centos-7>...
       Loaded tests from {:path=>".Users.aa220269.repo.repo-test.cookbooks.jenkins.test.smoke.default"} 

Profile: tests from {:path=>"/Users/aa220269/repo/repo-test/cookbooks/jenkins/test/smoke/default"} (tests from {:path=>".Users.aa220269.repo.repo-test.cookbooks.jenkins.test.smoke.default"})
Version: (not specified)
Target:  ssh://vagrant@127.0.0.1:2222

  System Package java-1.8.0-openjdk
     ✔  should be installed
     ✔  version should cmp >= "1.8.0"
  System Package jenkins
     ✔  should be installed
     ✔  version should cmp >= "2.54"

Test Summary: 4 successful, 0 failures, 0 skipped
       Finished verifying <jenkins-centos-7> (0m9.11s).
       Finished testing <jenkins-centos-7> (2m41.64s).
-----> Kitchen is finished. (2m44.45s)
```
問題なし

##Administrator passwordの表示

```
$ kitchen exec -c 'sudo cat /var/lib/jenkins/secrets/initialAdminPassword'
-----> Execute command on jenkins-centos-7.
       9e499d0dab1544e2a515dfd47e836d04
```

後で使用。

#Jenkinsへの接続
webブラウザーで http://192.168.33.10:8080 (もしくは http://127.0.0.1:58080) に接続。

Getting Started
Unlock Jenkins
の画面が表示されたら、成功
Administrator passwordの項にkitchen execで表示させた文字列を入力してContinue