From 1fe899744df85d514e6f1e7208ad56920d6a7992 Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Wed, 21 Feb 2024 13:58:42 +0100 Subject: [PATCH] Introduce namestandard flavors --- scripts/openstack-server | 64 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/scripts/openstack-server b/scripts/openstack-server index 1775349..616cc53 100755 --- a/scripts/openstack-server +++ b/scripts/openstack-server @@ -2,6 +2,7 @@ import argparse import json import os +import re import shlex import shutil import subprocess @@ -112,6 +113,12 @@ def get_options() -> dict: choices=['bind', 'json', 'shell', 'table', 'value', 'yaml'], default="json", ) + create_parser.add_argument( + "--namestandard", + help="Name of name standard", + default='default', + dest='namestandard', + ) delete_parser = subparsers.add_parser("delete") delete_parser.add_argument( @@ -140,6 +147,12 @@ def get_options() -> dict: help="Keep the configured disk in openstack", default=False, ) + delete_parser.add_argument( + "--namestandard", + help="Name of name standard", + default='default', + dest='namestandard', + ) show_parser = subparsers.add_parser("show") show_parser.add_argument( '--format', @@ -157,6 +170,12 @@ def get_options() -> dict: help="fqdn of server, MUST follow name standard", required=True, ) + show_parser.add_argument( + "--namestandard", + help="Name of name standard", + default='default', + dest='namestandard', + ) argcomplete.autocomplete(parser) args = parser.parse_args() @@ -164,7 +183,7 @@ def get_options() -> dict: # This is an opinionated name standard that we follow, and WILL rely on for various automation tasks fqdn = args.fqdn domain, environment, function, hostname, instance, location, number, service = parse_fqdn( - fqdn) + fqdn, args.namestandard) # We will allways put a server in i server group, you can override the default with the --sgroup switch if ('sgroup' not in args) or (args.sgroup == ""): @@ -183,6 +202,7 @@ def get_options() -> dict: 'hostname': hostname, 'instance': instance, 'location': location, + 'namestandard': args.namestandard, 'number': number, 'service': service, 'sgroup': sgroup @@ -204,8 +224,13 @@ def get_options() -> dict: return options +def parse_fqdn(fqdn: str, namestandard: str) -> tuple: + if namestandard == 'drive': + return parse_drive_fqdn(fqdn) + else: + return parse_default_fqdn(fqdn) -def parse_fqdn(fqdn: str) -> tuple: +def parse_default_fqdn(fqdn: str) -> tuple: domain = '.'.join([x for x in fqdn.split('.')[:1]]) hostname = fqdn.split('.')[0] instance, location, environment, function, number = hostname.split('-') @@ -213,6 +238,41 @@ def parse_fqdn(fqdn: str) -> tuple: return domain, environment, function, hostname, instance, location, number, service +def parse_drive_fqdn(fqdn: str) -> tuple: + type_regex = r'-*[1-9]*\..*' + server_type = re.sub(type_regex, '', fqdn) + + env_regex = r'.*(pilot|test).*' + environment = re.sub(env_regex, r'\1', fqdn) + domain = f'drive.{environment}.sunet.se' + customer = 'common' + + if environment not in ['pilot', 'test']: + environment = 'prod' + domain = 'drive.sunet.se' + + if server_type in ["backup", "intern-db", "node", "redis", "script"]: + customer = fqdn.split('.')[1] + if customer == 'drive': + customer = 'common' + elif server_type in ['gss', 'lookup']: + customer = server_type + elif server_type == 'gssbackup': + customer = 'gss' + elif server_type == 'lookupbackup': + customer = 'lookup' + else: + customer = 'common' + hostname = fqdn.split('.')[0] + number = re.sub(r'[a-z.]', '', hostname) + location = 'sto4' + if number == '3': + location = 'sto3' + if number == '1': + location = 'dco' + return domain, environment, server_type, hostname, customer, location, number, 'drive' + + def run_in_openstack(outer_command: list[str], rc_file: str) -> tuple: # This function runs the specified command with openstack cli using tsocks.