-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_capitains_repos.rb.erb
80 lines (75 loc) · 2.89 KB
/
update_capitains_repos.rb.erb
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
#!/usr/bin/ruby
require "net/http"
require "uri"
require "json"
require "openssl"
# vim: set ft=ruby :
@force = (ARGV[0] == "--force")
@disable = (ARGV[0] == "--disable")
REPOS = <%= scope.lookupvar('capitains::repos') %>
APP_ROOT = "<%= scope.lookupvar('capitains::app_root') %>"
DATA_ROOT = "<%= scope.lookupvar('capitains::data_root') %>"
WORKDIR = "<%= scope.lookupvar('capitains::workdir') %>"
VENVDIR = "<%= scope.lookupvar('capitains::venvdir') %>"
CI_URL = "<%= scope.lookupvar('capitains::ci_url') %>"
PYTHON = "#{VENVDIR}/bin/python3"
DEBUG = ENV['DEBUG'] || false
RELEASE_FILE = "#{APP_ROOT}/versions.py"
begin
current_releases = JSON.parse(File.read(RELEASE_FILE))
rescue
current_releases = {}
end
def debug_print(message)
if DEBUG
puts message
end
end
def shell(cmd)
debug_print "\nCommand: #{cmd}\n"
raw_output = `#{cmd}`
exitstatus = $?.exitstatus
debug_print "Output: #{raw_output}"
debug_print "Exit status: #{exitstatus}"
return raw_output
end
new_data = false
unless (@disable)
debug_print(current_releases.to_s)
REPOS.each do |r|
url = URI.parse("#{CI_URL}/#{r['cibase']}/releases")
debug_print("Get #{url.to_s}")
http = Net::HTTP::new(url.host,url.port)
http.use_ssl = true
response = http.send_request('GET',url.request_uri)
if (response.code == '200')
json = JSON.parse(response.body)
new_release = json[0]['tag_name']
if (! current_releases[r['cibase']] || current_releases[r['cibase']] != new_release || @force)
debug_print("Updating #{r['cibase']} to #{new_release}")
if json[0]['assets'].size > 0
archive = json[0]['assets'][0]['browser_download_url']
shell("rm -fr #{WORKDIR}/#{r['name']} && mkdir #{WORKDIR}/#{r['name']} && curl -L -o #{WORKDIR}/#{r['name']}/release.tar.gz #{archive} && cd #{WORKDIR}/#{r['name']} && tar -xzf release.tar.gz")
shell("rsync -Pav --delete #{WORKDIR}/#{r['name']}/ #{DATA_ROOT}/#{r['name']}/")
else
archive = json[0]['zipball_url']
shell("rm -fr #{WORKDIR}/#{r['name']} && mkdir #{WORKDIR}/#{r['name']} && curl -L -o #{WORKDIR}/#{r['name']}/release.zip #{archive} && cd #{WORKDIR}/#{r['name']} && unzip release.zip")
zip_base_dir = r['cibase'].sub(/\//, '-')
shell("rsync -Pav --delete #{WORKDIR}/#{r['name']}/#{zip_base_dir}*/ #{DATA_ROOT}/#{r['name']}/")
end
current_releases[r['cibase']] = new_release
new_data = true
else
debug_print("#{r['cibase']} has no new release")
end
else
debug_print("Error retrieving release info from Github at #{url.to_s}: #{response.code} #{response.message}")
end
end
end
if new_data or @force
File.write(RELEASE_FILE,JSON.pretty_generate(current_releases))
shell("#{PYTHON} #{APP_ROOT}/manager.py nautilus flush")
shell("#{PYTHON} #{APP_ROOT}/manager.py nautilus preprocess")
shell("/usr/sbin/service gunicorn restart")
end