This repository has been archived by the owner on Aug 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 516
/
oo-admin-ctl-app
executable file
·278 lines (253 loc) · 8.25 KB
/
oo-admin-ctl-app
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env oo-ruby
require 'rubygems'
require 'getoptlong'
CTL_APP_COMMANDS = "(start|stop|force-stop|restart|status|destroy|force-destroy|remove-gear|remove-cartridge|run-connection-hooks|set-multiplier|update-cluster|add-alias|remove-alias)"
def usage
puts <<USAGE
== Synopsis
oo-admin-ctl-app: Control user applications
== Usage
oo-admin-ctl-app OPTIONS
Options:
-l|--login <login_name>
Login with OpenShift access (required)
-a|--app <application>
Application name (alphanumeric) (required)
-n|--domain <domain>
Domain name
-c|--command <command>
#{CTL_APP_COMMANDS} (required)
-b|--bypass
Ignore warnings
--gear_uuid
Gear uuid to operate on
--cartridge
Cartridge to operate on
--alias
Alias to operate on
--multiplier
Multiplier factor to be set for the cartridge
-h|--help
Show Usage info
USAGE
exit 255
end
opts = GetoptLong.new(
["--login", "-l", GetoptLong::REQUIRED_ARGUMENT],
["--app", "-a", GetoptLong::REQUIRED_ARGUMENT],
["--domain", "-n", GetoptLong::REQUIRED_ARGUMENT],
["--command", "-c", GetoptLong::REQUIRED_ARGUMENT],
["--gear_uuid", GetoptLong::REQUIRED_ARGUMENT],
["--cartridge", GetoptLong::REQUIRED_ARGUMENT],
["--alias", GetoptLong::REQUIRED_ARGUMENT],
["--multiplier", GetoptLong::REQUIRED_ARGUMENT],
["--bypass", "-b", GetoptLong::NO_ARGUMENT],
["--help", "-h", GetoptLong::NO_ARGUMENT]
)
args = {}
begin
opts.each{ |k,v| args[k]=v }
rescue GetoptLong::Error => e
usage
end
login = args["--login"]
app_name = args["--app"]
domain = args["--domain"]
command = args['--command']
bypass = args['--bypass']
gear_uuid = args['--gear_uuid']
cartridge = args['--cartridge']
# dup is required here since the alias valiadation in add_alias calls a downcase!
# the string here seems to be frozen and cannot be modified, hence a dup is passed
server_alias = args['--alias'].dup if args['--alias']
multiplier = args['--multiplier']
if login.nil? or app_name.nil? or command.nil? or args["--help"]
usage
end
require "#{ENV['OPENSHIFT_BROKER_DIR'] || '/var/www/openshift/broker'}/config/environment"
include AdminHelper
# Disable analytics for admin scripts
Rails.configuration.analytics[:enabled] = false
user = nil
login = CloudUser.normalize_login(login)
begin
user = CloudUser.with(consistency: :eventual).find_by(login: login)
rescue Mongoid::Errors::DocumentNotFound
end
unless user
puts "User #{login} not found."
exit 1
end
if domain
app = Application.find_by(:name => app_name, :domain_namespace => domain, :owner_id => user._id)
else
app = Application.find_by_user(user, app_name)
end
unless app
puts "Application #{app_name} for user #{login} not found."
exit 1
end
def check_user_response
begin
agree = gets.chomp
unless agree.to_s.strip =~ /^(true|t|yes|y|1)$/i
puts "\n"
exit 217
end
rescue Interrupt
puts "\n"
exit 217
end
end
reply = ResultIO.new
begin
case command
when "start"
reply.append app.start
when "stop"
reply.append app.stop
when "force-stop"
reply.append app.stop(nil, true)
when "restart"
reply.append app.restart
when "status"
puts "Application UUID is #{app._id}"
app.gears.each do |gear|
puts "GEAR UUID is #{gear.uuid}"
end
app.cartridges.each do |cart|
reply.append app.status(cart.name)
end
when "force-destroy","destroy"
unless bypass
puts <<-WARNING
!!!! WARNING !!!! WARNING !!!! WARNING !!!!
You are about to delete the #{app_name} application.
This is NOT reversible, all remote data for this application will be removed.
WARNING
print "Do you want to delete this application (y/n): "
check_user_response
end
if command=="destroy"
app.destroy_app
elsif command=="force-destroy"
puts "Force deleting application #{app.name}..."
premium_carts = get_premium_carts
gear_count = 0
Lock.run_in_app_lock(app) do
# delete all gears, then delete app
app.group_instances.each do |gi|
gi.gears.each do |g|
gear_count += 1
g.deregister_dns rescue nil
g.destroy_gear rescue nil
begin
UsageRecord.track_usage(user._id, app.name, g._id, UsageRecord::EVENTS[:end], UsageRecord::USAGE_TYPES[:gear_usage], gi.gear_size)
addtl_fs_gb = gi.addtl_fs_gb
UsageRecord.track_usage(user._id, app.name, g._id, UsageRecord::EVENTS[:end], UsageRecord::USAGE_TYPES[:addtl_fs_gb], nil, addtl_fs_gb) if addtl_fs_gb > 0
component_instances = gi.component_instances + g.sparse_carts.map {|s| gi.application.component_instances.find_by(_id: s) }
carts = component_instances.map { |ci| ci.cartridge_name }.uniq
carts.each do |cart|
UsageRecord.track_usage(user._id, app.name, g._id, UsageRecord::EVENTS[:end], UsageRecord::USAGE_TYPES[:premium_cart], nil, nil, cart) if premium_carts.include?(cart)
end
rescue Exception => e
puts e.message
puts e.backtrace.inspect
end
end if gi.gears.present?
end if app.group_instances.present?
owner = app.domain.owner
Lock.run_in_app_user_lock(owner, app) do
owner.consumed_gears -= gear_count
owner.save!
end
app.delete
end
end
reply.resultIO << "Successfully deleted application: #{app.name}" if reply.resultIO.string.empty?
Rails.logger.info "Successfully deleted application: #{app.name} with id: #{app._id} on behalf of user: #{login}"
when "remove-cartridge"
unless cartridge
puts "Cartridge is required to remove-dependency"
exit 1
end
app.remove_cartridges([cartridge])
when "remove-gear"
unless gear_uuid
puts "Gear uuid is required to remove-gear"
exit 1
end
gear_id = nil
app.gears.select {|g| (g._id.to_s == gear_uuid) or (g.uuid == gear_uuid)}.each do |g|
if g.sparse_carts and (g.sparse_carts.length > 0)
puts "Gear #{gear_uuid} hosts sparse components within its group instance. You cannot remove it."
puts "You can either remove the cartridge or delete the application."
exit 1
else
cis = g.component_instances
cis.each do |ci|
if ci.gears.length == 1
puts "Gear #{gear_uuid} cannot be removed. You need to remove cartridge #{ci.cartridge_name}."
exit 1
end
end
gear_id = g._id.to_s
break
end
end
unless gear_id
puts "Gear #{gear_uuid} not found in #{app.name}"
exit 1
end
app.remove_gear(gear_id)
when "run-connection-hooks"
app.run_connection_hooks
when "set-multiplier"
unless cartridge
puts "Cartridge is required to remove-dependency"
exit 1
end
unless multiplier
puts "Multiplier factor is a required argument"
exit 1
end
begin
ci = app.component_instances.find_by(cartridge_name: cartridge)
rescue Exception => e
puts "Cartridge #{cartridge} not found in the application #{app.name}"
exit 1
end
unless ci.is_sparse?
puts "Cannot set multiplier factor. Cartridge #{cartridge} needs to be a sparse cart."
exit 1
end
app.update_component_limits(ci, nil, nil, nil, multiplier)
when "update-cluster"
app.update_cluster
when "remove-alias"
unless server_alias
puts "Alias is required. Use --alias to pass an alias."
exit 1
end
begin
app.remove_alias(server_alias)
rescue Mongoid::Errors::DocumentNotFound => e
raise OpenShift::UserException.new("Alias #{server_alias} not found")
end
when "add-alias"
unless server_alias
puts "Alias is required. Use --alias to pass an alias."
exit 1
end
app.add_alias(server_alias)
else
puts "Command must be one of: #{CTL_APP_COMMANDS}"
usage
end
rescue OpenShift::UserException => e
puts e.message
exit 1
end
puts "DEBUG OUTPUT:\n#{reply.debugIO.string}\n" unless reply.debugIO.string.empty?
puts "ERROR OUTPUT:\n#{reply.errorIO.string}\n" unless reply.errorIO.string.empty?
puts reply.resultIO.string.empty? ? "Success" : reply.resultIO.string