-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.py
executable file
·117 lines (93 loc) · 3.48 KB
/
deploy.py
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
#!/usr/bin/env python
# pip3 install python-dotenv requests simplejson
# Adapted from https://github.com/syepes/Hubitat/blob/master/deploy.py
try:
import argparse
import http.cookiejar
import os
import pathlib
import requests
# dotenv
from dotenv import find_dotenv, load_dotenv
except ModuleNotFoundError as e:
print(
"ModuleNotFoundError exception while attempting to import the needed modules: "
+ str(e)
)
exit(99)
def update(s, type, f, id):
print("> Processing {} {}".format(type, id))
response = s.get(url="{}/{}/ajax/code".format(he_url, type), params={"id": id})
# print(response.text)
# Check loging session
if (
"X-Frame-Options" in response.headers
and response.headers["X-Frame-Options"] == "DENY"
):
print(
"Your HE Login Session has expired or been reseted, delete the file: .creds/cookie-jar.txt"
)
exit(1)
if response.json()["status"] != "success":
print("\tFailed downloading")
return None
version = response.json()["version"]
print("\tCurrent version: " + str(version))
print("\tUploading driver")
sourceContents = f.read()
response = s.post(
url="{}/{}/ajax/update".format(he_url, type),
data={"id": id, "version": version, "source": sourceContents},
)
# print(response.text)
if response.json()["status"] == "success":
print("\tSuccessfully uploaded")
elif response.json()["status"] == "error":
print("\tFailed uploading: " + response.json()["errorMessage"])
return None
else:
print("\tFailed uploading: " + response.json())
return None
def he_login(path):
credentialStorageFolderPath = pathlib.Path(path, ".creds")
cookieJarFilePath = pathlib.Path(credentialStorageFolderPath, "cookie-jar.txt")
# print("str(cookieJarFilePath.resolve()): " + str(cookieJarFilePath.resolve()))
session = requests.Session()
cookieJarFilePath.resolve().parent.mkdir(parents=True, exist_ok=True)
session.cookies = http.cookiejar.MozillaCookieJar(
filename=str(cookieJarFilePath.resolve())
)
# Ensure that the cookie jar file exists and contains a working cookie to authenticate
# into the Hubitat web interface
if os.path.isfile(session.cookies.filename):
session.cookies.load(ignore_discard=True)
else:
# Collect username and password from the user
print("Hubitat username: ")
hubitatUsername = input()
print("Hubitat password: ")
hubitatPassword = input()
print("Entered " + hubitatUsername + " and " + hubitatPassword)
session.post(
he_url + "/login",
data={
"username": hubitatUsername,
"password": hubitatPassword,
"submit": "Login",
},
)
session.cookies.save(ignore_discard=True)
return session
parser = argparse.ArgumentParser()
parser.add_argument("type", help="Type of asset updated", choices=["app", "driver"])
parser.add_argument("file", help="File to upload", type=argparse.FileType("r"))
parser.add_argument("id", help="ID on HE")
args = parser.parse_args()
load_dotenv(find_dotenv(), verbose=True)
he_url = os.getenv("HE_URL")
if he_url is None:
print("HE_URL is not defined in the .env file")
exit(99)
print("Connecting to: " + he_url)
session = he_login(os.path.dirname(os.path.realpath(__file__)))
update(session, args.type, args.file, args.id)