HPCメモ

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

chef-zeroでwindows用開発環境を構築(その2)

さて、前回の続きです。

普段、windowsマシンを使う時には、次のような運用方針にしているんですが、このための設定をchefのレシピでなんとかしてみましょう。

  • C:\WORK を作成して、必要なファイル類はそこへ置く
  • bitbucketに置いてあるドットファイルをC:\WORK\unix_homeにcloneしてくる。
  • 環境変数 HOME にC:\WORK\unix_homeを設定
  • sshの鍵を作成

cookbookの作成

まずは、knifeを使って新しいcookbookを作ります。*1

> knife cookbook create windows_initial_settings -z
WARNING: No knife configuration file found
** Creating cookbook windows_initial_settings in C:/Users/n_so5/SkyDrive/chef-repo/cookbooks
** Creating README for cookbook: windows_initial_settings
** Creating CHANGELOG for cookbook: windows_initial_settings
** Creating metadata for cookbook: windows_initial_settings

無事にできたら、次のような感じのディレクトリが生成されているはずです。

>ls -R cookbooks\windows_initial_settings
cookbooks\windows_initial_settings:
CHANGELOG.md  README.md  attributes  definitions  files  libraries  metadata.rb  providers  recipes  resources  templates
cookbooks\windows_initial_settings/attributes:
cookbooks\windows_initial_settings/definitions:
cookbooks\windows_initial_settings/files:
default
cookbooks\windows_initial_settings/files/default:
cookbooks\windows_initial_settings/libraries:
cookbooks\windows_initial_settings/providers:
cookbooks\windows_initial_settings/recipes:
default.rb
cookbooks\windows_initial_settings/resources:
cookbooks\windows_initial_settings/templates:
default
cookbooks\windows_initial_settings/templates/default:

このうちの、recipes/default.rbにつらつらとレシピを書いていきます。

ディレクトリの作成

まず、最初のC:\WORKの作成ですが、これはその名もずばりの"directory"というリソースがあるので、これを使います。
https://docs.chef.io/resource_directory.html

  directory "C:\WORK" do
      action :create
  end

gitでドットファイルを持ってくる

こちらも"git"というリソースがあるので、これを使います。
https://docs.chef.io/resource_git.html

  home_dir="C:\\WORK\\unix_home"
  git "#{home_dir}" do
      repository "https://n_so5@bitbucket.org/XXXX.git"
      enable_submodules true
      action :sync
  end

.vimの下にいくつかsubmoduleとしてvimプラグインリポジトリを入れているので、"enable_submodule true"を指定しています。あと、sshの鍵を未だ登録していない環境からのアクセスを想定しているので、http経由でのアクセスにしています。

環境変数のHOMEを設定

これまた"env"というリソースがあるので、これを使います。
https://docs.chef.io/resource_env.html

  env "HOME" do
      value "#{home_dir}"
      action :create
  end

sshの鍵作成

これまた、chefのリソースでと思いきや、あんまりこういう用途は想定していないのかうまく使えそうな手がありません。とりあえず、executeを使って、ssh-keygenを呼び出すことにします。

  key_filename="C:\WORK\unix_home\.ssh\id_rsa"
  execute "ssh-keygen" do
      command "ssh-keygen -f #{key_filename} -t rsa "
      not_if {::File.exists?(key_filename)}
  end

既に鍵を作成した環境で実行した時に、上書きを避けるため、not_ifで条件を指定しています。

実際に適用してみる

ここまでをまとめてdefault.rbに書いた上で、windows以外の環境では実行されないように

if platform_family?('windows')
...
end

で囲っておきます。

レシピの適用前はこんな状況ですが

> ls C:\WORK
ls: C:\WORK: No such file or directory
> echo %HOME%
C:\Users\n_so5

実際に適用すると

> chef-client -z
[2015-03-19T19:15:33+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", "windows_initial_settings"]
Synchronizing Cookbooks:
  - firefox
  - windows
  - windows_initial_settings
  - 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 13 resources
Recipe: firefox::default
  * windows_package[Mozilla Firefox 36.0.1 en-US] action install
  Recipe: <Dynamically Defined Resource>
    * remote_file[C:\Users\n_so5\.chef\local-mode-cache\cache/Firefox Setup 36.0.1.exe] action create (up to date)

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)
Recipe: windows_initial_settings::default
  * directory[C:WORK] action create
    - create new directory C:WORK
  * git[C:\WORK\unix_home] action syncPassword for 'https://n_so5@bitbucket.org':
Password for 'https://n_so5@bitbucket.org':

    - clone from https://n_so5@bitbucket.org/XXXXX into C:\WORK\unix_home
    - checkout ref 26ea7cf8d79fb68b4273bc133c6165c636248b08 branch HEAD
  * directory[C:\WORK\unix_home\.ssh] action create
    - create new directory
  * execute[ssh-keygen] action runEnter passphrase (empty for no passphrase):
Enter same passphrase again:

    - execute ssh-keygen -f C:\WORK\unix_home\.ssh\id_rsa -t rsa

Running handlers:
Running handlers complete

といった具合に適用されます。gitでアクセスしている時にはbitbucketのパスワードを聞かれるので、入力してください。あと、ssh-keygenのレシピを適用している時も普通にssh-keygenした時と同じようにパスフレーズの入力を求められます。

でもって、どういう状態になったかと言うと

>ls C:\WORK
unix_home
> ls C:\WORK\unix_home\.ssh\
id_rsa  id_rsa.pub

> echo %HOME%
C:\Users\n_so5

あら?環境変数の変更が反映されていない・・・と思ったけどこれは単にコマンドプロンプトを再起動すれば反映されます。コマンドプロンプトを立ち上げ直すと、無事に反映されています。あと、.vim/bundleの下に色々と入っているのも確認。

> echo %HOME%
C:\WORK\unix_home
>ls %HOME%/.vim/bundle/*
C:\WORK\unix_home/.vim\bundle/indentLine:
README.md  after  doc

C:\WORK\unix_home/.vim\bundle/pydiction:
README  README.md  after  complete-dict  pydiction.py

C:\WORK\unix_home/.vim\bundle/syntastic:
CONTRIBUTING.md  LICENCE  README.markdown  _assets  autoload  doc  plugin  syntax_checkers

ついでに、この状態から再実行するとどうなるか試してみました。

> cdhef-client -z
[2015-03-19T19:29:26+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", "windows_initial_settings"]
Synchronizing Cookbooks:
  - firefox
  - windows
  - windows_initial_settings
  - 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 13 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)
Recipe: windows_initial_settings::default
  * directory[C:WORK] action create (up to date)
  * git[C:\WORK\unix_home] action syncPassword for 'https://n_so5@bitbucket.org':
 (up to date)
  * env[HOME] action create (up to date)
  * directory[C:\WORK\unix_home\.ssh] action create (up to date)
  * execute[ssh-keygen] action run (skipped due to not_if)

Running handlers:
Running handlers complete
Chef Client finished, 2/20 resources updated in 417.811231 seconds

%HOME%を設定した影響で、firefoxのアーカイブをキャッシュしてる場所が変わるので、アーカイブを再ダウンロードしていますが、他は(up to date)と表示されて、するすると終わっていきます。

とりあえず今日のとこはこれで終わり。

*1:このメタファー破綻してないか・・・?