Skip to content

Commit 17a5454

Browse files
committed
Baked acme.sh into binary and use it when required, removed docker healthcheck
1 parent 4d3dfdf commit 17a5454

File tree

8 files changed

+86
-6
lines changed

8 files changed

+86
-6
lines changed

backend/cmd/server/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os/signal"
66
"syscall"
77

8+
"npm/internal/acme"
89
"npm/internal/api"
910
"npm/internal/config"
1011
"npm/internal/database"
@@ -26,6 +27,7 @@ func main() {
2627
setting.ApplySettings()
2728
database.CheckSetup()
2829
go worker.StartCertificateWorker(appstate)
30+
acme.WriteAcmeSh()
2931

3032
api.StartServer()
3133
irqchan := make(chan os.Signal, 1)

backend/embed/acme.sh

100644100755
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#!/usr/bin/bash
22

3+
VER=0.0.0
4+
5+
echo "Given Args: ${*}"
6+
echo
37
echo "This is a placeholder for the official acme.sh script"
4-
echo "that will be embedded into the binary. If you are seeing"
5-
echo "this message then something is not quite right."
8+
echo "that will be embedded into the binary."
9+
echo "If you are seeing this message then something is not quite right!"
610
exit 1

backend/embed/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ var MigrationFiles embed.FS
1616

1717
// AcmeSh script
1818
//go:embed acme.sh
19-
var AcmeSh embed.FS
19+
var AcmeSh string

backend/internal/acme/acmesh.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package acme
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"os/exec"
8+
"path/filepath"
9+
"strings"
10+
11+
"npm/embed"
12+
"npm/internal/config"
13+
"npm/internal/logger"
14+
)
15+
16+
var acmeShFile string
17+
18+
// GetAcmeShVersion will return the acme.sh script version
19+
func GetAcmeShVersion() string {
20+
if r, err := acmeShExec("--version"); err == nil {
21+
// modify the output
22+
r = strings.Trim(r, "\n")
23+
v := strings.Split(r, "\n")
24+
return v[len(v)-1]
25+
}
26+
return ""
27+
}
28+
29+
func acmeShExec(args ...string) (string, error) {
30+
if _, err := os.Stat(acmeShFile); os.IsNotExist(err) {
31+
e := fmt.Errorf("%s does not exist", acmeShFile)
32+
logger.Error("AcmeShError", e)
33+
return "", e
34+
}
35+
36+
// nolint: gosec
37+
c := exec.Command(acmeShFile, args...)
38+
b, e := c.Output()
39+
40+
if e != nil {
41+
logger.Error("AcmeShError", fmt.Errorf("Command error: %s -- %v\n%+v", acmeShFile, args, e))
42+
logger.Warn(string(b))
43+
}
44+
45+
return string(b), e
46+
}
47+
48+
// WriteAcmeSh this will write our embedded acme.sh script to the data directory
49+
// and give it write permissions
50+
func WriteAcmeSh() {
51+
if config.Configuration.DataFolder == "" {
52+
logger.Error("AcmeShWriteError", fmt.Errorf("Configuration folder ___location is not set"))
53+
return
54+
}
55+
56+
acmeShFile = filepath.Clean(fmt.Sprintf("%s/acme.sh", config.Configuration.DataFolder))
57+
// nolint: gosec
58+
if err := ioutil.WriteFile(acmeShFile, []byte(embed.AcmeSh), 0755); err != nil {
59+
logger.Error("AcmeShWriteError", err)
60+
} else {
61+
logger.Info("Wrote %s", acmeShFile)
62+
}
63+
}

backend/internal/api/handler/health.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package handler
22

33
import (
44
"net/http"
5+
"npm/internal/acme"
56
h "npm/internal/api/http"
67
"npm/internal/config"
78
)
89

910
type healthCheckResponse struct {
1011
Version string `json:"version"`
1112
Commit string `json:"commit"`
13+
AcmeShVersion string `json:"acme.sh"`
1214
Healthy bool `json:"healthy"`
1315
IsSetup bool `json:"setup"`
1416
ErrorReporting bool `json:"error_reporting"`
@@ -23,6 +25,7 @@ func Health() func(http.ResponseWriter, *http.Request) {
2325
Commit: config.Commit,
2426
Healthy: true,
2527
IsSetup: config.IsSetup,
28+
AcmeShVersion: acme.GetAcmeShVersion(),
2629
ErrorReporting: config.ErrorReporting,
2730
}
2831

docker/Dockerfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ RUN rm -rf /etc/services.d/frontend \
6363
VOLUME /data
6464

6565
CMD [ "/init" ]
66-
# TODO: remove healthchecks
67-
HEALTHCHECK --interval=15s --timeout=3s CMD curl -f http://127.0.0.1:81/api || exit 1
6866

6967
ARG NOW
7068
ARG BUILD_VERSION

docker/rootfs/etc/cont-init.d/10-nginx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mkdir -p /tmp/nginx/body \
1818
/var/lib/nginx/cache/public \
1919
/var/lib/nginx/cache/private \
2020
/var/cache/nginx/proxy_temp \
21-
/data/acme.sh
21+
/data/acme
2222

2323
touch /var/log/nginx/error.log && chmod 777 /var/log/nginx/error.log && chmod -R 777 /var/cache/nginx
2424

scripts/ci/build-backend

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ BUILD_DATE=$(date '+%Y-%m-%d %T %Z')
1010
NOW=$(date --rfc-3339=s)
1111

1212
cd $DIR/../..
13+
BACKEND=$(realpath "${DIR}/../../backend")
1314

1415
if [ "$BUILD_COMMIT" = "" ]; then
1516
BUILD_COMMIT=$(git log -n 1 --format=%h)
@@ -59,7 +60,16 @@ build_backend() {
5960
./cmd/server
6061
}
6162

63+
get_acmesh() {
64+
ACME_FILE="${BACKEND}/embed/acme.sh"
65+
echo -e "${BLUE}${CYAN}Fetching latest acme.sh ...${RESET}"
66+
curl -o "${ACME_FILE}" 'https://raw.githubusercontent.com/acmesh-official/acme.sh/master/acme.sh'
67+
chmod +x "${ACME_FILE}"
68+
echo -e "${BLUE}${CYAN}Saved as ${YELLOW}${ACME_FILE}${RESET}"
69+
}
70+
6271
docker pull "${IMAGE}"
72+
get_acmesh
6373

6474
build_backend "darwin" "amd64"
6575
build_backend "darwin" "arm64"

0 commit comments

Comments
 (0)