LoginSignup
16
16

More than 5 years have passed since last update.

proxy 環境化の CentOS に Chef で Redmine をインストール

Last updated at Posted at 2014-02-05

はじめに

proxy に囲まれた Windows7 から CentOS に Chef で Redmine をインストールしてみようとしたところ右往左往してしまったので、どなたかの役に立てばと思い手順を書き留めておきます。

以下では Windows7 にインストールした Vagrant に Chef の用語で言うところの workstation と node を作成し、workstation から node に Chef で Redmine をインストールする手順を示します。

Windows7 から直接 CentOS にレシピを流せばいいじゃないかと思う方もいらっしゃるかと思いますが、ウインドーズで Ruby 周りのプロダクトを試すのは不要なトラブルに見舞われることが多いのでお勧めしません。

Cygwin も結局うまくいかないことが多いので、おとなしく Vagrant を利用することをお勧めします。

Windows7 に Vagrant をインストールする

VirtualBox

VirtualBox 4.3.6 r91406 をインストールします。インストーラを起動し、すべてデフォルトを受け入れます。

Vagrant

Vagrant 1.4.1 をインストールします。こちらもインストーラを起動し、すべてデフォルトを受け入れます。

インストールが完了したら、コマンドプロンプトから vagrant コマンドを実行し、パスが通っていることを確認します。

C:\Users\umezawa>vagrant --version
Vagrant 1.4.1

後続の手順でインターネットから box をダウンロードするので、環境変数にプロキシサーバの情報を設定します。your.proxy.host と 9999 は自身の環境に読み替えてください。

C:\Users\umezawa>echo %http_proxy%
http://your.proxy.host:9999/

C:\Users\umezawa>echo %https_proxy%
http://your.proxy.host:9999/

Vagrant 上に Workstation を作る

Vagrant 上に workstation を構築します。使用する OS に特に制限はありませんが、ここでは Ubuntu 12.04LTS 64bit を使用します。

A list of base boxes for Vagrant - Vagrantbox.es で公開されている box を Vagrant に追加します。

C:\Users\umezawa>vagrant box add precise64 http://files.vagrantup.com/precise64.box
Downloading box from URL: http://files.vagrantup.com/precise64.box
Extracting box...ate: 2124k/s, Estimated time remaining: --:--:--)
Successfully added box 'precise64' with provider 'virtualbox'!

workstation 用の適当なディレクトリを作成したら、移動して box を初期化します。

C:\Users\umezawa>mkdir c:\temp\workstation

C:\Users\umezawa>cd c:\temp\workstation

c:\temp\workstation>vagrant box list
precise64 (virtualbox)

c:\temp\workstation>vagrant init precise64
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

エディタで作成された Vagrantfile を開いて IP アドレスを設定します。ここではデフォルトの 192.168.33.10 を使用するので、コメントアウトされている設定を有効化しました。

c:\temp\workstation>notepad Vagrantfile
(snip)
  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network :private_network, ip: "192.168.33.10"

それでは box を起動しましょう。

c:\temp\workstation>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'precise64'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for machine to boot. This may take a few minutes...
DL is deprecated, please use Fiddle
[default] Machine booted and ready!
[default] The guest additions on this VM do not match the installed version of
VirtualBox! In most cases this is fine, but in rare cases it can
cause things such as shared folders to not work properly. If you see
shared folder errors, please make sure the guest additions within the
virtual machine match the version of VirtualBox you have installed on
your host and reload your VM.

Guest Additions Version: 4.2.0
VirtualBox Version: 4.3
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant

Guest Additions のバージョンがズレていると言われますが、無視して構いません。

box が起動したら teraterm などのターミナルから vagrant/vagrant で 192.168.33.10 に接続できることを確認します。

Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
Welcome to your Vagrant-built virtual machine.
Last login: Fri Sep 14 06:23:18 2012 from 10.0.2.2
vagrant@precise64:~$

以下、この workstation 上に Chef の実行に必要な環境を整えて行きます。

ssh, rsync

