fix_generate_docs.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import re
  2. import os
  3. file_path = "generate_docs.py"
  4. with open(file_path, "r", encoding="utf-8", errors="replace") as f:
  5. content = f.read()
  6. # Replace encoding issues in generate_docs.py header as well
  7. content = content.replace("Lange Franois", "Francois Lange")
  8. content = content.replace("Lange FranA ois", "Francois Lange")
  9. # 1. Find and replace Technical_Document.md .env configuration block
  10. old_tech_env = """ MYSQL_ROOT_PASSWORD=your_db_password_here
  11. DB_READER_PASS=your_db_password_here
  12. DB_LOADER_PASS=your_db_password_here
  13. DB_APP_AUTH_PASS=your_db_password_here
  14. MYSQL_ZABBIX_PASSWORD=your_db_password_here
  15. SERVER_HOST=192.168.130.170
  16. SERVER_USER=your_ssh_user
  17. SERVER_PASS=your_db_password_here"""
  18. new_tech_env = """ MYSQL_ROOT_PASSWORD=your_secure_root_password
  19. DB_READER_PASS=your_secure_reader_password
  20. DB_LOADER_PASS=your_secure_loader_password
  21. DB_APP_AUTH_PASS=your_secure_auth_password
  22. MYSQL_ZABBIX_PASSWORD=your_secure_zabbix_password
  23. SERVER_HOST=192.168.130.170
  24. SERVER_USER=your_ssh_user
  25. SERVER_PASS=your_secure_server_password
  26. # ZABBIX & SNMP CREDENTIALS
  27. ZABBIX_USER=Admin
  28. ZABBIX_PASS=zabbix
  29. ZABBIX_SNMP_USER=your_snmp_user
  30. ZABBIX_SNMP_AUTHKEY=your_snmp_authkey
  31. ZABBIX_SNMP_PRIVKEY=your_snmp_privkey
  32. DISCORD_WEBHOOK=https://discord.com/api/webhooks/your_webhook_id
  33. # EMAIL ALERTS CONFIGURATION
  34. EMAIL_USER=your_email@gmail.com
  35. EMAIL_PASS=your_email_app_password
  36. # TAIGA CREDENTIALS
  37. TAIGA_URL=https://192.168.130.161/taiga
  38. TAIGA_USER=your_taiga_user
  39. TAIGA_PASS=your_taiga_password"""
  40. content = content.replace(old_tech_env, new_tech_env)
  41. # 2. Overwrite Installation_Guide.md content block inside generate_docs.py
  42. old_install_guide_content = """ "Installation_Guide.md": \"\"\"# $Id$
  43. # Installation Guide
  44. ## Requirements
  45. - Ubuntu 24.04 LTS (or WSL2)
  46. - Docker & Docker Compose
  47. - 16GB RAM Minimum
  48. ## Deployment Steps
  49. 1. **Clone the Repository**:
  50. - *Online Mode*: `git clone https://git.btshub.lu/lanfr/LocalFoodAI_lanfr144.git`
  51. - *Offline/Disconnected Mode*: Copy the repository files directly to the target environment via SCP or USB storage.
  52. 2. `cd LocalFoodAI_lanfr144`
  53. 3. `chmod +x data_sync.sh backup_db.sh`
  54. 4. **Deploy Stack**:
  55. - For regular production: `docker compose up -d --build`
  56. - For local/offline single-node fallback: `docker compose -f docker-compose_skip.yml up -d`
  57. 5. Navigate to `http://localhost` (or `http://localhost:8502` for direct Streamlit port)
  58. \"\"\","""
  59. new_install_guide_content = """ "Installation_Guide.md": \"\"\"# $Id$
  60. # Local Food AI - Detailed Installation and Deployment Guide
  61. This guide describes how to provision the host hypervisor, install Docker on Ubuntu, clone the repository, check out the correct branch, and launch the application.
  62. ## 1. WSL2 Ubuntu Instance Setup
  63. To create a dedicated WSL2 environment for the application, execute the following command in an Administrator PowerShell window:
  64. ```powershell
  65. wsl --install -d Ubuntu-22.04 --name Dopro1
  66. ```
  67. During initialization, configure the default Unix user and password as prompted:
  68. ```
  69. Create a default Unix user account: lanfr144
  70. New password:
  71. Retype new password:
  72. passwd: password updated successfully
  73. ```
  74. > [!WARNING]
  75. > **WSL Filesystem Mounts**: By default, launching WSL may place you in a Windows filesystem mount (e.g. `/mnt/d/...`). To prevent performance degradation and permission bugs, navigate to your WSL home directory immediately:
  76. ```bash
  77. cd ~
  78. ```
  79. ---
  80. ## 2. Docker & Docker Compose Installation inside WSL Ubuntu
  81. To install Docker directly inside your WSL Ubuntu instance (without Docker Desktop):
  82. ### Step 2.1: Clean Existing Docker Versions
  83. ```bash
  84. sudo apt remove -y docker.io docker-compose docker-compose-v2 docker-doc podman-docker containerd runc
  85. ```
  86. ### Step 2.2: Add Docker's Official GPG Key & Repository
  87. ```bash
  88. sudo apt update
  89. sudo apt install -y ca-certificates curl
  90. sudo install -m 0755 -d /etc/apt/keyrings
  91. sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
  92. sudo chmod a+r /etc/apt/keyrings/docker.asc
  93. sudo tee /etc/apt/sources.list.d/docker.sources <<EOF
  94. Types: deb
  95. URIs: https://download.docker.com/linux/ubuntu
  96. Suites: \\$(. /etc/os-release && echo "\\${UBUNTU_CODENAME:-\\$VERSION_CODENAME}")
  97. Components: stable
  98. Architectures: \\$(dpkg --print-architecture)
  99. Signed-By: /etc/apt/keyrings/docker.asc
  100. EOF
  101. ```
  102. ### Step 2.3: Install Docker Components
  103. ```bash
  104. sudo apt update
  105. sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  106. ```
  107. ### Step 2.4: Start and Enable Docker Daemon
  108. ```bash
  109. sudo systemctl start docker
  110. sudo systemctl enable docker
  111. ```
  112. ### Step 2.5: Add User to the Docker Group
  113. Ensure you can execute Docker commands without `sudo`:
  114. ```bash
  115. grep "^docker:" /etc/group || sudo addgroup docker
  116. sudo usermod -aG docker \\$USER
  117. ```
  118. ### Step 2.6: Reboot the WSL Instance
  119. Execute the command below inside WSL to gracefully reboot the instance:
  120. ```bash
  121. cd /mnt/c/ && cmd.exe /c start "rebooting WSL" cmd /c "timeout 5 && wsl -d \\$WSL_DISTRO_NAME" && wsl.exe --terminate \\$WSL_DISTRO_NAME
  122. ```
  123. Upon reconnecting, verify Docker is running by starting the hello-world container:
  124. ```bash
  125. docker run hello-world
  126. ```
  127. ---
  128. ## 3. Network Configuration & Performance Tuning
  129. ### Step 3.1: Switch to Legacy IPTables
  130. Ubuntu 22.04 uses `nftables` by default. Switch to legacy iptables to ensure Docker network NAT rules match correctly:
  131. ```bash
  132. sudo update-alternatives --config iptables
  133. # Select option 1 (iptables-legacy)
  134. ```
  135. ### Step 3.2: Configure DNS Settings
  136. To ensure reliable package downloads and LLM registry calls:
  137. ```bash
  138. echo "1,\\$ s/^/#/
  139. \\$ a
  140. nameserver 1.1.1.1
  141. .
  142. w
  143. q" | sudo ed /etc/resolv.conf
  144. echo "\\$ a
  145. # Added these 2 lines:
  146. [network]
  147. generateResolvConf = false
  148. .
  149. w
  150. q" | sudo ed /etc/wsl.conf
  151. ```
  152. ---
  153. ## 4. Repository Clones & Branch Governance
  154. There are two repositories configured for this project:
  155. - Production Repository: `https://git.btshub.lu/lanfr/LocalFoodAI_lanfr144.git`
  156. - GitHub Mirror (Clone): `https://github.com/lanfr144/LocalFoodAI_lanfr144`
  157. Clone the primary repository inside your home directory:
  158. ```bash
  159. git clone https://git.btshub.lu/lanfr/LocalFoodAI_lanfr144.git
  160. cd LocalFoodAI_lanfr144
  161. ```
  162. ### Step 4.1: List Available Branches
  163. Inspect both local and remote branches on the server:
  164. ```bash
  165. git branch -a
  166. ```
  167. *(Shows available branches like `remotes/origin/main` or `remotes/origin/dev`)*
  168. ### Step 4.2: Track and Check Out the Right Branch
  169. Select the main production branch and extract it:
  170. ```bash
  171. git checkout main
  172. ```
  173. *(If the repository uses a master branch, replace 'main' with 'master')*
  174. ### Step 4.3: Set Default Branch (Optional)
  175. To set the default tracking branch for your local copy:
  176. ```bash
  177. git remote set-head origin main
  178. ```
  179. ---
  180. ## 5. Launching the App
  181. Ensure the runbooks and sync scripts have executable permissions:
  182. ```bash
  183. chmod +x data_sync.sh backup_db.sh manage_services.sh scripts/manage_models.sh
  184. ```
  185. Follow the standard runbook to initialize credentials and launch services:
  186. ```bash
  187. # 1. Create a local .env file based on step 3 guidelines
  188. # 2. Run the service manager to spin up containers
  189. ./manage_services.sh start
  190. ```
  191. \"\"\",
  192. """
  193. content = content.replace(old_install_guide_content, new_install_guide_content)
  194. with open(file_path, "w", encoding="utf-8") as f:
  195. f.write(content)
  196. print("generate_docs.py successfully updated and encoding sanitized.")