"""
Contains the compatible release version of the notifyone-core component.
Contains other information about the notifyone-core component
"""

SOURCE = "https://github.com/tata1mg/notifyone-core.git --branch 1.0.0"

components/core.py
tata1mg/notifyone
"""
Contains the compatible release version of the notifyone-gateway component.
Contains other information about the notifyone-gateway component
"""

SOURCE = "https://github.com/tata1mg/notifyone-gateway.git --branch 1.0.0"

components/gateway.py
tata1mg/notifyone
"""
Contains the compatible release version of the notifyone-handler component.
Contains other information about the notifyone-handler component
"""

SOURCE = "https://github.com/tata1mg/notifyone-handler.git --branch 1.0.0"

components/handler.py
tata1mg/notifyone
import subprocess
import json
import os
from utils import extract_values
from sys import platform

from components.core import SOURCE as CORE_SOURCE


def setup_core():
    print("\nSetting up notifyone-core....\n")

    _res = subprocess.run(["git clone {}".format(CORE_SOURCE)], shell=True,
                          capture_output=True)
    if _res.returncode != 0:
        print(str(_res.stderr.decode('utf-8')))
        #exit(1)

    os.chdir('notifyone-core')

    _res = subprocess.run(['cp config_template.json config.json'], shell=True, capture_output=True)
    if _res.returncode != 0:
        print(str(_res.stderr.decode('utf-8')))
        exit(1)

    with open('config.json') as c:
        _core_config = json.load(c)
        _core_queue_names = extract_values(_core_config, "QUEUE_NAME")
        _port = str(_core_config.get("PORT") or 9402)
        if platform.lower() == 'darwin':
            _core_config['DB_CONNECTIONS']['connections']['default']['credentials']['host'] = 'host.docker.internal'
            _core_config['SUBSCRIBE_NOTIFICATION_STATUS_UPDATES']['SQS']['SQS_ENDPOINT_URL'] = 'http://host.docker.internal:4566'
            _core_config['DISPATCH_NOTIFICATION_REQUEST']['SQS']['SQS_ENDPOINT_URL'] = 'http://host.docker.internal:4566'
            _core_config['NOTIFICATION_REQUEST']['SQS']['SQS_ENDPOINT_URL'] = 'http://host.docker.internal:4566'
            _core_config['REDIS_CACHE_HOSTS']['default']['REDIS_HOST'] = 'host.docker.internal'
            with open('config.json', 'w') as f:
                json.dump(_core_config, f)

    # create local stack queues
    for _q in _core_queue_names:
        _res = subprocess.run(["docker exec -i $(docker ps | grep localstack | awk '{print $1}') awslocal sqs create-queue --queue-name " + _q],
                              shell=True, capture_output=True)
        if _res.returncode != 0:
            print("\nError in creating queue {}\n".format(_q))
            print(_res.stderr.decode('utf-8'))
        else:
            pass

    _stat = subprocess.run(['docker rm --force notifyone-core'], shell=True)
    _stat = subprocess.run(["docker image rm notifyone-core"], shell=True)

    _res = subprocess.run(['docker build . --tag notifyone-core --build-arg SERVICE_NAME=notifyone_core'],
                          shell=True, capture_output=True)
    if _res.returncode != 0:
        print(str(_res.stderr.decode('utf-8')))
        exit(1)

    _res = subprocess.run(['docker run -p ' + _port + ':' + _port + ' --detach --name notifyone-core notifyone-core'],
                          shell=True, capture_output=True)
    if _res.returncode != 0:
        print(str(_res.stderr.decode('utf-8')))
        exit(1)

    _res = subprocess.run(["docker exec -i $(docker ps | grep notifyone-core | awk '{print $1}') python3 database.py upgrade "],
                          shell=True, capture_output=True)
    if _res.returncode != 0:
        print("\nError in DB upgrade\n")
        print(_res.stderr.decode('utf-8'))
    else:
        pass

    print("\nnotifyone-core setup completed\n")
core_setup.py
tata1mg/notifyone
import subprocess
import json
import os
from utils import extract_values
from sys import platform

from components.gateway import SOURCE as GATEWAY_SOURCE


