-
Notifications
You must be signed in to change notification settings - Fork 32
/
miner_latest.sh
executable file
·134 lines (104 loc) · 5.13 KB
/
miner_latest.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
#!/bin/bash
# Script for auto updating the helium miner.
# Set default values
MINER=miner
REGION=US915
GWPORT=1680
MINERPORT=44158
DATADIR=/home/pi/miner_data
LOGDIR=
QUAY_URL='https://quay.io/api/v1/repository/team-helium/miner/tag/?limit=20&page=1&onlyActiveTags=true'
GA=GA
#The alternative to GA is GA=64_202
# Make sure we have the latest version of the script
function update-git {
SCRIPT_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd)
cd "$SCRIPT_DIR" && git pull
}
command -v jq > /dev/null || sudo apt-get install jq curl -y
# Read switches to override any default values for non-standard configs
while getopts n:g:p:d:l:r: flag
do
case "${flag}" in
n) MINER=${OPTARG};;
g) GWPORT=${OPTARG};;
p) MINERPORT=${OPTARG};;
d) DATADIR=${OPTARG};;
l) LOGDIR=${OPTARG};;
r) REGION=${OPTARG};;
a) GA=${OPTARG};;
*) echo "Exiting"; exit;;
esac
done
# Autodetect running image version and set arch
if [ "$(uname -m)" == "x86_64" ]; then
ARCH=amd
else
ARCH=arm
fi
running_image=$(docker container inspect -f '{{.Config.Image}}' "$MINER" | awk -F: '{print $2}')
miner_quay=$(curl -s "$QUAY_URL" --write-out '\nHTTP_Response:%{http_code}')
miner_response=$(echo "$miner_quay" | grep "HTTP_Response" | cut -d":" -f2)
if [[ $miner_response -ne 200 ]];
then
echo "Bad Response from Server"
exit 0
fi
#if [[ $GA == GA ]];
# then
# miner_latest=$(echo "$miner_quay" | grep -v HTTP_Response | jq -c --arg ARCH "$ARCH" '[ .tags[] | select( .name | contains($ARCH)and contains("GA")) ][0].name' | cut -d'"' -f2)
# else
# miner_latest=$(echo "$miner_quay" | grep -v HTTP_Response | jq -c --arg ARCH "$ARCH" '[ .tags[] | select( .name | contains($ARCH)and contains("64_202")) ][0].name' | cut -d'"' -f2)
#fi
#test url
#miner_latest=$(echo "$miner_quay" | grep -v HTTP_Response | jq -c --arg ARCH "$ARCH" '[ .tags[] | select( .name | contains($ARCH)and contains("64_202")) ][0].name' | cut -d'"' -f2)
miner_latest=$(echo "$miner_quay" | grep -v HTTP_Response | jq -c --arg ARCH "$ARCH" '[ .tags[] | select( .name | contains($ARCH)and contains("GA")) ][0].name' | cut -d'"' -f2)
date
echo "$0 starting with MINER=$MINER GWPORT=$GWPORT MINERPORT=$MINERPORT DATADIR=$DATADIR LOGDIR=$LOGDIR REGION=$REGION ARCH=$ARCH running_image=$running_image miner_latest=$miner_latest GA=$GA"
#check to see if the miner is more than 50 block behind
current_height=$(curl -s https://api.helium.io/v1/blocks/height | jq .data.height)
miner_height=$(docker exec "$MINER" miner info height | awk '{print $2}')
height_diff=$(expr "$current_height" - "$miner_height")
# commenting this out until I can find a better solution. Seems to be causing more problems than solutions.
##if [[ $height_diff -gt 50 ]]; then docker stop "$MINER" && docker start "$MINER" && echo "stopping and starting the miner because it may be stuck syncing the blockchain" ; fi
#If the miner is more than 500 blocks behind, stop the image, remove the container, remove the image. It will be redownloaded later in the script.
##if [[ $height_diff -gt 500 ]]; then docker stop "$MINER" && docker rm "$MINER" && docker image rm quay.io/team-helium/miner:"$miner_latest" ; fi
##if echo "$miner_latest" | grep -q $ARCH;
## then echo "Latest miner version $miner_latest";
##elif miner_latest=$(echo "$miner_quay" | grep -v HTTP_Response | jq -r .tags[1].name)
## then echo "Latest miner version $miner_latest";
##fi
if [ "$miner_latest" = "$running_image" ];
then echo "already on the latest version"
update-git
exit 0
fi
# Pull the new miner image. Downloading it now will minimize miner downtime after stop.
docker pull quay.io/team-helium/miner:"$miner_latest"
echo "Stopping and removing old miner"
docker stop "$MINER" && docker rm "$MINER"
echo "Deleting old miner software"
for image in $(docker images quay.io/team-helium/miner | grep "quay.io/team-helium/miner" | awk '{print $3}'); do
image_cleanup=$(docker images | grep "$image" | awk '{print $2}')
#change this to $running_image if you want to keep the last 2 images
if [ "$image_cleanup" = "$miner_latest" ]; then
continue
else
echo "Cleaning up: $image_cleanup"
docker image rm "$image"
fi
done
echo "Provisioning new miner version"
if [ -n "$LOGDIR" ];
then
LOGMOUNT="--mount type=bind,source=$LOGDIR,target=/var/data/log"
fi
docker run -d --init --env REGION_OVERRIDE="$REGION" --restart always --publish "$GWPORT":"$GWPORT"/udp --publish "$MINERPORT":"$MINERPORT"/tcp --name "$MINER" $LOGMOUNT --mount type=bind,source="$DATADIR",target=/var/data quay.io/team-helium/miner:"$miner_latest"
if [ "$GWPORT" -ne 1680 ] || [ "$MINERPORT" -ne 44158 ]; then
echo "Using nonstandard ports, adjusting miner config"
docker exec "$MINER" sed -i "/^ {blockchain,/{N;s/$/\n {port, $MINERPORT},/}; s/1680/$GWPORT/" /opt/miner/releases/0.1.0/sys.config
fi
echo "Increasing memory limit for snapshots. See https://discord.com/channels/404106811252408320/730245219974381708/851336745538027550"
docker exec "$MINER" sed -i 's/{key, undefined}$/{key, undefined},{snapshot_memory_limit, 1000}/' /opt/miner/releases/0.1.0/sys.config
docker restart "$MINER"
update-git