<reponame>lizzyTheLizard/homeserver-azure
#!/usr/bin/env python3
import sys
import hashlib
import socket
from cdn import *
from cosmosdb import *
from function import *
from localServiceFile import *
from serviceBus import *
from storage import *

# Configuration
group = "Homeserver"
location = "switzerlandnorth"
dbName = "gutschihomeserver-db"
sbName = "gutschihomeserver-event"
cdnName = "gutschihomeserver-cdn"
appName = "Homeserver"
domain = "gutschi.site"
apps = {
    "cash-app" : { 
        "storageName" : "gutschihomeservercash",
        "functionName": "gutschihomeserver-cash",
        "subdomain"   : "cash",
        "ad"          : {
            "appURI": "api://gutschihomeserver",
            "appId" : "f79682fe-0761-4361-aa2e-317957284c3a",
            "tenant": "7bd72b43-52f6-4dc6-a856-5704e0f925bd",
        },
        "db"          : {
            "cash" : {
                "account"       : "/id",
                "booking"       : "/id",
                "closingBooking": "/id",
                "closingDate"   : "/owner",
                "accountBooking": "/accountId",
            },
        },
        "servicebus": ["closingdatechanged", "bookingchanged"],
    },
    "mova-app": {
        "storageName" : "gutschihomeservermova",
        "functionName": "gutschihomeserver-mova",
        "subdomain"   : "mova",
        "ad"          : None,
        "db"          : {
            "mova" : {
                "user"  : "/id",
                "rover" : "/id",
            },
        },
        "servicebus"  : None,
    },
}


def localService(environment):
    if len(sys.argv) <= 3:
        print("No app given.")
        sys.exit(-1)
    appName = sys.argv[3]
    postfix = getPostfix(environment)
    postfixDbName = dbName + postfix
    postfixSbName = sbName + postfix
    storageName = apps[appName]["storageName"]
    createLocalServiceFile(group, appName, postfixSbName, postfixDbName, storageName)

def getPostfix(environment):
    if environment == "dev":
        return "-dev" + hashlib.md5(socket.gethostname().encode('utf-8')).hexdigest()[0:4]
    elif environment == "ci":
        return "-ci"
    elif environment == "prod":
        return ""
    else:
        print("Invalid environment " + environment + " given. Valid is 'dev', 'ci', 'prod'")
        sys.exit(-1);

def create(environment):
    postfix = getPostfix(environment)
    postfixDbName = dbName + postfix
    postfixSbName = sbName + postfix

    createCosmosDBAccount(group, postfixDbName)
    createServiceBus(group, postfixSbName)
    for appName in apps:
        storageName = apps[appName]["storageName"]
        dbConfig = apps[appName]["db"]
        sbConfig = apps[appName]["servicebus"]

        createStorage(group, location, storageName)
        createCosmosDBDatabases(group, postfixDbName, dbConfig)
        createQueues(group, postfixSbName, sbConfig)

    if environment != "prod":
        return;

    createCDN(group, cdnName)
    for appName in apps:
        storageName = apps[appName]["storageName"]
        functionName = apps[appName]["functionName"]
        adConfig = apps[appName]["ad"]
        subdomain = apps[appName]["subdomain"]

        if not functionName is None:
            createFunction(group, functionName, location, postfixSbName, postfixDbName, storageName, adConfig)
        if not subdomain is None:
            createCDNEndpoint(group, cdnName, subdomain, domain, storageName)


if __name__ == "__main__":
    if len(sys.argv) <= 1:
        print("No command given. Valid is 'create', 'localservice'")
        sys.exit(-1)
    command = sys.argv[1]

    if len(sys.argv) <= 2:
        print("No environment given. Valid is 'dev', 'ci', 'prod'")
        sys.exit(-1)
    environment = sys.argv[2]

    if command == "localservice":
        localService(environment)
    elif command == "create":
        create(environment)
    else:
        print("Invalid command given '" + command + "'. Valid is 'create', 'localservice'")
        sys.exit(-1)


