読者です 読者をやめる 読者になる 読者になる

HPCメモ

HPC(High Performance Computing)に関連したりしなかったりすることのメモ書き

chef-zeroでwindows用開発環境を構築

最近、遅ればせながら

入門Chef Solo - Infrastructure as Code

入門Chef Solo - Infrastructure as Code

を読んだので、chefで遊んでいます。

普段利用する開発環境の構築なんかに使うぶんにはお手軽にできそうなんですがwindowsにインストールしてwindows環境の構築に使ってるって情報があんまり多くないみたいなので、ざっくりとまとめてみました。

インストール先の環境

マシンはDell Venue8 pro、ソフト環境はこんな感じです。

  • windows8.1 update (32bit版)
  • chef client v12.0.3-1

運用方法

chefを使ったシステムの構成は、ここの絵みたいに設定ファイルなどをまとめたchef-repoというレポジトリをworkstationと呼ばれるマシンに用意して、これをリモートにあるchef-server経由で管理対象マシン(node)に適用する。ということになっています。

An Overview of Chef — Chef Docs

今回は、nodeは全部windows(でOneDriveがインストール済)なのと、自分のマシンにしか適用しないので、次のような方針を採用しました。

  • chef-repoはOneDrive上に構築
  • レシピの適用はローカルマシン上でchef-clientのローカルモードを使って行う

下準備

msysgitのインストール

Git for Windows
のページへ行ってパッケージをダウンロード/インストールします。

chef-clientのインストール

まずはダウンロードページへ行きます。
Chef Client | Chef Downloads | Chef
左にあるプルダウンメニューから"windows" "8" "x86_64"を選びます。
今回のインストール先は32bit環境ですが、x86_64を選んでください。*1
msiファイルへのリンクが表示されるので、ダウンロードしてインストールしてください。途中で聞かれるものは全てデフォルトで大丈夫です。

インストールが完了すると

C:\opscode\chef\bin

にイロイロと入ってます。

chef-repoの作成

githubで公開されている、オフィシャルなchef-repoをcloneしてきます。そのあと、元のoriginをupstreamに変えた上で、適当なリポジトリ(私はbitbucketを使ってますが)をoriginに設定します。

> git clone git://github.com/chef/chef-repo.git
> cd chef-repo
> git remote rename origin upstream
> git remote add origin http://bitbucket.org/XXXXXX
> git checkout -b windows
> git push origin windows

公開されているcookbookをインポート

入れたいものはイロイロとあるんですが、とりあえずテストのためにsupermarketにあるfirefoxのcookbookをインポートしてインストールしてみます。

firefox Cookbook - Chef Supermarket

コマンドプロンプトから、さっき作ったchef-repoディレクトリで以下のコマンドを実行します。
warningが出てますがlocalmode(-z)の時には不要な設定ファイルが無いって話なので問題ないです。

>knife cookbook site install firefox -z
WARNING: No knife configuration file found
Installing firefox to C:/Users/n_so5/SkyDrive/chef-repo/cookbooks
Checking out the master branch.
Creating pristine copy branch chef-vendor-firefox
Downloading firefox from the cookbooks site at version 2.0.0 to C:/Users/n_so5/SkyDrive/chef-repo/cookbooks/firefox.tar.gz
Cookbook saved: C:/Users/n_so5/SkyDrive/chef-repo/cookbooks/firefox.tar.gz
Removing pre-existing version.
Uncompressing firefox version 2.0.0.
removing downloaded tarball
1 files updated, committing changes
Creating tag cookbook-site-imported-firefox-2.0.0
Checking out the master branch.
Updating e3efb8c..72fcfc0
Fast-forward
 cookbooks/firefox/.gitignore            |  6 +++
 cookbooks/firefox/.kitchen.yml          | 20 +++++++
 cookbooks/firefox/.rubocop.yml          | 20 +++++++
 cookbooks/firefox/.travis.yml           | 10 ++++
 cookbooks/firefox/Berksfile             |  5 ++
 cookbooks/firefox/CHANGELOG.md          | 39 ++++++++++++++
 cookbooks/firefox/Gemfile               |  7 +++
 cookbooks/firefox/README.md             | 46 +++++++++++++++++
 cookbooks/firefox/attributes/default.rb | 24 +++++++++
 cookbooks/firefox/chefignore            | 92 +++++++++++++++++++++++++++++++++
 cookbooks/firefox/libraries/default.rb  | 47 +++++++++++++++++
 cookbooks/firefox/metadata.json         | 43 +++++++++++++++
 cookbooks/firefox/metadata.rb           | 15 ++++++
 cookbooks/firefox/recipes/default.rb    | 42 +++++++++++++++
 14 files changed, 416 insertions(+)
 create mode 100644 cookbooks/firefox/.gitignore
 create mode 100644 cookbooks/firefox/.kitchen.yml
 create mode 100644 cookbooks/firefox/.rubocop.yml
 create mode 100644 cookbooks/firefox/.travis.yml
 create mode 100644 cookbooks/firefox/Berksfile
 create mode 100644 cookbooks/firefox/CHANGELOG.md
 create mode 100644 cookbooks/firefox/Gemfile
 create mode 100644 cookbooks/firefox/README.md
 create mode 100644 cookbooks/firefox/attributes/default.rb
 create mode 100644 cookbooks/firefox/chefignore
 create mode 100644 cookbooks/firefox/libraries/default.rb
 create mode 100644 cookbooks/firefox/metadata.json
 create mode 100644 cookbooks/firefox/metadata.rb
 create mode 100644 cookbooks/firefox/recipes/default.rb