レシピの転送に必要な ssh と rsync がインストールされていることを確認します。

vagrant@precise64:~$ ssh -v
OpenSSH_5.9p1 Debian-5ubuntu1, OpenSSL 1.0.1 14 Mar 2012
(snip)

vagrant@precise64:~$ rsync --version
rsync  version 3.0.9  protocol version 30
(snip)

git

レシピの clone に必要な git をインストールします。proxy 環境下から apt-get するためのプロキシ設定はいろいろな方法がありますが、ここでは /etc/environment にプロキシサーバの情報を設定します。your.proxy.host と 9999 は自身の環境に読み替えてください。

vagrant@precise64:~$ sudo vi /etc/environment
vagrant@precise64:~$ cat /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"

http_proxy="http://your.proxy.host:9999"
https_proxy="http://your.proxy.host:9999"

また、sudo した際に設定内容を保持するよう visudo で env_keep を指定します。

Ubuntu で visudo した際、デフォルトのエディタには nano が指定されているのですが、個人的に使いづらかったので vim に変更しました。

vagrant@precise64:~$ sudo update-alternatives --config editor
There are 3 choices for the alternative editor (providing /usr/bin/editor).

  Selection    Path               Priority   Status
------------------------------------------------------------
* 0            /bin/nano           40        auto mode
  1            /bin/ed            -100       manual mode
  2            /bin/nano           40        manual mode
  3            /usr/bin/vim.tiny   10        manual mode

Press enter to keep the current choice[*], or type selection number: 3
update-alternatives: using /usr/bin/vim.tiny to provide /usr/bin/editor (editor) in manual mode.

vagrant@precise64:~$ sudo visudo
vagrant@precise64:~$ sudo grep proxy /etc/sudoers
Defaults        env_keep="http_proxy https_proxy"

これで proxy 環境下からインターネットにつながるはずです。apt-get update したら apt-get install で git-core をインストールします。

vagrant@precise64:~$ sudo apt-get update
(snip)
Reading package lists... Done

vagrant@precise64:~$ sudo apt-get install -y git-core
(snip)
Setting up git (1:1.7.9.5-1) ...
Setting up git-core (1:1.7.9.5-1) ...
Setting up patch (2.6.1-3) ...

git がインストールされてパスが通ったことを確認したら、コミットログに使用される名前とメールアドレスを設定します。yourname と your@email.address は自身のものに読み替えてください。

vagrant@precise64:~$ git --version
git version 1.7.9.5

vagrant@precise64:~/work/chef-repo$ git config --global user.name "yourname"
vagrant@precise64:~/work/chef-repo$ git config --global user.email "your@email.address"

ruby

vagrant は自前で ruby 1.8.7 を持っており、/etc/profile.d/vagrant_ruby.sh でパスに追加しています。

vagrant@precise64:~$ ruby -v
ruby 1.8.7 (2012-02-08 patchlevel 358) [x86_64-linux]

vagrant@precise64:~$ whereis ruby
ruby: /opt/vagrant_ruby/bin/ruby

後述する berkshelf には ruby 1.9 以降が必要なので、改めてインストールします。

vagrant@precise64:~$ sudo apt-get install -y ruby1.9.3

再度ログインするか /etc/profile を source してパスを更新すれば、インストールした ruby 1.9.3 が参照されるようになります。

vagrant@precise64:~$ source /etc/profile

vagrant@precise64:~$ ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]

knife-solo

vagrant の ruby にも knife は入っていますが、knife と knife-solo はバージョンの組み合わせが悪いと動作しない場合があるので、改めてインストールします。

vagrant@precise64:~$ sudo gem install knife-solo --no-ri --no-rdoc
(snip)
Successfully installed pry-0.9.12.6
Successfully installed chef-11.8.2
Successfully installed knife-solo-0.4.1
29 gems installed

vagrant@precise64:~$ knife -v
Chef: 11.8.2

berkshelf

最後に Berkshelf をインストールします。

berkshelf が依存する gem に native build が必要なものがいくつかあるので、まず build-essential をインストールします。

