-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_tunnel_service.sh
More file actions
244 lines (214 loc) · 6.68 KB
/
Copy pathsetup_tunnel_service.sh
File metadata and controls
244 lines (214 loc) · 6.68 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/bin/bash
# VS Code Tunnel Service Setup Script
# Sets up VS Code tunnel as a systemd service on remote instance
set -e
INSTANCE_NAME="${1:-stable-dev}"
# Function to show help
show_help() {
cat << 'EOF'
VS Code Tunnel Service Setup Script
USAGE:
./setup_tunnel_service.sh [INSTANCE_NAME] [OPTIONS]
ARGUMENTS:
INSTANCE_NAME SSH host name from ~/.ssh/config (default: stable-dev)
OPTIONS:
-h, --help Show this help message
-s, --status Check tunnel service status only
-r, --restart Restart existing tunnel service
-u, --url Get tunnel URL from service logs
EXAMPLES:
./setup_tunnel_service.sh stable-dev
Set up tunnel service on stable-dev instance
./setup_tunnel_service.sh my-server
Set up tunnel service on my-server instance
./setup_tunnel_service.sh stable-dev --status
Check if tunnel service is running
./setup_tunnel_service.sh stable-dev --url
Get the tunnel URL
WHAT IT DOES:
• Creates systemd service file for VS Code tunnel
• Enables auto-start on boot
• Starts the tunnel service
• Shows tunnel URL and management commands
REQUIREMENTS:
• SSH access to the instance
• sudo access on remote instance
• VS Code already installed on instance
EOF
}
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_help
exit 0
;;
-s|--status)
ACTION="status"
shift
;;
-r|--restart)
ACTION="restart"
shift
;;
-u|--url)
ACTION="url"
shift
;;
*)
if [[ "$1" =~ ^- ]]; then
echo "❌ Unknown option: $1"
echo "Use --help for usage information"
exit 1
else
INSTANCE_NAME="$1"
fi
shift
;;
esac
done
# Function to check SSH connectivity
check_ssh() {
if ! ssh -o ConnectTimeout=5 -o BatchMode=yes "$INSTANCE_NAME" "echo 'SSH OK'" >/dev/null 2>&1; then
echo "❌ Cannot connect to instance: $INSTANCE_NAME"
echo "Check your SSH config and instance status"
exit 1
fi
}
# Function to check service status
check_status() {
echo "🔍 Checking VS Code tunnel service status on $INSTANCE_NAME..."
ssh "$INSTANCE_NAME" "
if systemctl is-active --quiet vscode-tunnel.service; then
echo '✅ VS Code tunnel service is running'
systemctl status vscode-tunnel.service --no-pager -l
else
echo '❌ VS Code tunnel service is not running'
echo 'Status:'
systemctl status vscode-tunnel.service --no-pager -l || true
fi
"
}
# Function to restart service
restart_service() {
echo "🔄 Restarting VS Code tunnel service on $INSTANCE_NAME..."
ssh "$INSTANCE_NAME" "
sudo systemctl restart vscode-tunnel.service
echo '✅ Service restarted'
sleep 3
systemctl status vscode-tunnel.service --no-pager -l
"
}
# Function to get tunnel URL
get_url() {
echo "🔗 Getting tunnel URL from $INSTANCE_NAME..."
ssh "$INSTANCE_NAME" "
echo 'Checking service logs for tunnel URL...'
sudo journalctl -u vscode-tunnel.service --no-pager | grep -i 'open this link' | tail -1 || echo 'No tunnel URL found in logs yet'
echo
echo 'Recent service status:'
systemctl status vscode-tunnel.service --no-pager -l
"
}
# Function to setup tunnel service
setup_service() {
echo "🚀 Setting up VS Code tunnel service on $INSTANCE_NAME..."
# Check if VS Code is installed
echo " Checking if VS Code is installed..."
ssh "$INSTANCE_NAME" "
if ! command -v code >/dev/null 2>&1; then
echo '❌ VS Code is not installed. Installing via snap...'
sudo snap install code --classic
else
echo '✅ VS Code is already installed'
fi
"
# Create and install systemd service
echo " Creating systemd service..."
ssh "$INSTANCE_NAME" "
echo 'Creating vscode-tunnel.service...'
sudo tee /etc/systemd/system/vscode-tunnel.service > /dev/null << 'EOF'
[Unit]
Description=VS Code Tunnel
After=network.target
[Service]
Type=simple
User=ubuntu
WorkingDirectory=/home/ubuntu
ExecStart=/snap/bin/code tunnel --accept-server-license-terms
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
EOF
echo '✅ Service file created'
"
# Enable and start service
echo " Enabling and starting service..."
ssh "$INSTANCE_NAME" "
sudo systemctl daemon-reload
sudo systemctl enable vscode-tunnel.service
sudo systemctl start vscode-tunnel.service
echo '✅ Service enabled and started'
"
# Wait a moment and check status
echo " Checking service status..."
sleep 5
ssh "$INSTANCE_NAME" "
if systemctl is-active --quiet vscode-tunnel.service; then
echo '✅ VS Code tunnel service is running successfully!'
echo
echo 'Service status:'
systemctl status vscode-tunnel.service --no-pager -l
else
echo '❌ Service failed to start'
echo 'Error details:'
sudo journalctl -u vscode-tunnel.service --no-pager -l
exit 1
fi
"
# Show management commands
echo ""
echo "🎉 VS Code tunnel service setup complete!"
echo ""
echo "📋 Management Commands:"
echo " Check status: ./setup_tunnel_service.sh $INSTANCE_NAME --status"
echo " Get URL: ./setup_tunnel_service.sh $INSTANCE_NAME --url"
echo " Restart: ./setup_tunnel_service.sh $INSTANCE_NAME --restart"
echo ""
echo "🔧 Direct SSH Commands:"
echo " Status: ssh $INSTANCE_NAME 'sudo systemctl status vscode-tunnel.service'"
echo " Logs: ssh $INSTANCE_NAME 'sudo journalctl -u vscode-tunnel.service -f'"
echo " Restart: ssh $INSTANCE_NAME 'sudo systemctl restart vscode-tunnel.service'"
echo ""
echo "⏰ Getting tunnel URL (may take 30-60 seconds)..."
sleep 10
get_url
}
# Main execution
echo "🔧 VS Code Tunnel Service Manager"
echo "Instance: $INSTANCE_NAME"
echo "======================================="
# Check SSH connectivity first
check_ssh
# Execute based on action
case "${ACTION:-setup}" in
status)
check_status
;;
restart)
restart_service
;;
url)
get_url
;;
setup)
setup_service
;;
*)
echo "❌ Unknown action: $ACTION"
exit 1
;;
esac