Cookbook firefox version 2.0.0 successfully installed

しれっとgitにコミットまでしてくれてますね。これはありがたい。

ローカルマシンへ適用

ダウンロードしてきたレシピを適用するには、ノードオブジェクトを作成して、レシピを適用対象にして、最後にcookという流れなんですが、ノードオブジェクトはchef-client -zを実行すれば、作ってくれるようなので、まず一回実行します。

> chef-client -z 
[2015-03-18T09:07:21+09:00] WARN: No config file found or specified on command line, using command line options.
Starting Chef Client, version 12.0.3
[2015-03-18T09:08:18+09:00] WARN: chef-client doesn't have administrator privileges on node venue8. This might cause unexpected resource failures.
resolving cookbooks for run list: []
Synchronizing Cookbooks:
Compiling Cookbooks...
[2015-03-18T09:08:18+09:00] WARN: Node venue8 has an empty run list.
Converging 0 resources

Running handlers:
Running handlers complete
Chef Client finished, 0/0 resources updated in 56.203554 seconds

無事にvenue8*2のノードが作成されて、「run listが空っぽだから0個convergeしました。」と表示されたら終了です。

では、さっきインストールしたfirefoxのレシピを適用対象にします。

> knife node run_list add venue8 firefox -z
WARNING: No knife configuration file found
venue8:
  run_list: recipe[firefox]

最後にこのレシピを適用します。
今回は管理者権限が必要になるので、コマンドプロンプトを「管理者権限で実行」で立ち上げなおしてください。でもって、chef-clientをポチっとなすると

> chef-client -z
[2015-03-18T19:37:50+09:00] WARN: No config file found or specified on command line, using command line options.
Starting Chef Client, version 12.0.3
resolving cookbooks for run list: ["firefox"]
Synchronizing Cookbooks:
  - firefox
Compiling Cookbooks...
Converging 1 resources
Recipe: firefox::default
  * windows_package[Mozilla Firefox 36.0.1 en-US] action install

    ================================================================================
    Error executing action `install` on resource 'windows_package[Mozilla Firefox 36.0.1 en-US]'
    ================================================================================

    RuntimeError
    ------------
    Unable to find a Chef::Provider::Package::Windows provider for installer_type 'custom'

    Resource Declaration:
    ---------------------
    # In C:/WORK/unix_home/.chef/local-mode-cache/cache/cookbooks/firefox/recipes/default.rb

     25:   windows_package "Mozilla Firefox #{version} #{node['firefox']['lang']}" do
     26:     source url
     27:     installer_type :custom
     28:     options '-ms'
     29:     action :install
     30:   end
     31: elsif platform_family?('mac_os_x')

    Compiled Resource:
    ------------------
    # Declared in C:/WORK/unix_home/.chef/local-mode-cache/cache/cookbooks/firefox/recipes/default.rb:25:in `from_file'

    windows_package("Mozilla Firefox 36.0.1 en-US") do
      action [:install]
      retries 0
      retry_delay 2
      default_guard_interpreter :default
      options "-ms"
      package_name "Mozilla Firefox 36.0.1 en-US"
      source "C:\\https:\\download-installer.cdn.mozilla.net\\pub\\firefox\\releases\\latest\\win32\\en-US\\Firefox%20Setup%2036.0.1.exe"
      timeout 600
      installer_type :custom
      returns [0]
      declared_type :windows_package
      cookbook_name "firefox"
      recipe_name "default"
    end


