From f02fe63299e90931db23a9c31a020bb70fa33adb Mon Sep 17 00:00:00 2001 From: pierre-o13 Date: Thu, 17 Jan 2013 16:46:34 +0000 Subject: [PATCH 1/5] make it compatible with war install and gentoo --- attributes/default.rb | 3 +- attributes/package.rb | 24 +++++++++ attributes/war.rb | 25 +++++++++ recipes/commons.rb | 69 ++++++++++++++++++++++++ recipes/default.rb | 104 ++++++------------------------------ recipes/package.rb | 63 ++++++++++++++++++++++ recipes/war.rb | 59 ++++++++++++++++++++ templates/gentoo/conf.d.erb | 54 +++++++++++++++++++ templates/gentoo/init.d.erb | 44 +++++++++++++++ 9 files changed, 356 insertions(+), 89 deletions(-) create mode 100644 attributes/package.rb create mode 100644 attributes/war.rb create mode 100644 recipes/commons.rb create mode 100644 recipes/package.rb create mode 100644 recipes/war.rb create mode 100644 templates/gentoo/conf.d.erb create mode 100644 templates/gentoo/init.d.erb diff --git a/attributes/default.rb b/attributes/default.rb index 42caf88..e1cce6c 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -22,9 +22,10 @@ # default[:jenkins][:mirror] = "http://mirrors.jenkins-ci.org" -default[:jenkins][:package_url] = "http://pkg.jenkins-ci.org" default[:jenkins][:java_home] = ENV['JAVA_HOME'] +default[:jenkins][:java_install] = false + default[:jenkins][:server][:home] = "/var/lib/jenkins" default[:jenkins][:server][:user] = "jenkins" diff --git a/attributes/package.rb b/attributes/package.rb new file mode 100644 index 0000000..5afc244 --- /dev/null +++ b/attributes/package.rb @@ -0,0 +1,24 @@ +# +# Cookbook Name:: jenkins +# Based on hudson +# Attributes:: package +# +# Author:: Doug MacEachern +# Author:: Fletcher Nichol +# +# Copyright 2010, VMware, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +default[:jenkins][:package_url] = "http://pkg.jenkins-ci.org" diff --git a/attributes/war.rb b/attributes/war.rb new file mode 100644 index 0000000..e6c2c1d --- /dev/null +++ b/attributes/war.rb @@ -0,0 +1,25 @@ +# +# Cookbook Name:: jenkins +# Based on hudson +# Attributes:: war +# +# Author:: Pierre Ozoux +# +# Copyright 2010, VMware, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +default[:jenkins][:war_url] = "http://mirrors.jenkins-ci.org/war/latest/jenkins.war" +default[:jenkins][:server][:log_dir] = "/var/log/jenkins" +default[:jenkins][:server][:java_war_dir] = "/usr/share/jenkins" diff --git a/recipes/commons.rb b/recipes/commons.rb new file mode 100644 index 0000000..6a157e0 --- /dev/null +++ b/recipes/commons.rb @@ -0,0 +1,69 @@ +# +# Cookbook Name:: jenkins +# Based on hudson +# Recipe:: default +# +# Author:: AJ Christensen +# Author:: Doug MacEachern +# Author:: Fletcher Nichol +# +# Copyright 2010, VMware, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +pkey = "#{node[:jenkins][:server][:home]}/.ssh/id_rsa" + +user node[:jenkins][:server][:user] do + home node[:jenkins][:server][:home] +end + +directory node[:jenkins][:server][:home] do + recursive true + owner node[:jenkins][:server][:user] + group node[:jenkins][:server][:group] +end + +directory "#{node[:jenkins][:server][:home]}/.ssh" do + mode 0700 + owner node[:jenkins][:server][:user] + group node[:jenkins][:server][:group] +end + +execute "ssh-keygen -f #{pkey} -N ''" do + user node[:jenkins][:server][:user] + group node[:jenkins][:server][:group] + not_if { File.exists?(pkey) } +end + +ruby_block "store jenkins ssh pubkey" do + block do + node.set[:jenkins][:server][:pubkey] = File.open("#{pkey}.pub") { |f| f.gets } + end +end + +directory "#{node[:jenkins][:server][:home]}/plugins" do + owner node[:jenkins][:server][:user] + group node[:jenkins][:server][:group] + only_if { node[:jenkins][:server][:plugins].size > 0 } +end + +node[:jenkins][:server][:plugins].each do |name| + remote_file "#{node[:jenkins][:server][:home]}/plugins/#{name}.hpi" do + source "#{node[:jenkins][:mirror]}/plugins/#{name}/latest/#{name}.hpi" + backup false + owner node[:jenkins][:server][:user] + group node[:jenkins][:server][:group] + action :create_if_missing + end +end diff --git a/recipes/default.rb b/recipes/default.rb index 7c359e2..5f546f4 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -22,88 +22,19 @@ # limitations under the License. # -pkey = "#{node[:jenkins][:server][:home]}/.ssh/id_rsa" -tmp = "/tmp" - -user node[:jenkins][:server][:user] do - home node[:jenkins][:server][:home] -end - -directory node[:jenkins][:server][:home] do - recursive true - owner node[:jenkins][:server][:user] - group node[:jenkins][:server][:group] -end - -directory "#{node[:jenkins][:server][:home]}/.ssh" do - mode 0700 - owner node[:jenkins][:server][:user] - group node[:jenkins][:server][:group] -end - -execute "ssh-keygen -f #{pkey} -N ''" do - user node[:jenkins][:server][:user] - group node[:jenkins][:server][:group] - not_if { File.exists?(pkey) } -end - -ruby_block "store jenkins ssh pubkey" do - block do - node.set[:jenkins][:server][:pubkey] = File.open("#{pkey}.pub") { |f| f.gets } - end -end +#"jenkins stop" may (likely) exit before the process is actually dead +#so we sleep until nothing is listening on jenkins.server.port (according to netstat) -directory "#{node[:jenkins][:server][:home]}/plugins" do - owner node[:jenkins][:server][:user] - group node[:jenkins][:server][:group] - only_if { node[:jenkins][:server][:plugins].size > 0 } -end +include_recipe 'jenkins::commons' -node[:jenkins][:server][:plugins].each do |name| - remote_file "#{node[:jenkins][:server][:home]}/plugins/#{name}.hpi" do - source "#{node[:jenkins][:mirror]}/plugins/#{name}/latest/#{name}.hpi" - backup false - owner node[:jenkins][:server][:user] - group node[:jenkins][:server][:group] - action :create_if_missing - end +if node.jenkins.java_install or node.platform == "gentoo" + include_recipe 'jenkins::war' +else + include_recipe 'jenkins::package' end -case node.platform -when "ubuntu", "debian" - include_recipe "apt" - include_recipe "java" +pid_file = node[:jenkins][:server][:pid_file] - pid_file = "/var/run/jenkins/jenkins.pid" - install_starts_service = true - - apt_repository "jenkins" do - uri "#{node.jenkins.package_url}/debian" - components %w[binary/] - key "http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key" - action :add - end -when "centos", "redhat" - include_recipe "yum" - - pid_file = "/var/run/jenkins.pid" - install_starts_service = false - - yum_key "jenkins" do - url "#{node.jenkins.package_url}/redhat/jenkins-ci.org.key" - action :add - end - - yum_repository "jenkins" do - description "repository for jenkins" - url "#{node.jenkins.package_url}/redhat/" - key "jenkins" - action :add - end -end - -#"jenkins stop" may (likely) exit before the process is actually dead -#so we sleep until nothing is listening on jenkins.server.port (according to netstat) ruby_block "netstat" do block do 10.times do @@ -145,22 +76,19 @@ action :nothing end -log "jenkins: install and start" do - notifies :install, "package[jenkins]", :immediately - notifies :start, "service[jenkins]", :immediately unless install_starts_service +log "jenkins: start" do + case node.platform + when "ubuntu", "debian" + #nothing to start + else + notifies :start, "service[jenkins]", :immediately + end notifies :create, "ruby_block[block_until_operational]", :immediately not_if do - File.exists? "/usr/share/jenkins/jenkins.war" + File.exists? "#{node.jenkins.server.java_war_dir}/jenkins.war" end end -template "/etc/default/jenkins" - -package "jenkins" do - action :nothing - notifies :create, "template[/etc/default/jenkins]", :immediately -end - # restart if this run only added new plugins log "plugins updated, restarting jenkins" do #ugh :restart does not work, need to sleep after stop. diff --git a/recipes/package.rb b/recipes/package.rb new file mode 100644 index 0000000..7aa36e9 --- /dev/null +++ b/recipes/package.rb @@ -0,0 +1,63 @@ +# +# Cookbook Name:: jenkins +# Based on hudson +# Recipe:: package +# +# Author:: AJ Christensen +# Author:: Doug MacEachern +# Author:: Fletcher Nichol +# +# Copyright 2010, VMware, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +case node.platform +when "ubuntu", "debian" + include_recipe "apt" + include_recipe "java" + + node.set[:jenkins][:server][:pid_file] = "/var/run/jenkins/jenkins.pid" + + apt_repository "jenkins" do + uri "#{node.jenkins.package_url}/debian" + components %w[binary/] + key "http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key" + action :add + end +when "centos", "redhat" + include_recipe "yum" + + node.set[:jenkins][:server][:pid_file] = "/var/run/jenkins.pid" + + yum_key "jenkins" do + url "#{node.jenkins.package_url}/redhat/jenkins-ci.org.key" + action :add + end + + yum_repository "jenkins" do + description "repository for jenkins" + url "#{node.jenkins.package_url}/redhat/" + key "jenkins" + action :add + end +end + +notifies :install, "package[jenkins]", :immediately + +template "/etc/default/jenkins" + +package "jenkins" do + action :nothing + notifies :create, "template[/etc/default/jenkins]", :immediately +end diff --git a/recipes/war.rb b/recipes/war.rb new file mode 100644 index 0000000..da14938 --- /dev/null +++ b/recipes/war.rb @@ -0,0 +1,59 @@ +# +# Cookbook Name:: jenkins +# Based on hudson +# Recipe:: war +# +# Author:: Pierre Ozoux +# +# Copyright 2010, VMware, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +node.set[:jenkins][:server][:pid_file] = "/var/run/jenkins.pid" + +directory node[:jenkins][:server][:java_war_dir] do + mode 0755 +end + +directory node[:jenkins][:server][:log_dir] do + mode 0755 + owner node[:jenkins][:server][:user] + group node[:jenkins][:server][:group] +end + +remote_file "#{node.jenkins.server.java_war_dir}/jenkins.war" do + source node[:jenkins][:war_url] + mode 00644 +end + +#init method +#It works on Gentoo, didn't test on other platform, that's why there is the test. +if node.platform == "gentoo" + + template "/etc/init.d/jenkins" do + source "init.d.erb" + mode 0755 + owner "root" + group "root" + end + + template "/etc/conf.d/jenkins" do + source "conf.d.erb" + mode 0644 + owner "root" + group "root" + end +else + #your init method +end diff --git a/templates/gentoo/conf.d.erb b/templates/gentoo/conf.d.erb new file mode 100644 index 0000000..457c2ba --- /dev/null +++ b/templates/gentoo/conf.d.erb @@ -0,0 +1,54 @@ +# Generated by Chef for <%= node[:fqdn] %> +# Local modifications will be overwritten. +# + +JENKINS_PIDFILE="<%= node[:jenkins][:server][:pid_file] %>" +JENKINS_WAR="<%= node[:jenkins][:server][:java_war_dir] %>/jenkins.war" + +RUN_AS="<%= node[:jenkins][:server][:user] %>" + +# Config file for /etc/init.d/jenkins + +# +# Directory where Jenkins store its configuration and working +# files (checkouts, build reports, artifacts, ...). +# +JENKINS_HOME="<%= node[:jenkins][:server][:home] %>" + +# +# Options to pass to java when running Jenkins. +# +JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true" + +# +# Port Jenkins is listening on. +# +JENKINS_PORT="<%= node[:jenkins][:server][:port] %>" + +# +# Debug level for logs -- the higher the value, the more verbose. +# 5 is INFO. +# +JENKINS_DEBUG_LEVEL="5" + +# +# Whether to enable access logging or not. +# +JENKINS_ENABLE_ACCESS_LOG="no" + +# +# Maximum number of HTTP worker threads. +# +JENKINS_HANDLER_MAX="100" + +# +# Maximum number of idle HTTP worker threads. +# +JENKINS_HANDLER_IDLE="20" + +# +# Pass arbitrary arguments to Jenkins. +# Eg.: --httpListenAddress=127.0.0.1 --prefix=/jenkins +# Full option list: java -jar jenkins.war --help +# +JENKINS_ARGS="" diff --git a/templates/gentoo/init.d.erb b/templates/gentoo/init.d.erb new file mode 100644 index 0000000..ef81104 --- /dev/null +++ b/templates/gentoo/init.d.erb @@ -0,0 +1,44 @@ +#!/sbin/runscript +# Copyright 1999-2012 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# $Header: $ + +depend() { + need net + use dns logger +} + +start() { + JAVA_HOME=`java-config --jre-home` + COMMAND=$JAVA_HOME/bin/java + JAVA_PARAMS="$JENKINS_JAVA_OPTIONS -DJENKINS_HOME=$JENKINS_HOME -jar $JENKINS_WAR" + # Don't use --daemon here, because in this case stop will not work + PARAMS="--logfile=<%= node[:jenkins][:server][:log_dir] %>/jenkins.log" + [ -n "$JENKINS_PORT" ] && PARAMS="$PARAMS --httpPort=<%= node[:jenkins][:server][:port] %>" + [ -n "$JENKINS_DEBUG_LEVEL" ] && PARAMS="$PARAMS --debug=$JENKINS_DEBUG_LEVEL" + [ -n "$JENKINS_HANDLER_STARTUP" ] && PARAMS="$PARAMS --handlerCountStartup=$JENKINS_HANDLER_STARTUP" + [ -n "$JENKINS_HANDLER_MAX" ] && PARAMS="$PARAMS --handlerCountMax=$JENKINS_HANDLER_MAX" + [ -n "$JENKINS_HANDLER_IDLE" ] && PARAMS="$PARAMS --handlerCountMaxIdle=$JENKINS_HANDLER_IDLE" + [ -n "$JENKINS_ARGS" ] && PARAMS="$PARAMS $JENKINS_ARGS" + if [ "$JENKINS_ENABLE_ACCESS_LOG" = "yes" ]; then + PARAMS="$PARAMS --accessLoggerClassName=winstone.accesslog.SimpleAccessLogger --simpleAccessLogger.format=combined --simpleAccessLogger.file=<%= node[:jenkins][:server][:log_dir] %>/access_log" + fi + ebegin "Starting ${SVCNAME}" + echo $JENKINS_PIDFILE + echo command: start-stop-daemon --start \ + --make-pidfile --pidfile $JENKINS_PIDFILE \ + --user $RUN_AS \ + --exec "${COMMAND}" -- $JAVA_PARAMS $PARAMS + start-stop-daemon --start \ + --make-pidfile --pidfile $JENKINS_PIDFILE \ + --background \ + --user $RUN_AS \ + --exec "${COMMAND}" -- $JAVA_PARAMS $PARAMS + eend $? + } + +stop() { + ebegin "Stopping ${SVCNAME}" + start-stop-daemon --stop --quiet --pidfile $JENKINS_PIDFILE + eend $? +} From 42b0250771f491e32577b1f10f5057609f81a548 Mon Sep 17 00:00:00 2001 From: pierre-o13 Date: Fri, 8 Feb 2013 14:56:56 +0000 Subject: [PATCH 2/5] removed runit for now --- metadata.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metadata.rb b/metadata.rb index 5690c14..bbf20df 100644 --- a/metadata.rb +++ b/metadata.rb @@ -5,5 +5,5 @@ long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) version "0.6.3" -%w(runit java).each { |cb| depends cb } +%w(java).each { |cb| depends cb } %w(iptables yum apt).each { |cb| recommends cb } From 4292019237992fb8811a112497dbb2efbf9401d1 Mon Sep 17 00:00:00 2001 From: pierre-o13 Date: Mon, 11 Feb 2013 14:27:34 +0000 Subject: [PATCH 3/5] add java recipe to war recipe --- recipes/war.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/recipes/war.rb b/recipes/war.rb index da14938..39088e2 100644 --- a/recipes/war.rb +++ b/recipes/war.rb @@ -20,6 +20,9 @@ # limitations under the License. # +include_recipe "java" + + node.set[:jenkins][:server][:pid_file] = "/var/run/jenkins.pid" directory node[:jenkins][:server][:java_war_dir] do From 121b0143cef21c6e852c3a7b35c9dd72b463df49 Mon Sep 17 00:00:00 2001 From: pierre-o13 Date: Mon, 18 Feb 2013 10:54:02 +0000 Subject: [PATCH 4/5] chef 11 bug (?) --- metadata.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/metadata.rb b/metadata.rb index bbf20df..2908a25 100644 --- a/metadata.rb +++ b/metadata.rb @@ -7,3 +7,4 @@ %w(java).each { |cb| depends cb } %w(iptables yum apt).each { |cb| recommends cb } +depends "nginx" From 43a9020ee5a2bf982893557f9ed26eecf96d5a9b Mon Sep 17 00:00:00 2001 From: pierre-o13 Date: Mon, 18 Feb 2013 10:54:17 +0000 Subject: [PATCH 5/5] avoid downloading again the war --- recipes/war.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/recipes/war.rb b/recipes/war.rb index 39088e2..da48d2e 100644 --- a/recipes/war.rb +++ b/recipes/war.rb @@ -38,6 +38,7 @@ remote_file "#{node.jenkins.server.java_war_dir}/jenkins.war" do source node[:jenkins][:war_url] mode 00644 + not_if "test -f #{node.jenkins.server.java_war_dir}/jenkins.war" end #init method