imgproxy-build-package 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #!/bin/bash
  2. set -e
  3. print_usage() {
  4. echo "Usage: $0 <package-type> <target-dir>"
  5. echo " package-type: Package type to build (deb, rpm, tar)"
  6. echo " target-dir: Target directory to save the package"
  7. }
  8. PACKAGE_TYPE=$1
  9. TARGET_DIR=$2
  10. if [ -z "$PACKAGE_TYPE" ] || [ -z "$TARGET_DIR" ]; then
  11. print_usage
  12. exit 1
  13. fi
  14. if [ "$PACKAGE_TYPE" != "deb" ] && [ "$PACKAGE_TYPE" != "rpm" ] && [ "$PACKAGE_TYPE" != "tar" ]; then
  15. echo "Unknown package type: $PACKAGE_TYPE"
  16. print_usage
  17. exit 1
  18. fi
  19. if [ ! -d "$TARGET_DIR" ]; then
  20. echo "Target path does not exist or is not a directory: $TARGET_DIR"
  21. exit 1
  22. fi
  23. if [ ! -w "$TARGET_DIR" ]; then
  24. echo "Target path is not writable: $TARGET_DIR"
  25. exit 1
  26. fi
  27. TARGET_DIR=$(realpath $TARGET_DIR)
  28. BUILDDIR=$(mktemp --tmpdir -d imgproxy.XXXXXXXX)
  29. trap "rm -rf $BUILDDIR" EXIT
  30. echo
  31. echo "Building in $BUILDDIR, target path: $TARGET_DIR"
  32. cd $BUILDDIR
  33. # ==============================================================================
  34. # Copy the package content
  35. echo
  36. echo "Copying the package content..."
  37. mkdir -p opt/imgproxy
  38. mkdir -p opt/imgproxy/libexec
  39. cp -r /opt/imgproxy/lib opt/imgproxy/
  40. cp -r /opt/imgproxy/bin/imgproxy opt/imgproxy/libexec/
  41. if [ -d /opt/imgproxy/share ]; then
  42. cp -r /opt/imgproxy/share opt/imgproxy/
  43. fi
  44. # ==============================================================================
  45. # Patching RPATHs
  46. echo
  47. echo "Patching RPATHs..."
  48. # Install patchelf
  49. apt-get update
  50. DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends patchelf
  51. # Patch the RPATH of the imgproxy binary
  52. patchelf --set-rpath '$ORIGIN/../lib' opt/imgproxy/libexec/imgproxy
  53. # Path the RPATH of the libvips shared libraries
  54. find opt/imgproxy/lib -type f -regex '.*/[^/]*\.so\(\.[^/]*\)?' -exec patchelf --set-rpath '$ORIGIN' {} \;
  55. # ==============================================================================
  56. # Build the RC file
  57. echo
  58. echo "Building the RC files..."
  59. mkdir -p opt/imgproxy/etc
  60. SYS_RC_FILE=opt/imgproxy/etc/imgproxy.sys.rc
  61. cat << 'EOF' > $SYS_RC_FILE
  62. #!/usr/bin/env bash
  63. set -e
  64. # !!!DO NOT EDIT THIS FILE!!!
  65. #
  66. # This file is generated by the imgproxy package build script and will be
  67. # overwritten on the next installation. Use imgproxy.rc instead.
  68. IMGPROXY_ROOT=$(readlink -f "$(dirname $(readlink -f "$0"))/..")
  69. EOF
  70. # Dump all IMGPROXY_ and VIPS_ environment variables to the launch script.
  71. # Declare them as exported variables with the default value if they are not set.
  72. ENV_VARS=$(awk 'BEGIN{for(v in ENVIRON) if (match(v, "^(IMGPROXY_|VIPS_).+") != 0) print v}')
  73. for VAR in $ENV_VARS; do
  74. # IMGPROXY_MALLOC doesn't work outside of the container
  75. if [ "$VAR" != "IMGPROXY_MALLOC" ]; then
  76. printf '%s=${%s:-%q}\n' "$VAR" "$VAR" "${!VAR}" >> $SYS_RC_FILE;
  77. echo "export $VAR=\${$VAR//\"/opt/imgproxy\"/\"\$IMGPROXY_ROOT\"}" >> $SYS_RC_FILE
  78. echo >> $SYS_RC_FILE
  79. fi
  80. done
  81. RC_FILE=opt/imgproxy/etc/imgproxy.rc
  82. cat << 'EOF' > $RC_FILE
  83. #!/usr/bin/env bash
  84. set -e
  85. # IMGPROXY CONFIGURATION FILE
  86. # ===========================
  87. #
  88. # This file is sourced by the imgproxy launch script.
  89. # You can use it to set environment variables for imgproxy using `export`:
  90. #
  91. # export IMGPROXY_MAX_SRC_RESOLUTION=99
  92. #
  93. # See https://docs.imgproxy.net/configuration/options for more information
  94. #
  95. EOF
  96. # ==============================================================================
  97. # Build the launch script
  98. echo
  99. echo "Building the launch script..."
  100. mkdir -p opt/imgproxy/bin
  101. LAUNCH_SCRIPT=opt/imgproxy/bin/imgproxy
  102. cat << 'EOF' > $LAUNCH_SCRIPT
  103. #!/usr/bin/env bash
  104. set -e
  105. SCRIPT_DIR=$(dirname $(readlink -f "$0"))
  106. IMGPROXY_SYS_RC=$(readlink -f "$SCRIPT_DIR/../etc/imgproxy.sys.rc")
  107. if [ -f "$IMGPROXY_SYS_RC" ]; then
  108. source $IMGPROXY_SYS_RC
  109. fi
  110. IMGPROXY_RC=$(readlink -f "$SCRIPT_DIR/../etc/imgproxy.rc")
  111. if [ -f "$IMGPROXY_RC" ]; then
  112. source $IMGPROXY_RC
  113. fi
  114. if [ -f "/etc/imgproxy.rc" ] && [ "$(readlink -f "/etc/imgproxy.rc")" != "$IMGPROXY_RC" ]; then
  115. source /etc/imgproxy.rc
  116. fi
  117. IMGPROXY_BIN=$(readlink -f "$SCRIPT_DIR/../libexec/imgproxy")
  118. $IMGPROXY_BIN $@
  119. EOF
  120. # Make the launch script executable
  121. chmod +x $LAUNCH_SCRIPT
  122. # Test the launch script
  123. echo -n "Testing the launch script... "
  124. if $LAUNCH_SCRIPT version > /dev/null; then
  125. echo "Success"
  126. else
  127. echo "Failed"
  128. fi
  129. # ==============================================================================
  130. # Packaging
  131. echo
  132. echo "Packaging..."
  133. VERSION=$(imgproxy version)
  134. RELEASE=$(printf '%(%y%m%d%H%M)T\n' -1)
  135. case $PACKAGE_TYPE in
  136. # TAR package ----------------------------------------------------------------
  137. tar)
  138. RESULT_FILE=imgproxy-$VERSION-$RELEASE.$(dpkg --print-architecture).tar.gz
  139. tar -czf $RESULT_FILE -C opt imgproxy
  140. cp $RESULT_FILE $TARGET_DIR
  141. ;;
  142. # DEB package ----------------------------------------------------------------
  143. deb)
  144. DEB_ARCH=$(dpkg --print-architecture)
  145. RESULT_FILE=imgproxy-$VERSION-$RELEASE.$DEB_ARCH.deb
  146. mkdir -p imgproxy
  147. mv opt imgproxy/
  148. mkdir -p imgproxy/usr/bin
  149. ln -s /opt/imgproxy/bin/imgproxy imgproxy/usr/bin/imgproxy
  150. mkdir -p imgproxy/DEBIAN
  151. cat << EOF > imgproxy/DEBIAN/control
  152. Package: imgproxy
  153. Version: $VERSION-$RELEASE
  154. Section: base
  155. Priority: optional
  156. Architecture: $DEB_ARCH
  157. Depends: bash, libc6, libstdc++6, fontconfig-config, ca-certificates, media-types
  158. Maintainer: Foxes With Matches <info@imgproxy.net>
  159. Homepage: https://imgproxy.net
  160. Description: imgproxy
  161. imgproxy is a fast and secure standalone server for resizing and converting remote images.
  162. EOF
  163. cat << 'EOF' > imgproxy/DEBIAN/conffiles
  164. /opt/imgproxy/etc/imgproxy.rc
  165. EOF
  166. cat << 'EOF' > imgproxy/DEBIAN/postinst
  167. #!/bin/bash
  168. set -e
  169. if [ ! -f /etc/imgproxy.rc ]; then
  170. ln -s /opt/imgproxy/etc/imgproxy.rc /etc/imgproxy.rc
  171. fi
  172. echo
  173. echo "imgproxy installed successfully!"
  174. echo "You can edit /etc/imgproxy.rc to configure imgproxy or set imgproxy environment variables in your shell profile."
  175. echo "See https://docs.imgproxy.net/configuration/options for more information."
  176. EOF
  177. chmod +x imgproxy/DEBIAN/postinst
  178. cat << 'EOF' > imgproxy/DEBIAN/prerm
  179. #!/bin/bash
  180. set -e
  181. if [ -d /opt/imgproxy/var ]; then
  182. rm -rf /opt/imgproxy/var
  183. fi
  184. if [ "$(readlink -f /etc/imgproxy.rc)" == "$(readlink -f /opt/imgproxy/etc/imgproxy.rc)" ]; then
  185. rm /etc/imgproxy.rc
  186. fi
  187. EOF
  188. chmod +x imgproxy/DEBIAN/prerm
  189. dpkg-deb --build imgproxy $RESULT_FILE
  190. cp $RESULT_FILE $TARGET_DIR
  191. ;;
  192. # RPM package ----------------------------------------------------------------
  193. rpm)
  194. DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends rpm
  195. RPM_ARCH=$(rpm --eval '%{_arch}')
  196. RESULT_FILE=imgproxy-$VERSION-$RELEASE.$RPM_ARCH.rpm
  197. mkdir -p rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
  198. mkdir -p rpmbuild/SOURCES/imgproxy
  199. mv opt rpmbuild/SOURCES/imgproxy/
  200. mkdir -p rpmbuild/SOURCES/imgproxy/usr/bin
  201. ln -s /opt/imgproxy/bin/imgproxy rpmbuild/SOURCES/imgproxy/usr/bin/imgproxy
  202. cat << 'EOF' > rpmbuild/SPECS/imgproxy.spec
  203. Name: imgproxy
  204. Version: %{version}
  205. Release: %{release}
  206. Summary: imgproxy
  207. License: MIT
  208. URL: https://imgproxy.net
  209. Group: Applications/Internet
  210. BuildArch: %{_arch}
  211. AutoReqProv: no
  212. Requires: bash
  213. Requires: glibc
  214. Requires: libstdc++
  215. Requires: fontconfig
  216. Requires: ca-certificates
  217. Requires: shared-mime-info
  218. Requires(interp): /bin/bash
  219. Requires(post): /bin/bash
  220. Requires(preun): /bin/bash
  221. %description
  222. imgproxy is a fast and secure standalone server for resizing and converting remote images.
  223. %prep
  224. mkdir -p $RPM_BUILD_ROOT
  225. mv %{_sourcedir}/imgproxy/opt $RPM_BUILD_ROOT/opt
  226. mv %{_sourcedir}/imgproxy/usr $RPM_BUILD_ROOT/usr
  227. %files
  228. /opt/imgproxy
  229. /usr/bin/imgproxy
  230. %config(noreplace) /opt/imgproxy/etc/imgproxy.rc
  231. %post
  232. if [ ! -f /etc/imgproxy.rc ]; then
  233. ln -s /opt/imgproxy/etc/imgproxy.rc /etc/imgproxy.rc
  234. fi
  235. %preun
  236. if [ -d /opt/imgproxy/var ]; then
  237. rm -rf /opt/imgproxy/var
  238. fi
  239. if [ "$(readlink -f /etc/imgproxy.rc)" == "$(readlink -f /opt/imgproxy/etc/imgproxy.rc)" ]; then
  240. rm /etc/imgproxy.rc
  241. fi
  242. %clean
  243. rm -rf $RPM_BUILD_ROOT
  244. exit
  245. EOF
  246. rpmbuild \
  247. --define "_topdir $(pwd)/rpmbuild" \
  248. --define "version $VERSION" \
  249. --define "release $RELEASE" \
  250. -bb rpmbuild/SPECS/imgproxy.spec
  251. cp rpmbuild/RPMS/$RPM_ARCH/$RESULT_FILE $TARGET_DIR
  252. ;;
  253. *)
  254. echo "Unknown package type: $PACKAGE_TYPE"
  255. print_usage
  256. exit 1
  257. ;;
  258. esac
  259. echo
  260. echo "Done!"
  261. echo "Package is saved to $(realpath "$TARGET_DIR/$RESULT_FILE")"
  262. echo