vagrant@precise64:~$ sudo apt-get install -y build-essential

続いて berkshelf をインストールします。native build にそれなりの時間が掛かるので注意してください。

vagrant@precise64:~$ sudo gem install berkshelf --no-ri --no-rdoc
(snip)
Successfully installed thor-0.18.1
Successfully installed rbzip2-0.2.0
Successfully installed berkshelf-2.0.13
43 gems installed

vagrant@precise64:~$ berks -v
Berkshelf (2.0.13)
(snip)

以上で workstation の環境構築は完了です。

Vagrant 上に node を作る

続いて vagrant 上に node を作ります。今回は手順確認のため vagrant 上に作成しましたが、実際にはレシピを適用する物理サーバや仮想サーバが node になるので、すでに作業対象サーバがある場合は vagrant 上に node を作成する必要はありません。

今回は node に CentOS 6.3 を採用する想定なので、Vagrant Base Box Downloads から適当な box を選んで追加しました。

C:\Users\umezawa>vagrant box add CentOS-6.3-x86_64-v20130101 http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.3-x86_64-v20130101.box
Downloading box from URL: http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.3-x86_64-v20130101.box
Extracting box...ate: 171k/s, Estimated time remaining: --:--:--)
Successfully added box 'CentOS-6.3-x86_64-v20130101' with provider 'virtualbox'!

node 用の適当なディレクトリを作成したら、移動して box を初期化します。

C:\Users\umezawa>mkdir c:\temp\node

c:\Users\umezawa>cd c:\temp\node

c:\temp\node>vagrant box list
CentOS-6.3-x86_64-v20130101 (virtualbox)
precise64                   (virtualbox)

c:\temp\node>vagrant init CentOS-6.3-x86_64-v20130101
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

エディタで作成された Vagrantfile を開いて workstation とは異なる IP アドレスを設定します。ここでは 192.168.33.100 としました。

c:\temp\node>notepad Vagrantfile
(snip)
  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network :private_network, ip: "192.168.33.100"

それでは box を起動しましょう。

c:\temp\node>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'CentOS-6.3-x86_64-v20130101'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Fixed port collision for 22 => 2222. Now on port 2200.
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2200 (adapter 1)
[default] Booting VM...
[default] Waiting for machine to boot. This may take a few minutes...
DL is deprecated, please use Fiddle
[default] Machine booted and ready!
[default] The guest additions on this VM do not match the installed version of
VirtualBox! In most cases this is fine, but in rare cases it can
cause things such as shared folders to not work properly. If you see
shared folder errors, please make sure the guest additions within the
virtual machine match the version of VirtualBox you have installed on
your host and reload your VM.

Guest Additions Version: 4.2.6
VirtualBox Version: 4.3
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant

box が起動したら teraterm などのターミナルから vagrant/vagrant で 192.168.33.100 に接続できることを確認します。

Last login: Thu Jan 30 05:43:34 2014 from 192.168.33.10
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$

ssh, rsync

レシピの転送に必要な ssh と rsync がインストールされていることを確認します。

[vagrant@localhost ~]$ ssh -v
OpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010
(snip)

[vagrant@localhost ~]$ rsync --version
-bash: rsync: command not found

rsync がインストールされていないので、yum でインストールしましょう。

/etc/yum.conf にプロキシサーバの情報を設定して、yum update してからインストールします。your.proxy.host と 9999 は自身の環境に読み替えてください。

[vagrant@localhost ~]$ sudo vi /etc/yum.conf
[vagrant@localhost ~]$ grep proxy /etc/yum.conf
proxy=http://your.proxy.host:9999/

[vagrant@localhost ~]$ sudo yum update

[vagrant@localhost ~]$ sudo yum -y install rsync
(snip)
Installed:
  rsync.x86_64 0:3.0.6-9.el6_4.1

Complete!

[vagrant@localhost ~]$ rsync --version
rsync  version 3.0.6  protocol version 30
(snip)

