-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
253 lines (208 loc) · 6.09 KB
/
server.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
package server
import (
"archive/tar"
"compress/gzip"
"context"
"database/sql"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
_ "embed"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/client"
"github.com/juls0730/flux/pkg"
_ "github.com/mattn/go-sqlite3"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
//go:embed schema.sql
schemaBytes []byte
DefaultConfig = FluxServerConfig{
Builder: "paketobuildpacks/builder-jammy-tiny",
Compression: pkg.Compression{
Enabled: false,
Level: 0,
},
}
Flux *FluxServer
logger *zap.SugaredLogger
)
type FluxServerConfig struct {
Builder string `json:"builder"`
Compression pkg.Compression `json:"compression"`
}
type FluxServer struct {
config FluxServerConfig
db *sql.DB
proxy *Proxy
rootDir string
appManager *AppManager
dockerClient *client.Client
Logger *zap.SugaredLogger
}
func NewFluxServer() *FluxServer {
dockerClient, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
logger.Fatalw("Failed to create docker client", zap.Error(err))
}
rootDir := os.Getenv("FLUXD_ROOT_DIR")
if rootDir == "" {
rootDir = "/var/fluxd"
}
if err := os.MkdirAll(rootDir, 0755); err != nil {
logger.Fatalw("Failed to create fluxd directory", zap.Error(err))
}
db, err := sql.Open("sqlite3", filepath.Join(rootDir, "fluxd.db"))
if err != nil {
logger.Fatalw("Failed to open database", zap.Error(err))
}
_, err = db.Exec(string(schemaBytes))
if err != nil {
logger.Fatalw("Failed to create database schema", zap.Error(err))
}
return &FluxServer{
db: db,
proxy: &Proxy{},
appManager: new(AppManager),
rootDir: rootDir,
dockerClient: dockerClient,
}
}
func (s *FluxServer) Stop() {
s.Logger.Sync()
}
func NewServer() *FluxServer {
verbosity, err := strconv.Atoi(os.Getenv("FLUXD_VERBOSITY"))
if err != nil {
verbosity = 0
}
config := zap.NewProductionConfig()
if os.Getenv("DEBUG") == "true" {
config = zap.NewDevelopmentConfig()
verbosity = -1
}
config.Level = zap.NewAtomicLevelAt(zapcore.Level(verbosity))
lameLogger, err := config.Build()
logger = lameLogger.Sugar()
if err != nil {
logger.Fatalw("Failed to create logger", zap.Error(err))
}
Flux = NewFluxServer()
Flux.Logger = logger
var serverConfig FluxServerConfig
// parse config, if it doesnt exist, create it and use the default config
configPath := filepath.Join(Flux.rootDir, "config.json")
if _, err := os.Stat(configPath); err != nil {
if err := os.MkdirAll(Flux.rootDir, 0755); err != nil {
logger.Fatalw("Failed to create fluxd directory", zap.Error(err))
}
configBytes, err := json.Marshal(DefaultConfig)
if err != nil {
logger.Fatalw("Failed to marshal default config", zap.Error(err))
}
logger.Debugw("Config file not found creating default config file at", zap.String("path", configPath))
if err := os.WriteFile(configPath, configBytes, 0644); err != nil {
logger.Fatalw("Failed to write config file", zap.Error(err))
}
}
configFile, err := os.ReadFile(configPath)
if err != nil {
logger.Fatalw("Failed to read config file", zap.Error(err))
}
if err := json.Unmarshal(configFile, &serverConfig); err != nil {
logger.Fatalw("Failed to parse config file", zap.Error(err))
}
Flux.config = serverConfig
logger.Infof("Pulling builder image %s this may take a while...", serverConfig.Builder)
events, err := Flux.dockerClient.ImagePull(context.Background(), fmt.Sprintf("%s:latest", serverConfig.Builder), image.PullOptions{})
if err != nil {
logger.Fatalw("Failed to pull builder image", zap.Error(err))
}
// blocking wait for the iamge to be pulled
io.Copy(io.Discard, events)
logger.Infow("Successfully pulled builder image", zap.String("image", serverConfig.Builder))
if err := os.MkdirAll(filepath.Join(Flux.rootDir, "apps"), 0755); err != nil {
logger.Fatalw("Failed to create apps directory", zap.Error(err))
}
Flux.appManager.Init()
port := os.Getenv("FLUXD_PROXY_PORT")
if port == "" {
port = "7465"
}
go func() {
logger.Infof("Proxy server starting on http://127.0.0.1:%s", port)
if err := http.ListenAndServe(fmt.Sprintf(":%s", port), Flux.proxy); err != nil && err != http.ErrServerClosed {
logger.Fatalw("Proxy server error", zap.Error(err))
}
}()
return Flux
}
func (s *FluxServer) UploadAppCode(code io.Reader, projectConfig pkg.ProjectConfig) (string, error) {
var err error
projectPath := filepath.Join(s.rootDir, "apps", projectConfig.Name)
if err = os.MkdirAll(projectPath, 0755); err != nil {
logger.Errorw("Failed to create project directory", zap.Error(err))
return "", err
}
var gzReader *gzip.Reader
defer func() {
if gzReader != nil {
gzReader.Close()
}
}()
if s.config.Compression.Enabled {
gzReader, err = gzip.NewReader(code)
if err != nil {
logger.Infow("Failed to create gzip reader", zap.Error(err))
return "", err
}
}
var tarReader *tar.Reader
if gzReader != nil {
tarReader = tar.NewReader(gzReader)
} else {
tarReader = tar.NewReader(code)
}
logger.Infow("Extracting files for project", zap.String("project", projectPath))
for {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
logger.Debugw("Failed to read tar header", zap.Error(err))
return "", err
}
// Construct full path
path := filepath.Join(projectPath, header.Name)
// Handle different file types
switch header.Typeflag {
case tar.TypeDir:
if err = os.MkdirAll(path, 0755); err != nil {
logger.Debugw("Failed to extract directory", zap.Error(err))
return "", err
}
case tar.TypeReg:
if err = os.MkdirAll(filepath.Dir(path), 0755); err != nil {
logger.Debugw("Failed to extract directory", zap.Error(err))
return "", err
}
outFile, err := os.Create(path)
if err != nil {
logger.Debugw("Failed to extract file", zap.Error(err))
return "", err
}
defer outFile.Close()
if _, err = io.Copy(outFile, tarReader); err != nil {
logger.Debugw("Failed to copy file during extraction", zap.Error(err))
return "", err
}
}
}
return projectPath, nil
}