Class: Geordi::DBCleaner
Instance Method Summary
collapse
#announce, #fail, #note, #note_cmd, #prompt, #strip_heredoc, #success, #warn
Constructor Details
#initialize(extra_flags) ⇒ DBCleaner
Returns a new instance of DBCleaner.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/geordi/db_cleaner.rb', line 9
def initialize()
puts 'Please enter your sudo password if asked, for db operations as system users'
puts "We're going to run `sudo -u postgres psql` for PostgreSQL"
puts ' and `sudo mysql` for MariaDB (which uses PAM auth)'
`sudo true`
fail 'sudo access is required for database operations as database users' if $? != 0
@derivative_dbname = /_(test\d?|development|cucumber)$/
base_directory = ENV['XDG_CONFIG_HOME']
base_directory = "#{Dir.home}" if base_directory.nil?
@whitelist_directory = File.join(base_directory, '.config', 'geordi', 'whitelists')
FileUtils.mkdir_p(@whitelist_directory) unless File.directory? @whitelist_directory
@mysql_command = decide_mysql_command(['mysql'])
@postgres_command = decide_postgres_command(['postgres'])
end
|
Instance Method Details
#clean_mysql ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/geordi/db_cleaner.rb', line 147
def clean_mysql
announce 'Checking for MySQL databases'
database_list = list_all_dbs('mysql')
deletable_dbs = confirm_deletion('mysql', database_list)
return if deletable_dbs.nil?
deletable_dbs.each do |db|
if @mysql_command.include? '-p'
puts "Please enter your MySQL/MariaDB account 'root' for: DROP DATABASE #{db}"
else
note "Dropping MySQL/MariaDB database #{db}"
end
`#{@mysql_command} -e 'DROP DATABASE \`#{db}\`;'`
end
end
|
#clean_postgres ⇒ Object
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/geordi/db_cleaner.rb', line 163
def clean_postgres
announce 'Checking for Postgres databases'
database_list = list_all_dbs('postgres')
deletable_dbs = confirm_deletion('postgres', database_list)
return if deletable_dbs.nil?
deletable_dbs.each do |db|
note "Dropping PostgreSQL database `#{db}`."
`#{@postgres_command} -c 'DROP DATABASE "#{db}";'`
end
end
|
#edit_whitelist(dbtype) ⇒ Object
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
|
# File 'lib/geordi/db_cleaner.rb', line 24
def edit_whitelist(dbtype)
whitelist = whitelist_fname(dbtype)
if File.exist? whitelist
whitelisted_dbs = Geordi::Util.stripped_lines(File.read(whitelist))\
.delete_if { |l| l.start_with? '#' }
else
whitelisted_dbs = Array.new
end
all_dbs = list_all_dbs(dbtype)
tmp = Tempfile.open("geordi_whitelist_#{dbtype}")
tmp.write <<-HEREDOC
# Put each whitelisted database on a new line.
# System databases will never be deleted.
# When you whitelist foo, foo_development and foo_test\\d? are whitelisted, too.
# This works even if foo does not exist. Also, you will only see foo in this list.
#
# Syntax: keep foo
# drop bar
HEREDOC
tmpfile_content = Array.new
all_dbs.each do |db|
next if is_whitelisted?(dbtype, db)
next if is_protected?(dbtype, db)
db.sub!(@derivative_dbname, '')
tmpfile_content.push(['drop', db])
end
whitelisted_dbs.each do |db|
tmpfile_content.push(['keep', db])
end
tmpfile_content.sort_by! { |k| k[1] }
tmpfile_content.uniq!
tmpfile_content.each do |line|
tmp.write("#{line[0]} #{line[1]}\n")
end
tmp.close
texteditor = Geordi::Util.decide_texteditor
system("#{texteditor} #{tmp.path}")
File.open(tmp.path, 'r') do |wl_edited|
whitelisted_dbs = Array.new
whitelist_storage = File.open(whitelist, 'w')
lines = Geordi::Util.stripped_lines(wl_edited.read)
lines.each do |line|
next if line.start_with?('#')
fail 'Invalid edit to whitelist file' unless line.split.length == 2
fail 'Invalid edit to whitelist file' unless %w[keep drop k d].include? line.split[0]
db_status, db_name = line.split
if db_status == 'keep'
whitelisted_dbs.push db_name
whitelist_storage.write(db_name << "\n")
end
end
whitelist_storage.close
end
end
|
#is_protected?(dbtype, database_name) ⇒ Boolean
212
213
214
215
216
217
218
|
# File 'lib/geordi/db_cleaner.rb', line 212
def is_protected?(dbtype, database_name)
protected = {
'mysql' => %w[mysql information_schema performance_schema sys],
'postgres' => ['postgres'],
}
protected[dbtype].include? database_name
end
|
#is_whitelisted?(dbtype, database_name) ⇒ Boolean
220
221
222
223
224
225
226
227
|
# File 'lib/geordi/db_cleaner.rb', line 220
def is_whitelisted?(dbtype, database_name)
if File.exist? whitelist_fname(dbtype)
whitelist_content = Geordi::Util.stripped_lines(File.open(whitelist_fname(dbtype), 'r').read)
else
whitelist_content = Array.new
end
whitelist_content.include? database_name.sub(@derivative_dbname, '')
end
|
#list_all_dbs(dbtype) ⇒ Object
128
129
130
131
132
133
134
|
# File 'lib/geordi/db_cleaner.rb', line 128
def list_all_dbs(dbtype)
if dbtype == 'postgres'
return list_all_postgres_dbs
else
return list_all_mysql_dbs
end
end
|
#list_all_mysql_dbs ⇒ Object
140
141
142
143
144
145
|
# File 'lib/geordi/db_cleaner.rb', line 140
def list_all_mysql_dbs
if @mysql_command.include? '-p'
puts "Please enter your MySQL/MariaDB account 'root' for: list all databases"
end
`#{@mysql_command} -B -N -e 'show databases'`.split
end
|
#list_all_postgres_dbs ⇒ Object
136
137
138
|
# File 'lib/geordi/db_cleaner.rb', line 136
def list_all_postgres_dbs
`#{@postgres_command} -t -A -c 'SELECT DATNAME FROM pg_database WHERE datistemplate = false'`.split
end
|
#whitelist_fname(dbtype) ⇒ Object
174
175
176
|
# File 'lib/geordi/db_cleaner.rb', line 174
def whitelist_fname(dbtype)
File.join(@whitelist_directory, dbtype) << '.txt'
end
|