ページ

ラベル chef の投稿を表示しています。 すべての投稿を表示
ラベル chef の投稿を表示しています。 すべての投稿を表示

2015年7月25日土曜日

Elixir/Phoenixで遊ぶ 1 - ChefでElixir環境作成 -

最近よく聞くElixir/Phoenixを使ってみる

Elixirとは
Erland VM上で動作するRubyに似た関数型言語
Phoenixとは
Elixirで実装されたウェブアプリケーションフレームワーク
Railsによく似ている(作者がRailsコミッタでもある)

環境作成


環境はVagrant(CentOS)+chef-soloで作成したいと思います。
Vagrantの環境作成はこちら, Chefの環境作成はこちらを参照
  • まずは専用のVagrantを作成
$ vagrant box list
centos6  (virtualbox, 0)
$ vagrant init centos6
Vagrantfileを以下に編集
1. ↓コメントアウト + port:4000をforwardするように修正
config.vm.network "forwarded_port", guest: 4000, host: 4000
2. ↓コメントアウト
config.vm.network "private_network", ip: "192.168.33.10"
vagrant起動
$ vagrant up
  • ssh設定
ssh.configにIPとホスト名の紐付けを行う
$ vagrant ssh-config --host elixir-phoenix >> ~/.ssh/config
ホスト名はelixir-phoenix
  • サーバーにchef-soloインストール
$ knife solo prepare elixir-phoenix

Cookbook作成


  • Elixirインストール用Cookbookの作成
$ knife cookbook create elixir -o site-cookbooks
各ファイルを以下内容で作成
Berksfile
source "https://supermarket.chef.io"
cookbook 'elixir', path: 'site-cookbooks/elixir'
metadata.rb
name             'elixir'
maintainer       '{name}'
maintainer_email '{name}@gmail.com'
license          'All rights reserved'
description      'Installs/Configures elixir'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version          '0.1.0'

depends "git"
depends "github"
depends "erlang"
attributes/default.rb
default['elixir']['version'] = '1.0.5'
default['elixir']['home'] = '/usr/local/elixir'
default['elixir']['link'] = '/usr/local/elixir/bin'
default['elixir']['repo'] = "https://github.com/elixir-lang/elixir.git"
default['elixir']['source']['path'] = '/usr/local/elixir/src'
recipes/default.rb
# install erlang
node.set[:erlang][:install_method]    = "source"
node.set[:erlang][:source][:version]  = "18.0"
node.set[:erlang][:source][:url]      = "http://erlang.org/download/otp_src_#{node[:erlang][:source][:version]}.tar.gz"
node.set[:erlang][:source][:checksum] = "a0b69da34b4f218eb7d63d9e96fc120aa7257bb6c37a0f40fb388e188b4111aa"

include_recipe "erlang::default"
include_recipe "git::default"

# create directory
directory node[:elixir][:home] do
  owner 'vagrant'
  group 'vagrant'
  mode '0755'
  action :create
end

# git clone to specified dir
git "elixir" do
  repository node[:elixir][:repo]
  revision "v#{node[:elixir][:version]}"
  destination node[:elixir][:source][:path]
  action :sync
end

# make
bash "make" do
  cwd node[:elixir][:source][:path]
  code "make"
  action :run
end

# create symbolic link
link node[:elixir][:link] do
  to "#{node[:elixir][:source][:path]}/bin"
end

# setting PATH environment variable
template "/etc/profile.d/elixir.sh" do
  mode 0755
  source "elixir.sh.erb"
  variables(:elixir_home => node[:elixir][:home])
end
※ erlangのotp_src_XXX.tar.gzだが、checksumが公式サイト
checksumを指定した所Chef::Exceptions::ChecksumMismatchが発生
ファイル自体破損してなさそう・・・?仕方ないのでトレースからchecksumをコピーしました
  • Chef-soloを実行
$ knife solo cook elixir-phoenix

正常に終了したらssh elixir-phoenixでサーバーへログイン
Interactive mode(rubyでいうirb)を試してみる
[vagrant@localhost ~]$ iex
Erlang/OTP 18 [erts-7.0] [source] [64-bit] [async-threads:10] [hipe] [kernel-poll:false]

