forked from wanelo-chef/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathark.rb
More file actions
222 lines (207 loc) · 8.04 KB
/
Copy pathark.rb
File metadata and controls
222 lines (207 loc) · 8.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#
# Author:: Bryan W. Berry (<bryan.berry@gmail.com>)
# Cookbook Name:: java
# Provider:: ark
#
# Copyright 2011, Bryan w. Berry
#
# 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 whyrun_supported?
true
end
def parse_app_dir_name url
file_name = url.split('/')[-1]
# funky logic to parse oracle's non-standard naming convention
# for jdk1.6
if file_name =~ /^(jre|jdk).*$/
major_num = file_name.scan(/\d/)[0]
update_num = file_name.scan(/\d+/)[1]
# pad a single digit number with a zero
if update_num.length < 2
update_num = "0" + update_num
end
package_name = file_name.scan(/[a-z]+/)[0]
app_dir_name = "#{package_name}1.#{major_num}.0_#{update_num}"
else
app_dir_name = file_name.split(/(.tar.gz|.zip)/)[0]
app_dir_name = app_dir_name.split("-bin")[0]
end
[app_dir_name, file_name]
end
def oracle_downloaded?(download_path, new_resource)
if ::File.exists? download_path
require 'digest'
downloaded_sha = Digest::SHA256.file(download_path).hexdigest
downloaded_sha == new_resource.checksum
else
return false
end
end
def download_direct_from_oracle(tarball_name, new_resource)
download_path = "#{Chef::Config[:file_cache_path]}/#{tarball_name}"
jdk_id = new_resource.url.scan(/\/([6789]u[0-9][0-9]?-b[0-9][0-9])\//)[0][0]
cookie = "oraclelicensejdk-#{jdk_id}-oth-JPR=accept-securebackup-cookie;gpw_e24=http://edelivery.oracle.com"
if node['java']['oracle']['accept_oracle_download_terms']
# install the curl package
p = package "curl" do
action :nothing
end
# no converge_by block since the package provider will take care of this run_action
p.run_action(:install)
description = "download oracle tarball straight from the server"
converge_by(description) do
Chef::Log.debug "downloading oracle tarball straight from the source"
cmd = Chef::ShellOut.new(
%Q[ curl -L --cookie "#{cookie}" #{new_resource.url} -o #{download_path} ]
)
cmd.run_command
cmd.error!
end
else
Chef::Application.fatal!("You must set the attribute node['java']['oracle']['accept_oracle_download_terms'] to true if you want to download directly from the oracle site!")
end
end
action :install do
app_dir_name, tarball_name = parse_app_dir_name(new_resource.url)
app_root = new_resource.app_home.split('/')[0..-2].join('/')
app_dir = app_root + '/' + app_dir_name
unless new_resource.default
Chef::Log.debug("processing alternate jdk")
app_dir = app_dir + "_alt"
app_home = app_dir
else
app_home = new_resource.app_home
end
unless ::File.exists?(app_dir)
Chef::Log.info "Adding #{new_resource.name} to #{app_dir}"
require 'fileutils'
unless ::File.exists?(app_root)
description = "create dir #{app_root} and change owner to #{new_resource.owner}"
converge_by(description) do
FileUtils.mkdir app_root, :mode => new_resource.app_home_mode
FileUtils.chown new_resource.owner, new_resource.owner, app_root
end
end
if new_resource.url =~ /^http:\/\/download.oracle.com.*$/
download_path = "#{Chef::Config[:file_cache_path]}/#{tarball_name}"
if oracle_downloaded?(download_path, new_resource)
Chef::Log.debug("oracle tarball already downloaded, not downloading again")
else
download_direct_from_oracle tarball_name, new_resource
end
else
Chef::Log.debug("downloading tarball from an unofficial repository")
r = remote_file "#{Chef::Config[:file_cache_path]}/#{tarball_name}" do
source new_resource.url
checksum new_resource.checksum
mode 0755
action :nothing
end
#no converge by on run_action remote_file takes care of it.
r.run_action(:create_if_missing)
end
require 'tmpdir'
description = "create tmpdir, extract compressed data into tmpdir,
move extracted data to #{app_dir} and delete tmpdir"
converge_by(description) do
tmpdir = Dir.mktmpdir
case tarball_name
when /^.*\.bin/
cmd = Chef::ShellOut.new(
%Q[ cd "#{tmpdir}";
cp "#{Chef::Config[:file_cache_path]}/#{tarball_name}" . ;
bash ./#{tarball_name} -noregister
] ).run_command
unless cmd.exitstatus == 0
Chef::Application.fatal!("Failed to extract file #{tarball_name}!")
end
when /^.*\.zip/
cmd = Chef::ShellOut.new(
%Q[ unzip "#{Chef::Config[:file_cache_path]}/#{tarball_name}" -d "#{tmpdir}" ]
).run_command
unless cmd.exitstatus == 0
Chef::Application.fatal!("Failed to extract file #{tarball_name}!")
end
when /^.*\.tar.gz/
cmd = Chef::ShellOut.new(
%Q[ tar xvzf "#{Chef::Config[:file_cache_path]}/#{tarball_name}" -C "#{tmpdir}" ]
).run_command
unless cmd.exitstatus == 0
Chef::Application.fatal!("Failed to extract file #{tarball_name}!")
end
end
cmd = Chef::ShellOut.new(
%Q[ mv "#{tmpdir}/#{app_dir_name}" "#{app_dir}" ]
).run_command
unless cmd.exitstatus == 0
Chef::Application.fatal!(%Q[ Command \' mv "#{tmpdir}/#{app_dir_name}" "#{app_dir}" \' failed ])
end
FileUtils.rm_r tmpdir
end
end
#update-alternatives
if new_resource.default
Chef::Log.debug "app_home is #{app_home} and app_dir is #{app_dir}"
current_link = ::File.symlink?(app_home) ? ::File.readlink(app_home) : nil
if current_link != app_dir
description = "symlink #{app_dir} to #{app_home}"
converge_by(description) do
Chef::Log.debug "symlinking #{app_dir} to #{app_home}"
FileUtils.rm_f app_home
FileUtils.ln_sf app_dir, app_home
end
end
if new_resource.bin_cmds
new_resource.bin_cmds.each do |cmd|
if ::File.exists? "/usr/bin/#{cmd}"
current_bin_link = ::File.readlink("/usr/bin/#{cmd}")
else
current_bin_link = false
end
should_be_link = "#{app_home}/bin/#{cmd}"
if current_bin_link != should_be_link
converge_by("update-alternatives") do
cmd = Chef::ShellOut.new(
%Q[ update-alternatives --install /usr/bin/#{cmd} #{cmd} #{app_home}/bin/#{cmd} 1;
update-alternatives --set #{cmd} #{app_home}/bin/#{cmd} ]
).run_command
unless cmd.exitstatus == 0
Chef::Application.fatal!(%Q[ update alternatives failed ])
end
end
end
end
end
end
end
action :remove do
app_dir_name, tarball_name = parse_app_dir_name(new_resource.url)
app_root = new_resource.app_home.split('/')[0..-2].join('/')
app_dir = app_root + '/' + app_dir_name
if ::File.exists?(app_dir)
new_resource.bin_cmds.each do |cmd|
cmd = execute "update_alternatives" do
command "update-alternatives --remove #{cmd} #{app_dir} "
returns [0,2]
action :nothing
end
# the execute resource will take care of of the run_action(:run)
cmd.run_action(:run)
end
description = "remove #{new_resource.name} at #{app_dir}"
converge_by(description) do
Chef::Log.info "Removing #{new_resource.name} at #{app_dir}"
FileUtils.rm_rf app_dir
end
end
end