From 2891e68ba26df7068f3c3ade4653765e45308e67 Mon Sep 17 00:00:00 2001 From: peefy Date: Fri, 23 Aug 2024 16:25:08 +0800 Subject: [PATCH 1/9] chore: bump kcl lib version to 0.10.0-beta.1 Signed-off-by: peefy --- package-lock.json | 11 ++++------- package.json | 4 ++-- requirements.txt | 2 +- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2e7ed13..a699753 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,12 @@ { - "name": "web", + "name": "pluto-kcl-playground", + "version": "0.10.0", "lockfileVersion": 3, "requires": true, "packages": { "": { - "license": "MIT", - "dependencies": { - "escape-html": "^1.0.3", - "i": "^0.3.7", - "lz-string": "^1.4.4" - }, + "name": "kcl-playground", + "version": "0.10.0", "devDependencies": { "codemirror": "^5.39.2", "copy-webpack-plugin": "^8.1.1", diff --git a/package.json b/package.json index 947b2f3..89a4ed3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "kcl-playground", - "version": "0.9.0", + "name": "pluto-kcl-playground", + "version": "0.10.0", "scripts": { "dev": "pluto run", "test:dev": "pluto test --sim", diff --git a/requirements.txt b/requirements.txt index 7904428..17b15e4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ pluto_client -kcl_lib==0.9.0 +kcl_lib==0.10.0b2 From c15eb1c4cc54545a13744b40eecb3039cc7e2264 Mon Sep 17 00:00:00 2001 From: peefy Date: Fri, 23 Aug 2024 16:40:15 +0800 Subject: [PATCH 2/9] ci: update test and release CI Signed-off-by: peefy --- .github/workflows/ci.yaml | 27 +++++++++++++++++++++ .github/workflows/release.yaml | 44 ++++++++++++++++++++++++++++++++++ Dockerfile | 4 ++++ 3 files changed, 75 insertions(+) create mode 100644 .github/workflows/ci.yaml create mode 100644 .github/workflows/release.yaml create mode 100644 Dockerfile diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..02bb880 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,27 @@ +name: CI +on: + pull_request: + branches: + - main + push: + branches: + - main + +jobs: + test: + name: Test + runs-on: ubuntu-latest + container: + image: plutolang/pluto + steps: + - name: Check out code + uses: actions/checkout@v4 + - name: Install dependencies + run: python -m pip install -r requirements + - name: Setup Docker Buildx + uses: docker/setup-buildx-action@v3 + with: + version: v0.11.2 + install: true + - name: Test application + run: pluto run diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..c60353e --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,44 @@ +# Reference from: +# https://goreleaser.com/ci/actions/ +name: Release +on: + push: + tags: + - "v*" +permissions: + contents: write +jobs: + image: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Setup Docker Buildx + uses: docker/setup-buildx-action@v3 + with: + version: v0.11.2 + install: true + + # <--- Login, build and push image to Docker Hub ---> + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v5 + with: + images: kcllang/pluto-kcl-playground + - name: Build and push Docker image + uses: docker/build-push-action@v6 + with: + context: . + platforms: linux/amd64 + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a1f479c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,4 @@ +FROM plutolang/pluto +WORKDIR / +COPY . . +EXPOSE 9443 From 071acfcd2ac664910c681b908b5984dc162e8a77 Mon Sep 17 00:00:00 2001 From: peefy Date: Fri, 23 Aug 2024 16:45:27 +0800 Subject: [PATCH 3/9] chore: update dockerfile Signed-off-by: peefy --- Dockerfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Dockerfile b/Dockerfile index a1f479c..3c6bf67 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,6 @@ FROM plutolang/pluto WORKDIR / COPY . . +RUN python -m pip install -r requirements +CMD ["pluto", "run"] EXPOSE 9443 From d67f20177e2c93273cef2898175278575adbaacf Mon Sep 17 00:00:00 2001 From: peefy Date: Fri, 23 Aug 2024 16:50:21 +0800 Subject: [PATCH 4/9] fix: python command Signed-off-by: peefy --- .github/workflows/ci.yaml | 2 +- Dockerfile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 02bb880..3445678 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -17,7 +17,7 @@ jobs: - name: Check out code uses: actions/checkout@v4 - name: Install dependencies - run: python -m pip install -r requirements + run: python3 -m pip install -r requirements - name: Setup Docker Buildx uses: docker/setup-buildx-action@v3 with: diff --git a/Dockerfile b/Dockerfile index 3c6bf67..83c1ed4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM plutolang/pluto WORKDIR / COPY . . -RUN python -m pip install -r requirements +RUN python3 -m pip install -r requirements CMD ["pluto", "run"] EXPOSE 9443 From 1079ae185e33fa178d8f496c530b7a8b5d3b32be Mon Sep 17 00:00:00 2001 From: peefy Date: Fri, 23 Aug 2024 17:30:19 +0800 Subject: [PATCH 5/9] fix: install dependecies in CI and dockerfile Signed-off-by: peefy --- web/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/package.json b/web/package.json index 0c0a226..d3e8a07 100644 --- a/web/package.json +++ b/web/package.json @@ -2,7 +2,7 @@ "private": true, "scripts": { "start": "webpack serve", - "build": "env NODE_ENV=production webpack-cli", + "build": "webpack-cli", "format": "prettier src/**/*.js src/**/*.css *.config.js --write", "deploy": "gh-pages -d dist", "predeploy": "yarn build" From e9607b7d0efe5fc4810481f26237115d44669ae4 Mon Sep 17 00:00:00 2001 From: peefy Date: Fri, 23 Aug 2024 17:34:40 +0800 Subject: [PATCH 6/9] test: web build pluto unit test in CI Signed-off-by: peefy --- .github/workflows/ci.yaml | 16 +++++----- .github/workflows/release.yaml | 2 -- .gitignore | 3 +- Dockerfile | 4 ++- README.md | 2 +- scripts/test.sh | 57 ++++++++++++++++++++++++++++++++++ scripts/test2.sh | 1 + 7 files changed, 72 insertions(+), 13 deletions(-) create mode 100755 scripts/test.sh create mode 100755 scripts/test2.sh diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 3445678..9f63223 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -17,11 +17,11 @@ jobs: - name: Check out code uses: actions/checkout@v4 - name: Install dependencies - run: python3 -m pip install -r requirements - - name: Setup Docker Buildx - uses: docker/setup-buildx-action@v3 - with: - version: v0.11.2 - install: true - - name: Test application - run: pluto run + run: | + npm install + python3 -m pip install -r requirements.txt + python3 -m pip install pytest + - name: Build web + run: cd web && npm install && npm run build + - name: Test + run: ./scripts/test2.sh diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index c60353e..f811667 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -1,5 +1,3 @@ -# Reference from: -# https://goreleaser.com/ci/actions/ name: Release on: push: diff --git a/.gitignore b/.gitignore index 4e5d0d1..c011859 100644 --- a/.gitignore +++ b/.gitignore @@ -22,4 +22,5 @@ zz_* node_modules # pluto -.pluto/**/* \ No newline at end of file +.pluto/**/* +__pycache__ diff --git a/Dockerfile b/Dockerfile index 83c1ed4..a37d269 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,8 @@ FROM plutolang/pluto WORKDIR / COPY . . -RUN python3 -m pip install -r requirements +RUN npm install +RUN cd web && npm run build +RUN python3 -m pip install -U -r ./requirements.txt CMD ["pluto", "run"] EXPOSE 9443 diff --git a/README.md b/README.md index 8e8f69e..1fff8ec 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ ## Quick Start ```shell -python -m pip install -r requirements +python3 -m pip install -U -r ./requirements.txt npm install npm install -g pluto make diff --git a/scripts/test.sh b/scripts/test.sh new file mode 100755 index 0000000..33ac20f --- /dev/null +++ b/scripts/test.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +# Start pluto locally +TMP_OUTPUT_FILE=/tmp/pluto_output.log +pluto run >$TMP_OUTPUT_FILE 2>&1 & +PID1=$! + +# Wait for Pluto to start the project completely +while :; do + if grep -q "Successfully applied!" $TMP_OUTPUT_FILE; then + echo "The project has been successfully started." + break + else + echo "Waiting for Pluto to start the project..." + sleep 1 + fi +done + +# Get the project name from package.json +PROJECT_NAME=$(grep '"name":' package.json | awk -F '"' '{print $4}') +echo "Project name: $PROJECT_NAME" + +# Set environment variables +PORT=$(lsof -Pan -p $PID1 -i | grep LISTEN | awk '{print $9}' | sed 's/.*://') +export PLUTO_PROJECT_NAME=$PROJECT_NAME +export PLUTO_STACK_NAME=local_run +export PLUTO_PLATFORM_TYPE=Simulator +export PLUTO_SIMULATOR_URL=http://localhost:$PORT + +# Run tests +print_separator() { + local message=$1 + local message_len=${#message} + + local width=$(tput cols) + local separator=$(printf '=%.0s' $(seq 1 $(((width - message_len - 4) / 2)))) + local bold=$(tput bold) + + printf "\033[34m${bold}${separator}= %s =${separator}\033[0m\n" "$message" +} + +# Output Pluto logs, which might contain useful information +tail -f $TMP_OUTPUT_FILE -n 0 & +PID2=$! + +# Execute tests in the app directory +print_separator "Executing test files in the app directory" +python3 -m pytest -s -q --no-header app + +# Execute tests within the app/main.py file +print_separator "Executing tests within the app/main.py file" +python3 -m pytest -s -q --no-header app/main.py + +# Cleanup +kill $PID1 +wait $PID1 +kill $PID2 diff --git a/scripts/test2.sh b/scripts/test2.sh new file mode 100755 index 0000000..8147c5f --- /dev/null +++ b/scripts/test2.sh @@ -0,0 +1 @@ +pluto run \ No newline at end of file From 9b7d5ac6c3e8e6ecdb866c638999d28963460949 Mon Sep 17 00:00:00 2001 From: peefy Date: Fri, 23 Aug 2024 17:42:05 +0800 Subject: [PATCH 7/9] test: web build pluto unit test in CI Signed-off-by: peefy --- .github/workflows/ci.yaml | 2 +- Dockerfile | 6 +++++- scripts/test.sh | 3 ++- scripts/test2.sh | 1 - 4 files changed, 8 insertions(+), 4 deletions(-) delete mode 100755 scripts/test2.sh diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 9f63223..7b6748e 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -24,4 +24,4 @@ jobs: - name: Build web run: cd web && npm install && npm run build - name: Test - run: ./scripts/test2.sh + run: ./scripts/test.sh diff --git a/Dockerfile b/Dockerfile index a37d269..f776a04 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,12 @@ FROM plutolang/pluto WORKDIR / COPY . . +# Install Pluto and dev dependencies RUN npm install -RUN cd web && npm run build +# Build frontend web application +RUN cd web && npm install && npm run build +# Install backend dependencies RUN python3 -m pip install -U -r ./requirements.txt +# Run CMD ["pluto", "run"] EXPOSE 9443 diff --git a/scripts/test.sh b/scripts/test.sh index 33ac20f..f91f572 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -49,7 +49,8 @@ python3 -m pytest -s -q --no-header app # Execute tests within the app/main.py file print_separator "Executing tests within the app/main.py file" -python3 -m pytest -s -q --no-header app/main.py +# TODO: pluto Website resource mock bugfix +# python3 -m pytest -s -q --no-header app/main.py # Cleanup kill $PID1 diff --git a/scripts/test2.sh b/scripts/test2.sh deleted file mode 100755 index 8147c5f..0000000 --- a/scripts/test2.sh +++ /dev/null @@ -1 +0,0 @@ -pluto run \ No newline at end of file From a7d1159295a9862c121eecc2dc2f0f27ae8ac1bf Mon Sep 17 00:00:00 2001 From: peefy Date: Fri, 23 Aug 2024 17:47:44 +0800 Subject: [PATCH 8/9] ci: add lsof depedencies Signed-off-by: peefy --- .github/workflows/ci.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 7b6748e..1eabe60 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -18,6 +18,8 @@ jobs: uses: actions/checkout@v4 - name: Install dependencies run: | + apt update + apt install lsof npm install python3 -m pip install -r requirements.txt python3 -m pip install pytest From 033cab8f662af630519a609ccb6b588c27c6175d Mon Sep 17 00:00:00 2001 From: peefy Date: Fri, 23 Aug 2024 19:17:47 +0800 Subject: [PATCH 9/9] ci: add unit test for the main app Signed-off-by: peefy --- Dockerfile | 4 +++- app/main.py | 15 ++++++++++++--- app/main_test.py | 10 ++++++++++ pluto.yml | 5 ++++- requirements.txt | 1 + scripts/test.sh | 5 ++--- 6 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 app/main_test.py diff --git a/Dockerfile b/Dockerfile index f776a04..60c550b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,4 +9,6 @@ RUN cd web && npm install && npm run build RUN python3 -m pip install -U -r ./requirements.txt # Run CMD ["pluto", "run"] -EXPOSE 9443 +EXPOSE 8080 +EXPOSE 8000 +EXPOSE 8001 diff --git a/app/main.py b/app/main.py index 213cc33..ff1e3d4 100644 --- a/app/main.py +++ b/app/main.py @@ -1,13 +1,22 @@ import json import re import tempfile -from pluto_client import Website, Router, HttpRequest, HttpResponse +from pluto_client import ( + Website, + WebsiteOptions, + Router, + RouterOptions, + HttpRequest, + HttpResponse, +) import kcl_lib.api as kcl_api color_pattern = re.compile(r"\x1b\[[0-9;]+m") api = kcl_api.API() -router = Router("router") -website = Website("./web/dist", "kcl-playground") +router = Router("router", RouterOptions(sim_host="0.0.0.0", sim_port="8000")) +website = Website( + "./web/dist", "kcl-playground", WebsiteOptions(sim_host="0.0.0.0", sim_port="8001") +) website.addEnv("BACKEND_URL", router.url()) diff --git a/app/main_test.py b/app/main_test.py new file mode 100644 index 0000000..4269f04 --- /dev/null +++ b/app/main_test.py @@ -0,0 +1,10 @@ +import requests +from main import website, router + + +def test_website(): + assert "8000" in website.url() + + +def test_router(): + assert "8001" in router.url() diff --git a/pluto.yml b/pluto.yml index beb8abf..5a3258e 100644 --- a/pluto.yml +++ b/pluto.yml @@ -1,4 +1,3 @@ -configs: {} current: aws language: python stacks: @@ -6,3 +5,7 @@ stacks: name: aws platformType: AWS provisionType: Pulumi + +configs: + simulator: + address: "0.0.0.0:8080" diff --git a/requirements.txt b/requirements.txt index 17b15e4..0a7c905 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ pluto_client kcl_lib==0.10.0b2 +requests diff --git a/scripts/test.sh b/scripts/test.sh index f91f572..37e4c25 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -21,7 +21,7 @@ PROJECT_NAME=$(grep '"name":' package.json | awk -F '"' '{print $4}') echo "Project name: $PROJECT_NAME" # Set environment variables -PORT=$(lsof -Pan -p $PID1 -i | grep LISTEN | awk '{print $9}' | sed 's/.*://') +PORT=8080 export PLUTO_PROJECT_NAME=$PROJECT_NAME export PLUTO_STACK_NAME=local_run export PLUTO_PLATFORM_TYPE=Simulator @@ -49,8 +49,7 @@ python3 -m pytest -s -q --no-header app # Execute tests within the app/main.py file print_separator "Executing tests within the app/main.py file" -# TODO: pluto Website resource mock bugfix -# python3 -m pytest -s -q --no-header app/main.py +python3 -m pytest -s -q --no-header app/main.py # Cleanup kill $PID1