diff --git a/bin/wtl-dummy-dataset b/bin/wtl-dummy-dataset new file mode 100755 index 0000000..a1b2093 --- /dev/null +++ b/bin/wtl-dummy-dataset @@ -0,0 +1,158 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +import requests +import os +import time +import argcomplete, argparse +import yaml +import subprocess +from pprint import pprint +import sys + +print("This scirpt doesn't work") +sys.exit(1) + +WTL_DEV_KIT_PATH = os.environ.get("WTL_DEV_KIT_PATH") +WTL_DEV_KIT_REPOS_PATH = os.environ.get("WTL_DEV_KIT_REPOS_PATH") +WTL_DEV_KIT_BIN_PATH = os.environ.get("WTL_DEV_KIT_BIN_PATH") + +config = {} + +with open(WTL_DEV_KIT_PATH + "/config/config.yml") as fh: + config['config'] = yaml.load(fh) + +if os.path.isfile(WTL_DEV_KIT_PATH + "/config/repositories.yml"): + with open(WTL_DEV_KIT_PATH + "/config/repositories.yml") as fh: + config['repositories'] = yaml.load(fh) +else: + with open(WTL_DEV_KIT_PATH + "/config/repositories.example.yml") as fh: + config['repositories'] = yaml.load(fh) + +parser = argparse.ArgumentParser() + +argcomplete.autocomplete(parser) +args = parser.parse_args() + +os.environ['KEYCLOAK_URI'] = 'http://{host_ip}:9080'.format(host_ip=config['config']['host_ip']) +os.environ['COURSES_BACKEND_URI'] = 'http://{host_ip}:10000'.format(host_ip=config['config']['host_ip']) +os.environ['CHAPTERS_BACKEND_URI'] = 'http://{host_ip}:10001'.format(host_ip=config['config']['host_ip']) +os.environ['PAGES_BACKEND_URI'] = 'http://{host_ip}:10002'.format(host_ip=config['config']['host_ip']) +os.environ['COURSE_MIDTIER_URI'] = 'http://{host_ip}:11000'.format(host_ip=config['config']['host_ip']) +os.environ['PWA_GATEWAY_URI'] = 'http://{host_ip}:12000'.format(host_ip=config['config']['host_ip']) +os.environ['PUBLIC_KEYCLOAK_URI'] = 'http://localhost:9080' +os.environ['PUBLIC_PWA_GATEWAY_URI'] = 'http://localhost:12000' + + +# keycloak config +keycloak_realm_id = "wikitolearn-local" +keycloak_realm_username = "admin" +keycloak_realm_password = "admin" +keycloak_base_url = os.environ.get("KEYCLOAK_URI") + "/auth/" +keycloak_realm_base_url = keycloak_base_url + "admin/realms/{}".format(keycloak_realm_id) + +keycloak_auth_obj = None +keycloak_last_login_success = 0 +def keycloak_get_auth_headers(base_headers={}): + global keycloak_auth_obj + global keycloak_base_url + global keycloak_last_login_success + headers = dict(base_headers) + do_login = False + if keycloak_auth_obj == None: + do_login = True + else: + if time.time() - keycloak_last_login_success > (keycloak_auth_obj['expires_in'] - 5): + do_login = True + + if do_login: + auth_endpoint = keycloak_base_url + "realms/master/" + data = {} + data["client_id"] = "admin-cli" + data["username"] = keycloak_realm_username + data["password"] = keycloak_realm_password + data["grant_type"] = "password" + + try: + auth_r = requests.post( + auth_endpoint + "protocol/openid-connect/token", + data=data + ) + auth_r.raise_for_status() + keycloak_auth_obj = auth_r.json() + keycloak_last_login_success = time.time() + except Exception as e: + print("Keycloak Login Exception") + raise e + if keycloak_auth_obj != None and 'access_token' in keycloak_auth_obj: + headers["Authorization"] = "Bearer {}".format(keycloak_auth_obj['access_token']) + else: + pprint(keycloak_auth_obj) + time.sleep(1) + return headers + +if requests.get(keycloak_realm_base_url,headers=keycloak_get_auth_headers()).status_code == 404: + print("create realm") + create_realm = requests.post( + keycloak_base_url + "admin/realms", + json={ + 'enabled': True, + 'id': keycloak_realm_id, + 'realm': keycloak_realm_id + }, + headers=keycloak_get_auth_headers() + ) + create_realm.raise_for_status() + +has_client_frontend = False + +clients_list_request = requests.get(keycloak_realm_base_url + "/clients",headers=keycloak_get_auth_headers()) +for client in clients_list_request.json(): + has_client_frontend = has_client_frontend or (client.get('clientId') == "frontend") + +if not has_client_frontend: + print("create client frontend") + create_frontend_client = requests.post( + keycloak_realm_base_url + "/clients", + json={ + "enabled":True, + "attributes":{}, + "redirectUris":[], + "clientId":"frontend", + "rootUrl":"http://localhost:13000", + "protocol":"openid-connect" + }, + headers=keycloak_get_auth_headers() + ) + create_frontend_client.raise_for_status() + +for user_n in range(0, 10): + keycloak_user = {} + keycloak_user['username'] = "user{}".format(user_n) + keycloak_user['firstName'] = "firstName" + "user{}".format(user_n) + keycloak_user['lastName'] = "lastName" + "user{}".format(user_n) + keycloak_user['email'] = "user{}".format(user_n) + "@localhost" + keycloak_user['credentials'] = [ + { + "type": "password", + "value": "password", + } + ] + keycloak_user["enabled"] = True + keycloak_user["emailVerified"] = True + keycloak_user["attributes"] = { + "mw_id": user_n, + } + + users_endpoint = keycloak_realm_base_url + "/users" + create_user_request = requests.post( + users_endpoint, + json=keycloak_user, + headers=keycloak_get_auth_headers() + ) + try: + create_user_request.raise_for_status() + except Exception as exc: + print(exc) + +courses_index_request = requests.get(os.environ.get("COURSES_BACKEND_URI") + "/v1/courses") +pprint(courses_index_request.json()) diff --git a/bin/wtl-services-build b/bin/wtl-services-build index 6d31dd7..02b2d6b 100755 --- a/bin/wtl-services-build +++ b/bin/wtl-services-build @@ -1,78 +1,78 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import argcomplete, argparse import yaml import subprocess from pprint import pprint WTL_DEV_KIT_PATH = os.environ.get("WTL_DEV_KIT_PATH") WTL_DEV_KIT_REPOS_PATH = os.environ.get("WTL_DEV_KIT_REPOS_PATH") WTL_DEV_KIT_BIN_PATH = os.environ.get("WTL_DEV_KIT_BIN_PATH") config = {} with open(WTL_DEV_KIT_PATH + "/config/config.yml") as fh: config['config'] = yaml.load(fh) if os.path.isfile(WTL_DEV_KIT_PATH + "/config/repositories.yml"): with open(WTL_DEV_KIT_PATH + "/config/repositories.yml") as fh: config['repositories'] = yaml.load(fh) else: with open(WTL_DEV_KIT_PATH + "/config/repositories.example.yml") as fh: config['repositories'] = yaml.load(fh) parser = argparse.ArgumentParser() argcomplete.autocomplete(parser) args = parser.parse_args() os.environ['KEYCLOAK_URI'] = 'http://{host_ip}:9080'.format(host_ip=config['config']['host_ip']) os.environ['COURSES_BACKEND_URI'] = 'http://{host_ip}:10000'.format(host_ip=config['config']['host_ip']) os.environ['CHAPTERS_BACKEND_URI'] = 'http://{host_ip}:10001'.format(host_ip=config['config']['host_ip']) os.environ['PAGES_BACKEND_URI'] = 'http://{host_ip}:10002'.format(host_ip=config['config']['host_ip']) os.environ['COURSE_MIDTIER_URI'] = 'http://{host_ip}:11000'.format(host_ip=config['config']['host_ip']) os.environ['PWA_GATEWAY_URI'] = 'http://{host_ip}:12000'.format(host_ip=config['config']['host_ip']) -os.environ['PUBLIC_KEYCLOAK_URI'] = 'http://{host_ip}:9080'.format(host_ip=config['config']['host_ip']) -os.environ['PUBLIC_PWA_GATEWAY_URI'] = 'http://{host_ip}:12000'.format(host_ip=config['config']['host_ip']) +os.environ['PUBLIC_KEYCLOAK_URI'] = 'http://localhost:9080' +os.environ['PUBLIC_PWA_GATEWAY_URI'] = 'http://localhost:12000' for service_name in [ "shared-services", "courses-backend", "chapters-backend", "pages-backend", "coursessecurity-backend", "course-midtier", "pwa-gateway", "frontend", ]: if service_name in config['repositories']: service_path = WTL_DEV_KIT_REPOS_PATH + "/" + service_name print("Build '{service_name}'".format(service_name=service_name)) if not os.path.isdir(service_path): clone_cmd = [ "git", "clone", config['repositories'][service_name]['git'], service_path ] if 'git_branch' in config['repositories'][service_name]: clone_cmd.append("-b") clone_cmd.append(config['repositories'][service_name]['git_branch']) subprocess.call(clone_cmd) subprocess.call([ "git", "pull", ], cwd=service_path) docker_compose_command = [ "docker-compose", "-f","docker-compose.yml", ] if os.path.isfile(service_path + "/" + "docker-compose-dev-deps.yml"): docker_compose_command.append("-f") docker_compose_command.append("docker-compose-dev-deps.yml") docker_compose_command.append("build") if subprocess.call(docker_compose_command, cwd=service_path, env=os.environ.copy()) != 0: raise Exception("{} failed".format(docker_compose_command)) diff --git a/bin/wtl-services-run b/bin/wtl-services-run index ff782f9..e4cbae8 100755 --- a/bin/wtl-services-run +++ b/bin/wtl-services-run @@ -1,62 +1,62 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import argcomplete, argparse import yaml import subprocess from pprint import pprint WTL_DEV_KIT_PATH = os.environ.get("WTL_DEV_KIT_PATH") WTL_DEV_KIT_REPOS_PATH = os.environ.get("WTL_DEV_KIT_REPOS_PATH") WTL_DEV_KIT_BIN_PATH = os.environ.get("WTL_DEV_KIT_BIN_PATH") config = {} with open(WTL_DEV_KIT_PATH + "/config/config.yml") as fh: config['config'] = yaml.load(fh) if os.path.isfile(WTL_DEV_KIT_PATH + "/config/repositories.yml"): with open(WTL_DEV_KIT_PATH + "/config/repositories.yml") as fh: config['repositories'] = yaml.load(fh) else: with open(WTL_DEV_KIT_PATH + "/config/repositories.example.yml") as fh: config['repositories'] = yaml.load(fh) parser = argparse.ArgumentParser() argcomplete.autocomplete(parser) args = parser.parse_args() os.environ['KEYCLOAK_URI'] = 'http://{host_ip}:9080'.format(host_ip=config['config']['host_ip']) os.environ['COURSES_BACKEND_URI'] = 'http://{host_ip}:10000'.format(host_ip=config['config']['host_ip']) os.environ['CHAPTERS_BACKEND_URI'] = 'http://{host_ip}:10001'.format(host_ip=config['config']['host_ip']) os.environ['PAGES_BACKEND_URI'] = 'http://{host_ip}:10002'.format(host_ip=config['config']['host_ip']) os.environ['COURSE_MIDTIER_URI'] = 'http://{host_ip}:11000'.format(host_ip=config['config']['host_ip']) os.environ['PWA_GATEWAY_URI'] = 'http://{host_ip}:12000'.format(host_ip=config['config']['host_ip']) -os.environ['PUBLIC_KEYCLOAK_URI'] = 'http://{host_ip}:9080'.format(host_ip=config['config']['host_ip']) -os.environ['PUBLIC_PWA_GATEWAY_URI'] = 'http://{host_ip}:12000'.format(host_ip=config['config']['host_ip']) +os.environ['PUBLIC_KEYCLOAK_URI'] = 'http://localhost:9080' +os.environ['PUBLIC_PWA_GATEWAY_URI'] = 'http://localhost:12000' for service_name in [ "shared-services", "courses-backend", "chapters-backend", "pages-backend", "coursessecurity-backend", "course-midtier", "pwa-gateway", "frontend", ]: if service_name in config['repositories']: service_path = WTL_DEV_KIT_REPOS_PATH + "/" + service_name print("Run '{service_name}'".format(service_name=service_name)) docker_compose_command = [ "docker-compose", "-f","docker-compose.yml", ] if os.path.isfile(service_path + "/" + "docker-compose-dev-deps.yml"): docker_compose_command.append("-f") docker_compose_command.append("docker-compose-dev-deps.yml") docker_compose_command.append("up") docker_compose_command.append("-d") subprocess.call(docker_compose_command, cwd=service_path) diff --git a/bin/wtl-services-stop b/bin/wtl-services-stop index 12a3d43..c1158c9 100755 --- a/bin/wtl-services-stop +++ b/bin/wtl-services-stop @@ -1,61 +1,61 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import argcomplete, argparse import yaml import subprocess from pprint import pprint WTL_DEV_KIT_PATH = os.environ.get("WTL_DEV_KIT_PATH") WTL_DEV_KIT_REPOS_PATH = os.environ.get("WTL_DEV_KIT_REPOS_PATH") WTL_DEV_KIT_BIN_PATH = os.environ.get("WTL_DEV_KIT_BIN_PATH") config = {} with open(WTL_DEV_KIT_PATH + "/config/config.yml") as fh: config['config'] = yaml.load(fh) if os.path.isfile(WTL_DEV_KIT_PATH + "/config/repositories.yml"): with open(WTL_DEV_KIT_PATH + "/config/repositories.yml") as fh: config['repositories'] = yaml.load(fh) else: with open(WTL_DEV_KIT_PATH + "/config/repositories.example.yml") as fh: config['repositories'] = yaml.load(fh) parser = argparse.ArgumentParser() argcomplete.autocomplete(parser) args = parser.parse_args() os.environ['KEYCLOAK_URI'] = 'http://{host_ip}:9080'.format(host_ip=config['config']['host_ip']) os.environ['COURSES_BACKEND_URI'] = 'http://{host_ip}:10000'.format(host_ip=config['config']['host_ip']) os.environ['CHAPTERS_BACKEND_URI'] = 'http://{host_ip}:10001'.format(host_ip=config['config']['host_ip']) os.environ['PAGES_BACKEND_URI'] = 'http://{host_ip}:10002'.format(host_ip=config['config']['host_ip']) os.environ['COURSE_MIDTIER_URI'] = 'http://{host_ip}:11000'.format(host_ip=config['config']['host_ip']) os.environ['PWA_GATEWAY_URI'] = 'http://{host_ip}:12000'.format(host_ip=config['config']['host_ip']) -os.environ['PUBLIC_KEYCLOAK_URI'] = 'http://{host_ip}:9080'.format(host_ip=config['config']['host_ip']) -os.environ['PUBLIC_PWA_GATEWAY_URI'] = 'http://{host_ip}:12000'.format(host_ip=config['config']['host_ip']) +os.environ['PUBLIC_KEYCLOAK_URI'] = 'http://localhost:9080' +os.environ['PUBLIC_PWA_GATEWAY_URI'] = 'http://localhost:12000' for service_name in reversed([ "shared-services", "courses-backend", "chapters-backend", "pages-backend", "coursessecurity-backend", "course-midtier", "pwa-gateway", "frontend", ]): if service_name in config['repositories']: service_path = WTL_DEV_KIT_REPOS_PATH + "/" + service_name print("Run '{service_name}'".format(service_name=service_name)) docker_compose_command = [ "docker-compose", "-f","docker-compose.yml", ] if os.path.isfile(service_path + "/" + "docker-compose-dev-deps.yml"): docker_compose_command.append("-f") docker_compose_command.append("docker-compose-dev-deps.yml") docker_compose_command.append("down") subprocess.call(docker_compose_command, cwd=service_path) diff --git a/config/repositories.example.yml b/config/repositories.example.yml index 57473a7..ae2a4e3 100644 --- a/config/repositories.example.yml +++ b/config/repositories.example.yml @@ -1,17 +1,17 @@ chapters-backend: - git: kde:scratch/atundo/wikitolearn-chapters-backend.git + git: kde:wikitolearn-chapters-backend.git course-midtier: - git: kde:scratch/atundo/wikitolearn-course-midtier.git + git: kde:wikitolearn-course-midtier.git courses-backend: - git: kde:scratch/atundo/wikitolearn-courses-backend.git + git: kde:wikitolearn-courses-backend.git coursessecurity-backend: git: kde:scratch/atundo/wikitolearn-coursessecurity-backend.git pages-backend: - git: kde:scratch/atundo/wikitolearn-pages-backend.git + git: kde:wikitolearn-pages-backend.git pwa-gateway: - git: kde:scratch/atundo/wikitolearn-pwa-gateway.git + git: kde:wikitolearn-pwa-gateway.git shared-services: - git: kde:scratch/atundo/wikitolearn-shared-services.git + git: kde:wikitolearn-shared-services.git frontend: - git: kde:scratch/cristianbaldi/wikitolearn-frontend.git + git: kde:wikitolearn-frontend.git git_branch: migration diff --git a/setup-env b/setup-env index d930de6..d4204d4 100755 --- a/setup-env +++ b/setup-env @@ -1,62 +1,62 @@ # Run this script with: # # source ./setup-env unset WTL_DEV_KIT_PATH unset WTL_DEV_KIT_REPOS_PATH unset WTL_DEV_KIT_BIN_PATH -if [ "$0" == "/bin/bash" ] +if [ "$0" == "/bin/bash" ] || [ "$0" == "bash" ] then unset WTL_THIS_SCRIPT_PATH if [[ $(echo -e "$BASH_VERSION\n4.4.0" | sort -V | head -1) != "4.4.0" ]] then echo "Your bash version is too old ($BASH_VERSION)" return 1 &> /dev/null exit 1 fi WTL_THIS_SCRIPT_PATH="$( cd "$(dirname "$BASH_SOURCE")" ; pwd -P )" else - echo "Can't detect a way to setup your shell, sorry" + echo "Can't detect a way to setup your shell ($0), sorry" echo echo "Are you load this file with '. ./setup-env' or 'source ./setup-env'?" echo return 1 &> /dev/null exit 1 fi if [[ -z "$WTL_THIS_SCRIPT_PATH" ]] then echo "Missing \$WTL_THIS_SCRIPT_PATH" echo "Error in the setup process, exit" return 1 &> /dev/null exit 1 fi WTL_DEV_KIT_PATH=$WTL_THIS_SCRIPT_PATH WTL_DEV_KIT_REPOS_PATH=$WTL_DEV_KIT_PATH"/repositories" if test ! -d $WTL_DEV_KIT_REPOS_PATH then echo "Missing directory '$WTL_DEV_KIT_REPOS_PATH'" return 1 fi WTL_DEV_KIT_BIN_PATH=$WTL_DEV_KIT_PATH"/bin" if test ! -d $WTL_DEV_KIT_BIN_PATH then echo "Missing directory '$WTL_DEV_KIT_BIN_PATH'" return 1 fi if ! echo "$PATH" | grep -Eq "(^|:)$WTL_DEV_KIT_BIN_PATH($|:)" then PATH="$PATH:$WTL_DEV_KIT_BIN_PATH" fi export WTL_DEV_KIT_PATH export WTL_DEV_KIT_REPOS_PATH export WTL_DEV_KIT_BIN_PATH eval "$(register-python-argcomplete3 wtl-services-build)" eval "$(register-python-argcomplete3 wtl-services-run)" eval "$(register-python-argcomplete3 wtl-services-stop)"