forked from filswan/go-computing-provider
-
Notifications
You must be signed in to change notification settings - Fork 11
/
buildspace.go
129 lines (113 loc) · 3.51 KB
/
buildspace.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
package computing
import (
"errors"
"fmt"
"github.com/filswan/go-mcs-sdk/mcs/api/common/logs"
"github.com/swanchain/go-computing-provider/conf"
"github.com/swanchain/go-computing-provider/internal/models"
"io"
"io/fs"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
var NotFoundError = errors.New("not found resource")
func BuildSpaceTaskImage(spaceUuid string, files []models.SpaceFile) (bool, string, string, string, string, error) {
var err error
buildFolder := "build/"
if len(files) > 0 {
for _, file := range files {
dirPath := filepath.Dir(file.Name)
if err = os.MkdirAll(filepath.Join(buildFolder, dirPath), os.ModePerm); err != nil {
return false, "", "", "", "", err
}
if err = downloadFile(filepath.Join(buildFolder, file.Name), file.URL); err != nil {
return false, "", "", "", "", fmt.Errorf("error downloading file: %w", err)
}
}
imagePath := filepath.Join(buildFolder, getDownloadPath(files[0].Name))
var containsYaml bool
var yamlPath string
var modelsSetting string
err = filepath.WalkDir(imagePath, func(path string, d fs.DirEntry, err error) error {
if strings.HasSuffix(strings.ToLower(d.Name()), "deploy.yaml") || strings.HasSuffix(strings.ToLower(d.Name()), "deploy.yml") {
containsYaml = true
yamlPath = path
}
if strings.EqualFold(d.Name(), "model-setting.json") {
modelsSetting = path
}
return nil
})
if err != nil {
return containsYaml, yamlPath, imagePath, modelsSetting, "", err
}
return containsYaml, yamlPath, imagePath, modelsSetting, "", nil
} else {
logs.GetLogger().Warnf("Space %s is not found.", spaceUuid)
}
return false, "", "", "", "", NotFoundError
}
func getDownloadPath(fileName string) string {
splits := strings.Split(fileName, "/")
return filepath.Join(splits[0], splits[1], splits[2])
}
func BuildImagesByDockerfile(jobUuid, spaceUuid, spaceName, imagePath string) (string, string) {
updateJobStatus(jobUuid, models.JobBuildImage)
spaceFlag := spaceName + spaceUuid[strings.LastIndex(spaceUuid, "-"):]
imageName := fmt.Sprintf("lagrange/%s:%d", spaceFlag, time.Now().Unix())
if conf.GetConfig().Registry.ServerAddress != "" {
imageName = fmt.Sprintf("%s/%s:%d",
strings.TrimSpace(conf.GetConfig().Registry.ServerAddress), spaceFlag, time.Now().Unix())
}
imageName = strings.ToLower(imageName)
dockerfilePath := filepath.Join(imagePath, "Dockerfile")
if dockerfilePath == "" {
dockerfilePath = filepath.Join(imagePath, "dockerfile")
}
log.Printf("Image path: %s", imagePath)
dockerService := NewDockerService()
if err := dockerService.BuildImage(imagePath, imageName); err != nil {
logs.GetLogger().Errorf("Error building Docker image: %v", err)
return "", ""
}
if conf.GetConfig().Registry.ServerAddress != "" {
updateJobStatus(jobUuid, models.JobPushImage)
if err := dockerService.PushImage(imageName); err != nil {
logs.GetLogger().Errorf("Error Docker push image: %v", err)
return "", ""
}
}
return imageName, dockerfilePath
}
func downloadFile(filepath string, url string) error {
out, err := os.Create(filepath)
if err != nil {
return err
}
defer func(out *os.File) {
err := out.Close()
if err != nil {
}
}(out)
resp, err := http.Get(url)
if err != nil {
return err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(resp.Body)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("url: %s, unexpected status code: %d", url, resp.StatusCode)
}
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}