-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeployment.go
139 lines (118 loc) · 2.72 KB
/
deployment.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
package main
import (
"encoding/json"
"fmt"
"os"
"path"
"strconv"
"strings"
"sync"
"time"
cp "github.com/otiai10/copy"
)
type Deployment struct {
Id string `json:"id"`
Time time.Time `json:"time"`
ContainerId *string `json:"container_id"`
GitBranch string `json:"git_branch"`
GitCommit string `json:"git_commit"`
Status string `json:"status"`
Port *string `json:"port"`
BuildJob *BuildJob `json:"build_job"`
RequestsLog []string `json:"-"`
RequestsLogLock *sync.Mutex `json:"-"`
App *App `json:"-"`
}
func (d Deployment) GetSlug() string {
return fmt.Sprintf("%s-%s-%s", d.App.GetSlug(), d.GitBranch, d.GitCommit[:7])
}
func (d Deployment) GetDomain() string {
return fmt.Sprintf("%s.%s", d.GetSlug(), domain)
}
func (d Deployment) GetUrl() 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", s, d.GetDomain(), p)
}
func (d Deployment) GetName() string {
short := d.GitCommit[:7]
return fmt.Sprintf("%s/%s", d.GitBranch, short)
}
func (d *Deployment) MarshalJSON() ([]byte, error) {
type Alias Deployment
return json.Marshal(struct {
*Alias
Name string `json:"name"`
Url string `json:"url"`
}{
Alias: (*Alias)(d),
Name: d.GetName(),
Url: d.GetUrl(),
})
}
func (d *Deployment) Run() (err error) {
// Update status
defer func() {
if err != nil {
d.Status = "Failed"
} else {
d.Status = "Running"
}
writeConfig()
}()
template := deploymentTemplates[*d.App.TemplateId]
// Select random host port for container
port, err := getFreePort()
if err != nil {
return
}
d.Port = ptr(strconv.Itoa(port))
// Create tmp mount dir
workDir, err := os.MkdirTemp("./mounts/running", "")
if err != nil {
return
}
// Copy artifacts into workDir
err = cp.Copy(d.BuildJob.ArtifactsPath, workDir)
if err != nil {
return
}
// Write run script into container
script := strings.ReplaceAll(template.Run.Script, "%pm%", d.App.PackageManager)
runScript := fmt.Sprintf(
"#!/bin/sh\n\ncd /runner/\n\n%s",
script,
)
err = os.WriteFile(path.Join(workDir, "r_run.sh"), []byte(runScript), 0755)
// Start container
containerId, err := dockerRun(
template.Run.Image,
"/runner/r_run.sh",
nil,
ptr(template.Run.Port),
ptr(strconv.Itoa(port)),
workDir,
)
if err != nil {
return
}
d.ContainerId = ptr(containerId)
return
}
func (d *Deployment) GetLogs() (logs string, err error) {
if d.ContainerId == nil {
return "", fmt.Errorf("No container found yet")
}
logs, err = dockerLogs(*d.ContainerId)
return
}