forked from postgres/postgres
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinit-server.sh
More file actions
executable file
·81 lines (68 loc) · 2.2 KB
/
init-server.sh
File metadata and controls
executable file
·81 lines (68 loc) · 2.2 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
#!/bin/bash
# Ask for the database cluster name if not provided
if [ -z "$1" ]; then
read -p "Enter the name of the database cluster to start: " DB_CLUSTER_NAME
else
DB_CLUSTER_NAME=$1
fi
# Define paths
ROOT_DIR=$(pwd)
SHARDING_DIR="$ROOT_DIR/sharding"
PG_CTL_DIR="$ROOT_DIR/src/bin/pg_ctl"
POSTGRES_EXECUTABLE="$ROOT_DIR/src/backend/postgres"
CLUSTERS_DIR="$ROOT_DIR/clusters"
DB_DIR="$CLUSTERS_DIR/$DB_CLUSTER_NAME"
LOG_FILE="$CLUSTERS_DIR/logfile"
CONFIG_FILE="$SHARDING_DIR/src/node/config/nodes_config.yaml" # Path to config.yaml
# Check for additional argument. If node type is not given, set it to "s" by default
NODE_TYPE=${2:-s}
# If we're on OS X, make sure that globals aren't stripped out.
if [ "$(uname)" == "Darwin" ]; then
export LDFLAGS="-Wl,-no_pie"
fi
./build-release.sh
echo "[init-server] Building the project..."
make
echo "[init-server] Copying postgres executable to pg_ctl directory..."
cd $PG_CTL_DIR
rm postgres
cd $ROOT_DIR
cp $POSTGRES_EXECUTABLE $PG_CTL_DIR
# Function to check if a port is available
port_available() {
local port=$1
# Check if port is in use
if nc -z localhost $port; then
echo "[init-server] Port $port is in use."
return 1 # Port is in use
else
echo "[init-server] Port $port is available."
return 0 # Port is available
fi
}
# Read ports from config.yaml using the Python script
ports=($(python3 parse_config_yaml.py $CONFIG_FILE))
# Find an available port
selected_port=""
for port in "${ports[@]}"; do
if port_available $port; then
selected_port=$port
break
fi
done
if [ -z "$selected_port" ]; then
echo "[init-server] Error: No available ports found in config.yaml"
exit 1
fi
echo "[init-server] Starting PostgreSQL server on port $selected_port for cluster $DB_CLUSTER_NAME with node type $NODE_TYPE..."
cd $PG_CTL_DIR
./pg_ctl -D $DB_DIR -l $LOG_FILE -o "-p $selected_port" start
# Check if pg_ctl ran successfully
if [ $? -ne 0 ]; then
echo "[init-server] Error: Failed to start PostgreSQL server."
exit 1
fi
# Run start-psql.sh
echo "[init-server] Calling start-psql.sh with nodeType $NODE_TYPE and port $selected_port..."
cd $ROOT_DIR
./start-psql.sh $selected_port $NODE_TYPE