ynh_add_extra_apt_repos__3 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #!/bin/bash
  2. # Pin a repository.
  3. #
  4. # usage: ynh_pin_repo --package=packages --pin=pin_filter [--priority=priority_value] [--name=name] [--append]
  5. # | arg: -p, --package - Packages concerned by the pin. Or all, *.
  6. # | arg: -i, --pin - Filter for the pin.
  7. # | arg: -p, --priority - Priority for the pin
  8. # | arg: -n, --name - Name for the files for this repo, $app as default value.
  9. # | arg: -a, --append - Do not overwrite existing files.
  10. #
  11. # See https://manpages.debian.org/stretch/apt/apt_preferences.5.en.html for information about pinning.
  12. #
  13. ynh_pin_repo () {
  14. # Declare an array to define the options of this helper.
  15. local legacy_args=pirna
  16. declare -Ar args_array=( [p]=package= [i]=pin= [r]=priority= [n]=name= [a]=append )
  17. local package
  18. local pin
  19. local priority
  20. local name
  21. local append
  22. # Manage arguments with getopts
  23. ynh_handle_getopts_args "$@"
  24. package="${package:-*}"
  25. priority=${priority:-50}
  26. name="${name:-$app}"
  27. append=${append:-0}
  28. if [ $append -eq 1 ]
  29. then
  30. append="tee -a"
  31. else
  32. append="tee"
  33. fi
  34. mkdir -p "/etc/apt/preferences.d"
  35. echo "Package: $package
  36. Pin: $pin
  37. Pin-Priority: $priority" \
  38. | $append "/etc/apt/preferences.d/$name"
  39. }
  40. # Add a repository.
  41. #
  42. # usage: ynh_add_repo --uri=uri --suite=suite --component=component [--name=name] [--append]
  43. # | arg: -u, --uri - Uri of the repository.
  44. # | arg: -s, --suite - Suite of the repository.
  45. # | arg: -c, --component - Component of the repository.
  46. # | arg: -n, --name - Name for the files for this repo, $app as default value.
  47. # | arg: -a, --append - Do not overwrite existing files.
  48. #
  49. # Example for a repo like deb http://forge.yunohost.org/debian/ stretch stable
  50. # uri suite component
  51. # ynh_add_repo --uri=http://forge.yunohost.org/debian/ --suite=stretch --component=stable
  52. #
  53. ynh_add_repo () {
  54. # Declare an array to define the options of this helper.
  55. local legacy_args=uscna
  56. declare -Ar args_array=( [u]=uri= [s]=suite= [c]=component= [n]=name= [a]=append )
  57. local uri
  58. local suite
  59. local component
  60. local name
  61. local append
  62. # Manage arguments with getopts
  63. ynh_handle_getopts_args "$@"
  64. name="${name:-$app}"
  65. append=${append:-0}
  66. if [ $append -eq 1 ]
  67. then
  68. append="tee -a"
  69. else
  70. append="tee"
  71. fi
  72. mkdir -p "/etc/apt/sources.list.d"
  73. # Add the new repo in sources.list.d
  74. echo "deb $uri $suite $component" \
  75. | $append "/etc/apt/sources.list.d/$name.list"
  76. }
  77. # Add an extra repository correctly, pin it and get the key.
  78. #
  79. # usage: ynh_install_extra_repo --repo="repo" [--key=key_url] [--priority=priority_value] [--name=name] [--append]
  80. # | arg: -r, --repo - Complete url of the extra repository.
  81. # | arg: -k, --key - url to get the public key.
  82. # | arg: -p, --priority - Priority for the pin
  83. # | arg: -n, --name - Name for the files for this repo, $app as default value.
  84. # | arg: -a, --append - Do not overwrite existing files.
  85. ynh_install_extra_repo () {
  86. # Declare an array to define the options of this helper.
  87. local legacy_args=rkpna
  88. declare -Ar args_array=( [r]=repo= [k]=key= [p]=priority= [n]=name= [a]=append )
  89. local repo
  90. local key
  91. local priority
  92. local name
  93. local append
  94. # Manage arguments with getopts
  95. ynh_handle_getopts_args "$@"
  96. name="${name:-$app}"
  97. append=${append:-0}
  98. key=${key:-0}
  99. priority=${priority:-}
  100. if [ $append -eq 1 ]
  101. then
  102. append="--append"
  103. wget_append="tee -a"
  104. else
  105. append=""
  106. wget_append="tee"
  107. fi
  108. # Split the repository into uri, suite and components.
  109. # Remove "deb " at the beginning of the repo.
  110. repo="${repo#deb }"
  111. # Get the uri
  112. local uri="$(echo "$repo" | awk '{ print $1 }')"
  113. # Get the suite
  114. local suite="$(echo "$repo" | awk '{ print $2 }')"
  115. # Get the components
  116. local component="${repo##$uri $suite }"
  117. # Add the repository into sources.list.d
  118. ynh_add_repo --uri="$uri" --suite="$suite" --component="$component" --name="$name" $append
  119. # Pin the new repo with the default priority, so it won't be used for upgrades.
  120. # Build $pin from the uri without http and any sub path
  121. local pin="${uri#*://}"
  122. pin="${pin%%/*}"
  123. # Set a priority only if asked
  124. if [ -n "$priority" ]
  125. then
  126. priority="--priority=$priority"
  127. fi
  128. ynh_pin_repo --package="*" --pin="origin \"$pin\"" $priority --name="$name" $append
  129. # Get the public key for the repo
  130. if [ -n "$key" ]
  131. then
  132. mkdir -p "/etc/apt/trusted.gpg.d"
  133. wget -q "$key" -O - | gpg --dearmor | $wget_append /etc/apt/trusted.gpg.d/$name.gpg > /dev/null
  134. fi
  135. # Update the list of package with the new repo
  136. ynh_package_update
  137. }
  138. # Remove an extra repository and the assiociated configuration.
  139. #
  140. # usage: ynh_remove_extra_repo [--name=name]
  141. # | arg: -n, --name - Name for the files for this repo, $app as default value.
  142. ynh_remove_extra_repo () {
  143. # Declare an array to define the options of this helper.
  144. local legacy_args=n
  145. declare -Ar args_array=( [n]=name= )
  146. local name
  147. # Manage arguments with getopts
  148. ynh_handle_getopts_args "$@"
  149. name="${name:-$app}"
  150. ynh_secure_remove "/etc/apt/sources.list.d/$name.list"
  151. ynh_secure_remove "/etc/apt/preferences.d/$name"
  152. ynh_secure_remove "/etc/apt/trusted.gpg.d/$name.gpg"
  153. ynh_secure_remove "/etc/apt/trusted.gpg.d/$name.asc"
  154. # Update the list of package to exclude the old repo
  155. ynh_package_update
  156. }
  157. # Install packages from an extra repository properly.
  158. #
  159. # usage: ynh_install_extra_app_dependencies --repo="repo" --package="dep1 dep2" [--key=key_url] [--name=name]
  160. # | arg: -r, --repo - Complete url of the extra repository.
  161. # | arg: -p, --package - The packages to install from this extra repository
  162. # | arg: -k, --key - url to get the public key.
  163. # | arg: -n, --name - Name for the files for this repo, $app as default value.
  164. ynh_install_extra_app_dependencies () {
  165. # Declare an array to define the options of this helper.
  166. local legacy_args=rpkn
  167. declare -Ar args_array=( [r]=repo= [p]=package= [k]=key= [n]=name= )
  168. local repo
  169. local package
  170. local key
  171. local name
  172. # Manage arguments with getopts
  173. ynh_handle_getopts_args "$@"
  174. name="${name:-$app}"
  175. key=${key:-0}
  176. # Set a key only if asked
  177. if [ -n "$key" ]
  178. then
  179. key="--key=$key"
  180. fi
  181. # Add an extra repository for those packages
  182. ynh_install_extra_repo --repo="$repo" $key --priority=995 --name=$name
  183. # Install requested dependencies from this extra repository.
  184. ynh_add_app_dependencies --package="$package"
  185. # Remove this extra repository after packages are installed
  186. ynh_remove_extra_repo --name=$app
  187. }
  188. #=================================================
  189. # patched version of ynh_install_app_dependencies to be used with ynh_add_app_dependencies
  190. # Define and install dependencies with a equivs control file
  191. # This helper can/should only be called once per app
  192. #
  193. # usage: ynh_install_app_dependencies dep [dep [...]]
  194. # | arg: dep - the package name to install in dependence
  195. # You can give a choice between some package with this syntax : "dep1|dep2"
  196. # Example : ynh_install_app_dependencies dep1 dep2 "dep3|dep4|dep5"
  197. # This mean in the dependence tree : dep1 & dep2 & (dep3 | dep4 | dep5)
  198. #
  199. # Requires YunoHost version 2.6.4 or higher.
  200. ynh_install_app_dependencies () {
  201. local dependencies=$@
  202. dependencies="$(echo "$dependencies" | sed 's/\([^\<=\>]\)\ \([^(]\)/\1, \2/g')"
  203. dependencies=${dependencies//|/ | }
  204. local manifest_path="../manifest.json"
  205. if [ ! -e "$manifest_path" ]; then
  206. manifest_path="../settings/manifest.json" # Into the restore script, the manifest is not at the same place
  207. fi
  208. local version=$(grep '\"version\": ' "$manifest_path" | cut -d '"' -f 4) # Retrieve the version number in the manifest file.
  209. if [ ${#version} -eq 0 ]; then
  210. version="1.0"
  211. fi
  212. local dep_app=${app//_/-} # Replace all '_' by '-'
  213. # Handle specific versions
  214. if [[ "$dependencies" =~ [\<=\>] ]]
  215. then
  216. # Replace version specifications by relationships syntax
  217. # https://www.debian.org/doc/debian-policy/ch-relationships.html
  218. # Sed clarification
  219. # [^(\<=\>] ignore if it begins by ( or < = >. To not apply twice.
  220. # [\<=\>] matches < = or >
  221. # \+ matches one or more occurence of the previous characters, for >= or >>.
  222. # [^,]\+ matches all characters except ','
  223. # Ex: package>=1.0 will be replaced by package (>= 1.0)
  224. dependencies="$(echo "$dependencies" | sed 's/\([^(\<=\>]\)\([\<=\>]\+\)\([^,]\+\)/\1 (\2 \3)/g')"
  225. fi
  226. cat > /tmp/${dep_app}-ynh-deps.control << EOF # Make a control file for equivs-build
  227. Section: misc
  228. Priority: optional
  229. Package: ${dep_app}-ynh-deps
  230. Version: ${version}
  231. Depends: ${dependencies}
  232. Architecture: all
  233. Description: Fake package for $app (YunoHost app) dependencies
  234. This meta-package is only responsible of installing its dependencies.
  235. EOF
  236. ynh_package_install_from_equivs /tmp/${dep_app}-ynh-deps.control \
  237. || ynh_die --message="Unable to install dependencies" # Install the fake package and its dependencies
  238. rm /tmp/${dep_app}-ynh-deps.control
  239. ynh_app_setting_set --app=$app --key=apt_dependencies --value="$dependencies"
  240. }
  241. ynh_add_app_dependencies () {
  242. # Declare an array to define the options of this helper.
  243. local legacy_args=pr
  244. declare -Ar args_array=( [p]=package= [r]=replace)
  245. local package
  246. local replace
  247. # Manage arguments with getopts
  248. ynh_handle_getopts_args "$@"
  249. replace=${replace:-0}
  250. local current_dependencies=""
  251. if [ $replace -eq 0 ]
  252. then
  253. local dep_app=${app//_/-} # Replace all '_' by '-'
  254. if ynh_package_is_installed --package="${dep_app}-ynh-deps"
  255. then
  256. current_dependencies="$(dpkg-query --show --showformat='${Depends}' ${dep_app}-ynh-deps) "
  257. fi
  258. current_dependencies=${current_dependencies// | /|}
  259. fi
  260. ynh_install_app_dependencies "${current_dependencies}${package}"
  261. }