Welcome to mirror list, hosted at ThFree Co, Russian Federation.

update.sh « Scripts « advanced - github.com/pi-hole/pi-hole.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4fceb931ed32205a616786fdeb7bd947033f2ee1 (plain)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env bash
# Pi-hole: A black hole for Internet advertisements
# (c) 2017 Pi-hole, LLC (https://pi-hole.net)
# Network-wide ad blocking via your own hardware.
#
# Check Pi-hole core and admin pages versions and determine what
# upgrade (if any) is required. Automatically updates and reinstalls
# application if update is detected.
#
# This file is copyright under the latest version of the EUPL.
# Please see LICENSE file for your rights under this license.



# Variables

readonly ADMIN_INTERFACE_GIT_URL="https://github.com/pi-hole/AdminLTE.git"
readonly ADMIN_INTERFACE_DIR="/var/www/html/admin"
readonly PI_HOLE_GIT_URL="https://github.com/pi-hole/pi-hole.git"
readonly PI_HOLE_FILES_DIR="/etc/.pihole"

PH_TEST=true
source ${PI_HOLE_FILES_DIR}/automated\ install/basic-install.sh

# is_repo() sourced from basic-install.sh
# make_repo() sourced from basic-install.sh
# update_repo() source from basic-install.sh
# getGitFiles() sourced from basic-install.sh

