Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update docs and do some cleanup #368

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions apps/frontend/frontend.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,6 @@ WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .


# Setup env vars
ARG MONGODB_PORT
ARG MONGODB_USERNAME
ARG MONGODB_PASSWORD
ENV MONGODB_PORT=${MONGODB_PORT}
ENV MONGODB_USERNAME=${MONGODB_USERNAME}
ENV MONGODB_PASSWORD=${MONGODB_PASSWORD}

RUN npm run build

FROM base AS runner
Expand Down
7 changes: 4 additions & 3 deletions apps/frontend/lib/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { getEnvVar } from '../utils/env';

// Adapted from https://github.com/vercel/next.js/tree/canary/examples/with-mongodb

const MONGODB_PORT = getEnvVar('MONGODB_PORT');
const USERNAME = getEnvVar('MONGODB_USERNAME');
const PASSWORD = getEnvVar('MONGODB_PASSWORD');
const MONGODB_PORT = process.env.MONGODB_PORT || '1234';
const USERNAME = process.env.MONGODB_USERNAME || 'user';
const PASSWORD = process.env.MONGODB_PASSWORD || 'pass';

const MONGODB_URI = `mongodb://${USERNAME}:${PASSWORD}@glados-service-mongodb:${MONGODB_PORT}`;
console.log(MONGODB_URI);
const MONGODB_OPTIONS = {};

export const DB_NAME = 'gladosdb';
Expand Down
125 changes: 125 additions & 0 deletions development_scripts/local/setup_local.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import base64
import os
import subprocess
import sys
import pathlib
import threading

def setup(args):
if len(args) == 0:
print("Please provide at least one of the following arguments: frontend, backend, runner, all")
return

if not pathlib.Path().resolve().name == "Monorepo":
print("Please run this script from the root of the Monorepo")
return

print("Setting up local environment")

# SET YOUR DOCKER HUB USERNAME HERE!
docker_hub_username = "YOUR_DOCKER_HUB_USERNAME"

# SET KEYCLOAK INFO HERE!
# Ask Riley how to set this up
keycloak_url = "http://glados-w0.csse.rose-hulman.edu:8080/realms/master"
keycloak_client_id = "YOUR_KEYCLOAK_CLIENT_ID"
keycloak_client_secret = "YOUR_KEYCLOAK_CLIENT_SECRET"

if "frontend" in args:
print("Building and pushing frontend image")
# open a terminal and run the following command
os.system(f"docker build -t {docker_hub_username}/glados-frontend:main -f ./apps/frontend/frontend.Dockerfile ./apps/frontend")
os.system(f"docker push {docker_hub_username}/glados-frontend:main")

if "backend" in args:
print("Building and pushing backend image")
# open a terminal and run the following command
os.system(f"docker build -t {docker_hub_username}/glados-backend:main -f ./apps/backend/backend.Dockerfile ./apps/backend")
os.system(f"docker push {docker_hub_username}/glados-backend:main")

if "runner" in args:
print("Building and pushing runner image")
# open a terminal and run the following command
os.system(f"docker build -t {docker_hub_username}/glados-runner:main -f ./apps/runner/runner.Dockerfile ./apps/runner")
os.system(f"docker push {docker_hub_username}/glados-runner:main")

if "all" in args:
print("Building and pushing all images")
# open a terminal and run the following command
os.system(f"docker build -t {docker_hub_username}/glados-frontend:main -f ./apps/frontend/frontend.Dockerfile ./apps/frontend")
os.system(f"docker push {docker_hub_username}/glados-frontend:main")
os.system(f"docker build -t {docker_hub_username}/glados-backend:main -f ./apps/backend/backend.Dockerfile ./apps/backend")
os.system(f"docker push {docker_hub_username}/glados-backend:main")
os.system(f"docker build -t {docker_hub_username}/glados-runner:main -f ./apps/runner/runner.Dockerfile ./apps/runner")
os.system(f"docker push {docker_hub_username}/glados-runner:main")

print("Setting up kubernetes")

os.system("python3 kubernetes_init\\init.py")

# try to delete the old service
# do this so that the old minikube service dies
os.system("kubectl delete svc glados-frontend")

