Class: K8sflow::Pg::PgBase
- Inherits:
-
Clitopic::Command::Base
- Object
- Clitopic::Command::Base
- K8sflow::Pg::PgBase
show all
- Extended by:
- Clitopic::Helpers
- Defined in:
- lib/k8sflow/command/pg/pg_base.rb
Class Method Summary
collapse
Class Method Details
.database(db = nil) ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/k8sflow/command/pg/pg_base.rb', line 108
def database(db=nil)
db = options[:database] if db.nil?
uri = URI.parse(db)
if uri.scheme == "postgres"
parse_pg_uri(db)
elsif databases[db]
return databases[db]
else
raise Clitopic::Commands::CommandFailed.new ("No database #{db}")
end
end
|
.databases ⇒ Object
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/k8sflow/command/pg/pg_base.rb', line 93
def databases
if @dbs.nil?
@dbs = {}
hash_opt(options[:databases]).each do |k,v|
h = parse_pg_uri(v)
@dbs[k] = h
end
end
return @dbs
end
|
.exec_sql(sql) ⇒ Object
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/k8sflow/command/pg/pg_base.rb', line 9
def exec_sql(sql)
begin
ENV["PGPASSWORD"] = database[:password]
ENV["PGSSLMODE"] = (database[:host] == 'localhost' ? 'prefer' : 'require' )
user_part = database[:user] ? "-U #{database[:user]}" : ""
output = `#{psql_cmd} -c "#{sql}" #{user_part} -h #{database[:host]} -p #{database[:port] || 5432} #{database[:database]}`
if (! $?.success?) || output.nil? || output.empty?
raise "psql failed. exit status #{$?.to_i}, output: #{output.inspect}"
end
output
rescue Errno::ENOENT
Clitopic::Helpers.output_with_bang "The local psql command could not be located"
abort
end
end
|
.hash_opt(opt) ⇒ Object
38
39
40
41
42
43
44
45
46
|
# File 'lib/k8sflow/command/pg/pg_base.rb', line 38
def hash_opt(opt)
return {} if opt.nil?
if opt.is_a?(Hash)
hash = opt
else
hash = kv_parse(options[opt])
end
return hash
end
|
.kv_parse(list) ⇒ Object
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/k8sflow/command/pg/pg_base.rb', line 26
def kv_parse(list)
vars = {}
list.each do |e|
sp = e.split("=")
key = sp[0..-2].join("=")
value = sp[-1]
vars[key] = value
end
return vars
end
|
.nine_two? ⇒ Boolean
59
60
61
62
|
# File 'lib/k8sflow/command/pg/pg_base.rb', line 59
def nine_two?
return @nine_two if defined? @nine_two
@nine_two = version.to_f >= 9.2
end
|
.parse_pg_uri(uri) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/k8sflow/command/pg/pg_base.rb', line 80
def parse_pg_uri(uri)
p = URI.parse(uri)
h = {
database: p.path[1..-1],
user: p.user,
password: p.password,
host: p.hostname || "localhost",
uri: uri,
port: p.port || 5432
}
return h
end
|
.pid_column ⇒ Object
64
65
66
67
68
69
70
|
# File 'lib/k8sflow/command/pg/pg_base.rb', line 64
def pid_column
if nine_two?
'pid'
else
'procpid'
end
end
|
.psql_cmd ⇒ Object
48
49
50
|
# File 'lib/k8sflow/command/pg/pg_base.rb', line 48
def psql_cmd
'psql'
end
|
.query_column ⇒ Object
72
73
74
75
76
77
78
|
# File 'lib/k8sflow/command/pg/pg_base.rb', line 72
def query_column
if nine_two?
'query'
else
'current_query'
end
end
|
.ssl? ⇒ Boolean
104
105
106
|
# File 'lib/k8sflow/command/pg/pg_base.rb', line 104
def ssl?
options[:ssl] ? "require" : "prefer"
end
|
.version ⇒ Object
52
53
54
55
56
57
|
# File 'lib/k8sflow/command/pg/pg_base.rb', line 52
def version
return @version if defined? @version
result = exec_sql("select version();").match(/PostgreSQL (\d+\.\d+\.\d+) on/)
fail("Unable to determine Postgres version") unless result
@version = result[1]
end
|