-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstaller.py
More file actions
186 lines (159 loc) · 5.92 KB
/
installer.py
File metadata and controls
186 lines (159 loc) · 5.92 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# Deprecated
import os
import subprocess
import tarfile
import tkinter as tk
from tkinter import messagebox
import paramiko
def get_args_gui():
root = tk.Tk()
root.title("SpiBerry Installer")
root.geometry("300x360") # Adjusted for hardware config area
root.resizable(False, False)
entries = {}
labels = {
"remote_host": "Remote Host",
"remote_user": "Remote User",
"remote_password": "Password",
"remote_dir": "Remote Directory",
}
defaults = {
"remote_user": "pi",
"remote_host": "raspberrypi",
"remote_password": "",
"remote_dir": "",
}
for idx, key in enumerate(labels):
tk.Label(root, text=labels[key]).grid(row=idx, column=0, padx=10, pady=8, sticky="w")
if key == "remote_password":
entry = tk.Entry(root, width=25, show="*")
else:
entry = tk.Entry(root, width=25)
entry.insert(0, defaults[key])
entry.grid(row=idx, column=1, padx=10, pady=8)
entries[key] = entry
# --- Hardware Config Area ---
hardware_label = tk.Label(root, text="Hardware Config", font=("Arial", 10, "bold"))
hardware_label.grid(row=len(labels), column=0, columnspan=2, pady=(10, 0))
hw_fields = [
("r_pin", "R pin", "0"),
("b_pin", "B pin", "11"),
("g_pin", "G pin", "9"),
("button_pin", "Button pin", "17"),
]
def validate_pin(P):
return P.isdigit() and len(P) <= 2 or P == ""
vcmd = (root.register(validate_pin), '%P')
for i, (key, label, default) in enumerate(hw_fields):
tk.Label(root, text=label).grid(row=len(labels)+1+i, column=0, padx=10, pady=5, sticky="w")
entry = tk.Entry(root, width=5, validate="key", validatecommand=vcmd)
entry.insert(0, default)
entry.grid(row=len(labels)+1+i, column=1, padx=10, pady=5, sticky="w")
entries[key] = entry
result = {}
def submit():
nonlocal result
result = {k: v.get() for k, v in entries.items()}
root.quit()
root.destroy()
btn = tk.Button(root, text="Start Installation", command=submit)
btn.grid(row=len(labels)+1+len(hw_fields), column=0, columnspan=2, pady=10)
root.mainloop()
return result
args = get_args_gui()
if not args:
raise SystemExit(0)
remote_user = args["remote_user"]
remote_host = args["remote_host"]
remote_password = args["remote_password"]
remote_dir = f"/home/{remote_user}/"+args["remote_dir"]
requirements_file = "requirements.txt"
# --- Main logic ---
if not os.path.exists("dependencies.tar.gz"):
# This code exists to install the dependencies but using the dependencies archive in the repo is strongly encouraged
os.makedirs("dependencies", exist_ok=True)
try:
subprocess.run([
"pip", "download", "-r", requirements_file, "-d", "./dependencies","--platform", "manylinux2014_aarch64", "--only-binary", ":all:"
], check=True)
except subprocess.CalledProcessError as e:
messagebox.showerror("Error", "pip download failed.")
if os.path.isdir("dependencies"):
if os.name == 'nt':
subprocess.run(["rmdir", "/S", "/Q", "dependencies"], shell=True)
else:
subprocess.run(["rm", "-rf", "dependencies"])
raise SystemExit(1)
with tarfile.open("dependencies.tar.gz", "w:gz") as tar:
tar.add("dependencies", arcname="dependencies")
if os.name == 'nt':
subprocess.run(["rmdir", "/S", "/Q", "dependencies"], shell=True, check=True)
else:
subprocess.run(["rm", "-rf", "dependencies"], check=True)
# --- Paramiko SSH Setup ---
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(remote_host, username=remote_user, password=remote_password)
except Exception as e:
messagebox.showerror("Error", f"SSH connection failed: {e}")
raise SystemExit(1)
# mkdir -p REMOTE_DIR with SSH
try:
stdin, stdout, stderr = ssh.exec_command(f"mkdir -p {remote_dir}")
exit_status = stdout.channel.recv_exit_status()
if exit_status != 0:
raise Exception(stderr.read().decode())
except Exception as e:
messagebox.showerror("Error", f"SSH mkdir failed: {e}")
ssh.close()
raise SystemExit(1)
# Step 4: Send archive to remote machine using SFTP
try:
sftp = ssh.open_sftp()
sftp.put("dependencies.tar.gz", f"{remote_dir}/dependencies.tar.gz")
sftp.put("SpiBerryEngine.py", f"{remote_dir}/SpiBerryEngine.py")
sftp.close()
except Exception as e:
messagebox.showerror("Error", f"SFTP failed: {e}")
ssh.close()
raise SystemExit(1)
# Step 5: Run extraction and install commands via SSH
ssh_commands = (
f"mkdir -p {remote_dir} && "
f"cd {remote_dir} && "
"python3 -m venv venv && "
"source venv/bin/activate && "
"tar zxvf dependencies.tar.gz && "
"cd dependencies && "
"pip install * -f ./ --no-index"
)
run_sh=f"""#!/bin/bash
cd {remote_dir}
source venv/bin/activate
python3 SpiBerryEngine.py --red {args['r_pin']} --green {args['g_pin']} --blue {args['b_pin']} --button {args['button_pin'] }
"""
# Message box: Installation in progress...
messagebox.showinfo("Info", "Installation in progress. This may take a few minutes...")
try:
stdin, stdout, stderr = ssh.exec_command(ssh_commands)
exit_status = stdout.channel.recv_exit_status()
if exit_status != 0:
raise Exception(stderr.read().decode())
# Send run_sh as a file and make it runnable
try:
sftp = ssh.open_sftp()
run_sh_path = f"/home/{remote_user}/run.sh"
with sftp.file(run_sh_path, "w") as f:
f.write(run_sh)
sftp.chmod(run_sh_path, 0o755)
sftp.close()
except Exception as e:
messagebox.showerror("Error", f"Failed to send run.sh: {e}")
ssh.close()
raise SystemExit(1)
except Exception as e:
messagebox.showerror("Error", f"SSH command failed: {e}")
ssh.close()
raise SystemExit(1)
ssh.close()