def setup_gateway():
    print("\nSetting up notifyone-gateway.....\n")

    _res = subprocess.run(["git clone {}".format(GATEWAY_SOURCE)], shell=True,
                          capture_output=True)
    if _res.returncode != 0:
        print(str(_res.stderr.decode('utf-8')))
        #exit(1)

    os.chdir('notifyone-gateway')

    _res = subprocess.run(['cp config_template.json config.json'], shell=True, capture_output=True)
    if _res.returncode != 0:
        print(str(_res.stderr.decode('utf-8')))
        exit(1)

    with open('config.json', 'r') as c:
        _gateway_config = json.load(c)
        _gateway_port = str(_gateway_config.get("PORT") or 9401)
        _gateway_queue_names = extract_values(_gateway_config, "QUEUE_NAME")
        if platform.lower() == 'darwin':
            _gateway_config['NOTIFICATION_SERVICE']['HOST'] = 'http://host.docker.internal:9402'
            _gateway_config['TRIGGER_NOTIFICATIONS']['SQS']['SQS_ENDPOINT_URL'] = 'http://host.docker.internal:4566'
            with open('config.json', 'w') as f:
                json.dump(_gateway_config, f)

    # create local stack queues
    for _q in _gateway_queue_names:
        _res = subprocess.run(["docker exec -i $(docker ps | grep localstack | awk '{print $1}') awslocal sqs create-queue --queue-name " + _q],
                              shell=True, capture_output=True)
        if _res.returncode != 0:
            print("\nError in creating queue {}\n".format(_q))
            print(_res.stderr.decode('utf-8'))
        else:
            pass

    _stat = subprocess.run(['docker rm --force notifyone-gateway'], shell=True)
    _stat = subprocess.run(["docker image rm notifyone-gateway"], shell=True)

    _res = subprocess.run(['docker build . --tag notifyone-gateway --build-arg SERVICE_NAME=notifyone_gateway'],
                          shell=True, capture_output=True)
    if _res.returncode != 0:
        print(str(_res.stderr.decode('utf-8')))
        exit(1)

    _res = subprocess.run([
                              'docker run -p ' + _gateway_port + ':' + _gateway_port + ' --detach --name notifyone-gateway notifyone-gateway'],
                          shell=True, capture_output=True)
    if _res.returncode != 0:
        print(str(_res.stderr.decode('utf-8')))
        exit(1)

    print("\nnotifyone-gateway setup completed\n")
gateway_setup.py
tata1mg/notifyone
import subprocess
import json
import os
from sys import platform
from utils import extract_values

from components.handler import SOURCE as HANDLER_SOURCE


def setup_handler():
    print("\nSetting up notifyone-handler.....\n")

    _res = subprocess.run(["git clone {}".format(HANDLER_SOURCE)], shell=True,
                          capture_output=True)
    if _res.returncode != 0:
        print(str(_res.stderr.decode('utf-8')))
        #exit(1)

    os.chdir('notifyone-handler')

    _res = subprocess.run(['cp config_template.json config.json'], shell=True, capture_output=True)
    if _res.returncode != 0:
        print(str(_res.stderr.decode('utf-8')))
        exit(1)

    with open('config.json') as c:
        _handler_config = json.load(c)
        _handler_queue_names = extract_values(_handler_config, "QUEUE_NAME")
        _port = str(_handler_config.get("PORT") or 9403)
        if platform.lower() == 'darwin':
            _handler_config['SQS_AUTH']['SQS_ENDPOINT_URL'] = 'http://host.docker.internal:4566'
            with open('config.json', 'w') as f:
                json.dump(_handler_config, f)

    # create local stack queues
    for _q in _handler_queue_names:
        _res = subprocess.run(["docker exec -i $(docker ps | grep localstack | awk '{print $1}') awslocal sqs create-queue --queue-name " + _q],
                              shell=True, capture_output=True)
        if _res.returncode != 0:
            print("\nError in creating queue {}\n".format(_q))
            print(_res.stderr.decode('utf-8'))
        else:
            pass

    _stat = subprocess.run(['docker rm --force notifyone-handler'], shell=True)
    _stat = subprocess.run(["docker image rm notifyone-handler"], shell=True)

    _res = subprocess.run(['docker build . --tag notifyone-handler --build-arg SERVICE_NAME=notifyone_handler'],
                          shell=True, capture_output=True)
    if _res.returncode != 0:
        print(str(_res.stderr.decode('utf-8')))
        exit(1)

    _res = subprocess.run(['docker run -p ' + _port + ':' + _port + ' --detach --name notifyone-handler notifyone-handler'],
                          shell=True, capture_output=True)
    if _res.returncode != 0:
        print(str(_res.stderr.decode('utf-8')))
        exit(1)

    print("\nnotifyone-core setup completed\n")