Running handlers:
[2015-03-18T19:38:39+09:00] ERROR: Running exception handlers
Running handlers complete
[2015-03-18T19:38:39+09:00] ERROR: Exception handlers complete
[2015-03-18T19:38:39+09:00] FATAL: Stacktrace dumped to C:/WORK/unix_home/.chef/local-mode-cache/cache/chef-stacktrace.out
Chef Client failed. 0 resources updated in 47.820474 seconds
[2015-03-18T19:38:39+09:00] FATAL: RuntimeError: windows_package[Mozilla Firefox 36.0.1 en-US] (firefox::default line 25) had an error: RuntimeError: Unable to find a Chef::Provider::Package::Windows provider for installer_type 'custom'

あら?
なんか、手順が抜けてるのかと思ってエラーメッセージでぐぐってみたところ、windows用の便利コマンド群をいれる必要があったらしいので、ダウンロードしてきて、run_listに追加します。

> knife cookbook site install windows -z
WARNING: No knife configuration file found
Installing windows to C:/Users/n_so5/SkyDrive/chef-repo/cookbooks
Checking out the master branch.
Creating pristine copy branch chef-vendor-windows
Downloading windows from the cookbooks site at version 1.36.6 to C:/Users/n_so5/SkyDrive/chef-repo/cookbooks/windows.tar.gz
Cookbook saved: C:/Users/n_so5/SkyDrive/chef-repo/cookbooks/windows.tar.gz
Removing pre-existing version.
Uncompressing windows version 1.36.6.
removing downloaded tarball
1 files updated, committing changes
Creating tag cookbook-site-imported-windows-1.36.6
Checking out the master branch.
Updating 22cf2eb..a01a793
Fast-forward
 cookbooks/windows/CHANGELOG.md                     | 320 +++++++++
 cookbooks/windows/README.md                        | 749 +++++++++++++++++++++
 cookbooks/windows/attributes/default.rb            |  24 +
 .../default/handlers/windows_reboot_handler.rb     |  76 +++
 cookbooks/windows/libraries/feature_base.rb        |  59 ++
 cookbooks/windows/libraries/matchers.rb            | 465 +++++++++++++
 cookbooks/windows/libraries/powershell_helper.rb   |  59 ++
 cookbooks/windows/libraries/powershell_out.rb      |  79 +++
 cookbooks/windows/libraries/registry_helper.rb     | 364 ++++++++++
 cookbooks/windows/libraries/version.rb             | 207 ++++++
 .../libraries/windows_architecture_helper.rb       |  87 +++
 cookbooks/windows/libraries/windows_helper.rb      | 148 ++++
 cookbooks/windows/libraries/windows_package.rb     | 224 ++++++
 cookbooks/windows/libraries/windows_privileged.rb  |  94 +++
 cookbooks/windows/libraries/wmi_helper.rb          |  32 +
 cookbooks/windows/metadata.json                    |  31 +
 cookbooks/windows/providers/auto_run.rb            |  33 +
 cookbooks/windows/providers/batch.rb               |  63 ++
 cookbooks/windows/providers/feature_dism.rb        |  64 ++
 cookbooks/windows/providers/feature_powershell.rb  |  38 ++
 .../windows/providers/feature_servermanagercmd.rb  |  61 ++
 cookbooks/windows/providers/font.rb                |  69 ++
 cookbooks/windows/providers/pagefile.rb            | 153 +++++
 cookbooks/windows/providers/path.rb                |  52 ++
 cookbooks/windows/providers/printer.rb             | 101 +++
 cookbooks/windows/providers/printer_port.rb        | 103 +++
 cookbooks/windows/providers/reboot.rb              |  33 +
 cookbooks/windows/providers/registry.rb            |  75 +++
 cookbooks/windows/providers/shortcut.rb            |  56 ++
 cookbooks/windows/providers/task.rb                | 167 +++++
 cookbooks/windows/providers/zipfile.rb             |  93 +++
 cookbooks/windows/recipes/default.rb               |  34 +
 cookbooks/windows/recipes/reboot_handler.rb        |  32 +
 cookbooks/windows/resources/auto_run.rb            |  30 +
 cookbooks/windows/resources/batch.rb               |  36 +
 cookbooks/windows/resources/feature.rb             |  44 ++
 cookbooks/windows/resources/font.rb                |  25 +
 cookbooks/windows/resources/pagefile.rb            |  29 +
 cookbooks/windows/resources/path.rb                |  28 +
 cookbooks/windows/resources/printer.rb             |  41 ++
 cookbooks/windows/resources/printer_port.rb        |  40 ++
 cookbooks/windows/resources/reboot.rb              |  29 +
 cookbooks/windows/resources/registry.rb            |  34 +
 cookbooks/windows/resources/shortcut.rb            |  35 +
 cookbooks/windows/resources/task.rb                |  50 ++
 cookbooks/windows/resources/zipfile.rb             |  33 +
 46 files changed, 4699 insertions(+)
 create mode 100644 cookbooks/windows/CHANGELOG.md
 create mode 100644 cookbooks/windows/README.md
 create mode 100644 cookbooks/windows/attributes/default.rb
 create mode 100644 cookbooks/windows/files/default/handlers/windows_reboot_handler.rb
 create mode 100644 cookbooks/windows/libraries/feature_base.rb
 create mode 100644 cookbooks/windows/libraries/matchers.rb
 create mode 100644 cookbooks/windows/libraries/powershell_helper.rb
 create mode 100644 cookbooks/windows/libraries/powershell_out.rb
 create mode 100644 cookbooks/windows/libraries/registry_helper.rb
 create mode 100644 cookbooks/windows/libraries/version.rb
 create mode 100644 cookbooks/windows/libraries/windows_architecture_helper.rb
 create mode 100644 cookbooks/windows/libraries/windows_helper.rb
 create mode 100644 cookbooks/windows/libraries/windows_package.rb
 create mode 100644 cookbooks/windows/libraries/windows_privileged.rb
 create mode 100644 cookbooks/windows/libraries/wmi_helper.rb
 create mode 100644 cookbooks/windows/metadata.json
 create mode 100644 cookbooks/windows/providers/auto_run.rb
 create mode 100644 cookbooks/windows/providers/batch.rb
 create mode 100644 cookbooks/windows/providers/feature_dism.rb
 create mode 100644 cookbooks/windows/providers/feature_powershell.rb
 create mode 100644 cookbooks/windows/providers/feature_servermanagercmd.rb
 create mode 100644 cookbooks/windows/providers/font.rb
 create mode 100644 cookbooks/windows/providers/pagefile.rb
 create mode 100644 cookbooks/windows/providers/path.rb
 create mode 100644 cookbooks/windows/providers/printer.rb
 create mode 100644 cookbooks/windows/providers/printer_port.rb
 create mode 100644 cookbooks/windows/providers/reboot.rb
 create mode 100644 cookbooks/windows/providers/registry.rb
 create mode 100644 cookbooks/windows/providers/shortcut.rb
 create mode 100644 cookbooks/windows/providers/task.rb
 create mode 100644 cookbooks/windows/providers/zipfile.rb
 create mode 100644 cookbooks/windows/recipes/default.rb
 create mode 100644 cookbooks/windows/recipes/reboot_handler.rb
 create mode 100644 cookbooks/windows/resources/auto_run.rb
 create mode 100644 cookbooks/windows/resources/batch.rb
 create mode 100644 cookbooks/windows/resources/feature.rb
 create mode 100644 cookbooks/windows/resources/font.rb
 create mode 100644 cookbooks/windows/resources/pagefile.rb
 create mode 100644 cookbooks/windows/resources/path.rb
 create mode 100644 cookbooks/windows/resources/printer.rb
 create mode 100644 cookbooks/windows/resources/printer_port.rb
 create mode 100644 cookbooks/windows/resources/reboot.rb
 create mode 100644 cookbooks/windows/resources/registry.rb
 create mode 100644 cookbooks/windows/resources/shortcut.rb
 create mode 100644 cookbooks/windows/resources/task.rb
 create mode 100644 cookbooks/windows/resources/zipfile.rb
