forked from redhat-consulting/ose-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oo-purge-apps
executable file
·145 lines (122 loc) · 4.25 KB
/
oo-purge-apps
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
#!/usr/bin/env oo-ruby
# Copyright 2014 Red Hat Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'getoptlong'
$:.unshift('/usr/local/lib')
opts = GetoptLong.new(
["--doit", GetoptLong::NO_ARGUMENT],
["--quiet", "-q", GetoptLong::NO_ARGUMENT],
["--warn", "-w", GetoptLong::OPTIONAL_ARGUMENT],
["--stop", "-s", GetoptLong::OPTIONAL_ARGUMENT],
["--destroy", "-r", GetoptLong::OPTIONAL_ARGUMENT],
["--user", "-u", GetoptLong::OPTIONAL_ARGUMENT],
)
def usage
print "Usage: oo-purge-apps [--warn days] [--stop days] [--destroy days] [--user username] [--doit]\n"
exit 1
end
args = {}
begin
opts.each{ |k,v| args[k]=v }
rescue GetoptLong::Error => e
usage
end
target_user = args["--user"]
destroy_secs = 0
stop_secs = 0
warn_secs = 0
if args["--destroy"]
destroy_str = args["--destroy"]
destroy_str = "-#{destroy_str} days" if /^\d+$/ =~ destroy_str
destroy_secs = `date -d'#{destroy_str}' '+%s'`.to_i
end
if args["--stop"]
stop_str = args["--stop"]
stop_str = "-#{stop_str} days" if /^\d+$/ =~ stop_str
stop_secs = `date -d'#{stop_str}' '+%s'`.to_i
end
if args["--warn"]
warn_str = args["--warn"]
warn_str = "-#{warn_str} days" if /^\d+$/ =~ warn_str
warn_secs = `date -d'#{warn_str}' '+%s'`.to_i
end
usage unless ( destroy_secs > 0 || warn_secs > 0 || stop_secs > 0 )
#print "#{warn_str} #{warn_secs} #{stop_str} #{stop_secs} #{destroy_str} #{destroy_secs}\n"
unless args["--quiet"]
print "\n!!!! Destroy apps created before #{Time.at(destroy_secs)} !!!!!\n\n" if destroy_secs > 0
print " Stop apps created before #{Time.at(stop_secs)}\n" if stop_secs > 0
print " Warn apps created before #{Time.at(warn_secs)}\n" if warn_secs > 0
print "\n"
if args["--doit"]
print "!!!! This is not a test - Ctrl-C will stop process !!!!!\n\n"
sleep(10) if ( destroy_secs > 0 || stop_secs > 0 )
end
end
require "#{ENV['OPENSHIFT_BROKER_DIR'] || '/var/www/openshift/broker'}/config/environment"
include AdminHelper
# Disable analytics for admin scripts
Rails.configuration.analytics[:enabled] = false
destroy_cnt = 0
stop_cnt = 0
warn_cnt = 0
total_cnt = 0
begin
CloudUser.all.sort{|a,b| a['login']<=>b['login']}.each do |user|
# This is a sloppy way to filter a single user but its only intended for testing
if target_user
next unless target_user == user.login
end
Application.where(owner_id: user._id).sort{|a,b| a['name']<=>b['name']}.each do |app|
total_cnt += 1
secs = app.created_at.to_i
days = (Time.zone.now - app.created_at).to_i/(24*3600)
#if app.gears.length == 0
# print "#{user.login} #{app.domain_namespace} #{app.canonical_name} #{app._id} created #{days} days ago - no gears\n"
# next
# end
action = nil
if secs <= destroy_secs
action = "destroy"
elsif secs <= stop_secs
action = "stop"
elsif secs <= warn_secs
action = "warn"
end
print "#{user.login} #{app.canonical_name}-#{app.domain_namespace} #{app._id} #{app.gears.length} gears created #{days} days ago action: #{action}\n" if action
if action && args["--doit"]
begin
case action
when "stop"
app.stop
stop_cnt += 1
when "destroy"
app.destroy_app
destroy_cnt += 1
end
rescue OpenShift::UserException => e
puts e.message
end
end
end
#print "#{user.login} no apps\n" if total_cnt == 0
end
end
unless args["--quiet"]
if total_cnt > 0
print "\n"
print "#{total_cnt} total applications\n"
print "#{stop_cnt} application(s) stopped\n" if stop_cnt > 0
print "#{destroy_cnt} application(s) destroyed\n" if destroy_cnt > 0
end
end