あとで knife prepare する際に必要になるので、先回りして /etc/environment と /etc/sudoers にもプロキシサーバの情報を設定しておきます。your.proxy.host と 9999 は自身の環境に読み替えてください。

[vagrant@localhost ~]$ sudo vi /etc/environment
[vagrant@localhost ~]$ sudo grep proxy /etc/environment
http_proxy="http://your.proxy.host:9999"
https_proxy="http://your.proxy.host:9999"

[vagrant@localhost ~]$ sudo visudo
[vagrant@localhost ~]$ sudo grep proxy /etc/sudoers
Defaults    env_keep += "http_proxy https_proxy"

以上で node の環境構築は完了です。

鍵交換

これまで何度か触れてきたとおり、chef は workstation から node に ssh を使ってアクセスします。

レシピ適用中に何度も認証情報を入力するのはとても面倒なので、workstation から node にパスワードを入力せずに ssh でアクセスできるよう、workstation の公開鍵を node に登録しましょう。

まず workstation で鍵ペアを作成します。何度か入力を求められますが、すべて何も入力せずに enter してください。

vagrant@precise64:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/vagrant/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/vagrant/.ssh/id_rsa.
Your public key has been saved in /home/vagrant/.ssh/id_rsa.pub.
(snip)

作成した鍵ペアのうち、公開鍵を node の /tmp ディレクトリに scp 転送します。当然ここではパスワードの入力を求められるので、vagrant を入力してください。

vagrant@precise64:~$ scp /home/vagrant/.ssh/id_rsa.pub 192.168.33.100:/tmp
The authenticity of host '192.168.33.100 (192.168.33.100)' can't be established.
RSA key fingerprint is 35:08:c4:e7:a1:77:af:b8:fa:a7:b0:9d:68:84:a9:5f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.33.100' (RSA) to the list of known hosts.
vagrant@192.168.33.100's password:
id_rsa.pub                                     100%  399     0.4KB/s   00:00

node に ssh でログインし、転送した公開鍵の内容を ~/.ssh/authorized_keys に追記します。その後、不要になった公開鍵を削除してログアウトします。

vagrant@precise64:~$ ssh 192.168.33.100
vagrant@192.168.33.100's password:
Welcome to your Vagrant-built virtual machine.

[vagrant@localhost ~]$ cat /tmp/id_rsa.pub >> ~/.ssh/authorized_keys

[vagrant@localhost ~]$ rm /tmp/id_rsa.pub

[vagrant@localhost ~]$ logout
Connection to 192.168.33.100 closed.

workstation から node にパスフレーズなしで ssh ログインできることを確認します。

vagrant@precise64:~$ ssh 192.168.33.100

Last login: Thu Jan 30 05:43:15 2014 from 192.168.33.10
Welcome to your Vagrant-built virtual machine.

以上で workstation と node の鍵交換は完了です。

workstation から node にレシピを適用する

いよいよ workstation から node にレシピを適用します。

knife configure

まずは workstation で chef の初期設定を行います。knife configure すると何度か入力を求められますが、すべてデフォルトで構わないので、なにも入力せずに enter してください。

