-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpackage.rb
More file actions
70 lines (58 loc) · 1.89 KB
/
package.rb
File metadata and controls
70 lines (58 loc) · 1.89 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
require 'fileutils'
require 'net/http'
require 'uri'
require 'zip'
require 'digest'
require 'tmpdir'
class Package
def initialize(tag:, file:, sha:)
@tag = tag
@file = file
@sha = sha
end
def download(path)
Dir.mktmpdir { |tmpdir|
zip = fetch(tmpdir)
unzip(zip, path)
}
end
private
def fetch(directory)
response = fetch_with_redirect("https://github.com/ChaosGroup/scriptup/releases/download/#{@tag}/#{@file}")
package = "#{directory}/#{@file}"
File.open(package, 'wb') { |f| f.write(response.body) }
sha = Digest::SHA256.file(package).hexdigest
return package if sha == @sha
raise "SDK Package (#{package}) sha (#{sha}) does match expected sha: #{@sha}"
end
def fetch_with_redirect(uri_str, limit = 10)
raise 'Too many redirects' if limit.zero?
uri = URI(uri_str)
request = Net::HTTP::Get.new(uri)
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(request) }
case response
when Net::HTTPSuccess then response
when Net::HTTPRedirection then fetch_with_redirect(response['location'], limit - 1)
else
raise "Failed to fetch package: #{response.code} #{response.message}"
end
end
# rubocop:disable Metrics/MethodLength
def unzip(zip_file_path, output)
Zip::File.open(zip_file_path) do |zip_file|
zip_file.each do |entry|
destination_path = "#{output}/#{entry.name}"
if entry.ftype == :symlink
target = entry.get_input_stream.read # Symlink target is stored as file content
File.symlink(target, destination_path)
elsif entry.directory?
FileUtils.mkdir_p(destination_path)
else
FileUtils.mkdir_p(File.dirname(destination_path))
entry.extract(destination_path) { true } # Overwrite existing files
end
end
end
end
# rubocop:enable Metrics/MethodLength
end