Skip to content

Commit

Permalink
Copy resource "jenkins_cli" from heavywater/chef-jenkins.
Browse files Browse the repository at this point in the history
  • Loading branch information
okinaka committed Dec 24, 2012
1 parent 2941763 commit 25f53ee
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 5 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Description
===========
Installs and configures Jenkins CI server.

Attributes
==========

* jenkins[:java_home] - Java install path, used for for cli commands
* jenkins[:server][:home] - JENKINS_HOME directory
* jenkins[:server][:user] - User the Jenkins server runs as
* jenkins[:server][:group] - Jenkins user primary group
* jenkins[:server][:port] - TCP listen port for the Jenkins server
* jenkins[:server][:url] - Base URL of the Jenkins server
* jenkins[:node][:home] - Home directory of the node

Usage
=====

'default' recipe
----------------

Installs a Jenkins CI server.

'jenkins_cli' resource provider
-------------------------------

This resource can be used to execute the Jenkins cli from your recipes. For example, install git plugin and restart Jenkins:

jenkins_cli "install-plugin git"
jenkins_cli "safe-restart"
24 changes: 24 additions & 0 deletions attributes/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
default[:jenkins][:java_home] = ENV['JAVA_HOME']

default[:jenkins][:server][:home] = "/var/lib/jenkins"
default[:jenkins][:server][:user] = "jenkins"

case node[:platform]
when "debian", "ubuntu"
default[:jenkins][:server][:group] = "nogroup"
else
default[:jenkins][:server][:group] = node[:jenkins][:server][:user]
end

default[:jenkins][:server][:port] = 8080
default[:jenkins][:server][:host] = "127.0.0.1"
default[:jenkins][:server][:url] = "http://#{node[:jenkins][:server][:host]}:#{node[:jenkins][:server][:port]}"

#download the latest version of plugins, bypassing update center
#example: ["git", "URLSCM", ...]
default[:jenkins][:server][:plugins] = []

#See Jenkins >> Nodes >> $name >> Configure

#"Remote FS root"
default[:jenkins][:node][:home] = "/home/jenkins"
6 changes: 1 addition & 5 deletions metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@
maintainer "OKINAKA Kenshin"
maintainer_email "[email protected]"
license "Apache 2.0"
description "Installs and configures Jenkins CI server & slaves"
description "Installs and configures Jenkins CI server"
version "0.0.1"

depends "java"
recommends "apt"
recommends "yum"

%w{ debian ubuntu centos redhat fedora scientific amazon }.each do |os|
supports os
end

recipe "jenkins", "Installs jenkins"
53 changes: 53 additions & 0 deletions providers/cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#
# Cookbook Name:: jenkins
# Based on hudson
# Provider:: cli
#
# Author:: Doug MacEachern <[email protected]>
# Author:: Fletcher Nichol <[email protected]>
#
# 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.
#

def action_run
url = @new_resource.url || node[:jenkins][:server][:url]
home = @new_resource.home || node[:jenkins][:node][:home]

#recipes will chown to jenkins later if this doesn't already exist
directory "home for jenkins-cli.jar" do
action :create
path node[:jenkins][:node][:home]
end

cli_jar = ::File.join(home, "jenkins-cli.jar")
remote_file cli_jar do
source "#{url}/jnlpJars/jenkins-cli.jar"
not_if { ::File.exists?(cli_jar) }
end

java_home = node[:jenkins][:java_home] || (node.has_key?(:java) ? node[:java][:jdk_dir] : nil)
if java_home == nil
java = "java"
else
java = ::File.join(java_home, "bin", "java")
end

command = "#{java} -jar #{cli_jar} -s #{url} #{@new_resource.command}"

jenkins_execute command do
cwd home
block { |stdout| new_resource.block.call(stdout) } if new_resource.block
end
end
50 changes: 50 additions & 0 deletions providers/execute.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#
# Cookbook Name:: jenkins
# Based on hudson
# Provider:: execute
#
# Author:: Doug MacEachern <[email protected]>
# Author:: Fletcher Nichol <[email protected]>
#
# 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.
#

#pruned Chef::Provider::Execute + optional `block' param

include Chef::Mixin::Command

def action_run
args = {
:command => @new_resource.command,
:command_string => @new_resource.to_s,
}
args[:only_if] = @new_resource.only_if if @new_resource.only_if
args[:not_if] = @new_resource.not_if if @new_resource.not_if
args[:timeout] = @new_resource.timeout if @new_resource.timeout
args[:cwd] = @new_resource.cwd if @new_resource.cwd

status, stdout, stderr = output_of_command(args[:command], args)
if status.exitstatus == 0
@new_resource.block.call(stdout) if @new_resource.block
@new_resource.updated_by_last_action(true)
Chef::Log.info("Ran #{@new_resource} successfully")
else
command_output = "JENKINS STDOUT: #{stdout}"
command_output << "JENKINS STDERR: #{stderr}"
handle_command_failures(status, command_output, args)
end
end


44 changes: 44 additions & 0 deletions resources/cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#
# Cookbook Name:: jenkins
# Based on hudson
# Resource:: cli
#
# Author:: Doug MacEachern <[email protected]>
# Author:: Fletcher Nichol <[email protected]>
#
# 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.
#

actions :run

attribute :url, :kind_of => String
attribute :home, :kind_of => String
attribute :command, :kind_of => String
attribute :timeout, :kind_of => Integer
attribute :block, :kind_of => Proc

def initialize(name, run_context=nil)
super
@action = :run
@command = name
end

def block(&block)
if block_given? and block
@block = block
else
@block
end
end
43 changes: 43 additions & 0 deletions resources/execute.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#
# Cookbook Name:: jenkins
# Based on hudson
# Resource:: execute
#
# Author:: Doug MacEachern <[email protected]>
# Author:: Fletcher Nichol <[email protected]>
#
# 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.
#

actions :run

attribute :command, :kind_of => String
attribute :cwd, :kind_of => String
attribute :timeout, :kind_of => Integer
attribute :block, :kind_of => Proc

def initialize(name, run_context=nil)
super
@action = :run
@command = name
end

def block(&block)
if block_given? and block
@block = block
else
@block
end
end

0 comments on commit 25f53ee

Please sign in to comment.