Cookbook windows version 1.36.6 successfully installed
Installing chef_handler to C:/Users/n_so5/SkyDrive/chef-repo/cookbooks
Checking out the master branch.
Creating pristine copy branch chef-vendor-chef_handler
Downloading chef_handler from the cookbooks site at version 1.1.6 to C:/Users/n_so5/SkyDrive/chef-repo/cookbooks/chef_handler.tar.gz
Cookbook saved: C:/Users/n_so5/SkyDrive/chef-repo/cookbooks/chef_handler.tar.gz
Removing pre-existing version.
Uncompressing chef_handler version 1.1.6.
removing downloaded tarball
1 files updated, committing changes
Creating tag cookbook-site-imported-chef_handler-1.1.6
Checking out the master branch.
Updating a01a793..26b9666
Fast-forward
 cookbooks/chef_handler/CHANGELOG.md                |  44 +++++++++
 cookbooks/chef_handler/README.md                   | 103 +++++++++++++++++++++
 cookbooks/chef_handler/attributes/default.rb       |  30 ++++++
 .../chef_handler/files/default/handlers/README     |   1 +
 cookbooks/chef_handler/libraries/matchers.rb       |  29 ++++++
 cookbooks/chef_handler/metadata.json               |  29 ++++++
 cookbooks/chef_handler/metadata.rb                 |   7 ++
 cookbooks/chef_handler/providers/default.rb        |  97 +++++++++++++++++++
 cookbooks/chef_handler/recipes/default.rb          |  33 +++++++
 cookbooks/chef_handler/recipes/json_file.rb        |  28 ++++++
 cookbooks/chef_handler/resources/default.rb        |  34 +++++++
 11 files changed, 435 insertions(+)
 create mode 100644 cookbooks/chef_handler/CHANGELOG.md
 create mode 100644 cookbooks/chef_handler/README.md
 create mode 100644 cookbooks/chef_handler/attributes/default.rb
 create mode 100644 cookbooks/chef_handler/files/default/handlers/README
 create mode 100644 cookbooks/chef_handler/libraries/matchers.rb
 create mode 100644 cookbooks/chef_handler/metadata.json
 create mode 100644 cookbooks/chef_handler/metadata.rb
 create mode 100644 cookbooks/chef_handler/providers/default.rb
 create mode 100644 cookbooks/chef_handler/recipes/default.rb
 create mode 100644 cookbooks/chef_handler/recipes/json_file.rb
 create mode 100644 cookbooks/chef_handler/resources/default.rb
