builder.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/bin/sh
  2. VERSION=${VERSION:-"1.0.5"}
  3. TELEMETRY=${ENABLE_TELEMETRY:-"true"}
  4. IMPORT="github.com/caddyserver/caddy"
  5. # version <1.0.1 needs to use old import path
  6. new_import=true
  7. if [ "$(echo $VERSION | cut -c1)" -eq 0 ] 2>/dev/null || [ "$VERSION" = "1.0.0" ]; then
  8. IMPORT="github.com/mholt/caddy" && new_import=false
  9. fi
  10. # add `v` prefix for version numbers
  11. [ "$(echo $VERSION | cut -c1)" -ge 0 ] 2>/dev/null && VERSION="v$VERSION"
  12. stage() {
  13. STAGE="$1"
  14. echo
  15. echo starting stage: $STAGE
  16. }
  17. end_stage() {
  18. if [ $? -ne 0 ]; then
  19. >&2 echo error at \'$STAGE\'
  20. exit 1
  21. fi
  22. echo finished stage: $STAGE ✓
  23. echo
  24. }
  25. use_new_import() (
  26. cd $1
  27. find . -name '*.go' | while read -r f; do
  28. sed -i.bak 's/\/mholt\/caddy/\/caddyserver\/caddy/g' $f && rm $f.bak
  29. done
  30. )
  31. get_package() {
  32. # go module require special dns handling
  33. if $go_mod && [ -f /dnsproviders/$1/$1.go ]; then
  34. mkdir -p /caddy/dnsproviders/$1
  35. cp -r /dnsproviders/$1/$1.go /caddy/dnsproviders/$1/$1.go
  36. echo "caddy/dnsproviders/$1"
  37. else
  38. GO111MODULE=off GOOS=linux GOARCH=amd64 caddyplug package $1 2> /dev/null
  39. fi
  40. }
  41. dns_plugins() {
  42. git clone https://github.com/caddyserver/dnsproviders /dnsproviders
  43. # temp hack for repo rename
  44. if $new_import; then use_new_import /dnsproviders; fi
  45. }
  46. plugins() {
  47. mkdir -p /plugins
  48. for plugin in $(echo $PLUGINS | tr "," " "); do \
  49. import_package=$(get_package $plugin)
  50. $go_mod || go get -v "$import_package" ; # not needed for modules
  51. $go_mod && package="main" || package="caddyhttp"
  52. printf "package $package\nimport _ \"$import_package\"" > \
  53. /plugins/$plugin.go ; \
  54. done
  55. }
  56. module() {
  57. mkdir -p /caddy
  58. cd /caddy # build dir
  59. # setup module
  60. go mod init caddy
  61. go get -v $IMPORT@$VERSION
  62. # plugins
  63. cp -r /plugins/. .
  64. # temp hack for repo rename
  65. go get -v -d # download possible plugin deps
  66. if $new_import; then use_new_import /go/pkg/mod; fi
  67. # main and telemetry
  68. cat > main.go <<EOF
  69. package main
  70. import "$IMPORT/caddy/caddymain"
  71. import "os"
  72. func main() {
  73. switch os.Getenv("ENABLE_TELEMETRY") {
  74. case "0", "false":
  75. caddymain.EnableTelemetry = false
  76. case "1", "true":
  77. caddymain.EnableTelemetry = true
  78. default:
  79. caddymain.EnableTelemetry = $TELEMETRY
  80. }
  81. caddymain.Run()
  82. }
  83. EOF
  84. }
  85. legacy() {
  86. cd /go/src/$IMPORT/caddy # build dir
  87. # plugins
  88. cp -r /plugins/. ../caddyhttp
  89. # telemetry
  90. run_file="/go/src/$IMPORT/caddy/caddymain/run.go"
  91. if [ "$TELEMETRY" = "false" ]; then
  92. cat > "$run_file.disablestats.go" <<EOF
  93. package caddymain
  94. import "os"
  95. func init() {
  96. switch os.Getenv("ENABLE_TELEMETRY") {
  97. case "0", "false":
  98. EnableTelemetry = false
  99. case "1", "true":
  100. EnableTelemetry = true
  101. default:
  102. EnableTelemetry = false
  103. }
  104. }
  105. EOF
  106. fi
  107. }
  108. # caddy source
  109. stage "fetching caddy source"
  110. git clone https://github.com/caddyserver/caddy -b "$VERSION" /go/src/$IMPORT \
  111. && cd /go/src/$IMPORT
  112. end_stage
  113. # plugin helper
  114. stage "installing plugin helper"
  115. GOOS=linux GOARCH=amd64 go get -v github.com/abiosoft/caddyplug/caddyplug
  116. end_stage
  117. # check for modules support
  118. go_mod=false
  119. [ -f /go/src/$IMPORT/go.mod ] && export GO111MODULE=on && go_mod=true
  120. # dns plugins
  121. stage "fetching dns plugin sources"
  122. dns_plugins
  123. end_stage
  124. # generate plugins
  125. stage "generating plugins"
  126. plugins
  127. end_stage
  128. # add plugins and telemetry
  129. stage "customising plugins and telemetry"
  130. if $go_mod; then module; else legacy; fi
  131. end_stage
  132. # build
  133. stage "building caddy"
  134. CGO_ENABLED=0 go build -o caddy
  135. end_stage
  136. # copy binary
  137. stage "copying binary"
  138. mkdir -p /install \
  139. && mv caddy /install \
  140. && /install/caddy -version
  141. end_stage
  142. echo "installed caddy version $VERSION at /install/caddy"