]> git.openstreetmap.org Git - chef.git/blob - cookbooks/awscli/recipes/default.rb
nominatim: install secondary importance file
[chef.git] / cookbooks / awscli / recipes / default.rb
1 #
2 # Cookbook:: awscli
3 # Recipe:: default
4 #
5 # Copyright:: 2023, OpenStreetMap Foundation
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 #     https://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 cache_dir = Chef::Config[:file_cache_path]
21
22 # Determine architecture of system for the AWS CLI download
23 awscli_arch = if arm?
24                 "aarch64"
25               else
26                 "x86_64"
27               end
28
29 awscli_version_suffix = if node[:awscli][:version] == "latest"
30                           "" # latest version does not have a suffix
31                         else
32                           "-#{node[:awscli][:version]}"
33                         end
34
35 awscli_zip = "awscliv2#{awscli_version_suffix}.zip"
36
37 # Ensure unpack directory is removed
38 directory "#{cache_dir}/awscli" do
39   action :delete
40   recursive true
41 end
42
43 # Remove any existing AWS CLI zip files, unless it's the one we're downloading
44 Dir.glob("#{cache_dir}/awscliv2*.zip").each do |zip|
45   next if zip == "#{cache_dir}/#{awscli_zip}"
46
47   file zip do
48     action :delete
49     backup false
50   end
51 end
52
53 # Download the AWS CLI zip file
54 remote_file "#{cache_dir}/#{awscli_zip}" do
55   source "https://awscli.amazonaws.com/awscli-exe-linux-#{awscli_arch}#{awscli_version_suffix}.zip"
56   owner "root"
57   group "root"
58   mode "644"
59   backup false
60   ignore_failure true
61 end
62
63 # Extract the AWS CLI zip file
64 archive_file "#{cache_dir}/#{awscli_zip}" do
65   path "#{cache_dir}/#{awscli_zip}"
66   destination "#{cache_dir}/awscli"
67   strip_components 1
68   action :nothing
69   subscribes :extract, "remote_file[#{cache_dir}/#{awscli_zip}]", :immediately
70 end
71
72 # Find the version of the AWS CLI we just downloaded
73 # Example version string: aws-cli/2.12.6 Python/3.11.4 Linux/5.15.49-linuxkit-pr exe/aarch64.ubuntu.22 prompt/off
74 # Install CLI to path: /opt/awscli/v2/current/bin/aws
75 ruby_block "install-awscli" do
76   block do
77     require "fileutils"
78     awscli_version_string = shell_out("#{cache_dir}/awscli/dist/aws", "--version")
79     awscli_version = awscli_version_string.stdout.split(" ").first.split("/").last
80     FileUtils.mkdir_p("/opt/awscli/v2/#{awscli_version}/bin/", :mode => 0755)
81     FileUtils.mv("#{cache_dir}/awscli/dist", "/opt/awscli/v2/#{awscli_version}/dist", :force => true)
82     FileUtils.ln_sf("/opt/awscli/v2/#{awscli_version}/dist/aws", "/opt/awscli/v2/#{awscli_version}/bin/aws")
83     FileUtils.ln_sf("/opt/awscli/v2/#{awscli_version}/dist/aws_completer", "/opt/awscli/v2/#{awscli_version}/bin/aws_completer")
84     FileUtils.rm("/opt/awscli/v2/current") if File.exist?("/opt/awscli/v2/current")
85     FileUtils.ln_sf("/opt/awscli/v2/#{awscli_version}", "/opt/awscli/v2/current")
86   end
87   action :nothing
88   subscribes :run, "archive_file[#{cache_dir}/#{awscli_zip}]", :immediately
89 end