ページ

2015年5月31日日曜日

Chefでお料理 2 - GitLab + Jenkins -

今回から「Gitlab + Jenkins環境構築のCookbookを作成」を目指して頑張ってみる


・saharaプラグインのインストール

chefを何度も実行しながらテストするので、ロールバック的な機能がvagrantにあれば
非常に助かる。と思っていたらそんなプラグインがありました。
sahara」いうらしい。

↓公式ページ
https://github.com/jedi4ever/sahara

↓使い方など
http://easyramble.com/install-sahara-into-vagrant.html

ロールバックや、コミットなどができて便利。早速インストール!!
$ vagrant plugin list
vagrant-berkshelf (4.0.4)
vagrant-omnibus (1.4.1)
vagrant-share (1.1.3, system)
$ vagrant plugin install sahara
Installing the 'sahara' plugin. This can take a few minutes...
Installed the plugin 'sahara (0.0.17)'!
$ vagrant plugin list
sahara (0.0.17)
vagrant-berkshelf (4.0.4)
vagrant-omnibus (1.4.1)
vagrant-share (1.1.3, system)

・vagrant plugin list
 現在のプラグインインストール状況をリスト表示

・vagrant plugin install sahara
    saharaプラグインのインストール

環境構築用にVMを作成。
$ mkdir shipmerry
$ ls
shipmerry
$ cd shipmerry/
$ ls
$ vagrant box list
centos6  (virtualbox, 0)
jenkins  (virtualbox, 0)
ubuntu14 (virtualbox, 0)
$ vagrant init centos6
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.
$ vagrant up


ディレクトリの名前は「shipmerry」IPアドレスは以下に設定しました
config.vm.network "private_network", ip: "192.168.55.35"

そうです。ワンピースのゴーイングメリー号です。。


saharaのスナップショットを取るモードに変更
$ vagrant sandbox on
[default] Starting sandbox mode...
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

sshのホスト設定
vagrant ssh-config --host merry >> ~/.ssh/config


・最初にJDKをインストールするレシピを作成する。

$ knife cookbook create jdk -o site-cookbooks
** Creating cookbook jdk in /Users/ユーザ名/Documents/develop/vagrant/boxes/shipmerry/chef-repo/site-cookbooks
** Creating README for cookbook: jdk
** Creating CHANGELOG for cookbook: jdk
** Creating metadata for cookbook: jdk

↓レシピのないよう

# JDKインストール
case node['platform_family']
when 'debian'
  package 'openjdk-7-jdk'
when 'rhel'
  package 'java-1.7.0-openjdk'
else
  fail "`#{node['platform_family']}' is not supported!"
end

# JAVA_HOME環境変数を設定
cookbook_file "profile-java.sh" do
  path "/etc/profile.d/java.sh"
end
~     


・case node[‘platform_family’]
プラットフォーム毎に処理を切り分ける。
http://qiita.com/DQNEO/items/b8b808394ca08a2a86f9

・cookbook_fileでファイルを配置
↓profile-java.shの中身
shipmerry/chef-repo/site-cookbooks/jdk/files/default/profile-java.sh
JAVA_PATH=`readlink -m $(which java)`
export JAVA_HOME=${JAVA_PATH%/*/*}

※readlinkでシンボリックリンクのリンク先を取ってくる。
   ${JAVA_PATH%/*/*}で2階層のパスを削除しJAVA_HOMEに設定
↓readlink
[vagrant@localhost ~]$ readlink -m $(which java)
/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.79.x86_64/jre/bin/java

↓JAVA_HOME
JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.79.x86_64/jre

参考URL
http://qiita.com/asam316/items/08fb59f1da9d78f0e1a6
http://d.hatena.ne.jp/torutk/20090628/p1


・次にAntのインストールを行うCookbookの作成

参考URL
https://github.com/opscode-cookbooks/ant

↓レシピ
#
# Cookbook Name:: ant
# Recipe:: default
#
# Copyright 2015, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#

ant_home = node['ant']['home']

# ファイル取得
remote_file "/tmp/ant.tar.gz" do
  source node['ant']['url']
  checksum node['ant']['checksum']
end

# 取得したファイルを解凍し移動
script "install_ant" do
  interpreter "bash"
  user "root"
  code <<-EOL
    tar zxvf /tmp/ant.tar.gz
    rm -rf "#{ant_home}"
    mv -f apache-ant-* "#{ant_home}"
  EOL
end

# 環境変数の設定
template "/etc/profile.d/ant_home.sh" do
  mode 0755
  source "ant_home.sh.erb"
  variables(:ant_home => node['ant']['home'])
end

↓実行して確認
[vagrant@localhost bin]$ ./ant -version
Unable to locate tools.jar. Expected to find it in /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.79.x86_64/lib/tools.jar
Apache Ant(TM) version 1.8.2 compiled on December 20 2010

