-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
138 lines (118 loc) · 2.82 KB
/
app.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
package main
import (
"encoding/json"
"fmt"
"log"
"strings"
"sync"
"time"
)
type App struct {
Id string `json:"id"`
Name string `json:"name"`
Port *string `json:"port"`
Env *string `json:"env"`
GitUrl string `json:"git_url"`
GitUsername *string `json:"git_username"`
GitPassword *string `json:"git_password"`
TemplateId *string `json:"template_id"`
Deployments []*Deployment `json:"deployments"`
WebhookSecret string `json:"webhook_secret"`
PackageManager string `json:"package_manager"`
}
func (a *App) Deploy(gitBranch, gitCommit string) (deployment *Deployment, err error) {
log.Println(
"[Deployment] Deploying branch:",
gitBranch,
"commit:",
gitCommit,
"for app:",
a.Name,
"with id:",
a.Id,
)
// templateId, err := a.suggestBuildTemplate(*a.RepoPath)
// if err != nil {
// return err
// }
// a.TemplateId = ptr(templateId)
deployment = &Deployment{
Id: makeId(),
Time: time.Now(),
App: a,
GitBranch: gitBranch,
GitCommit: gitCommit,
Status: "Initializing Build",
RequestsLogLock: &sync.Mutex{},
}
buildJob := &BuildJob{
Id: makeId(),
Deployment: deployment,
Status: "Building",
}
deployment.BuildJob = buildJob
a.Deployments = append(a.Deployments, deployment)
writeConfig()
// Build and deploy in background
go func(buildJob *BuildJob, deployment *Deployment) {
err = buildJob.Run()
if err != nil {
log.Println("[Build Job]", err)
}
deployment.Status = fmt.Sprintf("Build: %s", buildJob.Status)
err = deployment.Run()
if err != nil {
log.Println("[Build Job]", err)
}
writeConfig()
}(buildJob, deployment)
return
}
func (a App) suggestBuildTemplate(path string) (templateId string, err error) {
// Select deployment template based on project.json
var pkgJson map[string]interface{}
pkgJson, err = loadPackageJSON(path)
if err != nil {
return
}
deps, err := parseDependencies(pkgJson)
if err != nil {
return
}
templateId, err = findTemplateByDependencies(deps)
if err != nil {
return
}
return templateId, nil
}
func (a *App) GetWebhookUrl() string {
s := ""
p := ""
if ssl {
s = "s"
if sslPort != "443" {
p = fmt.Sprintf(":%s", sslPort)
}
} else {
if port != "80" {
p = fmt.Sprintf(":%s", port)
}
}
return fmt.Sprintf("http%s://%s%s/runner/api/app/%s/webhook/", s, domain, p, a.Id)
}
func (a *App) MarshalJSON() ([]byte, error) {
type Alias App
return json.Marshal(struct {
*Alias
WebhookUrl string `json:"webhook_url"`
}{
Alias: (*Alias)(a),
WebhookUrl: a.GetWebhookUrl(),
})
}
func (a *App) GetSlug() string {
slug := a.Name
slug = strings.ToLower(slug)
slug = strings.ReplaceAll(slug, " ", "-")
return slug
}