joinApps.sh 5.4 KB

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