Cookbook chef_handler version 1.1.6 successfully installed

>knife node run_list add venue8 windows -z
WARNING: No knife configuration file found
venue8:
  run_list:
    recipe[firefox]
    recipe[windows]

あらためて、 ポチっとなっと

> chef-client -z
[2015-03-19T08:48:04+09:00] WARN: No config file found or specified on command line, using command line options.
Starting Chef Client, version 12.0.3
resolving cookbooks for run list: ["firefox", "windows"]
Synchronizing Cookbooks:
  - firefox
  - windows
  - chef_handler
Compiling Cookbooks...
Recipe: windows::default
  * chef_gem[win32-api] action install (up to date)
  * chef_gem[win32-service] action install (up to date)
  * chef_gem[windows-api] action install (up to date)
  * chef_gem[windows-pr] action install (up to date)
  * chef_gem[win32-dir] action install (up to date)
  * chef_gem[win32-event] action install (up to date)
  * chef_gem[win32-mutex] action install (up to date)
  Converging 8 resources
Recipe: firefox::default
  * windows_package[Mozilla Firefox 36.0.1 en-US] action install
  Recipe: <Dynamically Defined Resource>
    * remote_file[C:\WORK\unix_home\.chef\local-mode-cache\cache/Firefox Setup 36.0.1.exe] action create
      - create new file C:\WORK\unix_home\.chef\local-mode-cache\cache/Firefox Setup 36.0.1.exe
      - update content in file C:\WORK\unix_home\.chef\local-mode-cache\cache/Firefox Setup 36.0.1.exe from none to ad32ad
      (file sizes exceed 10000000 bytes, diff output suppressed)

Recipe: windows::default
  * chef_gem[win32-api] action install (up to date)
  * chef_gem[win32-service] action install (up to date)
  * chef_gem[windows-api] action install (up to date)
  * chef_gem[windows-pr] action install (up to date)
  * chef_gem[win32-dir] action install (up to date)
  * chef_gem[win32-event] action install (up to date)
  * chef_gem[win32-mutex] action install (up to date)

Running handlers:
Running handlers complete
Chef Client finished, 2/16 resources updated in 182.121495 seconds

今度は無事にインストールできました。
先にwindowsのレシピを適用してから、firefoxを入れないといけないはずですが、その辺は自動的に解決してくれるようですね。*3


次回は、ソフトのインストール以外の設定をしてみます。

*1:そもそもwindows8だとx86_64しか選択肢はありませんが・・・

*2:本当は8proですが・・・

*3:依存性の情報もってるんだったら、最初にwindows rerepiを適用する前にfirefoxを入れようとした時にもっと分かりやすいメッセージ出してくれりゃいいのに・・・