vagrant@precise64:~$ knife configure
WARNING: No knife configuration file found
Where should I put the config file? [/home/vagrant/.chef/knife.rb]
Please enter the chef server URL: [https://precise64:443]
Please enter an existing username or clientname for the API: [vagrant]
Please enter the validation clientname: [chef-validator]
Please enter the location of the validation key: [/etc/chef-server/chef-validator.pem]
Please enter the path to a chef repository (or leave blank):
*****

You must place your client key in:
  /home/vagrant/.chef/vagrant.pem
Before running commands with Knife!

*****

You must place your validation key in:
  /etc/chef-server/chef-validator.pem
Before generating instance data with Knife!

*****
Configuration file written to /home/vagrant/.chef/knife.rb

作成された knife.rb にプロキシの情報を設定します。your.proxy.host と 9999 は自身の環境に読み替えてください。

vagrant@precise64:~$ vi /home/vagrant/.chef/knife.rb
vagrant@precise64:~$ cat /home/vagrant/.chef/knife.rb
(snip)
http_proxy               'your.proxy.host:9999/'
https_proxy              'your.proxy.host:9999/'

cookbook

続いて cookbook を用意します。今回は juanje/cookbook-redmine を fork して iptables と subversion の設定を追加したレシピを使用します。

workstation 上に作業用のディレクトリを作成し、garbagetown/cookbook-redmine を clone してください。

余談ですが、vagrant は仮想マシン上の /vagrant ディレクトリをホスト OS 上の Vagrantfile が存在するディレクトリにマウントしてくれるので、Winodows7 上の SublimeText2 など使い慣れた GUI エディタでレシピを編集したい場合は、作業ディレクトリを /vagrant 内に作成する便利です。

vagrant@precise64:~$ mkdir /vagrant/work; cd /vagrant/work
vagrant@precise64:/vagrant/work$ git clone https://github.com/garbagetown/cookbook-redmine.git
Cloning into 'cookbook-redmine'...
remote: Reusing existing pack: 305, done.
remote: Counting objects: 17, done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 322 (delta 2), reused 0 (delta 0)
Receiving objects: 100% (322/322), 50.05 KiB, done.
Resolving deltas: 100% (145/145), done.

clone したディレクトリに移動したら、knife solo prepare を実行してレシピを node に適用する準備をします。

vagrant@precise64:/vagrant/work$ cd cookbook-redmine/
vagrant@precise64:/vagrant/work/cookbook-redmine$ knife solo prepare 192.168.33.100
Bootstrapping Chef...
(snip)
Installing Chef 11.8.2
installing with rpm...
warning: /tmp/install.sh.17099/chef-11.8.2-1.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 83ef826a: NOKEY
Preparing...                ########################################### [100%]
   1:chef                   ########################################### [100%]
Thank you for installing Chef!
Generating node config 'nodes/192.168.33.100.json'...

作成された json に実行するレシピや attributes を設定します。password は自身の設定値に読み替えてください。

vagrant@precise64:/vagrant/work/cookbook-redmine$ vi nodes/192.168.33.100.json
vagrant@precise64:/vagrant/work/cookbook-redmine$ cat nodes/192.168.33.100.json
{
  "run_list":[
    "redmine",
    "mysql",
    "subversion::server"
  ],
  "redmine" : {
    "databases" : {
      "production" : {
        "password" : "password"
      }
    }
  },
  "mysql" : {
    "server_root_password" : "password",
    "server_repl_password" : "password",
    "server_debian_password" : "password"
  }
}

最後に、knife solo cook を実行します。

vagrant@precise64:/vagrant/work/cookbook-redmine$ knife solo cook 192.168.33.100
Running Chef on 192.168.33.100...
Checking Chef version...
Installing Berkshelf cookbooks to '/home/vagrant/.berkshelf/knife-solo/4cf4f5c173d6b541da0269be1b4eb5e4f9805994'...
Using redmine (0.1.0) from metadata
Installing apt (2.3.4) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks'
Installing yum (2.2.0) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks'
Installing iptables (0.12.0) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks'
Installing subversion (1.1.2) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks'
Installing git (2.9.0) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks'
Installing dmg (2.1.4) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks'
Installing build-essential (1.4.2) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks'
Installing windows (1.12.8) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks'
Installing chef_handler (1.1.4) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks'
Installing runit (1.5.8) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks'
Installing yum-epel (0.2.0) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks'
Installing apache2 (1.8.14) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks'
Installing passenger_apache2 (2.1.2) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks'
Installing mysql (4.0.20) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks'
Installing openssl (1.1.0) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks'
Installing postgresql (3.3.4) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks'
Installing database (1.6.0) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks'
Installing aws (1.0.0) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks'
Installing xfs (1.1.0) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks'
Uploading the kitchen...
(snip)
Chef Client finished, 137 resources updated

ホスト OS のブラウザから http://192.168.33.100/ にアクセスし、Redmine のトップ画面が表示されることを確認します。

参考

16
16
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
16