Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Vagrant | Jenkins環境をserverspecを利用したテストファーストで構築

概要

Jenkins環境を構築

詳細

Jenkins環境を構築します。
gitも一緒にインストールします。

設定手順

前提

利用plugin

vagrant-berkshelf
sahara
vagrant-omnibus

OS:Ubuntu1204
Servertest: serverspec

Chef Solo,Knife Soloを利用して作成します。

serverspec

環境構築をテストファーストします。
serverspec環境はあらかじめ設定済みとする。
詳しくは
serverspec | SSH経由でのテスト実行
http://d.hatena.ne.jp/tbpg/20131025/1382719718
を参照。

$ vagrant ssh
$ cd ./work/
$ mkdir specs
$ cd specs
$ mkdir jenkins
$ cd jenkins
vagrant@precise64:~/work/specs$ bundle exec serverspec-init
Select OS type:

  1) UN*X
  2) Windows

Select number: 1

Select a backend type:

  1) SSH
  2) Exec (local)

Select number: 1

Vagrant instance y/n: n
Input target host name: 192.168.33.11
 + spec/
 + spec/192.168.33.11/
 + spec/192.168.33.11/httpd_spec.rb
 + spec/spec_helper.rb
 + Rakefile

# httpd_spec.rbを編集(ファイル内容は後述)

# テスト実行
$ rake spec
Finished in 0.07651 seconds
7 examples, 7 failures
httpd_spec.rb
require 'spec_helper'

['openjdk-7-jdk', 'apache2', 'jenkins', 'git'].each do |s|
  describe package(s) do
    it { should be_installed }
  end
end

describe service('jenkins') do
  it { should be_enabled   }
  it { should be_running   }
end

describe port(8080) do
  it { should be_listening }
end
Jenkinsサーバー構築手順
# キッチンのテンプレート作成
$ knife solo init jenkins
$ cd jenkins

# Vagrantfileのテンプレート作成
$ vagrant init

# Vagrantfileを編集(後述)
$ vi vagrant

# 起動確認
$ vagrant ssh
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-omnibusによって指定したchef-soloがインストールされていることを確認
vagrant@precise64:~$ chef-solo -v
Chef: 11.6.0
vagrant@precise64:~$ exit

# クックブック作成
$ knife cookbook create jenkins -o site-cookbooks

# クックブック編集(後述)

# クックブックの実行
$ vagrant provision

# 構築完了
serverspec 環境でテストを実行して完成確認

※公開鍵の設定を先に行っておく。手順は下記参照
vagrant検証用に仮想環境を2環境作成してSSH接続確認を行う
http://d.hatena.ne.jp/tbpg/20131025/1382717749

# 前述行程よりログインしたままとする

$ pwd
/home/vagrant/work/specs/jenkins
$ rake spec
/home/vagrant/.rbenv/versions/2.0.0-p247/bin/ruby -S rspec spec/192.168.33.11/httpd_spec.rb
.......

Finished in 0.29538 seconds
7 examples, 0 failures

最後に画面で確認
http://192.168.33.11:8080にアクセス

Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu-12.04-x64"
  config.omnibus.chef_version = "11.6.0"
  config.vm.network :private_network, ip: "192.168.33.11"

  config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = ["./cookbooks", "./site-cookbooks"]
    chef.add_recipe "jenkins"
  end
end
クックブック編集

jenkins/site-cookbooks/jenkins/receipe/default.rb

#
# Cookbook Name:: jenkins
# Recipe:: default
#
# Copyright 2013, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#
log "start update apt-get"
execute "update package index" do
  command "apt-get update"
  ignore_failure true
  action :nothing
end.run_action(:run)
log "end   update apt-get"

log "start install openjdk-7-jdk"
package "openjdk-7-jdk" do
  action :install
end
log "end   install openjdk-7-jdk"

log "start prepare install jenkins"
execute "update package index" do
  command "wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -"
  command "sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list'"
end.run_action(:run)
log "end   prepare install jenkins"

log "start install jenkins"
package "jenkins" do
  action :install
end
log "end   install jenkins"

log "start install git"
package "git" do
  action :install
end
log "end   install git"

jenkins/nodes/192.168.33.11.json

{"run_list":[" recipe [ jenkins ]"]}