diff --git a/docker/apphub/Dockerfile b/docker/apphub/Dockerfile index 2eec96f8..e1592533 100644 --- a/docker/apphub/Dockerfile +++ b/docker/apphub/Dockerfile @@ -1,4 +1,4 @@ -# modify time: 202310181524, you can modify here to trigger Docker Build action +# modify time: 202310181824, you can modify here to trigger Docker Build action FROM python:3.10-bullseye AS buildstage LABEL maintainer="Websoft9" diff --git a/docker/apphub/config/developer.sh b/docker/apphub/config/developer.sh index a575f925..60dcbd7f 100644 --- a/docker/apphub/config/developer.sh +++ b/docker/apphub/config/developer.sh @@ -13,4 +13,4 @@ pip install -e $source_path echo "Running the apphub" cd $source_path -exec uvicorn src.main:app --reload --host 0.0.0.0 --port 8080 --log-level debug \ No newline at end of file +exec uvicorn src.main:app --reload --host 0.0.0.0 --port 8080 \ No newline at end of file diff --git a/docker/deployment/Dockerfile b/docker/deployment/Dockerfile index f97bd38b..ae4217cc 100644 --- a/docker/deployment/Dockerfile +++ b/docker/deployment/Dockerfile @@ -1,4 +1,4 @@ -# modify time: 202310181524, you can modify here to trigger Docker Build action +# modify time: 202310181830, you can modify here to trigger Docker Build action # step1: Build entrypoint execute program init_portainer by golang FROM golang:latest AS builder @@ -7,9 +7,9 @@ COPY init_portainer.go / RUN go build -o init_portainer /init_portainer.go RUN chmod +x /init_portainer -#COPY environment.go / -#RUN go build -o environment /environment.go -#RUN chmod +x /environment +COPY environment.go / +RUN go build -o environment /environment.go +RUN chmod +x /environment # step2: Copy build go program to portainer # Dockerfile refer to: https://github.com/portainer/portainer/blob/develop/build/linux/Dockerfile @@ -17,6 +17,6 @@ FROM portainer/portainer-ce:2.19.0 LABEL maintainer="websoft9" LABEL version="2.19.0" COPY --from=builder /init_portainer / -#COPY --from=builder /environment / +COPY --from=builder /environment / ENTRYPOINT ["/init_portainer"] \ No newline at end of file diff --git a/docker/deployment/environment.go b/docker/deployment/environment.go index de332196..4fe3786c 100644 --- a/docker/deployment/environment.go +++ b/docker/deployment/environment.go @@ -1,30 +1,97 @@ -#### go code below - package main -#1 wait and get credential -#2 portainer api login -#3 add local if not exist -``` -name: local -url: /var/run/docker.sock -``` +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "os" + "time" +) +const ( + AdminUser = "admin" + EndpointURL = "http://localhost:9000/api/endpoints" + CredentialLoc = "/data/credential" +) -curl -X POST -H "Content-Type: application/json" -d '{"username":"admin", "Password":"'$new_password'"}' http://$portainer_ip:9000/api/users/admin/init -curl "http://$appmanage_ip:5000/AppUpdateUser?user_name=admin&password=$new_password" +type Endpoint struct { + Name string `json:"Name"` + URL string `json:"URL"` +} +func main() { + var password string + for { + if _, err := os.Stat(CredentialLoc); os.IsNotExist(err) { + fmt.Printf("%s does not exist, waiting for 3 seconds...\n", CredentialLoc) + time.Sleep(3 * time.Second) + } else { + fmt.Printf("%s exists, proceeding...\n", CredentialLoc) + data, err := ioutil.ReadFile(CredentialLoc) + if err != nil { + fmt.Println("Failed to read file:", err) + return + } + password = string(data) + break + } + } -#### other place + client := &http.Client{} + req, err := http.NewRequest("GET", EndpointURL, nil) + req.SetBasicAuth(AdminUser, password) + resp, err := client.Do(req) + if err != nil { + fmt.Println("Failed to make request:", err) + return + } + defer resp.Body.Close() -- add it to Dockerfile -- docker compose health check + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + fmt.Println("Failed to read response:", err) + return + } -``` -healthcheck: -test: ["CMD", "/healthcheck"] -interval: 1m30s -timeout: 10s -retries: 3 -``` + var endpoints []Endpoint + err = json.Unmarshal(body, &endpoints) + if err != nil { + fmt.Println("Failed to parse JSON:", err) + return + } + + for _, endpoint := range endpoints { + if endpoint.Name == "local" && endpoint.URL == "/var/run/docker.sock" { + fmt.Println("Endpoint exists, exiting...") + return + } + } + + fmt.Println("Endpoint does not exist, creating...") + endpoint := Endpoint{ + Name: "local", + URL: "/var/run/docker.sock", + } + data, err := json.Marshal(endpoint) + if err != nil { + fmt.Println("Failed to encode JSON:", err) + return + } + + req, err = http.NewRequest("POST", EndpointURL, bytes.NewBuffer(data)) + req.SetBasicAuth(AdminUser, password) + resp, err = client.Do(req) + if err != nil { + fmt.Println("Failed to make request:", err) + return + } + + if resp.StatusCode != http.StatusCreated { + fmt.Println("Failed to create endpoint:", resp.Status) + } else { + fmt.Println("Endpoint created successfully") + } +} diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 5da98b33..1a1b2bfb 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -25,6 +25,11 @@ services: - /data/compose:/data/compose - /var/run/docker.sock:/var/run/docker.sock #- /run/podman/podman.sock:/var/run/docker.sock + healthcheck: + test: ["CMD", "/healthcheck"] + interval: 30s + timeout: 10s + retries: 3 labels: com.docker.compose.w9_http.port: 9000