-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjerm.go
272 lines (224 loc) · 5.4 KB
/
jerm.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
package jerm
import (
"archive/zip"
"fmt"
"io"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/spatocode/jerm/config"
"github.com/spatocode/jerm/internal/log"
"github.com/spf13/cobra"
)
// Config is the Jerm configuration
type Config config.Config
const (
Version = "0.1.3"
DefaultConfigFile = "jerm.json"
ArchiveFile = "jerm.zip"
)
var (
ReadConfig = config.ReadConfig
ParseConfig = config.ParseConfig
)
// Project holds details of a Jerm project
type Project struct {
config *config.Config
cloud CloudPlatform
}
// New creates a new Jerm project
func New(cfg *config.Config) (*Project, error) {
p := &Project{config: cfg}
return p, nil
}
// Logs shows the deployment logs
func (p *Project) Logs() {
log.PrintInfo("Fetching logs...")
p.cloud.Logs()
}
// SetPlatform sets the cloud platform
func (p *Project) SetPlatform(cloud CloudPlatform) {
p.cloud = cloud
}
// Invoke a function
func (p *Project) Invoke(command string) error {
err := p.cloud.Invoke(command)
if err != nil {
return err
}
return nil
}
// Deploy deploys the project to the cloud
func (p *Project) Deploy() error {
log.PrintfInfo("Deploying project %s...\n", p.config.Name)
start := time.Now()
deployInfo := func(size int64, start time.Time, buildDuration time.Duration) {
deployDuration := time.Since(start)
fmt.Printf("%s %s %v %s, (%s)\n", log.Magenta("build:"), log.Green("completed"), log.White(size/1000000), log.White("MB"), log.White(buildDuration.Round(time.Second)))
fmt.Printf("%s %s (%s)\n", log.Magenta("deploy:"), log.Green("completed"), log.White(deployDuration.Round(time.Second)))
}
file, size, err := p.packageProject()
if err != nil {
return err
}
defer os.RemoveAll(*file)
buildDuration := time.Since(start)
alreadyDeployed, err := p.cloud.Deploy(*file)
if err != nil {
return err
}
if alreadyDeployed {
log.Debug("project already deployed. updating...")
err = p.Update(file)
if err != nil {
return err
}
}
deployInfo(size, start, buildDuration)
return nil
}
// Update updates the deployed project
func (p *Project) Update(zipPath *string) error {
log.Debug("updating deployment...")
var err error
file := zipPath
if zipPath == nil {
file, _, err = p.packageProject()
if err != nil {
return err
}
}
err = p.cloud.Update(*file)
if err != nil {
return err
}
defer os.RemoveAll(*file)
return nil
}
// Undeploy terminates a deployment
func (p *Project) Undeploy() error {
log.PrintInfo("Undeploying project...")
start := time.Now()
err := p.cloud.Undeploy()
if err != nil {
return err
}
duration := time.Since(start)
fmt.Printf("%s %s (%s)\n", log.Magenta("undeploy:"), log.Green("completed"), log.White(duration.Round(time.Second)))
return err
}
// Rollback rolls back a deployment to previous versions
func (p *Project) Rollback(steps int) error {
log.PrintInfo("Rolling back deployment...")
start := time.Now()
err := p.cloud.Rollback(steps)
if err != nil {
return err
}
duration := time.Since(start)
fmt.Printf("%s %s (%s)\n", log.Magenta("rollback:"), log.Green("completed"), log.White(duration.Round(time.Second)))
return nil
}
// packageProject packages a project for deployment
func (p *Project) packageProject() (*string, int64, error) {
log.Debug("packaging project...")
dir, err := p.cloud.Build()
if err != nil {
return nil, 0, err
}
archivePath := path.Join(p.config.Dir, ArchiveFile)
size, err := p.archivePackage(archivePath, dir)
return &archivePath, size, err
}
// archivePackage creates an archive file from a project
func (p *Project) archivePackage(archivePath, project string) (int64, error) {
log.Debug("archiving package...")
archive, err := os.Create(archivePath)
if err != nil {
return 0, err
}
defer archive.Close()
writer := zip.NewWriter(archive)
defer writer.Close()
file, err := os.Open(project)
if err != nil {
return 0, err
}
defer file.Close()
fileInfo, err := file.Stat()
if err != nil {
return 0, err
}
if !fileInfo.IsDir() {
// project is probably a standalone executable
w, err := writer.Create(project)
if err != nil {
return 0, err
}
if _, err := io.Copy(w, file); err != nil {
return 0, err
}
info, err := archive.Stat()
if err != nil {
return 0, err
}
return info.Size(), nil
}
walker := func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
sPath := strings.Split(path, project)
zipContentPath := sPath[len(sPath)-1]
w, err := writer.Create(zipContentPath)
if err != nil {
return err
}
if _, err := io.Copy(w, f); err != nil {
return err
}
return nil
}
err = filepath.WalkDir(project, walker)
if err != nil {
return 0, err
}
info, err := archive.Stat()
if err != nil {
return 0, err
}
return info.Size(), err
}
// Configure sets up Jerm using jerm.json configuration file.
// If the configuration file is not found, it prompts the user for setup.
func Configure(configFile string) (*config.Config, error) {
cfg, err := ReadConfig(configFile)
if err != nil {
c := &config.Config{}
c, err = c.PromptConfig()
if err != nil {
return nil, err
}
return c, err
}
return cfg, err
}
func Verbose(cmd *cobra.Command) {
verbose, _ := cmd.Flags().GetBool("verbose")
if verbose {
os.Setenv("JERM_VERBOSE", "1")
} else {
os.Setenv("JERM_VERBOSE", "0")
}
}