OSDN Git Service

improves server key pair deployment.
[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   ::Chef::Recipe.send(:include, SSLCert::Helper)
74   cn = node['jenkins-grid']['ssl_cert']['common_name']
75   append_server_ssl_cn(cn)
76   include_recipe 'ssl_cert::server_key_pairs'
77   key_path = server_key_path(cn)
78
79   bash 'copy_ssl_server_key_for_jenkins' do
80     code <<-EOH
81       cp #{key_path} #{certs_dir}/server.key
82       chown #{jenkins_owner} #{certs_dir}/server.key
83       chmod 600 #{certs_dir}/server.key
84     EOH
85     sensitive true
86     action :run
87     not_if "cmp #{key_path} #{certs_dir}/server.key"
88     #action :nothing
89     #subscribes :run, "file[#{key_path}]"
90   end
91
92   vols.push("#{server_cert_path(cn)}:/var/lib/jenkins/server.crt:ro")
93   vols.push("#{certs_dir}/server.key:/var/lib/jenkins/server.key:ro")
94   jenkins_opts.push('--httpsCertificate=/var/lib/jenkins/server.crt')
95   jenkins_opts.push('--httpsPrivateKey=/var/lib/jenkins/server.key')
96 end
97
98 executors_conf = 'ref/init.groovy.d/executors.groovy'
99 vols.push("#{app_dir}/#{executors_conf}:/usr/share/jenkins/#{executors_conf}:ro")
100
101 unless jenkins_opts.empty?
102   if !config['services']['jenkins']['environment'].nil? \
103     && !config['services']['jenkins']['environment']['JENKINS_OPTS'].nil?
104     jenkins_opts.unshift(config['services']['jenkins']['environment']['JENKINS_OPTS'])
105   end
106   envs['JENKINS_OPTS'] = jenkins_opts.join(' ')
107 end
108
109 unless java_opts.empty?
110   if !config['services']['jenkins']['environment'].nil? \
111     && !config['services']['jenkins']['environment']['JAVA_OPTS'].nil?
112     java_opts.unshift(config['services']['jenkins']['environment']['JAVA_OPTS'])
113   end
114   envs['JAVA_OPTS'] = java_opts.join(' ')
115 end
116
117 # force_override for merging JENKINS_OPTS and JAVA_OPTS attributes' value.
118 force_override_config['services']['jenkins']['environment'] = envs unless envs.empty?
119 override_config['services']['jenkins']['volumes'] = vols unless vols.empty?
120
121 [
122   'docker-compose.yml',
123   'ref/init.groovy.d/executors.groovy',
124 ].each {|conf_file|
125   template "#{app_dir}/#{conf_file}" do
126     source  "opt/docker-compose/app/jenkins/#{conf_file}"
127     owner 'root'
128     group 'root'
129     mode '0644'
130   end
131 }
132
133 log <<-"EOM"
134 Note: You must execute the following command manually.
135   See #{doc_url}
136   * Start:
137     $ cd #{app_dir}
138     $ docker-compose up -d
139   * Stop
140     $ docker-compose down
141 EOM