-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbremote.sh
More file actions
executable file
·164 lines (141 loc) · 5.3 KB
/
Copy pathbremote.sh
File metadata and controls
executable file
·164 lines (141 loc) · 5.3 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
#!/bin/bash
## Copyright (c) 2024 Akop Karapetyan
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
# Build and deploy launcher to control server and remote to game server
# Change to the script's directory
cd "$(dirname "$0")"
set -e
# Verify prerequisites
if ! command -v yq &> /dev/null; then
echo "${CLR_ERR}Error: yq is not installed${CLR_RST}" >&2
exit 1
fi
GAME_SVR_HOST=`yq e '.game_server.hostname' deploy.yaml`
if [ -z "$GAME_SVR_HOST" ]; then
echo "Error: missing game server hostname in deploy.yaml" >&2
exit 1
fi
GAME_SVR_REMOTE=`yq e '.game_server.remote_path' deploy.yaml`
if [ -z "$GAME_SVR_REMOTE" ]; then
echo "Error: missing game server remote path in deploy.yaml" >&2
exit 1
fi
CONTROL_SVR_HOST=`yq e '.control_server.hostname' deploy.yaml`
if [ -z "$CONTROL_SVR_HOST" ]; then
echo "${CLR_ERR}Error: missing control server hostname in deploy.yaml${CLR_RST}" >&2
exit 1
fi
CONTROL_SVR_PATH=`yq e '.control_server.path' deploy.yaml`
if [ -z "$CONTROL_SVR_PATH" ]; then
echo "${CLR_ERR}Error: missing control server path in deploy.yaml${CLR_RST}" >&2
exit 1
fi
BUILD_SVR=`yq e '.common.build_host' deploy.yaml`
if [ -z "$BUILD_SVR" ]; then
echo "${CLR_ERR}Error: missing build server hostname in deploy.yaml${CLR_RST}" >&2
exit 1
fi
LOCAL_REMOTE_PATH=remote
REMOTE_SVC_FILE=remote.service
REMOTE_EXECUTABLE=start.sh
LOCAL_LAUNCHER_PATH=launcher
LAUNCHER_SVC_FILE=launcher.service
LAUNCHER_EXECUTABLE=start.sh
GENERATED_PATH=generated
BUILD_PATH="red_builds/${LOCAL_REMOTE_PATH}"
# Need to activate virtual environment to run gen-config.py
if [ ! -d ".venv" ]; then
echo "Setting up virtual environment to generate config..." >&2
python3 -m venv .venv
fi
source .venv/bin/activate
# Generate config files for remote
echo "Generating config files for remote..." >&2
./gen-config.py --config remote-config > ${LOCAL_REMOTE_PATH}/config.yaml
./gen-config.py --config remote-games > ${LOCAL_REMOTE_PATH}/games.yaml
# Generate config files for launcher
echo "Generating config files for launcher..." >&2
./gen-config.py --config launcher-config > ${LOCAL_LAUNCHER_PATH}/config.yaml
./gen-config.py --config launcher-games > ${LOCAL_LAUNCHER_PATH}/games.yaml
complete_setup() {
local host=$1
local service_path=$2
local svc_file=$3
local executable=$4
ssh "${host}" "
(
test -d \"${service_path}/.venv\" 2>/dev/null ||
(
echo \"Setting up virtual environment...\" >&2 &&
cd \"${service_path}\" &&
python3 -m venv .venv &&
source .venv/bin/activate &&
pip install -r requirements.txt
)
) &&
(
SYSTEMD_PATH=\"\${HOME}/.config/systemd/user\" &&
SVC_PATH=\"\${SYSTEMD_PATH}/red_${svc_file}\" &&
test -f \"\${SVC_PATH}\" 2>/dev/null ||
(
echo \"Setting up systemd service...\" >&2 &&
mkdir -p \"\${SYSTEMD_PATH}\" &&
P=\$(readlink -f \"${service_path}/${executable}\") &&
cat \"${service_path}/${svc_file}\" | sed \"s|{SERVICE_PATH}|\${P}|g\" > \"\${SVC_PATH}\" &&
systemctl --user daemon-reload &&
systemctl --user enable \"red_${svc_file}\" &&
sudo loginctl enable-linger \"\${USER}\"
)
) &&
(
echo \"Restarting service...\" >&2 &&
systemctl --user restart \"red_${svc_file}\" &&
rm \"${service_path}/${svc_file}\"
)
"
}
# Copy to build server
echo "Building protobuf files..." >&2
rsync -trphL \
--exclude '.*' \
"${LOCAL_REMOTE_PATH}/" \
"${BUILD_SVR}:${BUILD_PATH}/"
# Set up environment and compile protobufs on build server
ssh "${BUILD_SVR}" "cd ${BUILD_PATH} && ./build.sh"
# Copy generated code back to local
rsync -trph \
"${BUILD_SVR}:${BUILD_PATH}/${GENERATED_PATH}" \
"${LOCAL_REMOTE_PATH}/"
cp -r "${LOCAL_REMOTE_PATH}/${GENERATED_PATH}" "${LOCAL_LAUNCHER_PATH}/"
# Copy remote to game server
echo "Deploying remote to game server..." >&2
ssh "${GAME_SVR_HOST}" "mkdir -p \"${GAME_SVR_REMOTE}\""
rsync -trph \
--exclude '.*' \
--exclude 'build.sh' \
--exclude 'proto/' \
"${LOCAL_REMOTE_PATH}/" \
"${GAME_SVR_HOST}:${GAME_SVR_REMOTE}"
complete_setup "${GAME_SVR_HOST}" "${GAME_SVR_REMOTE}" "${REMOTE_SVC_FILE}" "${REMOTE_EXECUTABLE}"
# Copy launcher to control server
echo "Deploying launcher to control server..." >&2
ssh "${CONTROL_SVR_HOST}" "mkdir -p \"${CONTROL_SVR_PATH}\""
rsync -trph \
--exclude '.*' \
--exclude '*.db' \
--exclude '*.example' \
--exclude '__pycache__' \
"${LOCAL_LAUNCHER_PATH}/" \
"${CONTROL_SVR_HOST}:${CONTROL_SVR_PATH}/"
complete_setup "${CONTROL_SVR_HOST}" "${CONTROL_SVR_PATH}" "${LAUNCHER_SVC_FILE}" "${LAUNCHER_EXECUTABLE}"