Skip to content

Commit b849e6b

Browse files
committed
db fixes
1 parent 84e48da commit b849e6b

4 files changed

Lines changed: 47 additions & 15 deletions

File tree

nuvolaris/kustomize.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
import nuvolaris.kustomize as nku
2323
import nuvolaris.template as ntp
2424

25+
def _build(dir):
26+
res = subprocess.run(["kustomize", "build", dir], capture_output=True)
27+
if res.returncode != 0:
28+
error = res.stderr.decode("utf-8")
29+
raise Exception(error)
30+
return res.stdout.decode("utf-8")
31+
2532
# execute the kustomization of a folder under "deploy"
2633
# specified with `where`
2734
# it generate a kustomization.yaml, adding the header
@@ -66,8 +73,7 @@ def kustomize(where, *what, templates=[], data={}):
6673
out = f"deploy/{where}/__{template}"
6774
file = ntp.spool_template(template, out, data)
6875
f.write(f"- __{template}\n")
69-
res = subprocess.run(["kustomize", "build", dir], capture_output=True)
70-
return res.stdout.decode("utf-8")
76+
return _build(dir)
7177

7278
# execute the kustomization of a folder under "deploy"
7379
# specified with `where` returning the expanded kustomization
@@ -76,8 +82,7 @@ def kustomize(where, *what, templates=[], data={}):
7682
# the nuvolaris operator needs to delete a component
7783
def build(where):
7884
dir = f"deploy/{where}"
79-
res = subprocess.run(["kustomize", "build", dir], capture_output=True)
80-
return res.stdout.decode("utf-8")
85+
return _build(dir)
8186

8287
# execute the kustomization of a folder under "deploy"
8388
# specified with `where`
@@ -125,8 +130,7 @@ def restricted_kustomize(where, *what, templates=[], templates_filter=[],data={}
125130
out = f"deploy/{where}/__{template}"
126131
file = ntp.spool_template(template, out, data)
127132
f.write(f"- __{template}\n")
128-
res = subprocess.run(["kustomize", "build", dir], capture_output=True)
129-
return res.stdout.decode("utf-8")
133+
return _build(dir)
130134

131135
# generate image kustomization
132136
def image(name, newName=None, newTag=None):

nuvolaris/runtimes_preloader.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,17 @@
2525

2626
def create(owner=None):
2727
logging.info(f"*** configuring runtime preloader")
28-
only_apache = cfg.get("nuvolaris.preloader.only_apache", defval=True)
28+
config = cfg.getall()
29+
only_apache = config.get(
30+
"nuvolaris.preload.only-apache",
31+
config.get("nuvolaris.preloader.only_apache", True)
32+
)
2933

3034
runtimes_as_json = util.get_runtimes_json_from_config_map()
3135
data=rutil.parse_runtimes(json.loads(runtimes_as_json), only_apache)
36+
if not data['containers']:
37+
logging.info("*** skipped runtime preloader: no valid runtime images found")
38+
return "skipped runtime preloader"
3239

3340
kust = kus.patchTemplates("runtimes", ["runtimes-job-container-attach.yaml"], data)
3441
spec = kus.kustom_list("runtimes", kust, templates=[], data=data)
@@ -61,4 +68,4 @@ def delete(owner=None):
6168
if owner:
6269
return delete_by_owner()
6370
else:
64-
return delete_by_spec()
71+
return delete_by_spec()

nuvolaris/runtimes_util.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ def find_default_container(containers: list, container_name, runtime_list, only_
3232
img = runtime['image']
3333
is_apache = img['prefix'] == 'apache'
3434
if not only_apache or is_apache:
35+
if not img.get('tag'):
36+
logging.warning(f"skipping runtime preloader for {container_name}: missing image tag")
37+
continue
3538
container = {
3639
"name": container_name,
3740
"image": f"{img['prefix']}/{img['name']}:{img['tag']}"
@@ -54,4 +57,4 @@ def parse_runtimes(runtimes_as_json, only_apache=True):
5457
find_default_container(containers, name, runtimes_as_json["runtimes"][name], only_apache)
5558

5659
data['containers']=containers
57-
return data
60+
return data

nuvolaris/templates/postgres_manage_user_tpl.sql

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,32 @@
1818
*/
1919

2020
{% if mode == 'create' %}
21-
CREATE DATABASE {{database}};
22-
CREATE USER {{username}} WITH PASSWORD '{{password}}';
21+
SELECT 'CREATE DATABASE {{database}}'
22+
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '{{database}}')\gexec
23+
24+
DO $$
25+
BEGIN
26+
IF EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = '{{username}}') THEN
27+
ALTER USER {{username}} WITH PASSWORD '{{password}}';
28+
ELSE
29+
CREATE USER {{username}} WITH PASSWORD '{{password}}';
30+
END IF;
31+
END
32+
$$;
33+
2334
GRANT ALL PRIVILEGES ON DATABASE {{database}} to {{username}};
2435
REVOKE CONNECT ON DATABASE {{database}} from public;
2536
{% endif %}
2637

2738
{% if mode == 'delete' %}
28-
DROP DATABASE {{database}};
29-
DROP OWNED BY {{username}};
30-
DROP USER {{username}};
31-
{% endif %}
39+
DROP DATABASE IF EXISTS {{database}};
40+
41+
DO $$
42+
BEGIN
43+
IF EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = '{{username}}') THEN
44+
DROP OWNED BY {{username}};
45+
DROP USER {{username}};
46+
END IF;
47+
END
48+
$$;
49+
{% endif %}

0 commit comments

Comments
 (0)