PR

【Windows】Vagrant + Chef で環境構築を自動化しよう!

chef Windows

前回、Vagrant のプラグインを導入し、Chefを使う準備をしました。

Vagrant + VirtualBox でWindows上に開発環境を構築する(2)

今回は、Chefをインストールしてみたいと思います。

Chef DKをインストールする

ダウンロード

ChefDKのダウンロードページのWindows10の「Architecture: x86_64」の右にある「Download」をクリックして、Windows用のChefDKのインストーラーをダウンロードします。各自の環境にあったものを選択してください。

インストールは、インストーラに従い進めればOKです。

Chefリポジトリを作成する

デスクトップショートカット「ChefDK」を右クリックして、「管理者として実行」で起動します。PowerShellが起動します。

今回はFドライブの直下にリポジトリを作成して、そこで作業します。 (自分の環境に合わせて任意の場所で作業してください。)

PS C:\WINDOWS\system32> cd f:
PS F:\> chef generate repo chef-repo
C:/opscode/chefdk/embedded/lib/ruby/gems/2.6.0/gems/ffi-1.12.1-x64-mingw32/lib/ffi/struct.rb:207:in `layout': [DEPRECATION] Struct layout is already defined for class Windows::ServiceStructs::SERVICE_STATUS_PROCESS. Redefinition as in C:/opscode/chefdk/embedded/lib/ruby/gems/2.6.0/gems/win32-service-2.1.4/lib/win32/windows/structs.rb:72:in `<class:SERVICE_STATUS_PROCESS>' will be disallowed in ffi-2.0. (StructuredWarnings::StandardWarning)
+---------------------------------------------+
            Chef License Acceptance

Before you can continue, 3 product licenses
must be accepted. View the license at
https://www.chef.io/end-user-license-agreement/

Licenses that need accepting:
  * Chef Development Kit
  * Chef Infra Client
  * Chef InSpec

Do you accept the 3 product licenses (yes/no)?

 > yes

Persisting 3 product licenses...
✔ 3 product licenses persisted.

+---------------------------------------------+
Generating Chef Infra repo chef-repo
- Ensuring correct Chef Infra repo file content

Your new Chef Infra repo is ready! Type `cd chef-repo` to enter it.
PS F:\> cd chef-repo
PS F:\chef-repo>

apacheをインストール&起動するcookbookを作成します。

PS F:\chef-repo> cd .\cookbooks\
PS F:\chef-repo\cookbooks> chef generate cookbook apache
C:/opscode/chefdk/embedded/lib/ruby/gems/2.6.0/gems/ffi-1.12.1-x64-mingw32/lib/ffi/struct.rb:207:in `layout': [DEPRECATION] Struct layout is already defined for class Windows::ServiceStructs::SERVICE_STATUS_PROCESS. Redefinition as in C:/opscode/chefdk/embedded/lib/ruby/gems/2.6.0/gems/win32-service-2.1.4/lib/win32/windows/structs.rb:72:in `<class:SERVICE_STATUS_PROCESS>' will be disallowed in ffi-2.0. (StructuredWarnings::StandardWarning)
Generating cookbook apache
- Ensuring correct cookbook content

Your cookbook is ready. Type `cd apache` to enter it.

There are several commands you can run to get started locally developing and testing your cookbook.
Type `delivery local --help` to see a full list of local testing commands.

Why not start by writing an InSpec test? Tests for the default recipe are stored at:

test/integration/default/default_test.rb

If you'd prefer to dive right in, the default recipe can be found at:

recipes/default.rb

PS F:\chef-repo\cookbooks>

F:\chef-repo\cookbooks\apache\recipes\default.rb を開いて、apacheのインストール&起動の設定を行います。

#
# Cookbook:: apache
# Recipe:: default
#
# Copyright:: 2020, The Authors, All Rights Reserved.
package "httpd" do
  action :install
end

service "httpd" do
  action [ :enable, :start ]
end

Vagrantを起動する

起動の前に、Chefとの連携を設定しないといけません。

F:\vagrant\centos\Vagrantfile を開いて、下記を追加します。
一番最下部のendより上に追加すればOKです。

  config.omnibus.chef_version = "15.7.32"
  config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = "F:/chef-repo/cookbooks"
    chef.add_recipe "apache"
  end

Vagrantを起動します。

C:\WINDOWS\system32>cd /d f:\vagrant\centos

f:\vagrant\centos>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'bento/centos-6.7' version '2.2.7' is up to date...
==> default: Clearing any previously set forwarded ports...
==> 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: 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 aborted. Retrying...
==> default: Machine booted and ready!
[default] GuestAdditions seems to be installed (6.0.16) correctly, but not running.
VirtualBox Guest Additions: Starting.
VirtualBox Guest Additions: Building the VirtualBox Guest Additions kernel
modules.  This may take a while.
VirtualBox Guest Additions: To build modules for other installed kernels, run
VirtualBox Guest Additions:   /sbin/rcvboxadd quicksetup <version>
VirtualBox Guest Additions: or
VirtualBox Guest Additions:   /sbin/rcvboxadd quicksetup all
VirtualBox Guest Additions: Running kernel modules will not be replaced until
the system is restarted
VirtualBox Guest Additions: Starting.
VirtualBox Guest Additions: Building the VirtualBox Guest Additions kernel
modules.  This may take a while.
VirtualBox Guest Additions: To build modules for other installed kernels, run
VirtualBox Guest Additions:   /sbin/rcvboxadd quicksetup <version>
VirtualBox Guest Additions: or
VirtualBox Guest Additions:   /sbin/rcvboxadd quicksetup all
VirtualBox Guest Additions: Building the modules for kernel
2.6.32-573.el6.x86_64.
VirtualBox Guest Additions: Running kernel modules will not be replaced until
the system is restarted
Restarting VM to apply changes...
==> default: Attempting graceful shutdown of VM...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: Warning: Connection reset. Retrying...
    default: Warning: Connection aborted. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => F:/vagrant/centos
    default: /tmp/vagrant-chef/b8f9988f119676fe58f7fedfb779c05e/cookbooks => F:/chef-repo/cookbooks
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

“vagrant provision”を実施しろと言われているようなので、実施します。

f:\vagrant\centos>vagrant provision
==> default: Installing Chef 15.7.32 Omnibus package...
==> default: el 6 x86_64
==> default: Getting information for chef stable 15.7.32 for el...
==> default: downloading https://www.chef.io/stable/chef/metadata?v=15.7.32&p=el&pv=6&m=x86_64
==> default:   to file /tmp/install.sh.3684/metadata.txt
==> default: trying wget...
==> default: sha1       9cf007f150b3e70eaea49930e2faf081fb77cebf
==> default: sha256     1da1dd57c084a0470607fc997db1672695b6212c784cbd18c3dbe3f820877d5f
==> default: url        https://packages.chef.io/files/stable/chef/15.7.32/el/6/chef-15.7.32-1.el6.x86_64.rpm
==> default: version    15.7.32

==> default: downloaded metadata file looks valid...
==> default: downloading https://packages.chef.io/files/stable/chef/15.7.32/el/6/chef-15.7.32-1.el6.x86_64.rpm
==> default:   to file /tmp/install.sh.3684/chef-15.7.32-1.el6.x86_64.rpm
==> default: trying wget...
==> default: Comparing checksum with sha256sum...
==> default: Installing chef 15.7.32
==> default: installing with rpm...
==> default: warning:
==> default: /tmp/install.sh.3684/chef-15.7.32-1.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 83ef826a: NOKEY
==> default: Preparing...
==> default: ##################################################

==> default: removing /opt/chef...
==> default: chef
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #
==> default: #

==> default: Thank you for installing Chef Infra Client! For help getting started visit https://learn.chef.io
==> default: Running provisioner: chef_solo...
==> default: Detected Chef (latest) is already installed
==> default: Generating chef JSON and uploading...
==> default: Running chef-solo...
==> default: Chef Infra Client cannot execute without accepting the license
Chef never successfully completed! Any errors should be visible in the
output above. Please fix your recipes so that they properly complete.

f:\vagrant\centos>

ライセンス認証がうまく出来なかったようです。

この後、進めなくなってしまったので次回とします。

コメント

タイトルとURLをコピーしました