#
# Cookbook Name:: jenkins-grid
# Recipe:: docker-compose
#
# Copyright 2016, whitestar
#
# 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.
#

doc_url = 'https://hub.docker.com/_/jenkins/'

include_recipe 'docker-grid::compose'

java_opts = []
jenkins_opts = []
volumes = []

config = node['jenkins-grid']['docker-compose']['config']
override_config = node.override['jenkins-grid']['docker-compose']['config']
force_override_config = node.force_override['jenkins-grid']['docker-compose']['config']
app_dir = node['jenkins-grid']['docker-compose']['app_dir']
certs_dir = "#{app_dir}/certs"
groovy_dir = "#{app_dir}/ref/init.groovy.d"
#etc_dir = "#{app_dir}/etc"

[
  app_dir,
  certs_dir,
  groovy_dir,
  #etc_dir,
].each {|dir|
  resources(directory: dir) rescue directory dir do
    owner 'root'
    group 'root'
    mode '0755'
    recursive true
  end
}

jenkins_owner = node['jenkins-grid']['docker-compose']['jenkins_home']['owner']
jenkins_home_path = node['jenkins-grid']['docker-compose']['jenkins_home']['path']
unless jenkins_home_path.nil?
  directory jenkins_home_path do
    owner jenkins_owner
    group 'root'
    mode '0755'
    recursive true
  end
  volumes.push("#{jenkins_home_path}:/var/jenkins_home")

  template "#{jenkins_home_path}/log.properties" do
    source 'var/lib/jenkins_home/log.properties'
    owner 'root'
    group 'root'
    mode '0644'
  end
  java_opts.push('-Djava.util.logging.config.file=/var/jenkins_home/log.properties')
end

if node['jenkins-grid']['with_ssl_cert_cookbook']
  ::Chef::Recipe.send(:include, SSLCert::Helper)
  cn = node['jenkins-grid']['ssl_cert']['common_name']
  key_path = server_key_path(cn)

  bash 'copy_ssl_server_key' do
    code <<-EOH
      cp #{key_path} #{certs_dir}/server.key
      chown #{jenkins_owner} #{certs_dir}/server.key
      chmod 600 #{certs_dir}/server.key
    EOH
    sensitive true
    action :nothing
    subscribes :run, "file[#{key_path}]"
  end

  volumes.push("#{server_cert_path(cn)}:/var/lib/jenkins/server.crt:ro")
  volumes.push("#{certs_dir}/server.key:/var/lib/jenkins/server.key:ro")
  jenkins_opts.push('--httpsCertificate=/var/lib/jenkins/server.crt')
  jenkins_opts.push('--httpsPrivateKey=/var/lib/jenkins/server.key')
end

executors_conf = 'ref/init.groovy.d/executors.groovy'
volumes.push("#{app_dir}/#{executors_conf}:/usr/share/jenkins/#{executors_conf}:ro")

unless jenkins_opts.empty?
  jenkins_opts_str = config['services']['jenkins']['environment']['JENKINS_OPTS']
  jenkins_opts_str = jenkins_opts_str.nil? ? jenkins_opts.join(' ') : "#{jenkins_opts_str} #{jenkins_opts.join(' ')}"
  # force_override for merging JENKINS_OPTS attribute value.
  force_override_config['services']['jenkins']['environment']['JENKINS_OPTS'] = jenkins_opts_str
end

unless java_opts.empty?
  java_opts_str = config['services']['jenkins']['environment']['JAVA_OPTS']
  java_opts_str = java_opts_str.nil? ? java_opts.join(' ') : "#{java_opts_str} #{java_opts.join(' ')}"
  # force_override for merging JAVA_OPTS attribute value.
  force_override_config['services']['jenkins']['environment']['JAVA_OPTS'] = java_opts_str
end

unless volumes.empty?
  override_config['services']['jenkins']['volumes'] = volumes
end

[
  'docker-compose.yml',
  'ref/init.groovy.d/executors.groovy',
].each {|conf_file|
  template "#{app_dir}/#{conf_file}" do
    source  "opt/docker-compose/app/jenkins/#{conf_file}"
    owner 'root'
    group 'root'
    mode '0644'
  end
}

log <<-"EOM"
Note: You must execute the following command manually.
  See #{doc_url}
  - Start:
    $ cd #{app_dir}
    $ docker-compose up -d
  - Stop
    $ docker-compose down
EOM