handler_setup.py
tata1mg/notifyone
import subprocess
import os
from gateway_setup import setup_gateway
from core_setup import setup_core
from handler_setup import setup_handler

_docker_check_res = subprocess.run(['docker info'], shell=True, capture_output=True)
if _docker_check_res.returncode != 0:
    print("Either Docker is not installed or not running\n"+(str(_docker_check_res.stderr.decode('utf-8'))))
    exit(1)


print("\n Setting up Postgres on Docker\n")
_res = subprocess.run(['docker pull postgres:14.5'],  shell=True, capture_output=True)
if _res.returncode != 0:
    print(str(_res.stderr.decode('utf-8')))
    exit(1)

_stat = subprocess.run(['docker rm --force postgres_notify'], shell=True)
_res = subprocess.run(["docker run  --detach -p5432:5432 -e POSTGRES_PASSWORD='postgres' -e POSTGRES_USER=postgres --name postgres_notify postgres:14.5"],  shell=True, capture_output=True)
if _res.returncode != 0:
    print(str(_res.stderr.decode('utf-8')))
    exit(1)

print("\n Setting up Redis on Docker\n")
_stat = subprocess.run(['docker rm --force redis_notify'], shell=True)
_res = subprocess.run(["docker run --detach --name redis_notify -p 6379:6379 -d redis"],  shell=True, capture_output=True)
if _res.returncode != 0:
    print(str(_res.stderr.decode('utf-8')))
    exit(1)


print("\n Setting up localstack\n")
# install localstack
_res = subprocess.run(['localstack --version'], shell=True, capture_output=True)
if _res.returncode != 0:
    print("Localstack not installed \n"+(str(_docker_check_res.stderr.decode('utf-8'))))
    print("\n installing localstack \n")
    _localstack_res = subprocess.run(['python3 -m pip install --force-reinstall localstack'], shell=True, capture_output=True)
    if _localstack_res.returncode != 0:
        print(str(_localstack_res.stderr.decode('utf-8')))
        exit(1)
    else:
        pass
else:
    pass

_localstack_start_res = subprocess.run(['localstack start -d'], shell=True, capture_output=True)
if _localstack_start_res.returncode != 0:
    print(str(_localstack_start_res.stderr.decode('utf-8')))
    exit(1)
else:
    pass

subprocess.run('rm -rf notifyone-gateway/', shell=True, capture_output=True)
subprocess.run('rm -rf notifyone-core/', shell=True, capture_output=True)
subprocess.run('rm -rf notifyone-handler/', shell=True, capture_output=True)

setup_gateway()

os.chdir('../')

setup_core()

os.chdir('../')

setup_handler()

print('##### Congratulations! NotifyOne system setup Completed #####')
print('Service Hosts - \n\t notifyone-gateway : http://localhost:9401 \n\t notifyone-core : http://localhost:9402 \n\t notifyone-handler : http://localhost:9403')
print('Create App API documentation - \n\t http://localhost:9402/swagger/#/Apps/post_apps')
print('Create Event API documentation - \n\t http://localhost:9402/swagger/#/Events/post_event_create')
print('Send-Notification API documentation - \n\t http://localhost:9401/swagger/#/event_notification/post_send_notification')
print('Get-Notification-Detail API documentation - \n\t http://localhost:9401/swagger/#/event_notification/get_get_notification')

notify_setup.py
tata1mg/notifyone
def extract_values(obj, key):
    """Pull all values of specified key from nested JSON."""
    arr = []

    def extract(obj, arr, key):
        """Recursively search for values of key in JSON tree."""
        if isinstance(obj, dict):
            for k, v in obj.items():
                if isinstance(v, (dict, list)):
                    extract(v, arr, key)
                elif k == key and v:
                    arr.append(v)
        elif isinstance(obj, list):
            for item in obj:
                extract(item, arr, key)
        return arr

    results = extract(obj, arr, key)
    return results
utils.py
tata1mg/notifyone