v-update-sys-hestia-git 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #!/bin/bash
  2. # info: Install update from Git repository
  3. # options: REPOSITORY BRANCH INSTALL [PACKAGES]
  4. #
  5. # example: v-update-sys-hestia-git hestiacp staging/beta install
  6. # # Will download from the hestiacp repository
  7. # # Pulls code from staging/beta branch
  8. # # install: installs package immediately
  9. # # install-auto: installs package and schedules automatic updates from Git
  10. #
  11. # Downloads and compiles/installs packages from GitHub repositories
  12. #----------------------------------------------------------#
  13. # Variables & Functions #
  14. #----------------------------------------------------------#
  15. # shellcheck source=/etc/hestiacp/hestia.conf
  16. source /etc/hestiacp/hestia.conf
  17. # shellcheck source=/usr/local/hestia/func/main.sh
  18. source $HESTIA/func/main.sh
  19. # load config file
  20. source_conf "$HESTIA/conf/hestia.conf"
  21. # Perform verification if read-only mode is enabled
  22. check_hestia_demo_mode
  23. # Define download function
  24. download_file() {
  25. local url=$1
  26. local destination=$2
  27. local force=$3
  28. # Default destination is the current working directory
  29. local dstopt=""
  30. if [ ! -z "$(echo "$url" | grep -E "\.(gz|gzip|bz2|zip|xz)$")" ]; then
  31. # When an archive file is downloaded it will be first saved localy
  32. dstopt="--directory-prefix=$ARCHIVE_DIR"
  33. local is_archive="true"
  34. local filename="${url##*/}"
  35. if [ -z "$filename" ]; then
  36. >&2 echo "[!] No filename was found in url, exiting ($url)"
  37. exit 1
  38. fi
  39. if [ ! -z "$force" ] && [ -f "$ARCHIVE_DIR/$filename" ]; then
  40. rm -f $ARCHIVE_DIR/$filename
  41. fi
  42. elif [ ! -z "$destination" ]; then
  43. # Plain files will be written to specified location
  44. dstopt="-O $destination"
  45. fi
  46. # check for corrupted archive
  47. if [ -f "$ARCHIVE_DIR/$filename" ] && [ "$is_archive" = "true" ]; then
  48. tar -tzf "$ARCHIVE_DIR/$filename" > /dev/null 2>&1
  49. if [ $? -ne 0 ]; then
  50. >&2 echo "[!] Archive $ARCHIVE_DIR/$filename is corrupted, redownloading"
  51. rm -f $ARCHIVE_DIR/$filename
  52. fi
  53. fi
  54. if [ ! -f "$ARCHIVE_DIR/$filename" ]; then
  55. [ "$HESTIA_DEBUG" ] && >&2 echo DEBUG: wget $url -q $dstopt --show-progress --progress=bar:force --limit-rate=3m
  56. wget $url -q $dstopt --show-progress --progress=bar:force --limit-rate=3m
  57. if [ $? -ne 0 ]; then
  58. >&2 echo "[!] Archive $ARCHIVE_DIR/$filename is corrupted and exit script";
  59. rm -f $ARCHIVE_DIR/$filename
  60. exit 1;
  61. fi
  62. fi
  63. if [ ! -z "$destination" ] && [ "$is_archive" = "true" ]; then
  64. if [ "$destination" = "-" ]; then
  65. cat "$ARCHIVE_DIR/$filename"
  66. elif [ -d "$(dirname $destination)" ]; then
  67. cp "$ARCHIVE_DIR/$filename" "$destination"
  68. fi
  69. fi
  70. }
  71. get_branch_file() {
  72. local filename=$1
  73. local destination=$2
  74. [ "$HESTIA_DEBUG" ] && >&2 echo DEBUG: Get branch file "$filename" to "$destination"
  75. if [ "$use_src_folder" == 'true' ]; then
  76. if [ -z "$destination" ]; then
  77. [ "$HESTIA_DEBUG" ] && >&2 echo DEBUG: cp -f "$SRC_DIR/$filename" ./
  78. cp -f "$SRC_DIR/$filename" ./
  79. else
  80. [ "$HESTIA_DEBUG" ] && >&2 echo DEBUG: cp -f "$SRC_DIR/$filename" "$destination"
  81. cp -f "$SRC_DIR/$filename" "$destination"
  82. fi
  83. else
  84. download_file "https://raw.githubusercontent.com/$REPO/$branch/$filename" "$destination" $3
  85. fi
  86. }
  87. # Set compiling directory
  88. BUILD_DIR='/tmp/hestiacp-src'
  89. INSTALL_DIR='/tmp/hestia-src'
  90. SRC_DIR="$(cd "$(dirname "$0")/.." && pwd)"
  91. ARCHIVE_DIR="/tmp/hestia-src/archive/"
  92. architecture="$(arch)"
  93. if [ $architecture == 'aarch64' ]; then
  94. BUILD_ARCH='arm64'
  95. else
  96. BUILD_ARCH='amd64'
  97. fi
  98. RPM_DIR="$BUILD_DIR/rpm/"
  99. DEB_DIR="$BUILD_DIR/deb/"
  100. if [ -f '/etc/redhat-release' ]; then
  101. BUILD_RPM=true
  102. BUILD_DEB=false
  103. OSTYPE='rhel'
  104. else
  105. BUILD_RPM=false
  106. BUILD_DEB=true
  107. OSTYPE='debian'
  108. fi
  109. # Set command variables
  110. fork=$1
  111. branch=$2
  112. install=$3
  113. # Set Version for compiling
  114. BUILD_VER=$(curl -s https://raw.githubusercontent.com/$fork/hestiacp/$branch/src/deb/hestia/control | grep "Version:" | cut -d' ' -f2)
  115. HESTIA_V="${BUILD_VER}_${BUILD_ARCH}"
  116. # Create build directories
  117. rm -rf $BUILD_DIR
  118. mkdir -p $DEB_DIR
  119. mkdir -p $ARCHIVE_DIR
  120. # Set package dependencies for compiling
  121. SOFTWARE='build-essential libxml2-dev libz-dev libcurl4-gnutls-dev unzip openssl libssl-dev pkg-config setpriv'
  122. # Define a timestamp function
  123. timestamp() {
  124. date +%s
  125. }
  126. # Warning prompt to be used if auto-install flag is not specified
  127. warning_message() {
  128. echo ""
  129. echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  130. echo "WARNING - Development builds should not be installed on"
  131. echo "systems with live production data without understanding"
  132. echo "the potential risks that are involved!"
  133. echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  134. echo ""
  135. }
  136. # Build installation
  137. install_build() {
  138. # Install all available packages
  139. echo "Installing packages..."
  140. if [ "$OSTYPE" = 'rhel' ]; then
  141. for i in $RPM_DIR/*.rpm; do
  142. dnf -y install $i
  143. if [ $? -ne 0 ]; then
  144. exit 1;
  145. fi
  146. done
  147. else
  148. for i in $DEB_DIR/*.deb; do
  149. dpkg -i $i
  150. if [ $? -ne 0 ]; then
  151. exit 1;
  152. fi
  153. done
  154. fi
  155. unset $answer
  156. # Remove temporary files
  157. rm -rf $BUILD_DIR
  158. }
  159. # Set install flags
  160. if [ -n "$1" ]; then
  161. fork_check=$(curl -s --head -w %{http_code} https://raw.githubusercontent.com/$fork/hestiacp/main/src/deb/hestia/control -o /dev/null)
  162. if [ "$fork_check" -ne "200" ]; then
  163. echo "ERROR: invalid repository name specified."
  164. exit 1
  165. else
  166. echo "[!] Download code from GitHub repository: $fork"
  167. fork="$1"
  168. fi
  169. else
  170. fork="hestiacp"
  171. fi
  172. if [ -n "$2" ]; then
  173. branch_check=$(curl -s --head -w %{http_code} https://raw.githubusercontent.com/$fork/hestiacp/$branch/src/deb/hestia/control -o /dev/null)
  174. if [ $branch_check -ne "200" ]; then
  175. echo "ERROR: invalid branch name specified."
  176. exit 1
  177. else
  178. /usr/local/hestia/bin/v-change-sys-config-value 'RELEASE_BRANCH' "$branch"
  179. echo "[!] Changed system release branch to: $branch."
  180. fi
  181. else
  182. source /usr/local/hestia/conf/hestia.conf
  183. branch=$RELEASE_BRANCH
  184. branch_check=$(curl -s --head -w %{http_code} https://raw.githubusercontent.com/$fork/hestiacp/$branch/src/deb/hestia/control -o /dev/null)
  185. if [ "$branch_check" -ne "200" ]; then
  186. echo "ERROR: invalid branch name specified."
  187. exit 1
  188. fi
  189. fi
  190. if [ -z "$branch" ]; then
  191. echo "ERROR: No branch detected."
  192. exit
  193. fi
  194. REPO="$fork/hestiacp"
  195. # Forward slashes in branchname are replaced with dashes to match foldername in github archive.
  196. branch_dash=$(echo "$branch" |sed 's/\//-/g');
  197. #----------------------------------------------------------#
  198. # Action #
  199. #----------------------------------------------------------#
  200. # Install needed software
  201. if [ "$OSTYPE" = 'rhel' ]; then
  202. # Set package dependencies for compiling
  203. SOFTWARE=' rpm-build wget tar git curl unzip'
  204. echo "Updating system DNF repositories..."
  205. dnf install -y -q 'dnf-command(config-manager)'
  206. dnf install -y -q dnf-plugins-core
  207. dnf config-manager --set-enabled powertools > /dev/null 2>&1
  208. dnf config-manager --set-enabled PowerTools > /dev/null 2>&1
  209. dnf upgrade -y -q
  210. echo "Installing dependencies for compilation..."
  211. dnf install -y -q $SOFTWARE
  212. else
  213. # Set package dependencies for compiling
  214. SOFTWARE='build-essential wget tar git curl unzip'
  215. echo "Updating system APT repositories..."
  216. apt-get -qq update > /dev/null 2>&1
  217. echo "Installing dependencies for compilation..."
  218. apt-get -qq install -y $SOFTWARE > /dev/null 2>&1
  219. # Fix for Debian PHP Envroiment
  220. if [ $BUILD_ARCH == "amd64" ]; then
  221. if [ ! -L /usr/local/include/curl ]; then
  222. ln -s /usr/include/x86_64-linux-gnu/curl /usr/local/include/curl
  223. fi
  224. fi
  225. fi
  226. # Set git repository raw path
  227. GIT_REP='https://raw.githubusercontent.com/'$REPO'/'$branch'/src/deb'
  228. # Generate Links for sourcecode
  229. HESTIA_ARCHIVE_LINK='https://github.com/'$REPO'/archive/'$branch'.tar.gz'
  230. echo $HESTIA_ARCHIVE_LINK
  231. echo "Building Hestia Control Panel package..."
  232. BUILD_DIR_HESTIA=$BUILD_DIR/hestia_$HESTIA_V
  233. # Change to build directory
  234. cd $BUILD_DIR
  235. if [ "$KEEPBUILD" != 'true' ] || [ ! -d "$BUILD_DIR_HESTIA" ]; then
  236. # Check if target directory exist
  237. if [ -d $BUILD_DIR_HESTIA ]; then
  238. rm -r $BUILD_DIR_HESTIA
  239. fi
  240. # Create directory
  241. mkdir -p $BUILD_DIR_HESTIA
  242. fi
  243. cd $BUILD_DIR
  244. rm -rf $BUILD_DIR/hestiacp-$branch_dash
  245. # Download and unpack source files
  246. if [ "$use_src_folder" == 'true' ]; then
  247. [ "$HESTIA_DEBUG" ] && echo DEBUG: cp -rf "$SRC_DIR/" $BUILD_DIR/hestiacp-$branch_dash
  248. cp -rf "$SRC_DIR/" $BUILD_DIR/hestiacp-$branch_dash
  249. elif [ -d $SRC_DIR ]; then
  250. download_file $HESTIA_ARCHIVE_LINK '-' 'fresh' | tar xz
  251. fi
  252. mkdir -p $BUILD_DIR_HESTIA/usr/local/hestia
  253. # Move needed directories
  254. cd $BUILD_DIR/hestiacp-$branch_dash
  255. cp -rf bin func install web $BUILD_DIR_HESTIA/usr/local/hestia/
  256. # Set permissions
  257. find $BUILD_DIR_HESTIA/usr/local/hestia/ -type f -exec chmod -x {} \;
  258. # Allow send email via /usr/local/hestia/web/inc/mail-wrapper.php via cli
  259. chmod +x $BUILD_DIR_HESTIA/usr/local/hestia/web/inc/mail-wrapper.php
  260. # Allow the executable to be executed
  261. chmod +x $BUILD_DIR_HESTIA/usr/local/hestia/bin/*
  262. find $BUILD_DIR_HESTIA/usr/local/hestia/install/ \( -name '*.sh' \) -exec chmod +x {} \;
  263. chmod -x $BUILD_DIR_HESTIA/usr/local/hestia/install/*.sh
  264. chown -R root:root $BUILD_DIR_HESTIA
  265. if [ "$BUILD_DEB" = true ]; then
  266. # Get Debian package files
  267. mkdir -p $BUILD_DIR_HESTIA/DEBIAN
  268. get_branch_file 'src/deb/hestia/control' "$BUILD_DIR_HESTIA/DEBIAN/control"
  269. if [ "$BUILD_ARCH" != "amd64" ]; then
  270. sed -i "s/amd64/${BUILD_ARCH}/g" "$BUILD_DIR_HESTIA/DEBIAN/control"
  271. fi
  272. get_branch_file 'src/deb/hestia/copyright' "$BUILD_DIR_HESTIA/DEBIAN/copyright"
  273. get_branch_file 'src/deb/hestia/preinst' "$BUILD_DIR_HESTIA/DEBIAN/preinst"
  274. get_branch_file 'src/deb/hestia/postinst' "$BUILD_DIR_HESTIA/DEBIAN/postinst"
  275. chmod +x $BUILD_DIR_HESTIA/DEBIAN/postinst
  276. chmod +x $BUILD_DIR_HESTIA/DEBIAN/preinst
  277. echo Building Hestia DEB
  278. dpkg-deb -Zxz --build $BUILD_DIR_HESTIA $DEB_DIR
  279. fi
  280. if [ "$BUILD_RPM" = true ]; then
  281. # Get RHEL package files
  282. get_branch_file 'src/rpm/hestia/hestia.spec' "${BUILD_DIR_HESTIA}/hestia.spec"
  283. sed -i "s/%HESTIA-VERSION%/${HESTIA_V}/g" "${BUILD_DIR_HESTIA}/hestia.spec"
  284. get_branch_file 'src/rpm/hestia/hestia.service' "${BUILD_DIR_HESTIA}/hestia.service"
  285. # Build RPM package
  286. mkdir -p $BUILD_DIR/rpmbuild
  287. echo Building Hestia RPM
  288. rpmbuild -bb --define "sourcedir $BUILD_DIR_HESTIA" --buildroot=$BUILD_DIR/rpmbuild/ ${BUILD_DIR_HESTIA}/hestia.spec > ${BUILD_DIR_HESTIA}.rpm.log
  289. cp ~/rpmbuild/RPMS/$(arch)/hestia-*.rpm $RPM_DIR
  290. rm ~/rpmbuild/RPMS/$(arch)/hestia-*.rpm
  291. rm -rf $BUILD_DIR/rpmbuild
  292. fi
  293. # clear up the source folder
  294. if [ "$KEEPBUILD" != 'true' ]; then
  295. rm -r $BUILD_DIR_HESTIA
  296. rm -rf hestiacp-$branch_dash
  297. fi
  298. cd $BUILD_DIR/hestiacp-$branch_dash
  299. # Installation steps
  300. if [ "$install" = "install" ] || [ "$install" = "yes" ] || [ "$install" = "install-auto" ]; then
  301. install_build
  302. if [ "$install" = "install-auto" ]; then
  303. $HESTIA/bin/v-add-cron-hestia-autoupdate git
  304. fi
  305. else
  306. warning_message
  307. read -p "Do you wish to proceed with the installation? [y/n] " answer
  308. if [ "$answer" = 'y' ] || [ "$answer" = 'Y' ]; then
  309. install_build
  310. unset $answer
  311. else
  312. echo "Installation of development build aborted."
  313. echo "Removing temporary files..."
  314. rm -rf $BUILD_DIR
  315. unset "$answer"
  316. echo ""
  317. fi
  318. fi