os.system("kubectl expose deployment glados-frontend --type LoadBalancer --port 80 --target-port 3000")

# in a new thread we need to run the following command
# minikube service glados-frontend --url
# we then need to use that url to setup some secret stuff, and then redeploy while keeping that open

def run_minikube_service():
process = subprocess.Popen(["minikube", "service", "glados-frontend", "--url"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
for line in process.stdout: # type: ignore
url = line.strip()
port = url.split(":")[-1]

# go into the kubernetes_init/secrets folder and then update the auth url line to be
# http://localhost:<port>/api/auth
# update the line with AUTH_URL
# values are base64 encoded
f = open("kubernetes_init/kubernetes_secrets/secret.yaml", "r")
lines = f.readlines()
f.close()

for i in range(len(lines)):
if "AUTH_URL" in lines[i] or "AUTH_REDIRECT_PROXY_URL" in lines[i]:
# base 64 encode the url
b64Url = base64.b64encode(f"http://localhost:{port}/api/auth".encode()).decode()
lines[i] = f" AUTH_URL: {b64Url}\n"
break

if "AUTH_KEYCLOAK_ISSUER" in lines[i]:
b64Url = base64.b64encode(keycloak_url.encode()).decode()
lines[i] = f" AUTH_KEYCLOAK_ISSUER: {b64Url}\n"

if "AUTH_KEYCLOAK_ID" in lines[i]:
b64Id = base64.b64encode(keycloak_client_id.encode()).decode()
lines[i] = f" AUTH_KEYCLOAK_ID: {b64Id}\n"

if "AUTH_KEYCLOAK_SECRET" in lines[i]:
b64Secret = base64.b64encode(keycloak_client_secret.encode()).decode()
lines[i] = f" AUTH_KEYCLOAK_SECRET: {b64Secret}\n"

# update the file
f = open("kubernetes_init/kubernetes_secrets/secret.yaml", "w")
f.writelines(lines)
f.close()

# apply the new secrets
os.system("python3 kubernetes_init\\init.py")

print(f"Frontend is now running at: http://localhost:{port}")

break

thread = threading.Thread(target=run_minikube_service)
thread.start()


print("Setup complete")



if __name__ == "__main__":
args = sys.argv[1:]
setup(args)
33 changes: 31 additions & 2 deletions docs/docs/tutorial/local_testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ Which will show something like

```bash
NAME READY STATUS RESTARTS AGE
glados-backend-687fc6b7ff-dld2p 1/1 Running 0 74s
glados-frontend-5f575b99b7-9q9ml 1/1 Running 0 74s
glados-backend-687fc6b7ff-dld2p 1/1 Running 0 74s
glados-frontend-5f575b99b7-9q9ml 1/1 Running 0 74s
glados-mongodb-0 1/1 Running 1 (2m9s ago) 20d
glados-mongodb-1 1/1 Running 1 (2m9s ago) 20d
glados-mongodb-arbiter-0 1/1 Running 2 (20d ago) 20d
Expand All @@ -220,3 +220,32 @@ This will then show the image information. Make sure this points to your docker
In order to update the runner image, go to the apps/backend folder, and update the image in job-runner.yaml following the steps above.

Now you can use locally built images to run GLADOS!

## Prebuilt Script for Docker Image Management

Due to the complexity of getting Minikube to behave, I have created a python script to run the needed commands for you.

From the root of the Monorepo run the command:

```bash
python3 .\development_scripts\local\setup_local.py <args>
```

You can provide arguments for which elements of the project you would like to build.

Options are: frontend, backend, runner, all

In the python3 file you will need to set a couple of values to make sure that it is setup for your environment. Update those values and run the python script with the pieces you would like to build and push.

Note: You still need to make sure to follow the steps above for changing the image which you are running the cluster from.

After running the python script you will see something like:

```bash
Frontend is now running at: http://localhost:64068
```

Opening that link will bring you to a local version of GLADOS.

!!!Warning
With the local version of GLADOS being HTTP you may have weird networking issues due to the max number of connections to an HTTP/1.1 host. This will be fixed in a later update.
Loading