Skip to content

Commit 65c5b35

Browse files
borrrdenbarkha06
andauthored
Add in another service for Edge Server management (#327)
* Add in another service for Edge Server management Using a service called shell2http to expose scripts as HTTP endpoints. Roll back setting up Edge Server initially and have the tests start and stop edge server as needed since configuration changes require a restart. * Added iptables command to firewall.sh: - Empty allow and deny lists would remove all the rules added - Accepts allow and deny params as array of strings and adds the rules to ES_RULES chain --------- Co-authored-by: barkha06 <[email protected]>
1 parent 94cf14e commit 65c5b35

File tree

10 files changed

+139
-86
lines changed

10 files changed

+139
-86
lines changed

environment/aws/es_setup/configure-system.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,21 @@
22

33
set -x
44

5-
mkdir -p $HOME/config
65
mkdir -p $HOME/cert
76
mkdir -p $HOME/log
7+
mkdir -p $HOME/shell2http
88

99
curl -LO https://github.com/caddyserver/caddy/releases/download/v2.10.2/caddy_2.10.2_linux_amd64.tar.gz
1010
tar xvf caddy_2.10.2_linux_amd64.tar.gz caddy
11-
rm caddy_2.10.2_linux_amd64.tar.gz
11+
rm caddy_2.10.2_linux_amd64.tar.gz
12+
13+
pushd $HOME/shell2http
14+
curl -LO https://github.com/msoap/shell2http/releases/download/v1.17.0/shell2http_1.17.0_linux_amd64.tar.gz
15+
tar xvf shell2http_1.17.0_linux_amd64.tar.gz shell2http
16+
rm shell2http_1.17.0_linux_amd64.tar.gz
17+
18+
if ! command -v iptables &> /dev/null; then
19+
sudo yum install iptables-services -y
20+
sudo systemctl enable iptables
21+
sudo systemctl start iptables
22+
fi

environment/aws/es_setup/es_config.json

Lines changed: 0 additions & 36 deletions
This file was deleted.

environment/aws/es_setup/setup_edge_servers.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@ def remote_exec(
202202
click.echo()
203203

204204

205+
def remote_exec_bg(ssh: paramiko.SSHClient, command: str, desc: str) -> None:
206+
header(desc)
207+
ssh.exec_command(command, get_pty=False)
208+
header("Done!")
209+
click.echo()
210+
211+
205212
def setup_server(
206213
hostname: str, pkey: paramiko.Ed25519Key, es_info: EsDownloadInfo
207214
) -> None:
@@ -232,6 +239,15 @@ def setup_server(
232239
)
233240
remote_exec(ssh, "bash /tmp/configure-system.sh", "Setting up instance")
234241

242+
sftp_progress_bar(
243+
sftp, SCRIPT_DIR / "cert" / "es_cert.pem", "/home/ec2-user/cert/es_cert.pem"
244+
)
245+
sftp_progress_bar(
246+
sftp, SCRIPT_DIR / "cert" / "es_key.pem", "/home/ec2-user/cert/es_key.pem"
247+
)
248+
for file in (SCRIPT_DIR / "shell2http").iterdir():
249+
sftp_progress_bar(sftp, file, f"/home/ec2-user/shell2http/{file.name}")
250+
235251
if es_info.is_release:
236252
remote_exec(
237253
ssh,
@@ -246,10 +262,6 @@ def setup_server(
246262
f"/tmp/{es_info.local_filename}",
247263
)
248264

249-
sftp_progress_bar(sftp, SCRIPT_DIR / "start-es.sh", "/home/ec2-user/start-es.sh")
250-
sftp_progress_bar(
251-
sftp, SCRIPT_DIR / "es_config.json", "/home/ec2-user/config/es_config.json"
252-
)
253265
sftp_progress_bar(sftp, SCRIPT_DIR / "Caddyfile", "/home/ec2-user/Caddyfile")
254266
cert = create_self_signed_certificate(hostname)
255267
cert_pem = cert.pem_bytes()
@@ -280,7 +292,9 @@ def setup_server(
280292
"Installing Edge Server",
281293
)
282294
remote_exec(ssh, "/home/ec2-user/caddy start", "Starting ES log fileserver")
283-
remote_exec(ssh, "bash /home/ec2-user/start-es.sh", "Starting Edge Server")
295+
remote_exec_bg(
296+
ssh, "bash /home/ec2-user/shell2http/start.sh", "Starting ES management server"
297+
)
284298

285299
ssh.close()
286300

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
read_http_body() {
2+
local len="${HTTP_CONTENT_LENGTH:-}"
3+
if [[ -n "$len" ]]; then
4+
head -c "$len"
5+
else
6+
cat
7+
fi
8+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
3+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
4+
source "$SCRIPT_DIR/common.sh"
5+
6+
REQUEST_BODY=$(read_http_body)
7+
8+
ALLOW=$(echo $REQUEST_BODY | jq -r '.allow')
9+
DENY=$(echo $REQUEST_BODY | jq -r '.deny')
10+
if [[ ( -z "$ALLOW" || "$ALLOW" == "null" ) && ( -z "$DENY" || "$DENY" == "null" ) ]]; then
11+
# echo "Error: provide 'allow' or 'deny' (at least one)"
12+
# exit 1
13+
iptables -F ES_RULES
14+
exit 0
15+
fi
16+
17+
if ! iptables -L ES_RULES >/dev/null 2>&1; then
18+
iptables -N ES_RULES
19+
fi
20+
iptables -F ES_RULES
21+
22+
23+
if [[ "$ALLOW" != "null" ]]; then
24+
echo "$ALLOW" | jq -r '.[]' | while read ip; do
25+
iptables -A ES_RULES -s "$ip" -j ACCEPT
26+
done
27+
fi
28+
29+
if [[ "$DENY" != "null" ]]; then
30+
echo "$DENY" | jq -r '.[]' | while read ip; do
31+
iptables -A ES_RULES -s "$ip" -j DROP
32+
done
33+
fi
34+
35+
iptables -C INPUT -j ES_RULES 2>/dev/null || iptables -I INPUT -j ES_RULES
36+
iptables -L ES_RULES --line-numbers
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
PID=$(ps -ax | grep [e]dge-server | awk '{print $1}')
4+
if [[ "$PID" == "" ]]; then
5+
echo "Running process not found"
6+
exit 0
7+
fi
8+
9+
kill -SIGHUP $PID
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
3+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
4+
source "$SCRIPT_DIR/common.sh"
5+
REQUEST_BODY=$(read_http_body)
6+
7+
DB_FILENAME=$(echo $REQUEST_BODY | jq -r '.filename')
8+
if [ -z "$DB_FILENAME" ] || [ "$DB_FILENAME" == "null" ]; then
9+
echo "Error: 'filename' field is required in the request body"
10+
exit 1
11+
fi
12+
13+
# Ensure it ends with 'cblite2'
14+
if [[ "$DB_FILENAME" != *.cblite2 ]]; then
15+
echo "Error: 'filename' must end with '.cblite2'"
16+
exit 1
17+
fi
18+
19+
if [ ! -d "$DB_FILENAME" ]; then
20+
echo "Database file '$DB_FILENAME' does not exist"
21+
exit 0
22+
fi
23+
24+
rm -rf "$DB_FILENAME"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
4+
source "$SCRIPT_DIR/common.sh"
5+
REQUEST_BODY=$(read_http_body)
6+
7+
DIR=$(mktemp -d)
8+
rm $DIR/config.json || true
9+
echo $REQUEST_BODY > $DIR/config.json
10+
11+
LOG="$DIR/edge.log"
12+
nohup /opt/couchbase-edge-server/bin/couchbase-edge-server $DIR/config.json > $LOG 2>&1 < /dev/null &
13+
EDGE_SERVER_PID=$!
14+
sleep 1
15+
if kill -0 "$EDGE_SERVER_PID" 2>/dev/null; then
16+
echo "Edge server running"
17+
else
18+
echo "Edge server failed to start:"
19+
cat $LOG
20+
exit 1
21+
fi
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
4+
5+
setsid /home/ec2-user/shell2http/shell2http -no-index -cgi -500 -port 20001 \
6+
/firewall $SCRIPT_DIR/firewall.sh \
7+
/kill-edgeserver $SCRIPT_DIR/kill-edgeserver.sh \
8+
/reset-db $SCRIPT_DIR/reset-db.sh /start-edgeserver \
9+
$SCRIPT_DIR/start-edgeserver.sh > /dev/null 2>&1 &

environment/aws/es_setup/start-es.sh

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)