tenantgenerator/tenant/commands/init.py

104 lines
3.9 KiB
Python

# tenant/commands/init.py
import os
import shutil
from tenant.utils.common import get_secure_password, generate_key, generate_csr
from tenant.utils.terraform import create_tfvars_file
import tenant.utils.generate_secrets_file
from tenant.utils.template_values import template_values
def add_subparser(subparsers):
init_parser = subparsers.add_parser("init", help="Initialize a new tenant")
init_parser.add_argument(
"--target", default=".", help="Target directory (default: current directory)"
)
def execute(args):
tenant_name = os.environ.get("TENANT_NAME")
if not tenant_name:
tenant_name = input("Please enter the desired tenant name: ")
else:
user_confirmation = input(
f"Current tenant name is {tenant_name}. Is this correct? (y/n): "
)
if user_confirmation != "y":
tenant_name = input("Please enter the tenant name: ")
ingress = input(
"Please enter the FQDN of the Kibana ingress, without the 'kibana' prefix: "
)
tenant_size = input("Please enter the desired size of the tenant (S/M/L): ").upper()
target_directory = args.target
tenant_directory = os.path.join(target_directory, tenant_name)
# define and create necessary folder structure
terraform_directory = os.path.join(tenant_directory, "00-terraform")
certificates_directory = os.path.join(tenant_directory, "01-certificates")
kubernetes_directory = os.path.join(tenant_directory, "02-kubernetes")
helm_directory = os.path.join(tenant_directory, "03-helm")
values_file = os.path.join(helm_directory, tenant_name + ".values.yaml")
# generate key and csr if not exist
keyfile = os.path.join(certificates_directory, ingress + ".key")
csrfile = os.path.join(certificates_directory, ingress + ".csr")
# Check if the tenant directory already exists
if os.path.exists(tenant_directory):
user_input = input(
f"Attention: Tenant directory '{tenant_directory}' already exists. Do you want to continue? (y/n): "
)
if user_input.lower() == "y":
user_input = input(
"Should the directory be deleted? Otherwise we will use existing values to resize tenant. (y/n): "
)
if user_input.lower() != "y":
if os.path.exists(values_file):
template_values(
tenant_name, tenant_size, "laas", ingress, values_file
)
print(f"templated new values file in '{values_file}'")
exit(0)
else:
shutil.rmtree(tenant_directory)
else:
exit(1)
# Prompt the user for the GitSync password securely
git_sync_password = get_secure_password(
prompt="Please insert predefined password for GitSync: "
)
user_confirmation = input(f"Do you want a CSR/Keyfile to be created (y/n): ")
if user_confirmation == "y":
if os.path.exists(keyfile):
print("Keyfile file already exists")
print(keyfile)
exit(1)
else:
generate_key(keyfile)
generate_csr(csrfile, ingress)
# Create symbolic links for *.tf files in tenant directory
source_tf_dir = os.path.join(target_directory, "terraform")
target_tf_dir = terraform_directory
if os.path.exists(values_file):
print("Values file already exists")
print(values_file)
else:
template_values(tenant_name, "S", "laas", ingress, values_file)
# generate secrets file if not already exist, not yet encrypted on the fly
secrets_file = os.path.join(helm_directory, tenant_name + ".secrets.yaml")
if os.path.exists(secrets_file):
print("Secrets file already exists")
print(secrets_file)
else:
tenant.utils.generate_secrets_file.generate_secrets_file(
secrets_file, git_sync_password
)
print(f"Tenant '{tenant_name}' initialized in '{tenant_directory}'.")