Interactive Elixir (1.0.5) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> 40+2
42
iex(2)> "hello" <> " world"
"hello world"
iex(3)>
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
       (v)ersion (k)ill (D)b-tables (d)istribution

a
ちゃんと動作できてればOK
今回はここまで、次はPhoenixのインストールを実施!!

elixirのcookbookはこちらに公開しています。

2015年6月5日金曜日

Chefでお料理 3 - GitLab + JenkinsのCookbook作成 -

「Gitlab + Jenkins環境構築のCookbookを作成」のGitlabとJenkinsのCookBook作成


・Gitlab
gitlab_url = node['gitlab']['url']
gitlab_rpm = node['gitlab']['rpm']

# openssh-server インストール
package "openssh-server" do
  action :install
end

# postfix インストール
package "postfix" do
  action :install
end

# gitlab インストール
remote_file "/tmp/#{gitlab_rpm}" do
  source "#{gitlab_url}"
end

rpm_package "gitlab_rpm_package" do
  action :install
  provider Chef::Provider::Package::Rpm
  source "/tmp/#{gitlab_rpm}"
end

# 設定ファイル修正
template "/etc/gitlab/gitlab.rb" do
  mode 0755
  source "gitlab.rb.erb"
  variables(:external_url => node['gitlab']['external_url'])
end

# 起動
script "launch_gitlab" do
  interpreter "bash"
  user "root"
  code <<-EOL
    gitlab-ctl reconfigure
    lokkit -s http -s ssh
    gitlab-ctl start
  EOL
end

※設定ファイルにはテンプレートを使用
・gitlab.rb.erb
# Change the external_url to the address your users will type in their browser
external_url '<%= @external_url %>'


・Attributes
#
# Cookbook Name:: gitlab
# Attributes:: default

default['gitlab']['url'] = 'https://downloads-packages.s3.amazonaws.com/centos-6.5/gitlab-7.0.0_omnibus-1.el6.x86_64.rpm'
default['gitlab']['rpm'] = 'gitlab-7.0.0_omnibus-1.el6.x86_64.rpm'
default['gitlab']['external_url'] = 'http://localhost'


・Jenkins
# Recipe:: default
#
# Copyright 2015, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#

jenkins_yum = node['jenkins']['yum']
jenkins_url = node['jenkins']['url']
jenkins_key = node['jenkins']['key']

# リポジトリの追加
script "add_repo" do
  interpreter "bash"
  user "root"
  code <<-EOL
    wget -O "#{jenkins_yum}" "#{jenkins_url}"
    rpm --import "#{jenkins_key}"
  EOL
end

# インストール
package "jenkins" do
  action :install
end

# 環境変数の設定
template "/etc/sysconfig/jenkins" do
  mode 0600
  source "jenkins.erb"
  variables(:port => node['jenkins']['port'])
end

# 起動
service "jenkins" do
    action [ :enable, :start ]
end

# iptablesの無効化
service "iptables" do
    action [ :disable, :stop]
end

・Attributes
#
# Cookbook Name:: jenkins
# Attributes:: default

default['jenkins']['port'] = 8081
default['jenkins']['yum'] = '/etc/yum.repos.d/jenkins.repo'
default['jenkins']['url'] = 'http://pkg.jenkins-ci.org/redhat/jenkins.repo'
default['jenkins']['key'] = 'http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key'



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 ~]$ 

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

2015年5月30日土曜日

Chefでお料理 1 - ChefServer/ChefClint -

前回新しいい技術で勉強して、ちゃんと覚えれば役に立ちそうなので、
このまま使うように頑張ってみる。。


しかしこのChefやたら用語が沢山あってわかりづらいorz
一応整理すると・・・

使い方として大きく2種類ある。
  1. ChefServerを立てる
  2. スタンドアロン

個人でやるだけならChefServerを立てるまでもないので、スタンドアロンでやる。
スタンドアロンでやる時に使うツールが「Chef-solo」
そしてnodeにChef-Clientをインストールするなど便利なツールとして「Knife-solo」がある

↓現在インストールされているバージョン
$ chef-solo -v
Chef: 12.3.0


