joinApps.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/bin/bash
  2. # Blank portainer template (32 and 64 bits)
  3. json32='{"version":"2","templates":[]}'
  4. json64='{"version":"2","templates":[]}'
  5. # File variables
  6. appinfo='build/info.json'
  7. Oldtemplate32='pi-hosted_template/template/portainer-v2.json'
  8. template32='template/portainer-v2-arm32.json'
  9. template64='template/portainer-v2-arm64.json'
  10. # App info
  11. repo='https://github.com/pi-hosted/pi-hosted/blob/master/'
  12. header='<h3>Template created by Pi-Hosted Series</h3><b>Check our Github page: <a href="https://github.com/pi-hosted/pi-hosted" target="_blank">https://github.com/pi-hosted/pi-hosted</a></b><br>'
  13. # Run script from base directory
  14. scriptDir="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
  15. cd "$scriptDir/.." || exit
  16. # Parsing all json files in apps folder
  17. for app in template/apps/*.json; do
  18. # Adding app template to 32 and 64 bits variables
  19. appjson=$( cat "$app" )
  20. # Improve Notes
  21. note=$( echo "$appjson" | jq '.note' )
  22. # Clean Notes
  23. [ "$note" == "null" ] && unset note
  24. note=${note:1: -1}
  25. # Official Webpage
  26. if oweb=$( echo "$appjson" | jq -e '.webpage' ) ; then
  27. oweb="<br><b>Official Webpage: </b><a href=\"${oweb:1:-1}\" target=\"_blank\">${oweb:1:-1}</a>"
  28. appjson=$( echo "$appjson" | jq 'del(.webpage)' )
  29. else
  30. unset oweb
  31. fi
  32. # Official Documentation
  33. if odoc=$( echo "$appjson" | jq -e '.officialDoc' ) ; then
  34. odoc="<br><b>Official Docker Documentation: </b><a href=\"${odoc:1:-1}\" target=\"_blank\">${odoc:1:-1}</a>"
  35. appjson=$( echo "$appjson" | jq 'del(.officialDoc)' )
  36. else
  37. unset odoc
  38. fi
  39. # Pi-Hosted Documentation
  40. if PHDoc=$( echo "$appjson" | jq -e '.piHostedDoc' ) ; then
  41. PHDoc="<br><b>Pi-Hosted dedicated documentation: </b><a href=\"${repo}docs/${PHDoc:1:-1}\" target=\"_blank\">${PHDoc:1:-1}</a>"
  42. appjson=$( echo "$appjson" | jq 'del(.piHostedDoc)' )
  43. else
  44. unset PHDoc
  45. fi
  46. if Script=$( echo "$appjson" | jq -e '.preInstallScript' ) ; then
  47. Script="<br><b>Pre-installation script: </b><a href=\"${repo}tools/${Script:1:-1}\" target=\"_blank\">${Script:1:-1}</a>"
  48. appjson=$( echo "$appjson" | jq 'del(.preInstallScript)' )
  49. else
  50. unset Script
  51. fi
  52. if VideoID=$( echo "$appjson" | jq -e '.videoID' ) ; then
  53. VideoURL=$( jq '.youtube[] | select(.ID=='"$VideoID"') | .URL' "$appinfo" )
  54. VideoTitle=$( jq '.youtube[] | select(.ID=='"$VideoID"') | .Title' "$appinfo" | tr -d '"')
  55. VideoURL="<br><b>Youtube Video: </b><a href=$VideoURL target=\"_blank\">$VideoTitle</a><br>"
  56. appjson=$( echo "$appjson" | jq 'del(.videoID)' )
  57. else
  58. unset VideoID VideoURL
  59. fi
  60. if ExtraScript=$( echo "$appjson" | jq -e '.extraScript' ) ; then
  61. appjson=$( echo "$appjson" | jq 'del(.extraScript)' )
  62. # If only one entry
  63. if [ "$(echo "$ExtraScript" | wc -l )" == "1" ]; then
  64. ExtraHTML="<br><b>Extra useful script: </b><a href=\"${repo}tools/${ExtraScript:1:-1}\" target=\"_blank\">${ExtraScript:1:-1}</a>"
  65. # If multiples entries
  66. else
  67. n_ext=$(echo "$ExtraScript" | jq '. | length')
  68. ExtraHTML="<br><b>Extra useful scripts:</b><br><ul>"
  69. for n in $(seq 0 $(( n_ext - 1 ))); do
  70. ext=$(echo "$ExtraScript" | jq ".[$n]" | tr -d '"')
  71. ExtraHTML="$ExtraHTML<li><a href=\"${repo}tools/${ext}\" target=\"_blank\">$ext</a></li>"
  72. done
  73. ExtraHTML="$ExtraHTML</ul>"
  74. fi
  75. else
  76. unset ExtraHTML ExtraScript
  77. fi
  78. # Full Compiled Note
  79. note="$header$oweb$odoc$PHDoc<br>$Script$ExtraHTML<br>$VideoURL<br>$note"
  80. appjson=$( echo "$appjson" | jq --arg n "$note" '.note = $n' )
  81. # Splitting into 32 and 64 bit jsons
  82. appjson32=$appjson
  83. appjson64=$appjson
  84. # Check if app is to be applied to all (no arch identified)
  85. # If there is no indication of architecture (32 or 64) on image or stackfile keys
  86. # it's to assume that app is to be applied to both templates
  87. # else apply specific image/stackfile to indicated architecture
  88. if ! echo "$appjson" | grep -qE '"(image|stackfile)":' ; then
  89. # Parsing 32 bit apps (check if there is an image32 or stackfile32)
  90. if echo "$appjson32" | grep -qE '"(image|stackfile)32":' ; then
  91. # Remove 64 bit info
  92. appjson32=$( echo "$appjson32" | sed -E 's/"(image|stackfile)32":/"\1":/' )
  93. else
  94. # App does not contain 32bit template
  95. unset appjson32
  96. fi
  97. # Parsing 64 bit apps
  98. if echo "$appjson64" | grep -qE '"(image|stackfile)64":' ; then
  99. appjson64=$( echo "$appjson64" | sed -E 's/"(image|stackfile)64":/"\1":/' )
  100. else
  101. # App does not contain 64bit template
  102. unset appjson64
  103. fi
  104. fi
  105. # Appending to json32
  106. if [[ -n "$appjson32" ]]; then
  107. # Cleaning App json before adding to template
  108. appjson32=$( echo "$appjson32" | jq 'del(.image32, .image64, .repository.stackfile32, .repository.stackfile64)')
  109. json32=$( echo "$json32" | jq --argjson newApp "$appjson32" '.templates += [$newApp]' )
  110. fi
  111. # Appending to json64
  112. if [[ -n "$appjson64" ]]; then
  113. # Cleaning App json before adding to template
  114. appjson64=$( echo "$appjson64" | jq 'del(.image32, .image64, .repository.stackfile32, .repository.stackfile64)')
  115. json64=$( echo "$json64" | jq --argjson newApp "$appjson64" '.templates += [$newApp]' )
  116. fi
  117. # clean variables before next loop
  118. unset appjson32 appjson64 note
  119. done
  120. # Create Templates
  121. echo "$json32" | jq --tab '.templates |= sort_by(.title | ascii_upcase)' > "$template32"
  122. echo "$json64" | jq --tab '.templates |= sort_by(.title | ascii_upcase)' > "$template64"
  123. # Keep old template up to date
  124. cp -f "$template32" "$Oldtemplate32"