GitCheckUpdateAvail() {
  local directory="${1}"
  curdir=$PWD
  cd "${directory}"

  # Fetch latest changes in this repo
  git fetch --quiet origin

  # @ alone is a shortcut for HEAD. Older versions of git
  # need @{0}
  LOCAL="$(git rev-parse @{0})"

  # The suffix @{upstream} to a branchname
  # (short form <branchname>@{u}) refers
  # to the branch that the branch specified
  # by branchname is set to build on top of#
  # (configured with branch.<name>.remote and
  # branch.<name>.merge). A missing branchname
  # defaults to the current one.
  REMOTE="$(git rev-parse @{upstream})"

  if [[ ${#LOCAL} == 0 ]]; then
    echo "::: Error: Local revision could not be obtained, ask Pi-hole support."
    echo "::: Additional debugging output:"
    git status
    exit
  fi
  if [[ ${#REMOTE} == 0 ]]; then
    echo "::: Error: Remote revision could not be obtained, ask Pi-hole support."
    echo "::: Additional debugging output:"
    git status
    exit
  fi

  # Change back to original directory
  cd "${curdir}"

  if [[ "${LOCAL}" != "${REMOTE}" ]]; then
    # Local branch is behind remote branch -> Update
    return 0
  else
    # Local branch is up-to-date or in a situation
    # where this updater cannot be used (like on a
    # branch that exists only locally)
    return 1
  fi
}

FTLcheckUpdate() {

	local FTLversion=$(/usr/bin/pihole-FTL tag)
	local FTLlatesttag=$(curl -sI https://github.com/pi-hole/FTL/releases/latest | grep 'Location' | awk -F '/' '{print $NF}' | tr -d '\r\n')

	if [[ "${FTLversion}" != "${FTLlatesttag}" ]]; then
		return 0
	else
		return 1
	fi
}

main() {
  local pihole_version_current
  local web_version_current
  source "${setupVars}"

  #This is unlikely
  if ! is_repo "${PI_HOLE_FILES_DIR}" ; then
    echo "::: Critical Error: Core Pi-hole repo is missing from system!"
    echo "::: Please re-run install script from https://github.com/pi-hole/pi-hole"
    exit 1;
  fi

  echo "::: Checking for updates..."

  if GitCheckUpdateAvail "${PI_HOLE_FILES_DIR}" ; then
    core_update=true
    echo "::: Pi-hole Core:   update available"
  else
    core_update=false
    echo "::: Pi-hole Core:   up to date"
  fi

  if FTLcheckUpdate ; then
    FTL_update=true
    echo "::: FTL:            update available"
  else
    FTL_update=false
    echo "::: FTL:            up to date"
  fi

  if ${FTL_update}; then
    echo ":::"
    echo "::: FTL out of date"
    FTLdetect
    echo ":::"
  fi

  if [[ ${INSTALL_WEB} == true ]]; then
    if ! is_repo "${ADMIN_INTERFACE_DIR}" ; then
      echo "::: Critical Error: Web Admin repo is missing from system!"
      echo "::: Please re-run install script from https://github.com/pi-hole/pi-hole"
      exit 1;
    fi

    if GitCheckUpdateAvail "${ADMIN_INTERFACE_DIR}" ; then
      web_update=true
      echo "::: Web Interface:  update available"
    else
      web_update=false
      echo "::: Web Interface:  up to date"
    fi

    # Logic
    # If Core up to date AND web up to date:
    #            Do nothing
    # If Core up to date AND web NOT up to date:
    #            Pull web repo
    # If Core NOT up to date AND web up to date:
    #            pull pihole repo, run install --unattended -- reconfigure
    # if Core NOT up to date AND web NOT up to date:
    #            pull pihole repo run install --unattended

    if ! ${core_update} && ! ${web_update} ; then
      if ! ${FTL_update} ; then
        echo ":::"
        echo "::: Everything is up to date!"
        exit 0
      fi

    elif ! ${core_update} && ${web_update} ; then
      echo ":::"
      echo "::: Pi-hole Web Admin files out of date"
      getGitFiles "${ADMIN_INTERFACE_DIR}" "${ADMIN_INTERFACE_GIT_URL}"

    elif ${core_update} && ! ${web_update} ; then
      echo ":::"
      echo "::: Pi-hole core files out of date"
      getGitFiles "${PI_HOLE_FILES_DIR}" "${PI_HOLE_GIT_URL}"
      ${PI_HOLE_FILES_DIR}/automated\ install/basic-install.sh --reconfigure --unattended || echo "Unable to complete update, contact Pi-hole" && exit 1

    elif ${core_update} && ${web_update} ; then
      echo ":::"
      echo "::: Updating Pi-hole core and web admin files"
      getGitFiles "${PI_HOLE_FILES_DIR}" "${PI_HOLE_GIT_URL}"
      ${PI_HOLE_FILES_DIR}/automated\ install/basic-install.sh --unattended || echo "Unable to complete update, contact Pi-hole" && exit 1
    else
      echo "*** Update script has malfunctioned, fallthrough reached. Please contact support"
      exit 1
    fi
  else # Web Admin not installed, so only verify if core is up to date
    if ! ${core_update}; then
      if ! ${FTL_update} ; then
        echo ":::"
        echo "::: Everything is up to date!"
        exit 0
      fi
    else
      echo ":::"
      echo "::: Pi-hole core files out of date"
      getGitFiles "${PI_HOLE_FILES_DIR}" "${PI_HOLE_GIT_URL}"
      ${PI_HOLE_FILES_DIR}/automated\ install/basic-install.sh --reconfigure --unattended || echo "Unable to complete update, contact Pi-hole" && exit 1
    fi
  fi

  if [[ "${web_update}" == true ]]; then
    web_version_current="$(/usr/local/bin/pihole version --admin --current)"
    echo ":::"
    echo "::: Web Admin version is now at ${web_version_current}"
    echo "::: If you had made any changes in '/var/www/html/admin/', they have been stashed using 'git stash'"
  fi

  if [[ "${core_update}" == true ]]; then
    pihole_version_current="$(/usr/local/bin/pihole version --pihole --current)"
    echo ":::"
    echo "::: Pi-hole version is now at ${pihole_version_current}"
    echo "::: If you had made any changes in '/etc/.pihole/', they have been stashed using 'git stash'"
  fi

  if [[ ${FTL_update} == true ]]; then
    FTL_version_current="$(/usr/bin/pihole-FTL tag)"
    echo ":::"
    echo "::: FTL version is now at ${FTL_version_current}"
    start_service pihole-FTL
    enable_service pihole-FTL
  fi


  echo ""
  exit 0

}

main