websoft9/docker/w9deployment/init_portainer.go

100 lines
2.1 KiB
Go
Raw Normal View History

2023-09-06 11:47:18 +08:00
package main
import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"time"
2023-09-19 10:58:03 +08:00
"encoding/json"
2023-09-06 11:47:18 +08:00
)
func main() {
2023-09-18 09:43:03 +08:00
2023-09-18 09:49:31 +08:00
dirPath := "/var/websoft9"
if _, err := os.Stat("/var"); os.IsNotExist(err) {
err = os.Mkdir("/var", 0755)
if err != nil {
fmt.Println(err)
return
}
}
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
err = os.Mkdir(dirPath, 0755)
if err != nil {
fmt.Println(err)
return
}
}
2023-09-18 09:43:03 +08:00
filePath := "/var/websoft9/credential"
2023-09-06 11:47:18 +08:00
_, err := os.Stat(filePath)
if os.IsNotExist(err) {
password := generatePassword(16)
err := writeToFile(filePath, password)
if err != nil {
fmt.Println("write file error:", err)
return
}
}
2023-09-18 17:44:28 +08:00
content, err := readPasswordFromFile(filePath)
2023-09-06 11:47:18 +08:00
if err != nil {
fmt.Println("read file error:", err)
return
}
fmt.Println("-----portainer_admin_user: admin, portainer_admin_password: " + string(content) + " ------")
// call portainer
2023-09-18 09:43:03 +08:00
cmd := exec.Command("./portainer", "--admin-password-file", filePath)
2023-09-06 11:47:18 +08:00
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
fmt.Println("error running compiled_program:", err)
return
}
}
func generatePassword(length int) string {
rand.Seed(time.Now().UnixNano())
charset := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+{}[]:;?.,<>"
password := make([]byte, length)
for i := range password {
password[i] = charset[rand.Intn(len(charset))]
}
return string(password)
}
2023-09-18 17:44:28 +08:00
func readPasswordFromFile(filePath string) (string, error) {
data, err := ioutil.ReadFile(filePath)
if err != nil {
return "", err
}
var passwordMap map[string]string
err = json.Unmarshal(data, &passwordMap)
if err != nil {
return "", err
}
password := passwordMap["password"]
return password, nil
}
2023-09-18 09:49:31 +08:00
2023-09-18 17:44:28 +08:00
func writeToFile(filePath, password string) error {
data := map[string]string{"username": "admin", "password": password}
jsonData, err := json.Marshal(data)
if err != nil {
return err
}
err = ioutil.WriteFile(filePath, jsonData, 0755)
return err
2023-09-06 11:47:18 +08:00
}