freedombone-utils-selector 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Functions for selecting which apps to install or remove
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2015-2016 Bob Mottram <bob@robotics.uk.to>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. # Array containing names of available apps
  31. APPS_AVAILABLE=()
  32. # Array containing 1 or 0 indicating installed apps
  33. APPS_INSTALLED=()
  34. # Apps selected with checklist
  35. APPS_CHOSEN=()
  36. # A list of the names of installed apps
  37. APPS_INSTALLED_NAMES=()
  38. # file containing a list of removed apps
  39. REMOVED_APPS_FILE=/root/removed
  40. INSTALLED_APPS_LIST=/usr/share/${PROJECT_NAME}/installed.txt
  41. if [ ! $COMPLETION_FILE ]; then
  42. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  43. fi
  44. # Loads variables defined at the beginning of an app script
  45. function app_load_variables {
  46. app_name=$1
  47. config_var_name=${app_name}_variables
  48. if [ ! ${!config_var_name} ]; then
  49. echo $"${app_name}_variables was not found"
  50. return
  51. fi
  52. configvarname=$config_var_name[@]
  53. configvarname=( ${!configvarname} )
  54. for v in "${configvarname[@]}"
  55. do
  56. read_config_param $v
  57. done
  58. }
  59. # Saves variables for a given app script
  60. function app_save_variables {
  61. app_name=$1
  62. config_var_name=${app_name}_variables
  63. if [ ! ${!config_var_name} ]; then
  64. return
  65. fi
  66. configvarname=$config_var_name[@]
  67. configvarname=( ${!configvarname} )
  68. for v in "${configvarname[@]}"
  69. do
  70. write_config_param $v "${!v}"
  71. done
  72. }
  73. # gets the variants list from an app script
  74. function app_variants {
  75. filename=$1
  76. variants_line=$(cat ${filename} | grep 'VARIANTS=')
  77. if [[ "$variants_line" == *"'"* ]]; then
  78. variants_list=$(echo "$variants_line" | awk -F '=' '{print $2}' | awk -F "'" '{print $2}')
  79. else
  80. variants_list=$(echo "$variants_line" | awk -F '=' '{print $2}' | awk -F '"' '{print $2}')
  81. fi
  82. echo "$variants_list"
  83. }
  84. # whether a given item is in an array
  85. function item_in_array {
  86. local e
  87. for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
  88. return 1
  89. }
  90. # returns a list of available system variants
  91. # based upon the variants string in each app script
  92. function available_system_variants {
  93. function_check item_in_array
  94. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  95. new_available_variants_list=()
  96. for filename in $FILES
  97. do
  98. system_variants_list=$(app_variants $filename)
  99. variants_array=($system_variants_list)
  100. for variant_str in "${variants_array[@]}"
  101. do
  102. item_in_array "${variant_str}" "${new_available_variants_list[@]}"
  103. if [[ $? != 0 ]]; then
  104. new_available_variants_list+=("$variant_str")
  105. fi
  106. done
  107. done
  108. available_variants_list=($(sort <<<"${new_available_variants_list[*]}"))
  109. }
  110. function is_valid_variant {
  111. sys_type="$1"
  112. available_variants_list=()
  113. function_check available_system_variants
  114. available_system_variants
  115. for variant_str in "${available_variants_list[@]}"
  116. do
  117. if [[ "$sys_type" == "$variant_str" ]]; then
  118. return "1"
  119. fi
  120. done
  121. return "0"
  122. }
  123. function show_available_variants {
  124. available_variants_list=()
  125. function_check available_system_variants
  126. available_system_variants
  127. for variant_str in "${available_variants_list[@]}"
  128. do
  129. echo " $variant_str"
  130. done
  131. }
  132. # mark a given app as having been removed so that it doesn't get reinstalled on updates
  133. function remove_app {
  134. app_name=$1
  135. if [ ! -f $REMOVED_APPS_FILE ]; then
  136. touch $REMOVED_APPS_FILE
  137. fi
  138. if ! grep -Fxq "_${app_name}_" $REMOVED_APPS_FILE; then
  139. echo "_${app_name}_" >> $REMOVED_APPS_FILE
  140. fi
  141. if grep -Fxq "install_${app_name}" $COMPLETION_FILE; then
  142. sed -i "/install_${app_name}/d" $COMPLETION_FILE
  143. fi
  144. if grep -Fxq "install_${app_name}" $INSTALLED_APPS_LIST; then
  145. sed -i "/install_${app_name}/d" $INSTALLED_APPS_LIST
  146. fi
  147. }
  148. # returns 1 if an app has been marked as removed
  149. function app_is_removed {
  150. app_name="$1"
  151. if [ ! -f $REMOVED_APPS_FILE ]; then
  152. echo "0"
  153. return
  154. fi
  155. if ! grep -Fxq "_${app_name}_" $REMOVED_APPS_FILE; then
  156. echo "0"
  157. else
  158. echo "1"
  159. fi
  160. }
  161. # Allows an app to be reinstalled even if it was previously marked as being removed
  162. function reinstall_app {
  163. app_name=$1
  164. if [ ! -f $REMOVED_APPS_FILE ]; then
  165. return
  166. fi
  167. if [[ $(app_is_removed $app_name) == "1" ]]; then
  168. sed -i "/_${app_name}_/d" $REMOVED_APPS_FILE
  169. fi
  170. }
  171. # returns 1 if an app is installed
  172. function app_is_installed {
  173. app_name="$1"
  174. # Why does this secondary file exist, apart from COMPLETION_FILE ?
  175. # It's so that it is visible to unprivileged users from the user control panel
  176. if [ -f $INSTALLED_APPS_LIST ]; then
  177. if ! grep -Fxq "install_${app_name}" $INSTALLED_APPS_LIST; then
  178. echo "0"
  179. else
  180. echo "1"
  181. fi
  182. return
  183. fi
  184. # check the completion file to see if it was installed
  185. if [ ! -f $COMPLETION_FILE ]; then
  186. echo "0"
  187. return
  188. fi
  189. if ! grep -Fxq "install_${app_name}" $COMPLETION_FILE; then
  190. echo "0"
  191. else
  192. echo "1"
  193. fi
  194. }
  195. # called at the end of the install section of an app script
  196. function install_completed {
  197. if [ ! ${1} ]; then
  198. exit 673935
  199. fi
  200. if ! grep -Fxq "install_${1}" $COMPLETION_FILE; then
  201. echo "install_${1}" >> $COMPLETION_FILE
  202. fi
  203. }
  204. # populates an array of "0" or "1" for whether apps are installed
  205. function get_apps_installed {
  206. for a in "${APPS_AVAILABLE[@]}"
  207. do
  208. APPS_INSTALLED+=("$(app_is_installed $a)")
  209. done
  210. }
  211. # populates an array of installed app names
  212. function get_apps_installed_names {
  213. APPS_INSTALLED_NAMES=()
  214. for a in "${APPS_AVAILABLE[@]}"
  215. do
  216. if [[ $(app_is_installed $a) == "1" ]]; then
  217. APPS_INSTALLED_NAMES+=("$a")
  218. fi
  219. done
  220. }
  221. # detects what apps are available
  222. function detect_apps {
  223. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  224. function_check item_in_array
  225. APPS_AVAILABLE=()
  226. APPS_CHOSEN=()
  227. # for all the app scripts
  228. for filename in $FILES
  229. do
  230. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  231. item_in_array "${app_name}" "${APPS_AVAILABLE[@]}"
  232. if [[ $? != 0 ]]; then
  233. APPS_AVAILABLE+=("${app_name}")
  234. APPS_CHOSEN+=("0")
  235. fi
  236. done
  237. function_check get_apps_installed
  238. get_apps_installed
  239. get_apps_installed_names
  240. }
  241. # detects what apps are available and can be installed
  242. # If the variants list within an app script is an empty string then
  243. # it is considered to be too experimental to be installable
  244. function detect_installable_apps {
  245. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  246. APPS_AVAILABLE=()
  247. APPS_CHOSEN=()
  248. APPS_INSTALLED=()
  249. APPS_INSTALLED_NAMES=()
  250. function_check app_variants
  251. function_check app_is_installed
  252. function_check item_in_array
  253. # for all the app scripts
  254. for filename in $FILES
  255. do
  256. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  257. item_in_array "${app_name}" "${APPS_AVAILABLE[@]}"
  258. if [[ $? != 0 ]]; then
  259. variants_list=$(app_variants $filename)
  260. # check for empty string
  261. if [ ${#variants_list} -gt 0 ]; then
  262. APPS_AVAILABLE+=("${app_name}")
  263. APPS_CHOSEN+=("0")
  264. APPS_INSTALLED+=("$(app_is_installed $app_name)")
  265. if [[ $(app_is_installed $app_name) == "1" ]]; then
  266. APPS_INSTALLED_NAMES+=("$app_name")
  267. fi
  268. fi
  269. fi
  270. done
  271. }
  272. # creates the APPS_AVAILABLE and APPS_CHOSEN arrays based on
  273. # the given variant name
  274. function choose_apps_for_variant {
  275. variant_name="$1"
  276. function_check item_in_array
  277. function_check app_variants
  278. function_check app_is_removed
  279. if [ ${#variant_name} -eq 0 ]; then
  280. echo $"No variant name for choosing apps"
  281. exit 237567
  282. fi
  283. FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  284. APPS_CHOSEN=()
  285. # for all the app scripts
  286. for filename in $FILES
  287. do
  288. app_name=$(echo "${filename}" | awk -F '-app-' '{print $2}')
  289. item_in_array "${app_name}" "${APPS_AVAILABLE[@]}"
  290. if [[ $? == 0 ]]; then
  291. if grep -q "VARIANTS=" ${filename}; then
  292. variants_list=$(app_variants $filename)
  293. if [[ "${variants_list}" == 'all'* || \
  294. "${variants_list}" == "$variant_name" || \
  295. "${variants_list}" == "$variant_name "* || \
  296. "${variants_list}" == *" $variant_name "* || \
  297. "${variants_list}" == *" $variant_name" ]]; then
  298. if [[ $(app_is_removed ${a}) == "0" ]]; then
  299. echo $"${app_name} chosen"
  300. APPS_CHOSEN+=("1")
  301. else
  302. APPS_CHOSEN+=("0")
  303. fi
  304. else
  305. APPS_CHOSEN+=("0")
  306. fi
  307. else
  308. APPS_CHOSEN+=("0")
  309. fi
  310. fi
  311. done
  312. function_check get_apps_installed
  313. get_apps_installed
  314. }
  315. # show a list of apps which have been chosen
  316. function list_chosen_apps {
  317. app_index=0
  318. for a in "${APPS_AVAILABLE[@]}"
  319. do
  320. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  321. echo $"${a}"
  322. fi
  323. app_index=$[app_index+1]
  324. done
  325. }
  326. function remove_apps {
  327. app_index=0
  328. for a in "${APPS_AVAILABLE[@]}"
  329. do
  330. if [[ ${APPS_INSTALLED[$app_index]} == "1" ]]; then
  331. if [[ ${APPS_CHOSEN[$app_index]} == "0" ]]; then
  332. echo $"Removing application: ${a}"
  333. function_check app_load_variables
  334. app_load_variables ${a}
  335. function_check remove_app
  336. remove_app ${a}
  337. function_check remove_${a}
  338. remove_${a}
  339. echo $"${a} was removed"
  340. fi
  341. fi
  342. app_index=$[app_index+1]
  343. done
  344. update_installed_apps_list
  345. }
  346. function install_apps_interactive {
  347. echo $"Interactive installer"
  348. app_index=0
  349. for a in "${APPS_AVAILABLE[@]}"
  350. do
  351. if [[ ${APPS_INSTALLED[$app_index]} == "0" ]]; then
  352. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  353. # interactively obtain settings for this app
  354. if [[ $(function_exists install_interactive_${a}) == "1" ]]; then
  355. install_interactive_${a}
  356. fi
  357. fi
  358. fi
  359. app_index=$[app_index+1]
  360. done
  361. echo $"Interactive settings complete"
  362. }
  363. function install_apps {
  364. is_interactive=$1
  365. APP_INSTALLED_SUCCESS=1
  366. if [ -f /tmp/failed_apps ]; then
  367. rm /tmp/failed_apps
  368. fi
  369. # interactive install configuration for each app
  370. if [ ${is_interactive} ]; then
  371. install_apps_interactive
  372. fi
  373. # now install the apps
  374. app_index=0
  375. for a in "${APPS_AVAILABLE[@]}"
  376. do
  377. if [[ ${APPS_INSTALLED[$app_index]} == "0" ]]; then
  378. if [[ ${APPS_CHOSEN[$app_index]} == "1" ]]; then
  379. if [ ${is_interactive} ]; then
  380. # clears any removal indicator
  381. function_check reinstall_app
  382. reinstall_app ${a}
  383. function_check app_load_variables
  384. app_load_variables ${a}
  385. if [[ $(app_is_installed ${a}) == "1" ]]; then
  386. echo $"Upgrading application from interactive: ${a}"
  387. upgrade_${a}
  388. echo $"${a} was upgraded from interactive"
  389. else
  390. echo $"Installing application from interactive: ${a}"
  391. APP_INSTALLED=
  392. install_${a}
  393. if [ $APP_INSTALLED ]; then
  394. function_check app_save_variables
  395. app_save_variables ${a}
  396. function_check install_completed
  397. install_completed ${a}
  398. echo $"${a} was installed from interactive"
  399. else
  400. echo " ${a}" >> /tmp/failed_apps
  401. APP_INSTALLED_SUCCESS=
  402. echo $"${a} was not installed from interactive"
  403. fi
  404. fi
  405. else
  406. # check if the app was removed
  407. if [[ $(app_is_removed ${a}) == "0" ]]; then
  408. function_check app_load_variables
  409. app_load_variables ${a}
  410. if [[ $(app_is_installed ${a}) == "1" ]]; then
  411. echo $"Upgrading application: ${a}"
  412. upgrade_${a}
  413. echo $"${a} was upgraded"
  414. else
  415. echo $"Installing application: ${a}"
  416. APP_INSTALLED=
  417. install_${a}
  418. if [ $APP_INSTALLED ]; then
  419. function_check app_save_variables
  420. app_save_variables ${a}
  421. function_check install_completed
  422. install_completed ${a}
  423. echo $"${a} was installed"
  424. else
  425. echo " ${a}" >> /tmp/failed_apps
  426. APP_INSTALLED_SUCCESS=
  427. echo $"${a} was not installed"
  428. fi
  429. fi
  430. else
  431. echo $"${a} has been removed and so will not be reinstalled"
  432. fi
  433. fi
  434. fi
  435. fi
  436. app_index=$[app_index+1]
  437. done
  438. function_check update_installed_apps_list
  439. update_installed_apps_list
  440. }
  441. # NOTE: deliberately no exit 0