-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
start.sh
170 lines (143 loc) · 5.36 KB
/
start.sh
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
#!/usr/bin/env bash
# ---------------------------------------------------------------------------- #
# Function Definitions #
# ---------------------------------------------------------------------------- #
start_nginx() {
echo "Starting Nginx service..."
service nginx start
}
execute_script() {
local script_path=$1
local script_msg=$2
if [[ -f ${script_path} ]]; then
echo "${script_msg}"
bash ${script_path}
fi
}
generate_ssh_host_keys() {
if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then
ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -q -N ''
fi
if [ ! -f /etc/ssh/ssh_host_dsa_key ]; then
ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -q -N ''
fi
if [ ! -f /etc/ssh/ssh_host_ecdsa_key ]; then
ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -q -N ''
fi
if [ ! -f /etc/ssh/ssh_host_ed25519_key ]; then
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -q -N ''
fi
}
setup_ssh() {
echo "Setting up SSH..."
mkdir -p ~/.ssh
# Add SSH public key from environment variable to ~/.ssh/authorized_keys
# if the PUBLIC_KEY environment variable is set
if [[ ${PUBLIC_KEY} ]]; then
echo -e "${PUBLIC_KEY}\n" >> ~/.ssh/authorized_keys
fi
chmod 700 -R ~/.ssh
# Generate SSH host keys if they don't exist
generate_ssh_host_keys
service ssh start
echo "SSH host keys:"
cat /etc/ssh/*.pub
}
export_env_vars() {
echo "Exporting environment variables..."
printenv | grep -E '^RUNPOD_|^PATH=|^_=' | awk -F = '{ print "export " $1 "=\"" $2 "\"" }' >> /etc/rp_environment
echo 'source /etc/rp_environment' >> ~/.bashrc
}
start_jupyter() {
# Default to not using a password
JUPYTER_PASSWORD=""
# Allow a password to be set by providing the JUPYTER_PASSWORD environment variable
if [[ ${JUPYTER_LAB_PASSWORD} ]]; then
JUPYTER_PASSWORD=${JUPYTER_LAB_PASSWORD}
fi
echo "Starting Jupyter Lab..."
mkdir -p /workspace/logs
cd / && \
nohup jupyter lab --allow-root \
--no-browser \
--port=8888 \
--ip=* \
--FileContentsManager.delete_to_trash=False \
--ContentsManager.allow_hidden=True \
--ServerApp.terminado_settings='{"shell_command":["/bin/bash"]}' \
--ServerApp.token=${JUPYTER_PASSWORD} \
--ServerApp.allow_origin=* \
--ServerApp.preferred_dir=/workspace &> /workspace/logs/jupyter.log &
echo "Jupyter Lab started"
}
start_runpod_uploader() {
echo "Starting RunPod Uploader..."
nohup /usr/local/bin/runpod-uploader &> /workspace/logs/runpod-uploader.log &
echo "RunPod Uploader started"
}
configure_filezilla() {
# Only proceed if there is a public IP
if [[ ! -z "${RUNPOD_PUBLIC_IP}" ]]; then
# Server information
hostname="${RUNPOD_PUBLIC_IP}"
port="${RUNPOD_TCP_PORT_22}"
# Generate a random password
password=$(openssl rand -base64 12)
# Set the password for the root user
echo "root:${password}" | chpasswd
# Update SSH configuration
ssh_config="/etc/ssh/sshd_config"
# Enable PasswordAuthentication
grep -q "^PasswordAuthentication" ${ssh_config} && \
sed -i "s/^PasswordAuthentication.*/PasswordAuthentication yes/" ${ssh_config} || \
echo "PasswordAuthentication yes" >> ${ssh_config}
# Enable PermitRootLogin
grep -q "^PermitRootLogin" ${ssh_config} && \
sed -i "s/^PermitRootLogin.*/PermitRootLogin yes/" ${ssh_config} || \
echo "PermitRootLogin yes" >> ${ssh_config}
# Restart the SSH service
service ssh restart
# Create FileZilla XML configuration for SFTP
filezilla_config_file="/workspace/filezilla_sftp_config.xml"
cat > ${filezilla_config_file} << EOF
<?xml version="1.0" encoding="UTF-8"?>
<FileZilla3 version="3.66.1" platform="linux">
<Servers>
<Server>
<Host>${hostname}</Host>
<Port>${port}</Port>
<Protocol>1</Protocol> <!-- 1 for SFTP -->
<Type>0</Type>
<User>root</User>
<Pass encoding="base64">$(echo -n ${password} | base64)</Pass>
<Logontype>1</Logontype> <!-- 1 for Normal logon type -->
<EncodingType>Auto</EncodingType>
<BypassProxy>0</BypassProxy>
<Name>Generated Server</Name>
<RemoteDir>/workspace</RemoteDir>
<SyncBrowsing>0</SyncBrowsing>
<DirectoryComparison>0</DirectoryComparison>
<!-- Additional settings can be added here -->
</Server>
</Servers>
</FileZilla3>
EOF
echo "FileZilla SFTP configuration file created at: ${filezilla_config_file}"
else
echo "RUNPOD_PUBLIC_IP is not set. Skipping FileZilla configuration."
fi
}
# ---------------------------------------------------------------------------- #
# Main Program #
# ---------------------------------------------------------------------------- #
echo "Container Started, configuration in progress..."
start_nginx
setup_ssh
start_jupyter
start_runpod_uploader
execute_script "/pre_start.sh" "Running pre-start script..."
configure_filezilla
export_env_vars
execute_script "/post_start.sh" "Running post-start script..."
echo "Container is READY!"
sleep infinity