-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelay_creation.sh
307 lines (244 loc) · 8.32 KB
/
relay_creation.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/bin/bash
set -e
# Variables
USER="ubuntu"
CLOUDFLARE_API_TOKEN=""
DOMAIN=""
CLOUDFLARE_ZONE_ID=""
IDENTITY=""
# Function to show usage
usage() {
echo "Usage: $0 --identity=123 --user=ubuntu --cloudflaretoken=API_TOKEN --cloudflarezone= --domain=test.fx.land"
exit 1
}
# Parse named parameters
while [ "$1" != "" ]; do
case $1 in
--identity=*)
IDENTITY="${1#*=}"
;;
--cloudflaretoken=*)
CLOUDFLARE_API_TOKEN="${1#*=}"
;;
--cloudflarezone=*)
CLOUDFLARE_ZONE_ID="${1#*=}"
;;
--user=*)
USER="${1#*=}"
;;
--domain=*)
DOMAIN="${1#*=}"
;;
*)
echo "$1 i not supported"
usage
;;
esac
shift
done
if [ -z "$DOMAIN" ]; then
echo "missing domain parameter. Skipping the domain handling"
else
DOMAIN="functionyard.fula.network"
fi
if [ -z "$USER" ]; then
echo "missing USER parameter."
USER="ubuntu"
fi
if [ -z "$CLOUDFLARE_API_TOKEN" ]; then
echo "missing CLOUDFLARE_API_TOKEN parameter."
fi
if [ -z "$CLOUDFLARE_ZONE_ID" ]; then
echo "missing CLOUDFLARE_ZONE_ID parameter."
fi
if [ -z "$IDENTITY" ]; then
echo "missing identity parameter."
usage
fi
IDENTITY_FILE="/home/${USER}/identity.key"
CONFIG_FILE="/home/${USER}/config.json"
# Function to get the AWS Token
get_aws_token() {
echo $(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600" -s)
}
install_packages() {
sudo apt-get update -qq
sudo apt-get install -y docker.io nginx software-properties-common certbot python3-certbot-nginx
sudo apt-get install -y wget git curl build-essential jq pkg-config libssl-dev protobuf-compiler llvm libclang-dev clang plocate cmake
sudo apt-get install -y g++ libx11-dev libasound2-dev libudev-dev libxkbcommon-x11-0
sudo systemctl start docker
sudo systemctl enable docker
}
# Function to install Go 1.21 from source
install_go() {
echo "Installing go"
# Check if Go is already installed
if ! command -v go &> /dev/null && [ ! -d "/usr/local/go" ]; then
echo "Go is not installed. Installing Go..."
# Download the pre-compiled binary of Go 1.21
sudo wget https://golang.org/dl/go1.21.0.linux-amd64.tar.gz
sudo tar -xvf go1.21.0.linux-amd64.tar.gz
sudo mv go /usr/local
### Set environment variables so the system knows where to find Go
# echo "export GOROOT=/usr/local/go" | sudo tee /etc/profile.d/goenv.sh
# echo "export PATH=\$PATH:\$GOROOT/bin" | sudo tee -a /etc/profile.d/goenv.sh
# source /etc/profile.d/goenv.sh
# sudo ln -s /usr/local/go/bin/go /usr/local/bin/go
echo "export GOROOT=/usr/local/go" >> ~/.profile
echo "export PATH=\$PATH:\$GOROOT/bin" >> ~/.profile
source ~/.profile
else
echo "Go is already installed. Skipping installation."
fi
}
# Function to clone and build repositories
clone_and_build() {
echo "Installing relay"
if [ ! -d "/home/${USER}/go-libp2p-relay-daemon" ] || [ -z "$(ls -A /home/${USER}/go-libp2p-relay-daemon)" ]; then
git clone https://github.com/functionland/go-libp2p-relay-daemon.git /home/${USER}/go-libp2p-relay-daemon
fi
cd /home/${USER}/go-libp2p-relay-daemon
go build -o libp2p-relay-daemon ./cmd/libp2p-relay-daemon
cd ..
}
# Function to set up and extract keys
setup_and_extract_keys() {
echo "setup_and_extract_keys"
if [ ! -f $CONFIG_FILE ]; then
cp /home/${USER}/go-libp2p-relay-daemon/config.json $CONFIG_FILE
fi
if [ ! -f $IDENTITY_FILE ]; then
cd /home/${USER}/go-libp2p-relay-daemon
go run ./cmd/identity --identity="${IDENTITY}"
cp /home/${USER}/go-libp2p-relay-daemon/identity.key $IDENTITY_FILE
cd ..
fi
}
# Function to set up and start relay service
setup_relay_service() {
relay_service_file_path="/etc/systemd/system/relay.service"
echo "Setting up relay service at $relay_service_file_path"
# Check if the file exists and then remove it
if [ -f "$relay_service_file_path" ]; then
sudo systemctl stop relay.service
sudo systemctl disable relay.service
sudo rm "$relay_service_file_path"
sudo systemctl daemon-reload
echo "Removed $relay_service_file_path."
else
echo "$relay_service_file_path does not exist."
fi
# Create the service file using the provided path
sudo bash -c "cat > '$relay_service_file_path'" << EOF
[Unit]
Description=Relay Service
After=network.target
[Service]
Type=simple
Environment=HOME=/home/$USER
ExecStart=/home/$USER/go-libp2p-relay-daemon/libp2p-relay-daemon -config "${CONFIG_FILE}" -id "${IDENTITY_FILE}"
Restart=always
RestartSec=10s
StartLimitInterval=5min
StartLimitBurst=4
StandardOutput=journal
StandardError=inherit
[Install]
WantedBy=multi-user.target
EOF
# Reload systemd and enable the service
sudo systemctl daemon-reload
sudo systemctl enable relay.service
sudo systemctl start relay.service
echo "Relay service has been set up and started."
}
verify_services_status() {
echo "Checking status of services..."
# Define your services
declare -a services=("relay")
# Initialize a flag to keep track of service status
all_services_running=true
for service in "${services[@]}"; do
# Check the status of each service
if ! sudo systemctl is-active --quiet "$service"; then
echo "Error: Service $service is not running."
all_services_running=false
else
echo "Service $service is running."
fi
done
# Final check to see if any service wasn't running
if [ "$all_services_running" = false ]; then
echo "ERROR: One or more services are not running. Please check the logs for more details."
return 1
else
echo "OK All services are running as expected."
return 0
fi
}
cleanup() {
echo "Cleaning up..."
# Remove Go tarball
if [ -f "go1.21.0.linux-amd64.tar.gz" ]; then
echo "Removing Go tarball..."
sudo rm go1.21.0.linux-amd64.tar.gz
fi
# Add other cleanup tasks here
}
create_cloudflare_dns_record() {
public_ip="$1"
# Construct the DNS record name
dns_record="dev.relay.${DOMAIN}"
# Create DNS A Record using Cloudflare API
curl -s -X POST "https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE_ID}/dns_records" \
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \
-H "Content-Type: application/json" \
--data "{\"type\":\"A\",\"name\":\"${dns_record}\",\"content\":\"${public_ip}\",\"ttl\":120,\"proxied\":false}"
}
get_public_addr() {
# Function to get the AWS Region
local token=$1
echo $(curl -H "X-aws-ec2-metadata-token: $token" http://169.254.169.254/latest/meta-data/public-ipv4 -s)
}
# Main script execution
main() {
# Set DEBIAN_FRONTEND to noninteractive to avoid interactive prompts
export DEBIAN_FRONTEND=noninteractive
echo "\$nrconf{restart} = 'a';" | sudo tee /etc/needrestart/needrestart.conf
# Update and install dependencies
sudo apt update
sudo apt install -y zip wget git curl build-essential jq pkg-config libssl-dev protobuf-compiler llvm libclang-dev clang plocate cmake
install_packages
# Set LIBCLANG_PATH for the user
# echo "export LIBCLANG_PATH=/usr/lib/llvm-14/lib/" | sudo tee /etc/profile.d/libclang.sh
if ! grep -q 'export LIBCLANG_PATH=/usr/lib/llvm-14/lib/' ~/.profile; then
echo "export LIBCLANG_PATH=/usr/lib/llvm-14/lib/" >> ~/.profile
fi
source ~/.profile
# Install Go 1.21 from source
install_go
# Clone and build the necessary repositories
clone_and_build
setup_and_extract_keys
# Setup and start relay service
setup_relay_service
cleanup
echo "Setup complete."
verify_services() {
verify_services_status
services_running=$?
return $services_running
}
# Check the status of the services
verify_services
success_code=$?
# Determine final message based on success or failure
if [ $success_code -eq 0 ]; then
echo "Setup complete. All verifications were successful!"
else
echo "Setup encountered errors. Please review the logs for more details."
fi
echo "everything is finished"
}
# Run the main function with the provided region
main "$@"