-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
executable file
·195 lines (162 loc) · 7.92 KB
/
deploy.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
#/usr/bin/env bash
readonly APP_NAME=kid_data
readonly DESTINATION=$1
readonly DEST_PATH="/home/ripley/app"
readonly TARGET_ARCH=aarch64-unknown-linux-gnu
readonly SOURCE_PATH=target/${TARGET_ARCH}/release/$APP_NAME
# Look in the target/release/ directory for the binary and that is the name of the application
# APP_NAME=$(ls target/$TARGET_ARCH/release/ | head -n 1)
echo "APP_NAME: $APP_NAME"
if [ -z "$DESTINATION" ]; then
echo "Destination is required"
echo "EXAMPLE: ./deploy.sh 192.168.1.10"
exit 1
fi
# mkdir the target directory on the target machine
# ssh -t $DESTINATION "mkdir -p ${DEST_PATH}" #2>&1 > /dev/null
# Build the Rust application
echo -e "\e[33mBuilding the Rust application for Release\e[0m"
# cargo build --release --quiet # 2>&1 > /dev/null
cross build --release --target=aarch64-unknown-linux-gnu
echo -e "\e[33mCopying the build files to the server\e[0m"
sed -i 's/debug: true/debug: false/' $(find ./ -type f -name base.yaml)
rsync -Pauvht --stats {settings,.env,static,$SOURCE_PATH} $DESTINATION:${DEST_PATH} --exclude target --exclude .git --exclude .github --exclude .gitignore --exclude aj_quiz.log --exclude scan_yam.log --exclude README.md --exclude deploy.sh --exclude target --exclude tests --exclude .cargo --exclude \*\~ 2>&1 > /dev/null
sed -i 's/debug: false/debug: true/' $(find ./ -type f -name base.yaml)
# rsync -Paurvht --stats ./ $DESTINATION:${DEST_PATH} --exclude target --exclude .git --exclude .github --exclude .gitignore --exclude aj_quiz.log --exclude scan_yam.log --exclude README.md --exclude deploy.sh --exclude target --exclude tests --exclude .cargo --exclude \*\~
# Check if the last command was successful
if [ $? -eq 0 ]; then
# Echo in green
echo -e "\e[32m\n\n############################################################\e[0m"
echo -e "\e[32m################Transfer Successful###################\e[0m"
echo -e "\e[32m############################################################\e[0m\n"
else
# Echo in red
echo -e "\e[31m\n\n############################################################\e[0m"
echo -e "\e[31m################Transfer Failed###################\e[0m"
echo -e "\e[31m############################################################\e[0m"
exit 1
fi
# echo -e "\n\e[33mBuilding the Rust application on the server\e[0m"
# ssh -t $DESTINATION "~/.cargo/bin/cargo build --manifest-path ${DEST_PATH}/Cargo.toml --release 2>&1 > /dev/null"
# # Check if the last command was successful
# if [ $? -eq 0 ]; then
# # Echo in green
# echo -e "\e[32m\n\n############################################################\e[0m"
# echo -e "\e[32m################Build Successful###################\e[0m"
# echo -e "\e[32m############################################################\e[0m\n"
# else
# # Echo in red
# echo -e "\e[31m\n\n############################################################\e[0m"
# echo -e "\e[31m################Build Failed###################\e[0m"
# echo -e "\e[31m############################################################\e[0m"
# exit 1
# fi
# Check if the destination server has a systemctl service for the $APP_NAME
ssh -t $DESTINATION "systemctl status ${APP_NAME} 2>&1 > /dev/null"
# Check if the last command was successful
if [ $? -eq 0 ]; then
# Echo in green
echo -e "\e[32m\n\n############################################################\e[0m"
echo -e "\e[32m################Service is Running###################\e[0m"
echo -e "\e[32m############################################################\e[0m"
else
# Echo in red
echo -e "\e[31m\n\n############################################################\e[0m"
echo -e "\e[31m################Service is Not Running###################\e[0m"
echo -e "\e[31m############################################################\e[0m"
# Create the service file
cat > $APP_NAME.service <<EOF
[Unit]
Description='Track relevant child related information for parents'
After=network.target
[Service]
Type=simple
User=ripley
WorkingDirectory=/home/ripley/app
ExecStart=/home/ripley/app/$APP_NAME
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
SERVICE_FILE="$DEST_PATH/$(APP_NAME).service"
# Copy the service file to the server
rsync -Pauvht --stats $APP_NAME.service $DESTINATION:~/ 2>&1 > /dev/null
echo -e "\e[33mCopying the service file: ${APP_NAME}.service @ ${DESTINATION}\e[0m"
ssh -t $DESTINATION "sudo cp ~/$APP_NAME.service /etc/systemd/system/"
# Check if the last command was successful
if [ $? -eq 0 ]; then
# Echo in green
echo -e "\e[32m\n\n############################################################\e[0m"
echo -e "\e[32m################Service file copied successfully###################\e[0m"
echo -e "\e[32m############################################################\e[0m"
else
# Echo in red
echo -e "\e[31m\n\n############################################################\e[0m"
echo -e "\e[31m################Service file copy failed###################\e[0m"
echo -e "\e[31m############################################################\e[0m"
exit 1
fi
# Reload the systemctl daemon
echo -e "\e[33mReloading the systemd daemon: @ ${DESTINATION}\e[0m"
# ignore all of the output
ssh -t $DESTINATION "sudo systemctl daemon-reload"
# Check if the last command was successful
if [ $? -eq 0 ]; then
# Echo in green
echo -e "\e[32m\n\n############################################################\e[0m"
echo -e "\e[32m################Daemon reloaded successfully###################\e[0m"
echo -e "\e[32m############################################################\e[0m"
else
# Echo in red
echo -e "\e[31m\n\n############################################################\e[0m"
echo -e "\e[31m################Daemon reload failed###################\e[0m"
echo -e "\e[31m############################################################\e[0m"
fi
# Start the service
echo -e "\e[33mStarting the service: ${APP_NAME} @ ${DESTINATION}\e[0m"
ssh -t $DESTINATION "sudo systemctl start $APP_NAME"
# Check if the last command was successful
if [ $? -eq 0 ]; then
# Echo in green
echo -e "\e[32m\n\n############################################################\e[0m"
echo -e "\e[32m################Service started successfully###################\e[0m"
echo -e "\e[32m############################################################\e[0m"
exit 0
else
# Echo in red
echo -e "\e[31m\n\n############################################################\e[0m"
echo -e "\e[31m################Service start failed###################\e[0m"
echo -e "\e[31m############################################################\e[0m"
exit 1
fi
fi
# Restart the service
echo -e "\e[33mRestarting the service: ${APP_NAME}\e[0m"
ssh -t $DESTINATION "sudo systemctl restart $APP_NAME"
# Check if the last command was successful
if [ $? -eq 0 ]; then
# Echo in green
echo -e "\e[32m\n\n############################################################\e[0m"
echo -e "\e[32m################Service restarted successfully###################\e[0m"
echo -e "\e[32m############################################################\e[0m"
exit 0
else
# Echo in red
echo -e "\e[31m\n\n############################################################\e[0m"
echo -e "\e[31m################Service restart failed###################\e[0m"
echo -e "\e[31m############################################################\e[0m"
exit 1
fi
# set -o errexit
# set -o nounset
# set -o pipefail
# set -o xtrace
# USER_NAME="ripley"
# IP=192.168.110.51
# readonly TARGET_HOST=$USER_NAME@$IP
# readonly TARGET_PATH=/home/$USER_NAME
# readonly TARGET_ARCH=aarch64-unknown-linux-gnu
# readonly SOURCE_PATH=./target/${TARGET_ARCH}/release/kid_data
# cross build --release --target=${TARGET_ARCH}
# rsync -Pauvht --stats ${SOURCE_PATH} ${TARGET_HOST}:${TARGET_PATH}
# ssh -t ${TARGET_HOST} sudo systemctl restart antenna_switcher_api.service