forked from andrewbrimusu/ec2_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvs_code_setup.sh
More file actions
151 lines (128 loc) · 5.34 KB
/
vs_code_setup.sh
File metadata and controls
151 lines (128 loc) · 5.34 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
#!/bin/bash
# Step 1: Update system packages
echo ""
echo "Updating system packages..."
sudo apt update && sudo apt upgrade -y
# Install necessary dependencies
echo ""
echo "Installing necessary dependencies..."
sudo apt install -y unzip curl wget git openssl
# Step 2: Create the /var/app directory
echo ""
echo "Creating /var/app directory for storing VS Code Server files..."
sudo mkdir -p /var/app
# Step 3: Download the latest code-server release
echo ""
echo "Downloading the latest version of VS Code Server..."
latest_version=$(curl -s https://api.github.com/repos/coder/code-server/releases/latest | grep "browser_download_url.*linux-amd64.tar.gz" | cut -d '"' -f 4)
wget -O code-server-latest-linux-amd64.tar.gz $latest_version
# Extract and install code-server to /var/app
echo ""
echo "Extracting and installing VS Code Server into /var/app..."
tar -xvzf code-server-latest-linux-amd64.tar.gz
sudo mv code-server-*-linux-amd64 /var/app/code-server
sudo ln -s /var/app/code-server/bin/code-server /usr/local/bin/code-server
# Clean up the downloaded tar.gz file to save space
echo ""
echo "Cleaning up temporary installation files..."
rm -rf code-server-latest-linux-amd64.tar.gz
# Verify the installation of code-server
echo ""
echo "Verifying the VS Code Server installation..."
code-server --version
# Step 4: Generate self-signed SSL certificates
echo ""
echo "Generating self-signed SSL certificates..."
mkdir -p ~/.config/code-server
openssl req -newkey rsa:2048 -nodes -keyout ~/.config/code-server/selfsigned.key -x509 -days 365 -out ~/.config/code-server/selfsigned.crt -subj "/C=US/ST=State/L=City/O=Organization/OU=Unit/CN=localhost"
# Step 5: Create a configuration file for VS Code Server
echo ""
echo "Creating a configuration file for VS Code Server..."
cat > ~/.config/code-server/config.yaml << EOF
bind-addr: 0.0.0.0:443
auth: password
password: changeme!
cert: ~/.config/code-server/selfsigned.crt
cert-key: ~/.config/code-server/selfsigned.key
EOF
# Step 6: Create a script to start VS Code Server automatically
echo ""
echo "Creating a script to manage the VS Code Server startup..."
cat > ~/startup.sh << 'EOF'
#!/bin/bash
# Check if VS Code Server is already running
if ps -ef | grep -v grep | grep code-server > /dev/null
then
echo "VS Code Server is already running."
else
echo "Starting VS Code Server on port 443..."
sudo code-server --config /home/ubuntu/.config/code-server/config.yaml --bind-addr 0.0.0.0:443 --cert /home/ubuntu/.config/code-server/selfsigned.crt --cert-key /home/ubuntu/.config/code-server/selfsigned.key /home/ubuntu > ~/code-server.log 2>&1 &
echo "VS Code Server started on port 443."
fi
EOF
# Make the startup.sh script executable
echo ""
echo "Making the startup script executable..."
chmod +x ~/startup.sh
# Step 7: Automatically run the startup script when the user logs in
echo ""
echo "Adding the startup script to .bashrc for automatic execution..."
if ! grep -Fxq "~/startup.sh" /home/ubuntu/.bashrc; then
echo "~/startup.sh" >> /home/ubuntu/.bashrc
fi
# Run the startup script to start the VS Code Server immediately
echo ""
echo "Starting the VS Code Server..."
~/startup.sh
# Step 8: Retrieve the system's private and public IP addresses
echo ""
echo "Retrieving the private and public IP addresses of the system..."
PRIVATE_IP=$(hostname -I | awk '{print $1}')
PUBLIC_IP=$(curl -s http://checkip.amazonaws.com/)
# Export the IP addresses as environment variables
echo ""
echo "Exporting the private and public IP addresses as environment variables..."
echo "export HOST_PRIVATE_IP=$PRIVATE_IP" >> ~/.bashrc
echo "export HOST_PUBLIC_IP=$PUBLIC_IP" >> ~/.bashrc
# Source .bashrc to apply the changes immediately
source ~/.bashrc
# Display the IP addresses for reference
echo ""
echo "Private IP Address: $HOST_PRIVATE_IP"
echo "Public IP Address: $HOST_PUBLIC_IP"
# Step 9: Check if the default password is still in use
echo ""
echo "Checking if the password is still set to the default..."
if grep -q "password: changeme!" ~/.config/code-server/config.yaml; then
echo ""
echo "WARNING: The password is still set to 'changeme!'."
echo "Please update the password in the following file:"
echo "~/.config/code-server/config.yaml"
echo "Restart the VS Code Server after making changes."
fi
# Step 10: Optimize system limits for file watching
echo ""
echo "Increasing the number of file watchers for better performance..."
sudo bash -c 'echo "fs.inotify.max_user_watches=524288" > /etc/sysctl.d/99-sysctl.conf'
echo ""
echo "Applying the updated file watcher configuration..."
sudo sysctl -p /etc/sysctl.d/99-sysctl.conf
# Verify the configuration change
echo ""
echo "Verifying the file watcher configuration..."
CURRENT_VALUE=$(sysctl fs.inotify.max_user_watches | awk '{print $3}')
if [ "$CURRENT_VALUE" -eq 524288 ]; then
echo "File watcher limit successfully updated to $CURRENT_VALUE."
else
echo "Failed to update file watcher limit. Current value is $CURRENT_VALUE."
fi
# Final Step: Provide the user with the access link for VS Code Server
echo ""
echo "Setup completed successfully."
echo "You can manually start the server using: ~/startup.sh"
if [ -z "$HOST_PUBLIC_IP" ]; then
echo "Public IP not found. Ensure the instance has a public IP assigned."
else
echo "Access your VS Code Server at: https://$HOST_PUBLIC_IP"
fi
echo "Note: The default password is 'changeme!' and should be changed for better security."