diff --git a/five-minutes-to-feature-flags/assets/scripts/intro_foreground.sh b/five-minutes-to-feature-flags/assets/scripts/intro_foreground.sh index e8e87a5..c03d986 100644 --- a/five-minutes-to-feature-flags/assets/scripts/intro_foreground.sh +++ b/five-minutes-to-feature-flags/assets/scripts/intro_foreground.sh @@ -1,4 +1,6 @@ # DEBUG_VERSION=2 + +set -e # ----------------------------------- # APT Update # ----------------------------------- @@ -21,8 +23,11 @@ source ~/.bashrc # ----------------------------------- # Installing Node # ----------------------------------- -curl -fsSL https://deb.nodesource.com/setup_19.x | sudo -E bash - &&\ -apt install -y nodejs < /dev/null +# curl -fsSL https://deb.nodesource.com/setup_19.x | sudo -E bash - &&\ +# apt install -y nodejs < /dev/null +curl -fsSL https://deb.nodesource.com/setup_23.x | sudo -E bash - &&\ +apt-get install -y nodejs < /dev/null +node -v # ----------------------------------- diff --git a/flagd-demo/assets/docker-compose.yaml b/flagd-demo/assets/docker-compose.yaml new file mode 100644 index 0000000..aded345 --- /dev/null +++ b/flagd-demo/assets/docker-compose.yaml @@ -0,0 +1,34 @@ +networks: + gitea-net: + driver: bridge + +services: + server: + image: docker.gitea.com/gitea:1.23.8 + container_name: gitea + restart: always + environment: + # configuration of the app.ini, the proper GITEA__group__VALUE keyword can be found: + # https://docs.gitea.com/administration/config-cheat-sheet + # the group is the value in () for no app.ini configuration + - USER_UID=1000 + - USER_GID=1000 + - GITEA_DEFAULT__RUN_USER=git + - GITEA__database__DB_TYPE=sqlite3 + - GITEA__server__ROOT_URL=http://0.0.0.0:3000/ + - GITEA__server__START_SSH_SERVER=false + - GITEA__server__DISABLE_SSH=true + - GITEA__security__INSTALL_LOCK=true + - GITEA__repository__ENABLE_PUSH_CREATE_USE=true + - GITEA__repository__DEFAULT_PUSH_CREATE_PRIVATE=true + + networks: + - gitea-net + volumes: + - gitea-data:/data + - gitea-data:/etc/gitea + ports: + - "3000:3000" + +volumes: + gitea-data: diff --git a/flagd-demo/assets/scripts/intro_foreground.sh b/flagd-demo/assets/scripts/intro_foreground.sh index 58f6f8a..b5fb22b 100644 --- a/flagd-demo/assets/scripts/intro_foreground.sh +++ b/flagd-demo/assets/scripts/intro_foreground.sh @@ -1,37 +1,119 @@ #!/bin/bash DEBUG_VERSION=13 -GITEA_VERSION=1.19 +GITEA_VERSION=1.23.8 TEA_CLI_VERSION=0.9.2 FLAGD_VERSION=0.11.5 +USER_NAME="openfeature" +PASSWORD="openfeature" +USER_EMAIL=me@faas.com +TOKEN_NAME="tea_token" +REPO_NAME="flags" + +# Wait for Killercoda to set TRAFFIC_HOST1_3000 +while [[ -z "${TRAFFIC_HOST1_3000:-}" ]]; do + echo "Waiting for TRAFFIC_HOST1_3000 to be set by Killercoda..." + sleep 1 +done + +if [[ -n "${TRAFFIC_HOST1_3000:-}" ]]; then + BASE_URL="http://${TRAFFIC_HOST1_3000}" +elif [[ -n "${BASE_URL:-}" ]]; then + # Use passed-in BASE_URL environment variable (e.g. host.docker.internal on Mac/Windows) + # Makes it easier to run locally with mock killercoda env dockerfile + BASE_URL="${BASE_URL}" +else + # Fallback default + BASE_URL="http://gitea" +fi + +echo "Using Gitea URL: $BASE_URL" + +echo "Starting Gitea docker container..." +# Killercoda doesn't use the `docker compose` syntax as of now +if type -P docker-compose &>/dev/null; then + docker-compose -f ~/docker-compose.yaml up -d +else + docker compose -f ~/docker-compose.yaml up -d +fi +# docker compose -f ~/docker-compose.yaml up -d + +# Confirm gitea is functional before making calls +until curl -s "$BASE_URL:3000/api/v1/version" | grep -q "version"; do + echo "Gitea not ready yet..." + sleep 2 +done + +# First gitea is the container and the next is the call +user_list=$(docker exec -u git gitea gitea admin user list 2>/dev/null) + +# Check if openfeature user exists +if ! echo "$user_list" | grep -qw "$USER_NAME"; then + # Using the gitea service started with docker + echo "Creating openfeature admin gitea user..." + docker exec -u git gitea gitea admin user create \ + --username=$USER_NAME \ + --password=$PASSWORD \ + --email=$USER_EMAIL \ + --must-change-password=false +else + echo "User already exists. Continuing..." +fi + +echo "Checking for existing token ..." +user_tokens=$(docker exec gitea curl -s -H "Authorization: Basic $(echo -n "$USER_NAME:$USER_PASSWORD" | base64)" \ + "$BASE_URL/api/v1/users/$USER_NAME/tokens") + +# Output the token check into JSON array & looping to get id of tea_token +token_id=$(echo "$user_tokens" | jq -r '.[] | select(.name == $TOKEN_NAME) | .id') > /dev/null + +# When the token ID exists delete to regenerate to adhere to gitea usage +# non-empty && not null +if [ -n "$token_id" ] && [ "$token_id" != "null" ]; then + echo "Deleting existing token..." + docker exec gitea curl -s -X DELETE \ + "$BASE_URL/api/v1/users/$USER_NAME/tokens/$token_id" \ + -H "Authorization: Basic $(echo -n "$USER_NAME:$USER_PASSWORD" | base64)" + echo "Re-generating gitea access token for tea CLI..." +else + echo "No existing tea_token." + echo "Generating gitea access token for tea CLI..." +fi + +# Generate access token for tea CLI set up +docker exec -u git gitea gitea admin user generate-access-token \ + --username=$USER_NAME \ + --token-name=$TOKEN_NAME \ + --scopes=all \ + --raw > /tmp/output.log -# Download and install flagd -wget -O flagd.tar.gz https://github.com/open-feature/flagd/releases/download/flagd%2Fv${FLAGD_VERSION}/flagd_${FLAGD_VERSION}_Linux_x86_64.tar.gz -tar -xf flagd.tar.gz -mv flagd_linux_x86_64 flagd -chmod +x flagd -mv flagd /usr/local/bin - -# Download and install 'gitea' CLI: 'tea' -wget -O tea https://dl.gitea.com/tea/${TEA_CLI_VERSION}/tea-${TEA_CLI_VERSION}-linux-amd64 -chmod +x tea -mv tea /usr/local/bin +ACCESS_TOKEN=$(tail -n 1 /tmp/output.log) -################# -# Install postgresql for Gitea -################### -# Create the file repository configuration: -sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' +if ! type -P tea &> /dev/null; then + echo "Installing tea CLI..." + # Download and install 'gitea' CLI: 'tea' + wget -O tea https://dl.gitea.com/tea/${TEA_CLI_VERSION}/tea-${TEA_CLI_VERSION}-linux-amd64 + chmod +x tea + mv tea /usr/local/bin +fi -# Import the repository signing key: -wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - +# Authenticate the 'tea' CLI +echo "Authenticate tea CLI..." +tea login add \ + --name=local \ + --url="$BASE_URL:3000" \ + --token="$ACCESS_TOKEN" # > /dev/null 2>&1 -# Update the package lists: -sudo apt-get update +# Check if repo 'flags' exists +echo "Checking if repo 'flags' exists..." +repo_exists=$(tea repo list --json | jq -e '.[] | select(.name==$REPO_NAME)' >/dev/null 2>&1 && echo "yes" || echo "no") -# Install the latest version of PostgreSQL. -# If you want a specific version, use 'postgresql-12' or similar instead of 'postgresql': -sudo apt-get -y install postgresql < /dev/null +if [[ "$repo_exists" == "yes" ]]; then + echo "Repo 'flags' already exists. Skipping creation." +else + echo "Creating repo 'flags'..." + tea repo create --login=local --name=$REPO_NAME --branch=main --init=true >/dev/null +fi # Add 'git' user adduser \ @@ -44,121 +126,29 @@ adduser \ git # Configure git for 'ubuntu' and 'git' users -git config --system user.email "me@faas.com" -git config --system user.name "OpenFeature" - -# Download 'gitea' -wget -O gitea https://dl.gitea.com/gitea/${GITEA_VERSION}/gitea-${GITEA_VERSION}-linux-amd64 -chmod +x gitea -mv gitea /usr/local/bin -chown git:git /usr/local/bin/gitea - -# Set up directory structure for 'gitea' -mkdir -p /var/lib/gitea/{custom,data,log} -chown -R git:git /var/lib/gitea/ -chmod -R 750 /var/lib/gitea/ -mkdir /etc/gitea -chown git:git /etc/gitea -chmod 770 /etc/gitea - -# Create systemd service for 'gitea' -# Ref: https://github.com/go-gitea/gitea/blob/main/contrib/systemd/gitea.service -mv ~/gitea.service /etc/systemd/system/gitea.service -# cat < /etc/systemd/system/gitea.service -# [Unit] -# Description=Gitea (Git with a cup of tea) -# After=syslog.target -# After=network.target - -# Wants=postgresql.service -# After=postgresql.service - -# [Service] -# RestartSec=2s -# Type=simple -# User=git -# Group=git -# WorkingDirectory=/var/lib/gitea/ -# ExecStart=/usr/local/bin/gitea web --config /etc/gitea/app.ini -# Restart=always -# Environment=USER=git HOME=/home/git GITEA_WORK_DIR=/var/lib/gitea - -# [Install] -# WantedBy=multi-user.target -# EOF - -mv ~/gitea.app.ini /etc/gitea/app.ini -# cat < /etc/gitea/app.ini -# APP_NAME = "Gitea: Git with a cup of tea" -# RUN_USER = "git" -# [server] -# PROTOCOL = "http" -# DOMAIN = "http://0.0.0.0:3000" -# ROOT_URL = "http://0.0.0.0:3000" -# HTTP_ADDR = "0.0.0.0" -# HTTP_PORT = "3000" -# [database] -# DB_TYPE = "postgres" -# HOST = "0.0.0.0:5432" -# NAME = "giteadb" -# USER = "gitea" -# PASSWD = "gitea" -# [repository] -# ENABLE_PUSH_CREATE_USER = true -# DEFAULT_PUSH_CREATE_PRIVATE = false -# [security] -# INSTALL_LOCK = true -# EOF -chown -R git:git /etc/gitea - -# Set up gitea DB -sudo -u postgres -H -- psql --command "CREATE ROLE gitea WITH LOGIN PASSWORD 'gitea';" > /dev/null 2>&1 -sudo -u postgres -H -- psql --command "CREATE DATABASE giteadb WITH OWNER gitea TEMPLATE template0 ENCODING UTF8 LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';" > /dev/null 2>&1 - -# Start gitea -systemctl start gitea -# Migrate the DB to create all required tables and config -sudo -u git gitea migrate -c=/etc/gitea/app.ini - -# Create a user called 'openfeature' -# With password 'openfeature' -sudo -u git gitea admin user create \ - --username=openfeature \ - --password=openfeature \ - --email=me@faas.com \ - --must-change-password=false \ - -c=/etc/gitea/app.ini - -sudo -u git gitea admin user generate-access-token \ - --username=openfeature \ - --scopes=repo \ - -c=/etc/gitea/app.ini \ - --raw > /tmp/output.log +git config --system user.email $USER_EMAIL +git config --system user.name $USER_NAME -ACCESS_TOKEN=$(tail -n 1 /tmp/output.log) +git clone http://$USER_NAME:$USER_PASSWORD@${BASE_URL#http://}:3000/$USER_NAME/flags -# Wait for Gitea to be available -# Timeout after 2mins -timeout 120 bash -c 'while [[ "$(curl --insecure -s -o /dev/null -w ''%{http_code}'' http://0.0.0.0:3000)" != "200" ]]; do sleep 5; done' +cd $REPO_NAME +wget -O example_flags.flagd.json https://raw.githubusercontent.com/open-feature/flagd/main/samples/example_flags.flagd.json -# Authenticate the 'tea' CLI -tea login add \ - --name=openfeature \ - --user=openfeature \ - --password=openfeature \ - --url=http://0.0.0.0:3000 \ - --token=$ACCESS_TOKEN > /dev/null 2>&1 - -# Create an empty repo called 'flags' -# Clone the template repo -tea repo create --name=flags --branch=main --init=true > /dev/null 2>&1 -git clone http://openfeature:openfeature@0.0.0.0:3000/openfeature/flags -wget -O ~/flags/example_flags.flagd.json https://raw.githubusercontent.com/open-feature/flagd/refs/tags/flagd/v${FLAGD_VERSION}/samples/example_flags.flagd.json -cd ~/flags git config credential.helper cache git add -A -git commit -m "add flags" -git push +git commit -m "seed flags from flagd json" +git push origin main + +if ! type -P flagd &> /dev/null; then + echo "Installing flagd..." + wget -O flagd.tar.gz https://github.com/open-feature/flagd/releases/download/flagd%2Fv${FLAGD_VERSION}/flagd_${FLAGD_VERSION}_Linux_x86_64.tar.gz + tar -xf flagd.tar.gz + mv flagd_linux_x86_64 flagd + chmod +x flagd + mv flagd /usr/local/bin +fi + +echo 🎉 Installation Complete 🎉 Please proceed now... # ---------------------------------------------# # 🎉 Installation Complete 🎉 # diff --git a/flagd-demo/dockerfile b/flagd-demo/dockerfile new file mode 100644 index 0000000..910966d --- /dev/null +++ b/flagd-demo/dockerfile @@ -0,0 +1,26 @@ +FROM ubuntu:noble + +RUN apt-get update \ +&& DEBIAN_FRONTEND=noninteractive \ + apt-get install -y wget git vim sudo ca-certificates curl gnupg lsb-release coreutils jq \ +&& rm -rf /var/lib/apt/lists/* + +# Install docker in docker to mock killercoda's ability to use docker +RUN mkdir -p /etc/apt/keyrings && \ + curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \ + gpg --dearmor -o /etc/apt/keyrings/docker.gpg && \ + echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \ + https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | \ + tee /etc/apt/sources.list.d/docker.list > /dev/null && \ + apt-get update && \ + apt-get install -y docker-ce-cli docker-compose-plugin + +# Add the necessary files needed to run +COPY assets/scripts/intro_foreground.sh /usr/local/bin/intro_foreground.sh +COPY assets/docker-compose.yaml /root/docker-compose.yaml + +# Add executable permission +RUN chmod +x /usr/local/bin/intro_foreground.sh + +# Run the script file +# CMD ["/usr/local/bin/intro_foreground.sh"] \ No newline at end of file diff --git a/flagd-demo/index.json b/flagd-demo/index.json index 084687c..fbd43ea 100644 --- a/flagd-demo/index.json +++ b/flagd-demo/index.json @@ -45,15 +45,9 @@ }, "assets": { "host01": [ - { - "file": "gitea.app.ini", - "chmod": "+x", - "target": "~" - }, - { - "file": "gitea.service", - "chmod": "+x", - "target": "~" + { + "file": "flagd-demo/assets/docker-compose.yaml", + "target": "~/docker-compose.yaml" } ] } @@ -61,7 +55,17 @@ "environment": { "showdashboard": true, "dashboard": "Dashboard", - "uilayout": "terminal" + "uilayout": "terminal", + "ports": [ + { + "port": 3000, + "on": "host01" + }, + { + "port": 8013, + "on": "host01" + } + ] }, "backend": { "imageid": "ubuntu"