-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
78 lines (61 loc) · 1.81 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"time"
_ "github.com/joho/godotenv/autoload"
"github.com/rs/zerolog/log"
)
func main() {
log.Info().Msg("checking env variables")
server, err := getEnvVar("ARGOCD_SERVER")
if err != nil {
log.Panic().Msg("ARGOCD_SERVER variable is not set")
}
token, err := getEnvVar("ARGOCD_AUTH_TOKEN")
if err != nil {
log.Panic().Msg("ARGOCD_AUTH_TOKEN variable is not")
}
log.Info().Msg("going to check command arguments")
appName, err := getEnvVar("APP_NAME")
if err != nil {
log.Panic().Err(err).Msg("app name is not set")
}
log.Info().Msgf("going to sync the application %s, server: %s, token: %d", appName, server, len(token))
_, err = runCommand("argocd app sync "+appName, server, token)
syncFailed := false
if err != nil {
log.Error().Err(err).Msgf("problem trying to sync the application %s", appName)
syncFailed = true
}
log.Info().Msgf("going to wait for the application %s", appName)
output, err := runCommand("argocd app wait --sync "+appName, server, token)
fmt.Print(output)
if err != nil {
log.Error().Err(err).Msgf("problem trying to wait the application %s", appName)
syncFailed = true
}
if syncFailed {
log.Info().Msg("waiting for 3 minutes, sync failed so wait until automatic poll")
time.Sleep(time.Minute * 3)
return
}
log.Info().Msg("finished")
}
func runCommand(cmd string, server string, token string) (string, error) {
cmdString := "ARGOCD_SERVER=\"" + server + "\" && export ARGOCD_AUTH_TOKEN=\"" + token + "\" && " + cmd
output, err := exec.Command("sh", "-c", cmdString).CombinedOutput()
if err != nil {
return "", err
}
return string(output), nil
}
func getEnvVar(env string) (string, error) {
server := os.Getenv(env)
if server == "" {
return "", errors.New(env + " is not given")
}
return server, nil
}