-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.py
More file actions
145 lines (123 loc) · 5.82 KB
/
Copy pathdeploy.py
File metadata and controls
145 lines (123 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os
import sys
import shutil
import zipfile
import requests
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
ENV_PATH = os.path.join(BASE_DIR, "googleDrive", "env")
def load_env(env_path):
env = {}
if os.path.exists(env_path):
with open(env_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if line and not line.startswith('#'):
parts = line.split('=', 1)
if len(parts) == 2:
env[parts[0].strip()] = parts[1].strip()
return env
ENV = load_env(ENV_PATH)
API_URL = ENV.get("API_GATEWAY_URL", "http://127.0.0.1:8000") # PythonAnywhere URL
API_KEY = ENV.get("API_KEY", "alco_secure_api_key_2026")
def create_zip():
zip_path = os.path.join(BASE_DIR, "erp_setup.zip")
print("[ZIP] Packaging files into erp_setup.zip...")
# List of files/folders to include in deployment package
items = [
"telegram_bot.py",
"init_db.py",
"googleDrive",
"FieldEdit",
"fastapi_gateway",
"Dashboard",
"sales.db",
"step_1_extract_Product_Level_Net_Sales_csv.py",
"step_2_generate_MPO_Target_vs_Achievement_report.py",
"step_3_generate_Zone_Wise_Product_Sales_Report.py",
"step_4_analyze_Zone_Wise_Product_Sales_Report.py",
"manual_single_click_auto_run_no_need__following_steps.py",
"auto_single_click_auto_run_no_need__following_steps.py",
"above_5_box",
"telegram_notifier.py",
"local_gateway_sync.py"
]
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
for item in items:
item_path = os.path.join(BASE_DIR, item)
if not os.path.exists(item_path):
continue
if os.path.isdir(item_path):
for root, dirs, files in os.walk(item_path):
# Skip __pycache__, archives, and All_Depots
if "__pycache__" in root or "Archive" in root or "All_Depots" in root:
continue
for file in files:
file_path = os.path.join(root, file)
arcname = os.path.relpath(file_path, BASE_DIR)
zipf.write(file_path, arcname)
else:
zipf.write(item_path, item)
print(f"[ZIP] Created zip successfully at {zip_path} (Size: {os.path.getsize(zip_path)/(1024*1024):.2f} MB)")
return zip_path
def deploy_to_pythonanywhere():
zip_path = create_zip()
# PythonAnywhere API credentials from env
pa_username = ENV.get("PYTHONANYWHERE_USERNAME", "storealco")
pa_token = ENV.get("PYTHONANYWHERE_API_TOKEN") # Add PYTHONANYWHERE_API_TOKEN in googleDrive/env
if not pa_token:
print("\n[ERROR] PYTHONANYWHERE_API_TOKEN not found in googleDrive/env.")
print("Please configure PYTHONANYWHERE_API_TOKEN=your_token inside googleDrive/env.")
print("Create a token at: PythonAnywhere Account page -> API Token tab.")
return False
headers = {
"Authorization": f"Token {pa_token}"
}
# 1. Upload Zip File using PythonAnywhere Files API
upload_url = f"https://www.pythonanywhere.com/api/v0/user/{pa_username}/files/path/home/{pa_username}/erp_setup.zip"
print(f"\n[UPLOAD] Uploading erp_setup.zip to PythonAnywhere...")
try:
with open(zip_path, "rb") as f:
files = {"content": f}
r = requests.post(upload_url, headers=headers, files=files, timeout=120)
if r.status_code in [200, 201]:
print("[UPLOAD] File uploaded successfully.")
else:
print(f"[ERROR] Upload failed: HTTP {r.status_code} - {r.text}")
return False
# 2. Run remote unzip and pkill/restart telegram bot via Console API
console_create_url = f"https://www.pythonanywhere.com/api/v0/user/{pa_username}/consoles/"
print("\n[CONSOLE] Opening remote PythonAnywhere console to extract & restart bot...")
# Command sequence to extract zip and restart bot
cmd = (
"unzip -o erp_setup.zip && "
"pkill -f telegram_bot.py ; "
"workon erp_env && "
"nohup python telegram_bot.py > bot.log 2>&1 &"
)
console_payload = {
"executable": "bash",
"arguments": f"-c \"{cmd}\""
}
console_res = requests.post(console_create_url, headers=headers, json=console_payload, timeout=30)
if console_res.status_code == 201:
print("[CONSOLE] Remote extraction and bot restart triggered.")
else:
# Fallback warning if console limit reached
print(f"[WARN] Console trigger HTTP {console_res.status_code}. (You may need to manually run: unzip -o erp_setup.zip && pkill -f telegram_bot.py ; workon erp_env && nohup python telegram_bot.py > bot.log 2>&1 &)")
# 3. Reload Web App (FastAPI Gateway)
domain = f"{pa_username}.pythonanywhere.com"
reload_url = f"https://www.pythonanywhere.com/api/v0/user/{pa_username}/webapps/{domain}/reload/"
print(f"\n[RELOAD] Reloading Web App ({domain})...")
webapp_res = requests.post(reload_url, headers=headers, timeout=30)
if webapp_res.status_code == 200:
print("[RELOAD] FastAPI Web App reloaded successfully!")
print("\n[SUCCESS] ERP Deployment completed successfully!")
return True
else:
print(f"[ERROR] Web App reload failed: HTTP {webapp_res.status_code} - {webapp_res.text}")
return False
except Exception as e:
print(f"[ERROR] Deployment connection error: {e}")
return False
if __name__ == "__main__":
deploy_to_pythonanywhere()