Spaces:
Runtime error
Runtime error
MohammedAlakhras
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,150 @@
|
|
1 |
-
import os
|
2 |
-
import subprocess
|
3 |
-
import logging
|
4 |
-
import threading
|
5 |
|
6 |
-
# Set up logging
|
7 |
-
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', handlers=[logging.StreamHandler()])
|
8 |
|
9 |
-
def setup_chrome_remote_desktop():
|
10 |
-
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
|
21 |
-
|
22 |
-
|
23 |
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
# Run the Chrome Remote Desktop setup in a separate thread
|
27 |
-
crd_thread = threading.Thread(target=setup_chrome_remote_desktop)
|
28 |
-
crd_thread.start()
|
29 |
|
30 |
-
# Keep the container running
|
31 |
-
subprocess.run(["tail", "-f", "/dev/null"])
|
|
|
1 |
+
# import os
|
2 |
+
# import subprocess
|
3 |
+
# import logging
|
4 |
+
# import threading
|
5 |
|
6 |
+
# # Set up logging
|
7 |
+
# logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', handlers=[logging.StreamHandler()])
|
8 |
|
9 |
+
# def setup_chrome_remote_desktop():
|
10 |
+
# logging.info("Starting Chrome Remote Desktop setup...")
|
11 |
|
12 |
+
# # Execute the command with the DISPLAY environment variable set
|
13 |
+
# command = [
|
14 |
+
# "bash", "-c",
|
15 |
+
# 'DISPLAY= /opt/google/chrome-remote-desktop/start-host --code="4/0AQlEd8yLHti1p8P_Oi7Df1oKGRZ50qCxAKWyMj0VGxBzCfRlnmSPa52PcQ51CCdItLHobQ" '
|
16 |
+
# '--redirect-url="https://remotedesktop.google.com/_/oauthredirect" '
|
17 |
+
# '--name=$(hostname) '
|
18 |
+
# '--pin=123456'
|
19 |
+
# ]
|
20 |
|
21 |
+
# # Run the command
|
22 |
+
# subprocess.run(command, shell=False)
|
23 |
|
24 |
+
# logging.info("Chrome Remote Desktop setup complete.")
|
25 |
+
|
26 |
+
# # Run the Chrome Remote Desktop setup in a separate thread
|
27 |
+
# crd_thread = threading.Thread(target=setup_chrome_remote_desktop)
|
28 |
+
# crd_thread.start()
|
29 |
+
|
30 |
+
# # Keep the container running
|
31 |
+
# subprocess.run(["tail", "-f", "/dev/null"])
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
|
38 |
+
#@title **Create User**
|
39 |
+
#@markdown Enter Username and Password
|
40 |
+
|
41 |
+
import os
|
42 |
+
|
43 |
+
username = "mohammed" #@param {type:"string"}
|
44 |
+
password = "root" #@param {type:"string"}
|
45 |
+
|
46 |
+
print("Creating User and Setting it up")
|
47 |
+
|
48 |
+
# Creation of user
|
49 |
+
os.system(f"useradd -m {username}")
|
50 |
+
|
51 |
+
# Add user to sudo group
|
52 |
+
os.system(f"adduser {username} sudo")
|
53 |
+
|
54 |
+
# Set password of user to 'root'
|
55 |
+
os.system(f"echo '{username}:{password}' | sudo chpasswd")
|
56 |
+
|
57 |
+
# Change default shell from sh to bash
|
58 |
+
os.system("sed -i 's/\/bin\/sh/\/bin\/bash/g' /etc/passwd")
|
59 |
+
|
60 |
+
print(f"User created and configured having username `{username}` and password `{password}`")
|
61 |
+
|
62 |
+
|
63 |
+
#@title **RDP**
|
64 |
+
#@markdown It takes 4-5 minutes for installation
|
65 |
+
|
66 |
+
import os
|
67 |
+
import subprocess
|
68 |
+
|
69 |
+
#@markdown Visit http://remotedesktop.google.com/headless and copy the command after Authentication
|
70 |
+
|
71 |
+
CRP = "DISPLAY= /opt/google/chrome-remote-desktop/start-host --code="4/0AQlEd8yLHti1p8P_Oi7Df1oKGRZ50qCxAKWyMj0VGxBzCfRlnmSPa52PcQ51CCdItLHobQ" --redirect-url="https://remotedesktop.google.com/_/oauthredirect" --name=$(hostname)" #@param {type:"string"}
|
72 |
+
|
73 |
+
#@markdown Enter a Pin (more or equal to 6 digits)
|
74 |
+
Pin = 123456 #@param {type: "integer"}
|
75 |
+
|
76 |
+
#@markdown Autostart Notebook in RDP
|
77 |
+
Autostart = True #@param {type: "boolean"}
|
78 |
+
|
79 |
+
|
80 |
+
class CRD:
|
81 |
+
def __init__(self, user):
|
82 |
+
os.system("apt update")
|
83 |
+
self.installCRD()
|
84 |
+
self.installDesktopEnvironment()
|
85 |
+
self.installGoogleChorme()
|
86 |
+
self.finish(user)
|
87 |
+
print("\nRDP created succesfully move to https://remotedesktop.google.com/access")
|
88 |
+
|
89 |
+
@staticmethod
|
90 |
+
def installCRD():
|
91 |
+
print("Installing Chrome Remote Desktop")
|
92 |
+
subprocess.run(['wget', 'https://dl.google.com/linux/direct/chrome-remote-desktop_current_amd64.deb'], stdout=subprocess.PIPE)
|
93 |
+
subprocess.run(['dpkg', '--install', 'chrome-remote-desktop_current_amd64.deb'], stdout=subprocess.PIPE)
|
94 |
+
subprocess.run(['apt', 'install', '--assume-yes', '--fix-broken'], stdout=subprocess.PIPE)
|
95 |
+
|
96 |
+
@staticmethod
|
97 |
+
def installDesktopEnvironment():
|
98 |
+
print("Installing Desktop Environment")
|
99 |
+
os.system("export DEBIAN_FRONTEND=noninteractive")
|
100 |
+
os.system("apt install --assume-yes xfce4 desktop-base xfce4-terminal")
|
101 |
+
os.system("bash -c 'echo \"exec /etc/X11/Xsession /usr/bin/xfce4-session\" > /etc/chrome-remote-desktop-session'")
|
102 |
+
os.system("apt remove --assume-yes gnome-terminal")
|
103 |
+
os.system("apt install --assume-yes xscreensaver")
|
104 |
+
os.system("systemctl disable lightdm.service")
|
105 |
+
|
106 |
+
@staticmethod
|
107 |
+
def installGoogleChorme():
|
108 |
+
print("Installing Google Chrome")
|
109 |
+
subprocess.run(["wget", "https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb"], stdout=subprocess.PIPE)
|
110 |
+
subprocess.run(["dpkg", "--install", "google-chrome-stable_current_amd64.deb"], stdout=subprocess.PIPE)
|
111 |
+
subprocess.run(['apt', 'install', '--assume-yes', '--fix-broken'], stdout=subprocess.PIPE)
|
112 |
+
|
113 |
+
@staticmethod
|
114 |
+
def finish(user):
|
115 |
+
print("Finalizing")
|
116 |
+
if Autostart:
|
117 |
+
os.makedirs(f"/home/{user}/.config/autostart", exist_ok=True)
|
118 |
+
link = "https://colab.research.google.com/github/PradyumnaKrishna/Colab-Hacks/blob/master/Colab%20RDP/Colab%20RDP.ipynb"
|
119 |
+
colab_autostart = """[Desktop Entry]
|
120 |
+
Type=Application
|
121 |
+
Name=Colab
|
122 |
+
Exec=sh -c "sensible-browser {}"
|
123 |
+
Icon=
|
124 |
+
Comment=Open a predefined notebook at session signin.
|
125 |
+
X-GNOME-Autostart-enabled=true""".format(link)
|
126 |
+
with open(f"/home/{user}/.config/autostart/colab.desktop", "w") as f:
|
127 |
+
f.write(colab_autostart)
|
128 |
+
os.system(f"chmod +x /home/{user}/.config/autostart/colab.desktop")
|
129 |
+
os.system(f"chown {user}:{user} /home/{user}/.config")
|
130 |
+
|
131 |
+
os.system(f"adduser {user} chrome-remote-desktop")
|
132 |
+
command = f"{CRP} --pin={Pin}"
|
133 |
+
os.system(f"su - {user} -c '{command}'")
|
134 |
+
os.system("service chrome-remote-desktop start")
|
135 |
+
|
136 |
+
|
137 |
+
print("Finished Succesfully")
|
138 |
+
|
139 |
+
|
140 |
+
try:
|
141 |
+
if CRP == "":
|
142 |
+
print("Please enter authcode from the given link")
|
143 |
+
elif len(str(Pin)) < 6:
|
144 |
+
print("Enter a pin more or equal to 6 digits")
|
145 |
+
else:
|
146 |
+
CRD(username)
|
147 |
+
except NameError as e:
|
148 |
+
print("'username' variable not found, Create a user first")
|
149 |
|
|
|
|
|
|
|
150 |
|
|
|
|