-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
89 lines (74 loc) · 2.53 KB
/
Copy pathapp.py
File metadata and controls
89 lines (74 loc) · 2.53 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
from flask import Flask, render_template, request, redirect, url_for, session
import requests
import os
from dotenv import load_dotenv
import socket
load_dotenv() # Load environment variables from .env file if it exists
app = Flask(__name__)
app.secret_key = os.urandom(24)
DIGITALOCEAN_TOKEN = os.getenv("DIGITALOCEAN_TOKEN")
DROPLET_ID = os.getenv("DROPLET_ID")
DROPLET_IP = os.getenv("DROPLET_IP")
APP_PORT = int(os.getenv("APP_PORT"))
HEADERS = {
"Authorization": f"Bearer {DIGITALOCEAN_TOKEN}",
"Content-Type": "application/json"
}
def fetch_status():
try:
r = requests.get(
f"https://api.digitalocean.com/v2/droplets/{DROPLET_ID}",
headers=HEADERS
)
return r.json()["droplet"]["status"]
except:
return "unknown"
def is_server_responsive(ip, port, timeout=2):
try:
with socket.create_connection((ip, port), timeout=timeout):
return True
except:
return False
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
action = request.form.get("action")
current_status = fetch_status()
if action == "power_on" and current_status != "active":
session['status'] = "starting"
requests.post(
f"https://api.digitalocean.com/v2/droplets/{DROPLET_ID}/actions",
headers=HEADERS,
json={"type": "power_on"}
)
elif action == "power_off" and current_status != "off":
session['status'] = "stopping"
requests.post(
f"https://api.digitalocean.com/v2/droplets/{DROPLET_ID}/actions",
headers=HEADERS,
json={"type": "power_off"}
)
return redirect(url_for("index"))
# Determine what status to show
live_status = fetch_status()
session_status = session.get('status')
if session_status == "starting":
if live_status == "active":
if is_server_responsive(DROPLET_IP, APP_PORT):
session.pop('status')
status = "active"
else:
status = "starting"
else:
status = "starting"
elif session_status == "stopping":
if live_status == "off":
session.pop('status')
status = "off"
else:
status = "stopping"
else:
status = live_status
return render_template("index.html", status=status, DROPLET_IP=DROPLET_IP, APP_PORT=APP_PORT)
if __name__ == "__main__":
app.run(debug=True)