generate: generate: Implement writing the control scripts
Implement writing all control scripts and making them executable. Signed-off-by: Enno Tensing <tenno@suij.in>
This commit is contained in:
parent
015f2e5a16
commit
b0df681fea
1 changed files with 42 additions and 4 deletions
|
@ -2,6 +2,7 @@
|
|||
"""Generate the control files.."""
|
||||
|
||||
import json
|
||||
import stat
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
@ -48,17 +49,21 @@ def create_container_from_config(data: dict, log: Log) -> Container | None:
|
|||
|
||||
def main() -> None:
|
||||
"""Run the program."""
|
||||
argc_threshold = 2
|
||||
if len(sys.argv) < argc_threshold:
|
||||
config_threshold = 2
|
||||
log_threshold = 3
|
||||
if len(sys.argv) < config_threshold:
|
||||
logger = Log("/dev/stdout")
|
||||
logger.log_error("No arguments passed!")
|
||||
sys.exit(1)
|
||||
config_file = ""
|
||||
log_file = ""
|
||||
if len(sys.argv) >= argc_threshold:
|
||||
base = "/var/lib/containerctl/containers"
|
||||
if len(sys.argv) >= config_threshold:
|
||||
config_file = sys.argv[1]
|
||||
if len(sys.argv) > argc_threshold:
|
||||
if len(sys.argv) >= log_threshold:
|
||||
log_file = sys.argv[2]
|
||||
if len(sys.argv) > log_threshold:
|
||||
base = sys.argv[3]
|
||||
logger = Log(log_file)
|
||||
data = load_container_config(Path(config_file), logger)
|
||||
if data is None:
|
||||
|
@ -71,6 +76,39 @@ def main() -> None:
|
|||
sys.exit(1)
|
||||
print(ct)
|
||||
print(vars(ct))
|
||||
scripts = {
|
||||
"create-volumes": ct.create_volumes,
|
||||
"create-secrets": ct.create_secrets,
|
||||
"create-environment": ct.create_environment,
|
||||
"remove-volumes": ct.remove_volumes,
|
||||
"remove-secrets": ct.remove_secrets,
|
||||
"create": ct.create_container,
|
||||
"remove": ct.remove_container,
|
||||
"purge": ct.purge_container,
|
||||
"start": ct.start_container,
|
||||
"stop": ct.stop_container,
|
||||
"restart": ct.restart_container,
|
||||
"upgrade": ct.upgrade_container,
|
||||
}
|
||||
base = f"{base}/{ct.name}"
|
||||
if not Path(base).exists():
|
||||
Path(base).mkdir()
|
||||
for script, method in scripts.items():
|
||||
s = Path(f"{base}/{script}")
|
||||
with s.open("w+", encoding="utf-8") as f:
|
||||
f.write("#!/bin/sh\n")
|
||||
if script == "create":
|
||||
f.write(ct.create_volumes())
|
||||
f.write(ct.create_secrets())
|
||||
f.write(ct.create_environment())
|
||||
f.write(method())
|
||||
s.chmod(
|
||||
stat.S_IRWXU
|
||||
| stat.S_IRGRP
|
||||
| stat.S_IXGRP
|
||||
| stat.S_IROTH
|
||||
| stat.S_IXOTH,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Add table
Reference in a new issue