Skip to content

Commit 808153a

Browse files
committed
Use postgres user for container gateway backup/restore
1 parent cc3b128 commit 808153a

4 files changed

Lines changed: 43 additions & 34 deletions

File tree

definitions/checks/foreman/validate_external_db_version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ValidateExternalDbVersion < ForemanMaintain::Check
1111
end
1212

1313
def run
14-
current_db_version = feature(:foreman_database).db_version
14+
current_db_version = feature(:foreman_database).db_version(feature(:foreman_database).local?)
1515
fail!(db_upgrade_message(current_db_version)) if current_db_version.major < 13
1616
end
1717

definitions/features/container_gateway_database.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ def load_configuration
3232
if connection_string
3333
uri = URI.parse(connection_string)
3434
@configuration['connection_string'] = connection_string
35-
@configuration['user'] = 'foreman-proxy'
3635
@configuration['database'] = uri.path[1..]
3736
end
3837
@configuration

definitions/procedures/backup/online/container_gateway_db.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ class ContainerGatewayDB < ForemanMaintain::Procedure
1212

1313
def run
1414
with_spinner('Getting Container Gateway DB dump') do
15+
local = feature(:container_gateway_database).local?
1516
feature(:container_gateway_database).
16-
dump_db(File.join(@backup_dir, 'container_gateway.dump'))
17+
dump_db(File.join(@backup_dir, 'container_gateway.dump'), local)
1718
end
1819
end
1920
end

lib/foreman_maintain/concerns/base_database.rb

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -56,49 +56,54 @@ def query_csv(sql, config = configuration)
5656

5757
def psql(query, config = configuration)
5858
if ping(config)
59-
execute(psql_command(config),
60-
:stdin => query,
61-
:hidden_patterns => [config['password']],
62-
:config => user)
59+
if local?
60+
execute(psql_command(config),
61+
:stdin => query,
62+
:hidden_patterns => [config['password']],
63+
:user => 'postgres')
64+
else
65+
execute(psql_command(config),
66+
:stdin => query,
67+
:hidden_patterns => [config['password']])
68+
end
6369
else
6470
raise_service_error
6571
end
6672
end
6773

6874
def ping(config = configuration)
69-
execute?(psql_command(config) + ' -c "SELECT 1 as ping"',
70-
:hidden_patterns => [config['password']],
71-
:user => config['user'])
75+
if local?
76+
execute?(psql_command(config) + ' -c "SELECT 1 as ping"',
77+
:hidden_patterns => [config['password']],
78+
:user => 'postgres')
79+
else
80+
execute?(psql_command(config) + ' -c "SELECT 1 as ping"',
81+
:hidden_patterns => [config['password']])
82+
end
7283
end
7384

7485
def dump_db(file, config = configuration)
75-
if config['user']
76-
execute!("chown -R #{config['user']}:#{config['user']} #{File.dirname(file)}")
86+
if local?
87+
execute!("chown -R postgres:postgres #{File.dirname(file)}")
88+
execute!(dump_command(config) + " -f #{file}",
89+
:hidden_patterns => [config['password']], :user => 'postgres')
90+
else
91+
execute!(dump_command(config) + " -f #{file}", :hidden_patterns => [config['password']])
7792
end
78-
execute!(dump_command(config) + " -f #{file}",
79-
:hidden_patterns => [config['password']], :user => config['user'])
8093
end
8194

82-
def restore_dump(file, localdb, config = configuration)
83-
if localdb
95+
def restore_dump(file, config = configuration)
96+
if local?
8497
dump_cmd = "runuser - postgres -c 'pg_restore -C -d postgres #{file}'"
8598
execute!(dump_cmd)
8699
else
87100
# TODO: figure out how to completely ignore errors. Currently this
88101
# sometimes exits with 1 even though errors are ignored by pg_restore
89-
base_cmd = if config['connection_string']
90-
'pg_restore'
91-
else
92-
base_command(config, 'pg_restore')
93-
end
94-
dump_cmd = base_cmd +
102+
dump_cmd = base_command(config, 'pg_restore') +
95103
' --no-privileges --clean --disable-triggers -n public ' \
96104
"-d #{config['database']} #{file}"
97-
if config['user']
98-
execute!("chown -R #{config['user']}:#{config['user']} #{File.dirname(file)}")
99-
end
100105
execute!(dump_cmd, :hidden_patterns => [config['password']],
101-
:valid_exit_statuses => [0, 1], :user => config['user'])
106+
:valid_exit_statuses => [0, 1])
102107
end
103108
end
104109

@@ -137,14 +142,18 @@ def dropdb(config = configuration)
137142
end
138143

139144
def db_version(config = configuration)
140-
if ping(config)
141-
# Note - t removes headers, -A removes alignment whitespace
142-
server_version_cmd = psql_command(config) + ' -c "SHOW server_version" -t -A'
143-
version_string = execute!(server_version_cmd, :hidden_patterns => [config['password']])
144-
version(version_string)
145-
else
146-
raise_service_error
147-
end
145+
if ping(config)
146+
# Note - t removes headers, -A removes alignment whitespace
147+
server_version_cmd = psql_command(config) + ' -c "SHOW server_version" -t -A'
148+
if local?
149+
version_string = execute!(server_version_cmd, :hidden_patterns => [config['password']], :user => 'postgres')
150+
else
151+
version_string = execute!(server_version_cmd, :hidden_patterns => [config['password']])
152+
end
153+
version(version_string)
154+
else
155+
raise_service_error
156+
end
148157
end
149158

150159
def psql_cmd_available?

0 commit comments

Comments
 (0)