#!/usr/bin/env python3
import sys
import hashlib
import socket
from cdn import *
from cosmosdb import *
from function import *
from localServiceFile import *
from serviceBus import *
from storage import *

# Configuration
group = "Homeserver"
location = "switzerlandnorth"
dbName = "gutschihomeserver-db"
sbName = "gutschihomeserver-event"
cdnName = "gutschihomeserver-cdn"
appName = "Homeserver"
domain = "gutschi.site"
apps = {
    "cash-app" : { 
        "storageName" : "gutschihomeservercash",
        "functionName": "gutschihomeserver-cash",
        "subdomain"   : "cash",
        "ad"          : {
            "appURI": "api://gutschihomeserver",
            "appId" : "f79682fe-0761-4361-aa2e-317957284c3a",
            "tenant": "7bd72b43-52f6-4dc6-a856-5704e0f925bd",
        },
        "db"          : {
            "cash" : {
                "account"       : "/id",
                "booking"       : "/id",
                "closingBooking": "/id",
                "closingDate"   : "/owner",
                "accountBooking": "/accountId",
            },
        },
        "servicebus": ["closingdatechanged", "bookingchanged"],
    },
    "mova-app": {
        "storageName" : "gutschihomeservermova",
        "functionName": "gutschihomeserver-mova",
        "subdomain"   : "mova",
        "ad"          : None,
        "db"          : {
            "mova" : {
                "user"  : "/id",
                "rover" : "/id",
            },
        },
        "servicebus"  : None,
    },
}


def localService(environment):
    if len(sys.argv) <= 3:
        print("No app given.")
        sys.exit(-1)
    appName = sys.argv[3]
    postfix = getPostfix(environment)
    postfixDbName = dbName + postfix
    postfixSbName = sbName + postfix
    storageName = apps[appName]["storageName"]
    createLocalServiceFile(group, appName, postfixSbName, postfixDbName, storageName)

def getPostfix(environment):
    if environment == "dev":
        return "-dev" + hashlib.md5(socket.gethostname().encode('utf-8')).hexdigest()[0:4]
    elif environment == "ci":
        return "-ci"
    elif environment == "prod":
        return ""
    else:
        print("Invalid environment " + environment + " given. Valid is 'dev', 'ci', 'prod'")
        sys.exit(-1);

def create(environment):
    postfix = getPostfix(environment)
    postfixDbName = dbName + postfix
    postfixSbName = sbName + postfix

    createCosmosDBAccount(group, postfixDbName)
    createServiceBus(group, postfixSbName)
    for appName in apps:
        storageName = apps[appName]["storageName"]
        dbConfig = apps[appName]["db"]
        sbConfig = apps[appName]["servicebus"]

        createStorage(group, location, storageName)
        createCosmosDBDatabases(group, postfixDbName, dbConfig)
        createQueues(group, postfixSbName, sbConfig)

    if environment != "prod":
        return;

    createCDN(group, cdnName)
    for appName in apps:
        storageName = apps[appName]["storageName"]
        functionName = apps[appName]["functionName"]
        adConfig = apps[appName]["ad"]
        subdomain = apps[appName]["subdomain"]

        if not functionName is None:
            createFunction(group, functionName, location, postfixSbName, postfixDbName, storageName, adConfig)
        if not subdomain is None:
            createCDNEndpoint(group, cdnName, subdomain, domain, storageName)


if __name__ == "__main__":
    if len(sys.argv) <= 1:
        print("No command given. Valid is 'create', 'localservice'")
        sys.exit(-1)
    command = sys.argv[1]

    if len(sys.argv) <= 2:
        print("No environment given. Valid is 'dev', 'ci', 'prod'")
        sys.exit(-1)
    environment = sys.argv[2]

    if command == "localservice":
        localService(environment)
    elif command == "create":
        create(environment)
    else:
        print("Invalid command given '" + command + "'. Valid is 'create', 'localservice'")
        sys.exit(-1)
#!/usr/bin/env python3 # Configuration