mirror of
https://github.com/Websoft9/websoft9.git
synced 2024-11-22 23:50:31 +00:00
enpoint init do nothing
This commit is contained in:
parent
e0fe7a4c85
commit
a6f3ddd656
3 changed files with 2 additions and 147 deletions
|
@ -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 /
|
|
@ -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
|
||||
|
|
|
@ -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))
|
||||
}
|
Loading…
Reference in a new issue