-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.go
277 lines (237 loc) · 6.35 KB
/
deploy.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package main
import (
"bufio"
"fmt"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
"harness/defaults"
"harness/globals"
. "harness/utils"
"os"
"strings"
"time"
)
func DeployProject(c *cli.Context) error {
color.Set(color.FgYellow)
fmt.Println("Deploying project...")
color.Unset()
framework, language, err := loadProjectInfo()
if err != nil {
return err
}
ProjectprogressBar("Loading Project Info...")
fmt.Printf("\nLoaded framework: %s\n", GetColoredText(framework, color.FgCyan))
fmt.Printf("Loaded language: %s\n", GetColoredText(language, color.FgCyan))
hasDockerfile, err := checkDockerfile()
if err != nil {
return err
}
if hasDockerfile {
color.Set(color.FgGreen)
fmt.Println("Awesome! 🐋 Dockerfile found.")
color.Unset()
} else {
fmt.Print("No Dockerfile found. Would you like to create one? (y/n) : ")
var response string
fmt.Scanln(&response)
if response == "y" {
err = createDockerfile(framework, language)
if err != nil {
return err
}
ProjectprogressBar("Creating Dockerfile...")
color.Set(color.FgGreen)
fmt.Println("\n🐋 Dockerfile created.")
color.Unset()
hasDockerfile = true
}
}
err = saveDockerfileInfo(hasDockerfile)
if err != nil {
return err
}
fmt.Print("\nDo you want to proceed deploying using Harness? (y/n): ")
var proceed string
fmt.Scanln(&proceed)
if proceed != "y" {
fmt.Println("Deployment aborted.")
return nil
}
orgName := promptUser("\nOrg Name (default)", "default")
projectName := promptUser("Project Name (default_project)", "default_project")
_, err = CheckOrgExistsAndCreate(c, orgName)
if err != nil {
return err
}
_, err = CheckProjectExistsAndCreate(c, orgName, projectName)
if err != nil {
return err
}
globals.OrgId = orgName
globals.ProjectId = projectName
_, err = DockerConnector(c)
if err != nil {
return err
}
fmt.Print("\n\nDo you want to use the Harness Code Repository for code hosting? (y/n): ")
var useHarnessRepo string
fmt.Scanln(&useHarnessRepo)
if useHarnessRepo == "y" {
fmt.Printf("\nAlready using Harness Code Repository for code hosting ? (y/n): ")
var useHarnessCodeRepo string
fmt.Scanln(&useHarnessCodeRepo)
if useHarnessCodeRepo == "y" {
fmt.Println("\nProvide the repository Name: ")
repoName := promptUser("Repository Name", "default_repo")
globals.RepoName = repoName
} else {
err = UploadToHarnessCodeRepo()
if err != nil {
return err
}
}
} else {
fmt.Println("Feature not supported yet.")
}
CreatePipeline()
RemoveTempFiles()
color.Set(color.FgGreen)
fmt.Println("Deployment Done.")
color.Unset()
color.Set(color.FgYellow)
fmt.Println("Thank you for using Harness Commander CLI.")
color.Unset()
return nil
}
func RemoveTempFiles() {
err := os.Remove(defaults.TEMPFILEPATH)
if err != nil {
_ = fmt.Errorf("failed to delete temp file: %v", err)
}
err = os.Remove("base_pipeline.yaml")
if err != nil {
_ = fmt.Errorf("failed to delete base pipeline file: %v", err)
}
err = os.Remove("modified_pipeline.yaml")
if err != nil {
_ = fmt.Errorf("failed to delete modified pipeline file: %v", err)
}
}
func promptUser(prompt, defaultValue string) string {
fmt.Printf("%s: ", prompt)
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
if input == "" {
return defaultValue
}
return input
}
func loadProjectInfo() (string, string, error) {
file, err := os.Open(defaults.TEMPFILEPATH)
if err != nil {
return "", "", fmt.Errorf("please run 'harness init' first")
}
defer file.Close()
scanner := bufio.NewScanner(file)
var framework, language string
for scanner.Scan() {
line := scanner.Text()
parts := strings.Split(line, "=")
if len(parts) == 2 {
switch parts[0] {
case "framework":
framework = parts[1]
case "language":
language = parts[1]
}
}
}
if err := scanner.Err(); err != nil {
return "", "", fmt.Errorf("failed to read temp file: %v", err)
}
return framework, language, nil
}
func checkDockerfile() (bool, error) {
if _, err := os.Stat("Dockerfile"); err == nil {
return true, nil
} else if os.IsNotExist(err) {
return false, nil
} else {
return false, fmt.Errorf("failed to check Dockerfile: %v", err)
}
}
func createDockerfile(framework, language string) error {
// TODO : uncomment this block
if framework != "Spring Boot" || language != "Java" {
return fmt.Errorf("unsupported framework or language: %s (%s)", framework, language)
}
file, err := os.Create("Dockerfile")
if err != nil {
return fmt.Errorf("failed to create Dockerfile: %v", err)
}
defer file.Close()
_, err = file.WriteString(`# Dockerfile for Spring Boot (Java)
FROM eclipse-temurin:17-jdk-focal
WORKDIR /app
COPY .mvn/ .mvn
COPY mvnw pom.xml ./
RUN ./mvnw dependency:go-offline
COPY src ./src
CMD ["./mvnw", "spring-boot:run"]
`)
if err != nil {
return fmt.Errorf("failed to write Dockerfile: %v", err)
}
return nil
}
func saveDockerfileInfo(hasDockerfile bool) error {
file, err := os.Open(defaults.TEMPFILEPATH)
if err != nil {
return fmt.Errorf("failed to open temp file: %v", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
var lines []string
dockerfileExists := false
for scanner.Scan() {
line := scanner.Text()
parts := strings.Split(line, "=")
if len(parts) == 2 && parts[0] == "dockerfile" {
lines = append(lines, fmt.Sprintf("dockerfile=%t", hasDockerfile))
dockerfileExists = true
} else {
lines = append(lines, line)
}
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("failed to read temp file: %v", err)
}
if !dockerfileExists {
lines = append(lines, fmt.Sprintf("dockerfile=%t", hasDockerfile))
}
file, err = os.OpenFile(defaults.TEMPFILEPATH, os.O_WRONLY|os.O_TRUNC, 0600)
if err != nil {
return fmt.Errorf("failed to open temp file for writing: %v", err)
}
defer file.Close()
for _, line := range lines {
_, err = file.WriteString(line + "\n")
if err != nil {
return fmt.Errorf("failed to write to temp file: %v", err)
}
}
return nil
}
func ProjectprogressBar(info string) {
barLength := 20
spinChars := []string{"|", "/", "-", "\\"}
for i := 0; i <= barLength; i++ {
spinner := spinChars[i%len(spinChars)]
progress := strings.Repeat("=", i) + strings.Repeat(" ", barLength-i)
color.Set(color.FgCyan)
fmt.Printf("\r[%s] %s %s", progress, spinner, info)
color.Unset()
time.Sleep(time.Millisecond * 100)
}
}