forked from maxroll-media-group/hetzner-auction-hunter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.sh
206 lines (165 loc) · 5.72 KB
/
functions.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
196
197
198
199
200
201
202
203
204
205
206
#!/bin/bash
# This file can only be sources
# It CANNOT be directly executed !
if [ ${0##*/} == ${BASH_SOURCE[0]##*/} ]; then
echo "WARNING"
echo "This script is not meant to be executed directly!"
echo "Use this script only by sourcing it."
echo -e "\n"
exit 1
fi
##################################
####### Helper Functions #########
##################################
# Repeat Character N times
repeat_character() {
# Character to repeat
local lcharacter=${1}
# Number of Repetitions
local lrepetitions=${2}
# Print using Brace Expansion
#for i in {1 ... ${lrepetitions}}
for i in $(seq 1 1 ${lrepetitions})
do
echo -n "${lcharacter}"
done
}
# Add Line Separator
add_separator() {
local lcharacter=${1-"#"}
local lrows=${2-"1"}
# Get width of Terminal
local lwidth=$(tput cols)
# Repeat Character
for r in $(seq 1 1 ${lrows})
do
repeat_character "${lcharacter}" "${lwidth}"
done
}
# Add Line Separator with Description
add_section() {
local lcharacter=${1-"#"}
local lrows=${2-"1"}
local ldescription=${3-""}
# Determine number of Separators BEFORE and AFTER the Description
#local lrowsseparatorsbefore=$(echo "${lrows-1} / ( 2 )" | bc -l)
#local lrowsseparatorafter="${lrowsseparatorsbefore}"
local lrowsbefore="${lrows}"
local lrowsafter="${lrows}"
# Add Separator
add_separator "${lcharacter}" "${lrowsbefore}"
# Add Header with Description
add_description "${lcharacter}" "${ldescription}"
# Add Separator
add_separator "${lcharacter}" "${lrowsafter}"
}
add_description() {
# User Inputs
local lcharacter=${1-"#"}
local ldescription=${2-""}
# Add one Space before and after the original String
ldescription=" ${ldescription} "
# Get width of Terminal
local lwidth=$(tput cols)
# Get length of Description
local llengthdescription=${#ldescription}
# Get width of Terminal
local lwidth=$(tput cols)
# Subtract Description from Terminal Width
local llengthseparator=$((lwidth - llengthdescription))
# Divide by two
local llengtheachseparator=$(echo "${llengthseparator} / ( 2 )" | bc -l)
# Remainer
local lremainer=$((llengthseparator % 2))
local lextrastr=$(repeat_character "${lcharacter}" "${lremainer}")
# Get String of Characters for BEFORE and AFTER the Description
local lseparator=$(repeat_character "${lcharacter}" "${llengtheachseparator}")
# Print Description Line
echo "${lseparator}${ldescription}${lextrastr}${lseparator}"
}
##################################
######## Store RAW Data ##########
##################################
store_raw_data() {
# Build Section
if [ -n "${PRINT_CONFIGURATION}" ] && [ "${PRINT_CONFIGURATION}" == "yes" ]
then
add_section "#" "1" "Caching Hetzner Server List"
fi
# Generate Timestamp
local ltimestamp=$(date +%Y-%m-%d_%Hh%M)
# Download and Store the Data in Cache only once, so we don't hit Hetzner Servers too hard
curl -s https://www.hetzner.com/_resources/app/jsondata/live_data_sb.json | jq > ${APP_HOST_SEARCH_PATH}/live_data_sb.json
# Copy file so that we always have a Historic View
cp ${APP_HOST_SEARCH_PATH}/live_data_sb.json ${APP_HOST_SEARCH_PATH}/${ltimestamp}-live_data_sb.json
}
##################################
######## Perform Search ##########
##################################
perform_search() {
# Build Section
if [ -n "${PRINT_CONFIGURATION}" ] && [ "${PRINT_CONFIGURATION}" == "yes" ]
then
add_section "#" "1" "Performing Server Search"
fi
local lsearchlabel="$1"
local lsearchcriteria="${@:2}"
#local lprice="$2"
#local lram="$3"
#local ldisk="$4"
#local lcpusearch="$5"
echo "Search Arguments: ${lsearchcriteria[*]}"
# The Argument List is passed by nameref
#declare -n lhahargsref="${1}" # Reference to output array
# Generate Timestamp
local ltimestamp=$(date +%Y-%m-%d_%Hh%M)
# Build list of Arguments for hah.py
local lhahargs=()
lhahargs+=("--data-url")
lhahargs+=("file:///${APP_SEARCH_PATH}/live_data_sb.json")
lhahargs+=("-f")
lhahargs+=("${APP_RESULTS_PATH}/${ltimestamp}-${lsearchlabel}.txt")
lhahargs+=("--provider")
lhahargs+=("${HAH_PROVIDER}")
# Add other Arguments that have been passed to this Script
for larg in "${lsearchcriteria}"
do
lhahargs+=("$larg")
done
# Decide how to Run
if [[ "${RUN_MODE}" == "container" ]]
then
# Define Container Name
local lcontainer="hetzner-auction-hunter-${lsearchlabel}"
# Stop and remove Container if it's currently running
$engine stop --ignore "${lcontainer}"
$engine rm --ignore "${lcontainer}"
# Run the new Container
$engine run --rm --replace \
--name="${lcontainer}" \
-v ${APP_HOST_DATA_PATH}:${APP_CONTAINER_DATA_PATH} \
--net ${CONTAINER_NETWORK} \
--env-file "./.env" \
-e "NOTIFIERS_PUSHOVER_USER"="${NOTIFIERS_PUSHOVER_USER}" \
-e "NOTIFIERS_PUSHOVER_TOKEN"="${NOTIFIERS_PUSHOVER_TOKEN}" \
hetzner-auction-hunter:latest \
${lhahargs[*]}
elif [[ "${RUN_MODE}" == "local" ]]
then
# Setup Python venv
if [[ ! -d "${toolpath}/venv" ]]
then
python3 -m venv venv
fi
# Activate Python venv
source "${toolpath}/venv/bin/activate"
# Install Python venv
pip install -q -r "${toolpath}/app/requirements.txt"
# Automatically Upgrade Python venv
pip install -q --upgrade -r "${toolpath}/app/requirements.txt"
# Run the APP
./app/hah.py ${lhahargs[*]}
else
echo "Invalid value for RUN_MODE. Allowed Options are: <local> / <container>. Got <${RUN_MODE}>."
fi
}