んで自分でCookbookを作る時には「site-cookbooks」配下に作っていく。
ベンダー製のCookBookを使えたりもする。

ベンダー製のCookBookを使う場合Berkshelfが便利。Rubyでいう所のBundler
使い方が掲載されたサイトを見て使ってみたがどうやら古いらしい。。
以下のサイトを発見!!
http://qiita.com/wwacky/items/66b493a05c99b5e754e8

・Berksfileの中身
site :opscode
cookbook 'java', '~> 1.31.0'
~                               

・コマンド実行!!
$ berks install
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...
Fetching cookbook index from https://supermarket.chef.io...
Installing java (1.31.0)


$ berks vendor
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...
Using java (1.31.0)
Vendoring java (1.31.0) to /Users/{ユーザ名}/Documents/develop/vagrant/boxes/cheftest/chef-repo/berks-cookbooks/java

・node.jsonの中身
{
  "run_list": [
     "java"
  ],
  "automatic": {
    "ipaddress": "chopper"
  }
}
~

・いざ実行!!
$ knife solo cook chopper
Running Chef on chopper...
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...
Using java (1.31.0)
Vendoring java (1.31.0) to cookbooks/java
Uploading the kitchen...
Generating solo config...
Running Chef...
Starting Chef Client, version 12.3.0
Compiling Cookbooks...
Converging 6 resources
Recipe: java::openjdk
  * yum_package[java-1.6.0-openjdk] action install
    - install version 1.6.0.35-1.13.7.1.el6_6 of package java-1.6.0-openjdk
  * yum_package[java-1.6.0-openjdk-devel] action install
    - install version 1.6.0.35-1.13.7.1.el6_6 of package java-1.6.0-openjdk-devel
  * java_alternatives[set-java-alternatives] action set
    - Add alternative for appletviewer
    - Add alternative for apt
    - Add alternative for extcheck
    - Add alternative for idlj
    - Add alternative for jar
    - Add alternative for jarsigner
    - Add alternative for java
    - Add alternative for javac
    - Add alternative for javadoc
    - Add alternative for javah
    - Add alternative for javap
    - Add alternative for jconsole
    - Add alternative for jdb
    - Add alternative for jhat
    - Add alternative for jinfo
    - Add alternative for jmap
    - Add alternative for jps
    - Add alternative for jrunscript
    - Add alternative for jsadebugd
    - Add alternative for jstack
    - Add alternative for jstat
    - Add alternative for jstatd
    - Add alternative for keytool
    - Add alternative for native2ascii
    - Add alternative for orbd
    - Add alternative for pack200
    - Add alternative for policytool
    - Add alternative for rmic
    - Add alternative for rmid
    - Add alternative for rmiregistry
    - Add alternative for schemagen
    - Add alternative for serialver
    - Add alternative for servertool
    - Add alternative for tnameserv
    - Add alternative for unpack200
    - Add alternative for wsgen
    - Add alternative for wsimport
    - Add alternative for xjc
Recipe: java::set_java_home
  * ruby_block[set-env-java-home] action run
    - execute the ruby block set-env-java-home
  * directory[/etc/profile.d] action create (up to date)
  * file[/etc/profile.d/jdk.sh] action create
    - create new file /etc/profile.d/jdk.sh
    - update content in file /etc/profile.d/jdk.sh from none to 311825
    --- /etc/profile.d/jdk.sh 2015-05-30 08:08:03.326817968 +0000
    +++ /etc/profile.d/.jdk.sh20150530-6022-1r7hm6j 2015-05-30 08:08:03.326817968 +0000
    @@ -1 +1,2 @@
    +export JAVA_HOME=/usr/lib/jvm/java-1.6.0
    - change mode from '' to '0755'

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

・確認
$ ssh chopper
Last login: Sat May 30 08:06:45 2015 from 10.0.2.2
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$ java -version
java version "1.6.0_35"


↓サードパティーのCookbookがあるサイト
https://supermarket.chef.io/cookbooks-directory

↓参考にしたサイト
http://kimikimi714.hatenablog.com/entry/2014/01/13/サードパーティ製chefレシピ使ってたの忘れてた
https://docs.chef.io/index.html#cookbooks
http://thinkit.co.jp/story/2013/11/18/4679