2023-09-06 03:47:18 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2023-09-18 01:43:03 +00:00
|
|
|
|
2023-10-17 04:06:03 +00:00
|
|
|
filePath := "/data/credential"
|
2023-09-06 03:47:18 +00:00
|
|
|
|
|
|
|
_, err := os.Stat(filePath)
|
|
|
|
if os.IsNotExist(err) {
|
2023-09-19 08:36:28 +00:00
|
|
|
fmt.Println("credential is not exist, create it.")
|
2023-09-06 03:47:18 +00:00
|
|
|
password := generatePassword(16)
|
|
|
|
|
|
|
|
err := writeToFile(filePath, password)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("write file error:", err)
|
|
|
|
return
|
|
|
|
}
|
2023-10-17 06:42:09 +00:00
|
|
|
// 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
|
|
|
|
}
|
2023-09-19 08:28:36 +00:00
|
|
|
}else{
|
2023-10-17 04:06:03 +00:00
|
|
|
fmt.Println("credential is exist, skip it.")
|
2023-09-19 08:28:36 +00:00
|
|
|
cmd := exec.Command("./portainer")
|
|
|
|
cmd.Run()
|
2023-09-06 03:47:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func generatePassword(length int) string {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
|
2023-10-17 09:42:38 +00:00
|
|
|
charset := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@$()_"
|
2023-09-06 03:47:18 +00:00
|
|
|
|
|
|
|
password := make([]byte, length)
|
|
|
|
for i := range password {
|
|
|
|
password[i] = charset[rand.Intn(len(charset))]
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(password)
|
|
|
|
}
|
|
|
|
|
2023-09-19 08:07:56 +00:00
|
|
|
func writeToFile(filePath , content string) error {
|
2023-09-18 01:49:31 +00:00
|
|
|
|
2023-09-19 08:07:56 +00:00
|
|
|
return ioutil.WriteFile(filePath , []byte(content), 0755)
|
2023-09-06 03:47:18 +00:00
|
|
|
}
|