From a6f3ddd65682b00342c128021590b35197b0a7e3 Mon Sep 17 00:00:00 2001 From: qiaofeng1227 <76487013@qq.com> Date: Wed, 13 Sep 2023 08:38:24 +0800 Subject: [PATCH] enpoint init do nothing --- docker/w9portainer/Dockerfile | 6 +- docker/w9portainer/docker-compose.yml | 5 - docker/w9portainer/init_endpoint.go | 138 -------------------------- 3 files changed, 2 insertions(+), 147 deletions(-) delete mode 100644 docker/w9portainer/init_endpoint.go diff --git a/docker/w9portainer/Dockerfile b/docker/w9portainer/Dockerfile index 0069701c..18e1eb9b 100644 --- a/docker/w9portainer/Dockerfile +++ b/docker/w9portainer/Dockerfile @@ -4,10 +4,8 @@ WORKDIR / COPY init_portainer.go / COPY init_endpoint.go / RUN go build -o init_portainer /init_portainer.go -RUN go build -o init_endpoint /init_endpoint.go -RUN chmod +x /init_portainer /init_endpoint +RUN chmod +x /init_portainer # step2: copy build go program to portainer FROM portainer/portainer-ce:2.19.0 -COPY --from=builder /init_portainer / -COPY --from=builder /init_endpoint / \ No newline at end of file +COPY --from=builder /init_portainer / \ No newline at end of file diff --git a/docker/w9portainer/docker-compose.yml b/docker/w9portainer/docker-compose.yml index 15a78abc..912fdffc 100644 --- a/docker/w9portainer/docker-compose.yml +++ b/docker/w9portainer/docker-compose.yml @@ -11,11 +11,6 @@ services: dockerfile: Dockerfile entrypoint: ["/init_portainer"] restart: unless-stopped - healthcheck: - test: ["CMD", "/init_endpoint"] - interval: 5s - timeout: 5s - retries: 3 volumes: - portainer:/data - /var/run/docker.sock:/var/run/docker.sock diff --git a/docker/w9portainer/init_endpoint.go b/docker/w9portainer/init_endpoint.go deleted file mode 100644 index 4e852044..00000000 --- a/docker/w9portainer/init_endpoint.go +++ /dev/null @@ -1,138 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "io/ioutil" - "net/http" - "net/url" - "strings" -) - -type AuthResponse struct { - JWT string `json:"jwt"` -} - -type EndpointResponse struct { - // ... -} - -func main() { - baseURL := "http://119.8.96.66:9091" - // Read the /portainer_password file - password, err := ioutil.ReadFile("/portainer_password") - if err != nil { - fmt.Println("Failed to read password file:", err) - return - } - - // Trim whitespace and newlines from the password - password = []byte(strings.TrimSpace(string(password))) - - // Build the request body for the /auth API - authRequestBody := fmt.Sprintf(`{"password": "%s", "username": "admin"}`, password) - - // Perform authentication by calling the /auth API - authURL := baseURL + "/api/auth" - resp, err := http.Post(authURL, "application/json", strings.NewReader(authRequestBody)) - if err != nil { - fmt.Println("Failed to perform authentication:", err) - return - } - defer resp.Body.Close() - - // Check the authentication response - if resp.StatusCode != http.StatusOK { - fmt.Println("Authentication failed:", resp.Status) - return - } - - fmt.Println("Authentication successful!") - - // Read the authentication response - authResponse, err := ioutil.ReadAll(resp.Body) - if err != nil { - fmt.Println("Failed to read authentication response:", err) - return - } - - // Parse the authentication response JSON - var authResponseJSON AuthResponse - if err := json.Unmarshal(authResponse, &authResponseJSON); err != nil { - fmt.Println("Failed to parse authentication response:", err) - return - } - - // Extract the access token from the authentication response - accessToken := authResponseJSON.JWT - - // Call the /endpoints API with GET method to check if data exists - endpointsURL := baseURL + "/api/endpoints" - req, err := http.NewRequest("GET", endpointsURL, nil) - if err != nil { - fmt.Println("Failed to create request:", err) - return - } - req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", accessToken)) - - client := &http.Client{} - resp, err = client.Do(req) - if err != nil { - fmt.Println("Failed to check endpoint data:", err) - return - } - defer resp.Body.Close() - - // Read the endpoint data response - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - fmt.Println("Failed to read endpoint data response:", err) - return - } - - // Parse the endpoint data response JSON into a slice or array - var endpointResponse []EndpointResponse - if err := json.Unmarshal(body, &endpointResponse); err != nil { - fmt.Println("Failed to parse endpoint data response:", err) - return - } - - // Check if data exists - if len(endpointResponse) > 0 { - // Data exists, perform further operations or return - fmt.Println("Data exists:", string(body)) - return - } - - // Data does not exist, call the /endpoints API to get the endpoint information - fmt.Println("Data does not exist, need to create endpoint") - req, err = http.NewRequest("POST", endpointsURL, nil) - if err != nil { - fmt.Println("Failed to create request:", err) - return - } - req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", accessToken)) - - // Add form data parameters - data := url.Values{} - data.Set("Name", "local_test") - data.Set("EndpointCreationType", "1") - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") - req.Body = ioutil.NopCloser(strings.NewReader(data.Encode())) - - resp, err = client.Do(req) - if err != nil { - fmt.Println("Failed to get endpoint information:", err) - return - } - defer resp.Body.Close() - - // Read the endpoint information response - body, err = ioutil.ReadAll(resp.Body) - if err != nil { - fmt.Println("Failed to read endpoint information response:", err) - return - } - - fmt.Println("Endpoint information:", string(body)) -}