-
-
Notifications
You must be signed in to change notification settings - Fork 605
/
provider_install.rb
147 lines (125 loc) · 4.86 KB
/
provider_install.rb
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
# Chef Provider for installing or removing Elasticsearch from package or tarball
# downloaded from elasticsearch.org and installed by package manager or ark resource
class ElasticsearchCookbook::InstallProvider < Chef::Provider::LWRPBase
include ElasticsearchCookbook::Helpers
include Chef::DSL::IncludeRecipe
provides :elasticsearch_install
def whyrun_supported?
false
end
action :install do
if new_resource.type == 'tarball'
install_tarball_wrapper_action
elsif new_resource.type == 'package'
install_package_wrapper_action
else
raise "#{install_type} is not a valid install type"
end
end
action :remove do
if new_resource.type == 'tarball'
remove_tarball_wrapper_action
elsif new_resource.type == 'package'
remove_package_wrapper_action
else
raise "#{install_type} is not a valid install type"
end
end
protected
def install_package_wrapper_action
download_url = determine_download_url(new_resource, node)
filename = download_url.split('/').last
checksum = determine_download_checksum(new_resource, node)
package_options = new_resource.package_options
unless checksum
Chef::Log.warn("No checksum was provided for #{download_url}, this may download a new package on every chef run!")
end
remote_file_r = remote_file "#{Chef::Config[:file_cache_path]}/#{filename}" do
source download_url
checksum checksum
mode 00644
action :nothing
end
remote_file_r.run_action(:create)
new_resource.updated_by_last_action(true) if remote_file_r.updated_by_last_action?
pkg_r = if node['platform_family'] == 'debian'
dpkg_package "#{Chef::Config[:file_cache_path]}/#{filename}" do
options package_options
action :nothing
end
else
package "#{Chef::Config[:file_cache_path]}/#{filename}" do
options package_options
action :nothing
end
end
pkg_r.run_action(:install)
new_resource.updated_by_last_action(true) if pkg_r.updated_by_last_action?
end
def remove_package_wrapper_action
package_url = determine_download_url(new_resource, node)
filename = package_url.split('/').last
pkg_r = if node['platform_family'] == 'debian'
dpkg_package "#{Chef::Config[:file_cache_path]}/#{filename}" do
action :nothing
end
else
package "#{Chef::Config[:file_cache_path]}/#{filename}" do
action :nothing
end
end
pkg_r.run_action(:remove)
new_resource.updated_by_last_action(true) if pkg_r.updated_by_last_action?
end
def install_tarball_wrapper_action
include_recipe 'ark'
es_user = find_es_resource(run_context, :elasticsearch_user, new_resource)
ark_r = ark 'elasticsearch' do
url determine_download_url(new_resource, node)
owner es_user.username
group es_user.groupname
version new_resource.version
has_binaries ['bin/elasticsearch', 'bin/elasticsearch-plugin']
checksum determine_download_checksum(new_resource, node)
prefix_root new_resource.dir
prefix_home new_resource.dir
not_if do
link = "#{new_resource.dir}/elasticsearch"
target = "#{new_resource.dir}/elasticsearch-#{new_resource.version}"
binary = "#{target}/bin/elasticsearch"
::File.directory?(link) && ::File.symlink?(link) && ::File.readlink(link) == target && ::File.exist?(binary)
end
action :nothing
end
ark_r.run_action(:install)
new_resource.updated_by_last_action(true) if ark_r.updated_by_last_action?
# destroy the sample config directory for tarball installs, or it will
# take precedence beyond the default stuff in /etc/elasticsearch and within
# /etc/sysconfig or /etc/default
sample_r = directory "#{new_resource.dir}/elasticsearch/config" do
action :nothing
recursive true
end
sample_r.run_action(:delete)
new_resource.updated_by_last_action(true) if sample_r.updated_by_last_action?
end
def remove_tarball_wrapper_action
# remove the symlink to this version
link_r = link "#{new_resource.dir}/elasticsearch" do
only_if do
link = "#{new_resource.dir}/elasticsearch"
target = "#{new_resource.dir}/elasticsearch-#{new_resource.version}"
::File.directory?(link) && ::File.symlink?(link) && ::File.readlink(link) == target
end
action :nothing
end
link_r.run_action(:delete)
new_resource.updated_by_last_action(true) if link_r.updated_by_last_action?
# remove the specific version
d_r = directory "#{new_resource.dir}/elasticsearch-#{new_resource.version}" do
action :nothing
end
d_r.run_action(:delete)
new_resource.updated_by_last_action(true) if d_r.updated_by_last_action?
end
end