OSDN Git Service

ce1fbf2aebb6d4c8b9da85701277208410288c4f
[metasearch/grid-chef-repo.git] / cookbooks / jenkins-grid / recipes / docker-compose.rb
1 #
2 # Cookbook Name:: jenkins-grid
3 # Recipe:: docker-compose
4 #
5 # Copyright 2016-2017, whitestar
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #     http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19
20 doc_url = 'https://hub.docker.com/_/jenkins/'
21
22 include_recipe 'docker-grid::compose'
23
24 config = node['jenkins-grid']['docker-compose']['config']
25 override_config = node.override['jenkins-grid']['docker-compose']['config']
26 force_override_config = node.force_override['jenkins-grid']['docker-compose']['config']
27 app_dir = node['jenkins-grid']['docker-compose']['app_dir']
28 certs_dir = "#{app_dir}/certs"
29 groovy_dir = "#{app_dir}/ref/init.groovy.d"
30
31 envs = {}
32 java_opts = []
33 jenkins_opts = []
34 vols = config['services']['jenkins']['volumes'].to_a
35
36 [
37   app_dir,
38   certs_dir,
39   groovy_dir,
40 ].each {|dir|
41   resources(directory: dir) rescue directory dir do
42     owner 'root'
43     group 'root'
44     mode '0755'
45     recursive true
46   end
47 }
48
49 ports = config['services']['jenkins']['ports']
50 override_config['services']['jenkins']['ports'] = ['8080:8080', '50000:50000'] if ports.empty?
51
52 jenkins_owner = node['jenkins-grid']['docker-compose']['jenkins_home']['owner']
53 jenkins_home_path = node['jenkins-grid']['docker-compose']['jenkins_home']['path']
54 unless jenkins_home_path.nil?
55   directory jenkins_home_path do
56     owner jenkins_owner
57     group 'root'
58     mode '0755'
59     recursive true
60   end
61   vols.push("#{jenkins_home_path}:/var/jenkins_home")
62
63   template "#{jenkins_home_path}/log.properties" do
64     source 'var/lib/jenkins_home/log.properties'
65     owner 'root'
66     group 'root'
67     mode '0644'
68   end
69   java_opts.push('-Djava.util.logging.config.file=/var/jenkins_home/log.properties')
70 end
71
72 if node['jenkins-grid']['with_ssl_cert_cookbook']
73   include_recipe 'ssl_cert::server_key_pairs'
74   ::Chef::Recipe.send(:include, SSLCert::Helper)
75   cn = node['jenkins-grid']['ssl_cert']['common_name']
76   key_path = server_key_path(cn)
77
78   bash 'copy_ssl_server_key_for_jenkins' do
79     code <<-EOH
80       cp #{key_path} #{certs_dir}/server.key
81       chown #{jenkins_owner} #{certs_dir}/server.key
82       chmod 600 #{certs_dir}/server.key
83     EOH
84     sensitive true
85     action :run
86     not_if "cmp #{key_path} #{certs_dir}/server.key"
87     #action :nothing
88     #subscribes :run, "file[#{key_path}]"
89   end
90
91   vols.push("#{server_cert_path(cn)}:/var/lib/jenkins/server.crt:ro")
92   vols.push("#{certs_dir}/server.key:/var/lib/jenkins/server.key:ro")
93   jenkins_opts.push('--httpsCertificate=/var/lib/jenkins/server.crt')
94   jenkins_opts.push('--httpsPrivateKey=/var/lib/jenkins/server.key')
95 end
96
97 executors_conf = 'ref/init.groovy.d/executors.groovy'
98 vols.push("#{app_dir}/#{executors_conf}:/usr/share/jenkins/#{executors_conf}:ro")
99
100 unless jenkins_opts.empty?
101   if !config['services']['jenkins']['environment'].nil? \
102     && !config['services']['jenkins']['environment']['JENKINS_OPTS'].nil?
103     jenkins_opts.unshift(config['services']['jenkins']['environment']['JENKINS_OPTS'])
104   end
105   envs['JENKINS_OPTS'] = jenkins_opts.join(' ')
106 end
107
108 unless java_opts.empty?
109   if !config['services']['jenkins']['environment'].nil? \
110     && !config['services']['jenkins']['environment']['JAVA_OPTS'].nil?
111     java_opts.unshift(config['services']['jenkins']['environment']['JAVA_OPTS'])
112   end
113   envs['JAVA_OPTS'] = java_opts.join(' ')
114 end
115
116 # force_override for merging JENKINS_OPTS and JAVA_OPTS attributes' value.
117 force_override_config['services']['jenkins']['environment'] = envs unless envs.empty?
118 override_config['services']['jenkins']['volumes'] = vols unless vols.empty?
119
120 [
121   'docker-compose.yml',
122   'ref/init.groovy.d/executors.groovy',
123 ].each {|conf_file|
124   template "#{app_dir}/#{conf_file}" do
125     source  "opt/docker-compose/app/jenkins/#{conf_file}"
126     owner 'root'
127     group 'root'
128     mode '0644'
129   end
130 }
131
132 log <<-"EOM"
133 Note: You must execute the following command manually.
134   See #{doc_url}
135   * Start:
136     $ cd #{app_dir}
137     $ docker-compose up -d
138   * Stop
139     $ docker-compose down
140 EOM