Compare commits
2 commits
340383f160
...
5257d4e70b
Author | SHA1 | Date | |
---|---|---|---|
5257d4e70b | |||
c5a8cfe852 |
3 changed files with 39 additions and 2 deletions
|
@ -49,7 +49,9 @@ in
|
||||||
user = {
|
user = {
|
||||||
destroyAfterDays = 0;
|
destroyAfterDays = 0;
|
||||||
create = user: psqlSecret "${genPassword} ${user}" ''
|
create = user: psqlSecret "${genPassword} ${user}" ''
|
||||||
CREATE USER ${user} PASSWORD '@SECRET@';
|
SELECT 'CREATE USER ${user}'
|
||||||
|
WHERE NOT EXISTS (SELECT FROM pg_roles WHERE rolname = '${user}')\gexec
|
||||||
|
ALTER USER ${user} PASSWORD '@SECRET@';
|
||||||
'';
|
'';
|
||||||
destroy = psqlSecret "printenv OBJECT" ''
|
destroy = psqlSecret "printenv OBJECT" ''
|
||||||
DROP USER @SECRET@;
|
DROP USER @SECRET@;
|
||||||
|
@ -59,7 +61,11 @@ in
|
||||||
destroyAfterDays = 30;
|
destroyAfterDays = 30;
|
||||||
deps = [ "user" ];
|
deps = [ "user" ];
|
||||||
create = db: psql ''
|
create = db: psql ''
|
||||||
CREATE DATABASE ${db} OWNER ${cfg.databases.${db}.owner};
|
SELECT 'CREATE DATABASE ${db} OWNER ${cfg.databases.${db}.owner}'
|
||||||
|
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '${db}')\gexec
|
||||||
|
'';
|
||||||
|
change = db: psql ''
|
||||||
|
ALTER DATABASE ${db} OWNER TO ${cfg.databases.${db}.owner};
|
||||||
'';
|
'';
|
||||||
destroy = psqlSecret "printenv OBJECT" ''
|
destroy = psqlSecret "printenv OBJECT" ''
|
||||||
DROP DATABASE @SECRET@;
|
DROP DATABASE @SECRET@;
|
||||||
|
|
|
@ -3,12 +3,17 @@
|
||||||
patroni = lib.mkIf config.simulacrum {
|
patroni = lib.mkIf config.simulacrum {
|
||||||
databases = config.lib.forService "patroni" {
|
databases = config.lib.forService "patroni" {
|
||||||
testdb.owner = "testuser";
|
testdb.owner = "testuser";
|
||||||
|
existingdb.owner = "existinguser";
|
||||||
};
|
};
|
||||||
users = config.lib.forService "patroni" {
|
users = config.lib.forService "patroni" {
|
||||||
testuser.locksmith = {
|
testuser.locksmith = {
|
||||||
nodes = config.services.patroni.nodes.haproxy;
|
nodes = config.services.patroni.nodes.haproxy;
|
||||||
format = "pgpass";
|
format = "pgpass";
|
||||||
};
|
};
|
||||||
|
existinguser.locksmith = {
|
||||||
|
nodes = config.services.patroni.nodes.haproxy;
|
||||||
|
format = "pgpass";
|
||||||
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,5 +87,31 @@ in
|
||||||
for client in clients:
|
for client in clients:
|
||||||
client.succeed(f"PGPASSFILE=/run/locksmith/patroni-testuser psql -h ${link.ipv4} -p ${link.portStr} -U testuser -d testdb --command='create table test_table_{client.name} as select * from generate_series(1, 10) as val;'")
|
client.succeed(f"PGPASSFILE=/run/locksmith/patroni-testuser psql -h ${link.ipv4} -p ${link.portStr} -U testuser -d testdb --command='create table test_table_{client.name} as select * from generate_series(1, 10) as val;'")
|
||||||
client.fail("PGPASSFILE=/run/locksmith/patroni-testuser psql -h ${link.ipv4} -p ${link.portStr} -U testuser -d postgres --command='select * from dummy;'")
|
client.fail("PGPASSFILE=/run/locksmith/patroni-testuser psql -h ${link.ipv4} -p ${link.portStr} -U testuser -d postgres --command='select * from dummy;'")
|
||||||
|
|
||||||
|
with subtest("should take over existing databases and users via incandescence"):
|
||||||
|
for cmd in [
|
||||||
|
"drop database existingdb;",
|
||||||
|
"drop user existinguser;",
|
||||||
|
"create database existingdb owner postgres;",
|
||||||
|
"create user existinguser;"
|
||||||
|
]:
|
||||||
|
clients[0].succeed(f"psql -h ${link.ipv4} -p ${link.portStr} -U postgres --command='{cmd}'")
|
||||||
|
|
||||||
|
for client in clients:
|
||||||
|
client.fail(f"PGPASSFILE=/run/locksmith/patroni-existinguser psql -h ${link.ipv4} -p ${link.portStr} -U existinguser -d existingdb --command='create table test_table_{client.name} as select * from generate_series(1, 10) as val;'")
|
||||||
|
|
||||||
|
consulConfig = json.loads(clients[0].succeed("cat /etc/consul.json"))
|
||||||
|
addr = consulConfig["addresses"]["http"]
|
||||||
|
port = consulConfig["ports"]["http"]
|
||||||
|
setEnv = f"CONSUL_HTTP_ADDR={addr}:{port}"
|
||||||
|
clients[0].succeed(f"{setEnv} consul kv delete --recurse services/incandescence/providers/patroni/formulae/database/existingdb")
|
||||||
|
clients[0].succeed(f"{setEnv} consul kv delete --recurse services/incandescence/providers/patroni/formulae/user/existinguser")
|
||||||
|
|
||||||
|
for node in nodes:
|
||||||
|
node.systemctl("restart incandescence-patroni.target")
|
||||||
|
clients[0].succeed("[[ $(psql -h ${link.ipv4} -p ${link.portStr} -U postgres --tuples-only --csv --command=\"SELECT pg_roles.rolname FROM pg_database JOIN pg_roles ON pg_database.datdba = pg_roles.oid WHERE pg_database.datname = 'existingdb'\") == existinguser ]]")
|
||||||
|
for client in clients:
|
||||||
|
client.succeed(f"PGPASSFILE=/run/locksmith/patroni-existinguser psql -h ${link.ipv4} -p ${link.portStr} -U existinguser -d existingdb --command='create table test_table_{client.name} as select * from generate_series(1, 10) as val;'")
|
||||||
|
client.fail("PGPASSFILE=/run/locksmith/patroni-existinguser psql -h ${link.ipv4} -p ${link.portStr} -U existinguser -d postgres --command='select * from dummy;'")
|
||||||
'';
|
'';
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue