|
19 | 19 | package cmd |
20 | 20 |
|
21 | 21 | import ( |
22 | | - "github.com/a8m/envsubst" |
23 | | - "github.com/joho/godotenv" |
| 22 | + "github.com/epos-eu/opensource-kubernetes/cmd/methods" |
24 | 23 | "github.com/spf13/cobra" |
25 | | - "os" |
26 | | - "os/exec" |
27 | | - "regexp" |
28 | | - "strings" |
29 | | - "time" |
30 | | -) |
31 | | - |
32 | | -var ( |
33 | | - lineBreakRegExp = regexp.MustCompile(`\r?\n`) |
34 | 24 | ) |
35 | 25 |
|
36 | 26 | var deployCmd = &cobra.Command{ |
37 | 27 | Use: "deploy", |
38 | 28 | Short: "Deploy an environment on Kubernetes", |
39 | 29 | Long: `Deploy an enviroment with .env set up on Kubernetes`, |
40 | | - Run: func(cmd *cobra.Command, args []string) { |
| 30 | + RunE: func(cmd *cobra.Command, args []string) error { |
41 | 31 | env, _ := cmd.Flags().GetString("env") |
42 | 32 | context, _ := cmd.Flags().GetString("context") |
43 | 33 | namespace, _ := cmd.Flags().GetString("namespace") |
44 | 34 | tag, _ := cmd.Flags().GetString("tag") |
45 | | - isDefaultEnv := false |
46 | | - if env == "" { |
47 | | - env = generateTempFile(configurations) |
48 | | - isDefaultEnv = true |
49 | | - } |
50 | | - fileContents, err := os.ReadFile(env) |
51 | | - if err != nil { |
52 | | - printError("Loading env variables from " + env + " cause: " + err.Error()) |
53 | | - os.Exit(0) |
54 | | - } |
55 | | - fileLines := lineBreakRegExp.Split(string(fileContents), -1) |
56 | | - for _, line := range fileLines { |
57 | | - if strings.Contains(line, "=") { |
58 | | - res1 := strings.Split(line, "=") |
59 | | - os.Setenv(res1[0], res1[1]) |
60 | | - } |
61 | | - } |
62 | | - |
63 | | - os.Setenv("CONTEXT", context) |
64 | | - os.Setenv("DEPLOY_TAG", tag) |
65 | | - os.Setenv("NAMESPACE", namespace) |
66 | | - os.Setenv("DEPLOY_PATH", "/"+namespace+"/") |
67 | | - os.Setenv("BASE_CONTEXT", "/"+namespace) |
68 | | - os.Setenv("POSTGRESQL_CONNECTION_STRING", "jdbc:postgresql://"+os.Getenv("POSTGRESQL_HOST")+"/"+os.Getenv("POSTGRES_DB")+"?user="+os.Getenv("POSTGRES_USER")+"&password="+os.Getenv("POSTGRESQL_PASSWORD")+"") |
69 | | - if isDefaultEnv { |
70 | | - checkImagesUpdate() |
71 | | - } |
72 | | - setupIPs() |
73 | | - |
74 | | - operator, err = envsubst.Bytes([]byte(operator)) |
75 | | - if err != nil { |
76 | | - printError("Updating env variables of " + string(operator) + " cause: " + err.Error()) |
77 | | - os.Exit(0) |
78 | | - } |
79 | | - rabbitmqoperatorfile := generateTempFile(operator) |
80 | | - |
81 | | - rabbitmq, err = envsubst.Bytes([]byte(rabbitmq)) |
82 | | - if err != nil { |
83 | | - printError("Updating env variables of " + string(rabbitmq) + " cause: " + err.Error()) |
84 | | - os.Exit(0) |
85 | | - } |
86 | | - rabbitmqfile := generateTempFile(rabbitmq) |
87 | | - |
88 | | - logging, err = envsubst.Bytes([]byte(logging)) |
89 | | - if err != nil { |
90 | | - printError("Updating env variables of " + string(logging) + " cause: " + err.Error()) |
91 | | - os.Exit(0) |
92 | | - } |
93 | | - loggingfile := generateTempFile(logging) |
94 | | - |
95 | | - secrets, err = envsubst.Bytes([]byte(secrets)) |
96 | | - if err != nil { |
97 | | - printError("Updating env variables of " + string(secrets) + " cause: " + err.Error()) |
98 | | - os.Exit(0) |
99 | | - } |
100 | | - secretsfile := generateTempFile(secrets) |
101 | | - |
102 | | - backoffice, err = envsubst.Bytes([]byte(backoffice)) |
103 | | - if err != nil { |
104 | | - printError("Updating env variables of " + string(backoffice) + " cause: " + err.Error()) |
105 | | - os.Exit(0) |
106 | | - } |
107 | | - backofficefile := generateTempFile(backoffice) |
108 | | - |
109 | | - dataMetadata, err = envsubst.Bytes([]byte(dataMetadata)) |
110 | | - if err != nil { |
111 | | - printError("Updating env variables of " + string(dataMetadata) + " cause: " + err.Error()) |
112 | | - os.Exit(0) |
113 | | - } |
114 | | - datametadatafile := generateTempFile(dataMetadata) |
115 | | - |
116 | | - externalAccess, err = envsubst.Bytes([]byte(externalAccess)) |
117 | | - if err != nil { |
118 | | - printError("Updating env variables of " + string(externalAccess) + " cause: " + err.Error()) |
119 | | - os.Exit(0) |
120 | | - } |
121 | | - externalaccessfile := generateTempFile(externalAccess) |
122 | | - |
123 | | - ingestor, err = envsubst.Bytes([]byte(ingestor)) |
124 | | - if err != nil { |
125 | | - printError("Updating env variables of " + string(ingestor) + " cause: " + err.Error()) |
126 | | - os.Exit(0) |
127 | | - } |
128 | | - ingestorfile := generateTempFile(ingestor) |
129 | | - |
130 | | - metadataDatabase, err = envsubst.Bytes([]byte(metadataDatabase)) |
131 | | - if err != nil { |
132 | | - printError("Updating env variables of " + string(metadataDatabase) + " cause: " + err.Error()) |
133 | | - os.Exit(0) |
134 | | - } |
135 | | - metadatadatabasefile := generateTempFile(metadataDatabase) |
| 35 | + autoupdate, _ := cmd.Flags().GetString("autoupdate") |
| 36 | + update, _ := cmd.Flags().GetString("update") |
136 | 37 |
|
137 | | - redisDatabase, err = envsubst.Bytes([]byte(redisDatabase)) |
138 | | - if err != nil { |
139 | | - printError("Updating env variables of " + string(redisDatabase) + " cause: " + err.Error()) |
140 | | - os.Exit(0) |
| 38 | + if err := methods.CreateEnvironment(env, context, namespace, tag, autoupdate, update); err != nil { |
| 39 | + return err |
141 | 40 | } |
142 | | - redisdatabasefile := generateTempFile(redisDatabase) |
143 | | - |
144 | | - resources, err = envsubst.Bytes([]byte(resources)) |
145 | | - if err != nil { |
146 | | - printError("Updating env variables of " + string(resources) + " cause: " + err.Error()) |
147 | | - os.Exit(0) |
148 | | - } |
149 | | - resourcesfile := generateTempFile(resources) |
150 | | - |
151 | | - gateway, err = envsubst.Bytes([]byte(gateway)) |
152 | | - if err != nil { |
153 | | - printError("Updating env variables of " + string(gateway) + " cause: " + err.Error()) |
154 | | - os.Exit(0) |
155 | | - } |
156 | | - gatewayfile := generateTempFile(gateway) |
157 | | - |
158 | | - converter, err = envsubst.Bytes([]byte(converter)) |
159 | | - if err != nil { |
160 | | - printError("Updating env variables of " + string(converter) + " cause: " + err.Error()) |
161 | | - os.Exit(0) |
162 | | - } |
163 | | - converterfile := generateTempFile(converter) |
164 | | - |
165 | | - list_of_services := [13]string{rabbitmqoperatorfile, rabbitmqfile, loggingfile, secretsfile, metadatadatabasefile, |
166 | | - backofficefile, datametadatafile, externalaccessfile, ingestorfile, |
167 | | - redisdatabasefile, resourcesfile, gatewayfile, converterfile} |
168 | | - |
169 | | - if err := godotenv.Load(env); err != nil { |
170 | | - printError("Error loading env variables from " + env + " cause: " + err.Error()) |
171 | | - os.Exit(0) |
172 | | - } |
173 | | - |
174 | | - printSetup(env, context, namespace, tag) |
175 | | - printTask("Switching context to " + context) |
176 | | - |
177 | | - execute_command(cmd, exec.Command("kubectl", |
178 | | - "config", |
179 | | - "use-context", |
180 | | - context)) |
181 | | - |
182 | | - printTask("Deploy of ingress nginx") |
183 | | - |
184 | | - execute_command(cmd, exec.Command("kubectl", |
185 | | - "apply", |
186 | | - "-f", |
187 | | - "https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.3.1/deploy/static/provider/cloud/deploy.yaml")) |
188 | | - |
189 | | - printTask("Creating namespace: " + namespace) |
190 | | - execute_command(cmd, exec.Command("kubectl", |
191 | | - "create", |
192 | | - "ns", |
193 | | - namespace)) |
194 | | - |
195 | | - time.Sleep(10 * time.Second) |
196 | | - |
197 | | - printTask("Deploy of " + list_of_services[0]) |
198 | | - |
199 | | - execute_command(cmd, exec.Command("kubectl", |
200 | | - "apply", |
201 | | - "-f", |
202 | | - list_of_services[0])) |
203 | | - |
204 | | - time.Sleep(10 * time.Second) |
205 | | - |
206 | | - for i := 1; i < 13; i++ { |
207 | | - printTask("Deploy of " + list_of_services[i]) |
208 | | - execute_command(cmd, exec.Command( |
209 | | - "kubectl", |
210 | | - "apply", |
211 | | - "-f", |
212 | | - list_of_services[i], |
213 | | - "-n", |
214 | | - namespace)) |
215 | | - printTask("Waiting for conditions met") |
216 | | - execute_command(cmd, exec.Command("kubectl", |
217 | | - "wait", |
218 | | - "--for=condition=Ready", |
219 | | - "pods", |
220 | | - "--all", |
221 | | - "-n", |
222 | | - namespace)) |
223 | | - |
224 | | - time.Sleep(10 * time.Second) |
225 | | - } |
226 | | - |
227 | | - print_urls() |
228 | | - |
| 41 | + return nil |
229 | 42 | }, |
230 | 43 | } |
231 | 44 |
|
232 | | -func execute_command(cmd *cobra.Command, command *exec.Cmd) { |
233 | | - command.Stdout = os.Stdout |
234 | | - command.Stderr = os.Stderr |
235 | | - if err := command.Run(); err != nil { |
236 | | - printError("Error on executing command, cause: " + err.Error()) |
237 | | - } |
238 | | -} |
239 | | - |
240 | 45 | func init() { |
241 | 46 | deployCmd.Flags().String("context", "", "Kubernetes context") |
242 | 47 | deployCmd.MarkFlagRequired("context") |
243 | 48 | deployCmd.Flags().String("namespace", "", "Kubernetes namespace") |
244 | 49 | deployCmd.MarkFlagRequired("namespace") |
245 | 50 | deployCmd.Flags().String("tag", "", "Version Tag") |
246 | 51 | deployCmd.MarkFlagRequired("tag") |
| 52 | + deployCmd.Flags().String("autoupdate", "", "Auto update the images versions (true|false)") |
| 53 | + deployCmd.Flags().String("update", "", "Update of an existing deployment (true|false), default false") |
247 | 54 | } |
0 commit comments