OSDN Git Service

improve the backup and restore scripts.
[metasearch/grid-chef-repo.git] / cookbooks / docker-grid / recipes / compose.rb
1 #
2 # Cookbook Name:: docker-grid
3 # Recipe:: compose
4 #
5 # Copyright 2016-2018, 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 # See: https://docs.docker.com/compose/install/
21
22 install_flavor = node['docker-grid']['compose']['install_flavor']
23 auto_upgrade = node['docker-grid']['compose']['auto_upgrade']
24
25 if node['docker-grid']['compose']['skip_setup']
26   log 'Skip the Docker Compose setup.'
27   return
28 end
29
30 include_recipe 'docker-grid::engine'
31
32 home_dir = node['docker-grid']['compose']['home_dir']
33 app_dir = node['docker-grid']['compose']['app_dir']
34 [
35   home_dir,
36   app_dir,
37 ].each {|dir|
38   resources(directory: dir) rescue directory dir do
39     owner 'root'
40     group 'root'
41     mode '0755'
42     recursive true
43   end
44 }
45
46 docker_compose_path = '/usr/local/bin/docker-compose'
47 docker_compose_link = '/usr/sbin/docker-compose'
48
49 # clean up installed docker-compose(s)
50 file docker_compose_path do
51   action :delete
52   not_if { install_flavor == 'dockerproject' || install_flavor == 'pypi' }
53 end
54
55 link "delete #{docker_compose_link}" do
56   target_file docker_compose_link
57   action :delete
58   not_if { install_flavor == 'dockerproject' }
59 end
60
61 package 'delete docker-compose' do
62   package_name 'docker-compose'
63   action :remove
64   not_if { install_flavor == 'os-repository' }
65 end
66
67 execute 'pip uninstall docker-compose' do
68   user 'root'
69   environment 'HOME' => '/root'
70   command 'pip uninstall docker-compose'
71   action :run
72   only_if 'pip list | grep docker-compose'
73   not_if { install_flavor == 'pypi' }
74 end
75
76 case install_flavor
77 when 'dockerproject'
78   [
79     'curl',
80   ].each {|pkg|
81     resources(package: pkg) rescue package pkg do
82       action :install
83     end
84   }
85
86   release_url = node['docker-grid']['compose']['release_url']
87   expected_ver = release_url.match(/(\d+\.\d+.\d+)/)
88   expected_ver = expected_ver[1] unless expected_ver.nil?
89   # latest version
90   if expected_ver.nil?
91     pkgs = ['jq']
92     pkgs.unshift('epel-release') if node['platform_family'] == 'rhel'
93     pkgs.each {|pkg|
94       resources(package: pkg) rescue package pkg do
95         action :install
96       end.run_action(:install)
97     }
98
99     result = shell_out!('curl -s https://api.github.com/repos/docker/compose/releases/latest | jq -r .tag_name')
100     expected_ver = result.stdout.chomp if result.exitstatus.zero? && !result.stdout.chomp.empty?
101     release_url = "https://github.com/docker/compose/releases/download/#{expected_ver}/docker-compose-#{node['kernel']['name']}-#{node['kernel']['machine']}"
102   end
103
104   execute 'install_docker_compose' do
105     user 'root'
106     command "curl -L \"#{release_url}\" -o #{docker_compose_path} && chmod +x #{docker_compose_path}"
107     action :run
108     not_if "#{docker_compose_path} -v | grep #{expected_ver},"
109     not_if { ::File.exist?(docker_compose_path) } unless auto_upgrade
110   end
111
112   # for sudo secure_path on RHEL family OS
113   link docker_compose_link do
114     to docker_compose_path
115     only_if { node['platform_family'] == 'rhel' }
116   end
117 when 'os-repository'
118   act = auto_upgrade ? :upgrade : :install
119   package 'docker-compose' do
120     action act
121   end
122 when 'pypi'
123   expected_ver = node['docker-grid']['compose']['version']
124   expected_ver = '' if expected_ver.nil?
125   pin_ver = expected_ver.empty? ? '' : "==#{expected_ver}"
126
127   include_recipe 'platform_utils::pip'
128
129   # Note: installed to /usr/local/bin/docker-compose
130   execute 'pip_install_docker_compose' do
131     user 'root'
132     environment 'HOME' => '/root'  # = sudo -H
133     command "pip install docker-compose#{pin_ver}"
134     action :run
135     not_if "pip list | grep docker-compose | grep '(#{expected_ver})'" unless expected_ver.empty?
136     not_if 'pip list | grep docker-compose' unless auto_upgrade
137   end
138 end