-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudinit.sh
More file actions
232 lines (190 loc) · 6.94 KB
/
cloudinit.sh
File metadata and controls
232 lines (190 loc) · 6.94 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
#!/bin/bash
LOG_FILE="/var/log/install_syncthing.log"
exec > >(tee -i $LOG_FILE)
exec 2>&1
echo "Starting the Syncthing installation."
# 1. Define variables
DOWNLOAD_DIR="/home/opc"
CONFIG_PATH="/home/opc/.local/state/syncthing/config.xml"
# Bucket related variables
S3FS_CREDENTIALS_FILE="${s3fs_credentials_file}"
ACCESS_KEY_ID="${access_key_id}"
SECRET_ACCESS_KEY="${secret_access_key}"
BUCKET_NAME="${bucket_name}"
MOUNT_POINT="/home/opc/logging"
REGION="${region}"
NAMESPACE="${namespace}"
echo "Configuring Syncthing config file"
# 2. Download and Install Syncthing
echo "Setting download directory to $DOWNLOAD_DIR"
cd $DOWNLOAD_DIR || { echo "Error: Failed to change directory to $DOWNLOAD_DIR"; exit 1; }
echo "Downloading Syncthing..."
curl -LO $(curl -s https://api.github.com/repos/syncthing/syncthing/releases/latest | grep browser_download_url | grep linux-amd64 | cut -d '"' -f 4)
if [ $? -ne 0 ]; then
echo "Error: Failed to download Syncthing."
exit 1
fi
echo "Syncthing downloaded to $DOWNLOAD_DIR"
echo "Extracting Syncthing..."
tar -xzf syncthing-linux-amd64*.tar.gz
# Verify the extracted directory exists
EXTRACTED_DIR=$(find . -maxdepth 1 -type d -name "syncthing-linux-amd64*" | head -n 1)
if [ -z "$EXTRACTED_DIR" ]; then
echo "Error: Extracted directory not found."
exit 1
fi
echo "Syncthing extracted to $EXTRACTED_DIR"
if [ -f "$EXTRACTED_DIR/syncthing" ]; then
sudo mv $EXTRACTED_DIR/syncthing /usr/local/bin/
if [ $? -ne 0 ]; then
echo "Error: Failed to move Syncthing binary to /usr/local/bin."
exit 1
fi
sudo chmod +x /usr/local/bin/syncthing
else
echo "Error: Syncthing binary not found in extracted directory."
exit 1
fi
echo "Verifying Syncthing installation..."
if ! which syncthing > /dev/null 2>&1; then
log "Error: Syncthing binary not found in PATH. Exiting."
exit 1
fi
echo "Syncthing installation completed successfully. Version:"
export HOME=/home/opc
syncthing --version
# 3. Start Syncthing for Initial Setup
echo "Starting Syncthing for initial setup as the current user..."
nohup sudo -u opc /usr/local/bin/syncthing >> "$LOG_FILE" 2>&1 &
if [ $? -ne 0 ]; then
echo "Error: Failed to start Syncthing."
exit 1
fi
echo "Waiting for Syncthing to generate the configuration file..."
for i in {1..40}; do # Wait for up to 40 seconds
if [ -f "$CONFIG_PATH" ]; then
echo "Configuration file generated: $CONFIG_PATH"
break
fi
sleep 1
done
# Check if the file still doesn't exist after waiting
if [ ! -f "$CONFIG_PATH" ]; then
echo "Error: Configuration file was not generated within the timeout period."
exit 1
fi
# Function to clean up previous Syncthing processes
stop_syncthing() {
echo "Stopping Syncthing..."
PIDS=$(pgrep -f /usr/local/bin/syncthing)
if [ -n "$PIDS" ]; then
echo "Found Syncthing processes: $PIDS"
sudo kill -9 $PIDS 2>/dev/null
sleep 3
echo "All Syncthing processes stopped."
else
echo "No running Syncthing processes found."
fi
}
# Stop Syncthing safely before exiting
stop_syncthing
# 4. Modify or Add the <gui> Section in config.xml
echo "Checking if the Syncthing configuration file exists at $CONFIG_PATH..."
if [ -f "$CONFIG_PATH" ]; then
echo "Found configuration file: $CONFIG_PATH"
if grep -q "<gui[[:space:]]" "$CONFIG_PATH"; then # Fix: Detect <gui> with attributes
echo "The <gui> section exists. Modifying it..."
if grep -q "<address>" "$CONFIG_PATH"; then
sed -i 's|<address>.*</address>|<address>0.0.0.0:8384</address>|' "$CONFIG_PATH"
else
echo "No <address> tag found in <gui>. Adding it..."
sed -i '/<gui[[:space:]]/a \ <address>0.0.0.0:8384</address>' "$CONFIG_PATH"
fi
else
echo "The <gui> section is missing. Adding it to the configuration..."
sed -i '/<\/configuration>/i \
<gui>\n <address>0.0.0.0:8384</address>\n </gui>' "$CONFIG_PATH"
fi
echo "Configuration updated. Verifying changes..."
grep -A 5 "<gui" "$CONFIG_PATH"
else
echo "Error: Configuration file not found at $CONFIG_PATH."
exit 1
fi
# 5. Open Firewall Port
echo "Configuring firewall to have port 8384 accessible."
sudo systemctl stop firewalld
sudo firewall-offline-cmd --zone=public --add-port=8384/tcp
sudo systemctl start firewalld
# 6. Start Syncthing in Detached Mode and Wait
echo "Starting Syncthing in detached mode..."
nohup sudo -u opc /usr/local/bin/syncthing > /var/log/install_syncthing.log 2>&1 &
if [ $? -ne 0 ]; then
echo "Error: Failed to start Syncthing."
exit 1
fi
echo "Waiting 10 seconds for Syncthing to initialize..."
sleep 10
# 7. Verify Syncthing is Listening
echo "Verifying that Syncthing is listening on port 8384..."
if ss -tuln | grep -q ':8384'; then
echo "Success: Syncthing is running and listening on port 8384."
else
echo "Error: Syncthing is not listening on port 8384. Check logs for more details."
tail -n 20 /var/log/install_syncthing.log
exit 1
fi
echo "Syncthing installation complete. Please allow up to 90 seconds for the Web UI to start."
echo "Starting installation of S3FS-Fuse..."
# 8. Install s3fs
# Install required dependencies
sudo yum install automake fuse fuse-devel gcc-c++ git libcurl-devel libxml2-devel make openssl-devel -y
# Clone the S3FS repository as opc user
sudo -u opc git clone https://github.com/s3fs-fuse/s3fs-fuse.git /home/opc/s3fs-fuse
# Navigate to the cloned directory
cd /home/opc/s3fs-fuse || { echo "Error: Failed to enter s3fs-fuse directory"; exit 1; }
# Compile and install S3FS
echo "Compiling S3FS-Fuse..."
sudo -u opc ./autogen.sh
sudo -u opc ./configure
sudo -u opc make
sudo make install
# Verify installation
if [ -f "/usr/local/bin/s3fs" ]; then
echo "S3FS installed successfully!"
ls /usr/local/bin/s3fs
else
echo "Error: S3FS installation failed."
exit 1
fi
echo "S3FS-Fuse installation completed."
echo "Creating S3FS credentials file..."
# Write the credentials to the S3FS password file
echo "$ACCESS_KEY_ID:$SECRET_ACCESS_KEY" | sudo tee $S3FS_CREDENTIALS_FILE > /dev/null
# Set correct permissions
sudo chmod 600 $S3FS_CREDENTIALS_FILE
sudo chown opc:opc $S3FS_CREDENTIALS_FILE
# Verify the file exists
if [ -f "$S3FS_CREDENTIALS_FILE" ]; then
echo "S3FS credentials file created successfully."
else
echo "Error: Failed to create S3FS credentials file."
exit 1
fi
# 9. Mount the bucket as FS to the VM
# Ensure the mount point exists
if [ ! -d "$MOUNT_POINT" ]; then
echo "Creating mount point: $MOUNT_POINT"
sudo mkdir -p $MOUNT_POINT
sudo chown opc:opc $MOUNT_POINT
sudo chmod 755 $MOUNT_POINT
fi
# Mount the bucket
echo "Mounting the OCI Object Storage bucket..."
sudo /usr/local/bin/s3fs $BUCKET_NAME $MOUNT_POINT \
-o endpoint=$REGION \
-o passwd_file=$S3FS_CREDENTIALS_FILE \
-o url=https://$NAMESPACE.compat.objectstorage.$REGION.oraclecloud.com/ \
-o nomultipart \
-o use_path_request_style \
-o allow_other