|
| 1 | +# Flex Net Sim Backend API - Development and Deployment Instructions |
| 2 | + |
| 3 | +This document is for developers who want to set up, develop, and deploy the Flex Net Sim Backend API. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +* Python 3.9 or higher |
| 8 | +* g++ (GNU C++ Compiler) |
| 9 | +* Docker (for containerization) |
| 10 | +* Google Cloud SDK (for deployment to Google Cloud Run) |
| 11 | +* A Google Cloud Project with Cloud Run API enabled. |
| 12 | + |
| 13 | +## Getting Started (Local Development) |
| 14 | + |
| 15 | +1. **Clone the repository:** |
| 16 | + ```bash |
| 17 | + git clone [https://github.com/MirkoZETA/FlexNetSim-API.git](https://github.com/MirkoZETA/FlexNetSim-API.git) |
| 18 | + cd flask-simulation-backend |
| 19 | + ``` |
| 20 | + |
| 21 | +2. **Create a Python virtual environment (recommended):** |
| 22 | + ```bash |
| 23 | + python3 -m venv .venv |
| 24 | + ``` |
| 25 | + |
| 26 | + On Linux/macOS: |
| 27 | + ```bash |
| 28 | + source .venv/bin/activate |
| 29 | + ``` |
| 30 | + |
| 31 | + On Windows |
| 32 | + ```bash |
| 33 | + .venv\Scripts\activate |
| 34 | + ``` |
| 35 | +3. **Install Python dependencies:** |
| 36 | + ```bash |
| 37 | + pip install -r requirements.txt |
| 38 | + ``` |
| 39 | + |
| 40 | +4. **Run the Flask backend:** |
| 41 | + ```bash |
| 42 | + flask --app backend run |
| 43 | + ``` |
| 44 | + The backend will be accessible at `http://127.0.0.1:5000`. |
| 45 | + |
| 46 | +5. **Test**: |
| 47 | + ```bash |
| 48 | + curl http://127.0.0.1:5000/help |
| 49 | + ``` |
| 50 | + or |
| 51 | + ```bash |
| 52 | + curl -X POST -H "Content-Type: application/json" -d '{}' http://127.0.0.1:5000/run_simulation |
| 53 | + ``` |
| 54 | + |
| 55 | +### Dockerization |
| 56 | + |
| 57 | +To build the Docker image: |
| 58 | + |
| 59 | +```bash |
| 60 | +docker build -t fns-api . |
| 61 | +``` |
| 62 | + |
| 63 | +## GCloud Deployment Configuration |
| 64 | + |
| 65 | +As a prerequisite is mandatory to apply the following steps to the GCloud project for the docker image build and upload to artifacts, and also service account creation and IAM policy binding: |
| 66 | + |
| 67 | +[GCloud Configuration Video Tutorial](https://www.youtube.com/watch?v=KQUKDiBz3IA) |
| 68 | + |
| 69 | +This video will guide you through the necessary configurations in the Google Cloud Console to prepare your project for Cloud Run deployments using GitHub Actions. |
| 70 | + |
| 71 | +**Key Reminders from the Video & for Successful Deployment:** |
| 72 | + |
| 73 | +* **Keep Track of Docker Image Name, Project ID:** Note these down during the video configuration, as you will need them in subsequent steps and for your GitHub Actions workflow. |
| 74 | +* **Service Account Email:** Ensure you create a Service Account as shown in the video and securely download and store the JSON key file. You'll also need to note the Service Account's email address. |
| 75 | + |
| 76 | +**Post-Configuration Steps (using `gcloud` and `cloud-run`):** |
| 77 | + |
| 78 | +1. Activate necessary apis: |
| 79 | + |
| 80 | + * `gcloud services enable run.googleapis.com` |
| 81 | + |
| 82 | +2. Create cloud run service: |
| 83 | + * Navigate to the Cloud Run section in your Google Cloud Console. |
| 84 | + * Create a **Service**. |
| 85 | + * Select *Use an inline editor to create a function*. |
| 86 | + * Set a name, in this case *fns-api-cloud-run* will be used. |
| 87 | + * Note down the Endpoint URL, because it will the defaul url for the API. |
| 88 | + * Select the authetification preferences. |
| 89 | + * Create. |
| 90 | + |
| 91 | +3. Update access of service accounts to cloud run resources: |
| 92 | + |
| 93 | + ```bash |
| 94 | + gcloud projects add-iam-policy-binding "<YOUR-GOOGLE-PROJECT-ID>" --member="serviceAccount:<SERVICE_ACCOUNT_EMAIL>" --role="roles/run.admin" |
| 95 | + ``` |
| 96 | + |
| 97 | + and |
| 98 | + |
| 99 | + ```bash |
| 100 | + gcloud iam service-accounts add-iam-policy-binding "<YOUR_PROJECT_NUMBER>-compute@developer.gserviceaccount.com" --member="serviceAccount:<SERVICE_ACCOUNT_EMAIL>" --role="roles/iam.serviceAccountActor" |
| 101 | + ``` |
| 102 | + |
| 103 | +4. **Test the Deployed API (using `curl`):** |
| 104 | + |
| 105 | + Use the `curl` command with the `ENDPOINT-URL` you obtained from the previous steps to test your deployed API. Replace `YOUR-ENDPOINT-URL` with the actual `ENDPOINT-URL`. |
| 106 | + |
| 107 | + ```bash |
| 108 | + curl -X POST -H "Content-Type: application/json" -d '{"algorithm": "FirstFit", "networkType": 1, "bitrate": "fixed-rate"}' <YOUR-ENDPOINT-URL>/run_simulation |
| 109 | + ``` |
| 110 | + |
| 111 | + Remember that depending on the authetification preferences you might need to authetificate to send request to the Endpoint just created. |
| 112 | + |
| 113 | +**Remember**: These GCloud configurations, along with the repository's `gke-cd.yml` GitHub Actions workflow and correctly configured GitHub secrets, are essential for successful automated deployment of your FlexNetSim-API application to Google Cloud Run. |
0 commit comments