ん?tools.jarがないと怒られている・・・
vagrant@localhost bin]$ cd /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.79.x86_64/
[vagrant@localhost java-1.7.0-openjdk-1.7.0.79.x86_64]$ ls
ASSEMBLY_EXCEPTION  LICENSE  THIRD_PARTY_README  jre
[vagrant@localhost java-1.7.0-openjdk-1.7.0.79.x86_64]$ cd jre/
[vagrant@localhost jre]$ ls
bin  lib
[vagrant@localhost jre]$ cd bin
[vagrant@localhost bin]$ ls
java     orbd     policytool  rmiregistry  tnameserv
keytool  pack200  rmid        servertool   unpack200
[vagrant@localhost bin]$ cd ../
[vagrant@localhost jre]$ ls
bin  lib
[vagrant@localhost jre]$ cd lib
[vagrant@localhost lib]$ ls
accessibility.properties  flavormap.properties  psfont.properties.ja
amd64                     images                psfontj2d.properties
applet                    jce.jar               resources.jar
audio                     jexec                 rhino.jar
calendars.properties      jsse.jar              rt.jar
charsets.jar              jvm.hprof.txt         security
classlist                 logging.properties    sound.properties
cmm                       management            tz.properties
content-types.properties  management-agent.jar  zi
currency.data             meta-index
ext                       net.properties

実際ない><;
このJDKじゃダメじゃんorz

そういう事でJDKのCookbookを修正。
#
# Cookbook Name:: jdk
# Recipe:: default
#
# Copyright 2015, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#

# JDKインストール
#case node['platform_family']
#when 'debian'
#  package 'openjdk-7-jdk'
#when 'rhel'
#  package 'java-1.7.0-openjdk'
#else
#  fail "`#{node['platform_family']}' is not supported!"
#end

cookbook_file node['jdk']['file']  do
  path "/tmp/jdk-linux-x64.tar"
end

script "install_jdk" do
  interpreter "bash"
  user "root"
  code <<-EOL
    tar xvf /tmp/jdk-linux-x64.tar
    mv -f /jdk* /usr/local/jdk
    rm /usr/bin/java
    rm /usr/bin/javac
    ln -s /usr/local/jdk/bin/java /usr/bin/java
    ln -s /usr/local/jdk/bin/javac /usr/bin/javac
  EOL
end

# JAVA_HOME環境変数を設定
cookbook_file "profile-java.sh" do
  path "/etc/profile.d/java.sh"
end

↓改めて実行!!
i$ knife solo cook merry
Running Chef on merry...
Checking Chef version...
Installing Berkshelf cookbooks to 'cookbooks'...
DEPRECATED: Your Berksfile contains a site location pointing to the Opscode Community Site (site :opscode). Site locations have been replaced by the source location. Change this to: 'source "https://supermarket.chef.io"' to remove this warning. For more information visit https://github.com/berkshelf/berkshelf/wiki/deprecated-locations
Resolving cookbook dependencies...
Uploading the kitchen...
WARNING: Local cookbook_path '/Users/ユーザ名/Documents/develop/vagrant/boxes/shipmerry/chef-repo/cookbooks' does not exist
Generating solo config...
Running Chef...
Starting Chef Client, version 12.3.0
Compiling Cookbooks...
Converging 6 resources
Recipe: jdk::default
  * cookbook_file[jdk-7u79-linux-x64.tar] action create
    - create new file /tmp/jdk-linux-x64.tar
    - update content in file /tmp/jdk-linux-x64.tar from none to 084d1d
    (file sizes exceed 10000000 bytes, diff output suppressed)
  * script[install_jdk] action run
    - execute "bash"  "/tmp/chef-script20150531-2983-sepjz6"
  * cookbook_file[profile-java.sh] action create
    - create new file /etc/profile.d/java.sh
    - update content in file /etc/profile.d/java.sh from none to 09fb37
    --- /etc/profile.d/java.sh 2015-05-31 09:24:14.085580102 +0000
    +++ /etc/profile.d/.profile-java.sh20150531-2983-zepb85 2015-05-31 09:24:14.085580102 +0000
    @@ -1 +1,3 @@
    +JAVA_PATH=`readlink -m $(which java)`
    +export JAVA_HOME=${JAVA_PATH%/*/*}
Recipe: ant::default
  * remote_file[/tmp/ant.tar.gz] action create
    - create new file /tmp/ant.tar.gz
    - update content in file /tmp/ant.tar.gz from none to 664f48
    (new content is binary, diff output suppressed)
  * script[install_ant] action run
    - execute "bash"  "/tmp/chef-script20150531-2983-k8fmyi"
  * template[/etc/profile.d/ant_home.sh] action create
    - create new file /etc/profile.d/ant_home.sh
    - update content in file /etc/profile.d/ant_home.sh from none to cef22d
    --- /etc/profile.d/ant_home.sh 2015-05-31 09:24:35.433248587 +0000
    +++ /tmp/chef-rendered-template20150531-2983-cv3gg2 2015-05-31 09:24:35.433248587 +0000
    @@ -1 +1,4 @@
    +export ANT_HOME=/usr/local/ant
    +PATH=$PATH:/usr/local/ant/bin
    +export PATH
    - change mode from '' to '0755'

Running handlers:
Running handlers complete
Chef Client finished, 6/6 resources updated in 44.699034477 seconds


↓確認
$ ssh merry
Last login: Sun May 31 09:23:48 2015 from 10.0.2.2
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$ which java
/usr/bin/java
[vagrant@localhost ~]$ which javac
/usr/bin/javac
[vagrant@localhost ~]$ ant
Buildfile: build.xml does not exist!
Build failed
[vagrant@localhost ~]$ 

ちゃんとインストールされている〜^^

0 件のコメント:

コメントを投稿