websoft9/docker/deployment/init_portainer.go
2023-10-17 17:42:38 +08:00

60 lines
No EOL
1.1 KiB
Go

package main
import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"time"
)
func main() {
filePath := "/data/credential"
_, err := os.Stat(filePath)
if os.IsNotExist(err) {
fmt.Println("credential is not exist, create it.")
password := generatePassword(16)
err := writeToFile(filePath, password)
if err != nil {
fmt.Println("write file error:", err)
return
}
// call portainer
cmd := exec.Command("./portainer", "--admin-password-file", filePath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
fmt.Println("error running compiled_program:", err)
return
}
}else{
fmt.Println("credential is exist, skip it.")
cmd := exec.Command("./portainer")
cmd.Run()
}
}
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)
}
func writeToFile(filePath , content string) error {
return ioutil.WriteFile(filePath , []byte(content), 0755)
}