-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
164 lines (137 loc) · 4.24 KB
/
db.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
package main
import (
"database/sql"
"errors"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/sqlite3"
"mintlify-previewer-backend/log"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/mattn/go-sqlite3"
)
type Deployment struct {
UUID string `json:"uuid"`
GitHubURL string `json:"github_url"`
Branch string `json:"branch"`
DocsPath string `json:"docs_path"`
DeployURL string `json:"deployment_url"`
Status string `json:"status"`
}
var db *sql.DB
func initDB() {
var err error
dbPath := "./.sqlite/deployments.db"
dir := "./.sqlite"
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
log.Fatal("failed to create database directory: ", err)
}
db, err = sql.Open("sqlite3", dbPath)
if err != nil {
log.Fatal("failed to open database: ", err)
}
driver, err := sqlite3.WithInstance(db, &sqlite3.Config{})
if err != nil {
log.Fatal("failed to create SQLite driver instance: ", err)
}
m, err := migrate.NewWithDatabaseInstance(
"file://migrations",
"sqlite",
driver,
)
if err != nil {
log.Fatal("failed to create migration instance: ", err)
}
err = m.Up()
if err != nil && !errors.Is(err, migrate.ErrNoChange) {
log.Fatal("failed to apply migrations: ", err)
}
log.Info("Migrations applied successfully!")
}
func restoreDeployments() {
// TODO: Stop deployments after x number of days
dir, err := os.Getwd()
if err != nil {
log.Fatal("Failed to get working directory")
return
}
rows, err := db.Query("SELECT uuid, github_url, branch, docs_path, deployment_url, status FROM deployments WHERE status IN ('running', 'starting')")
if err != nil {
log.Fatalf("Failed to query deployments: %v", err)
}
defer func(rows *sql.Rows) {
err2 := rows.Close()
if err2 != nil {
log.Error("Failed to close rows: ", err2)
}
}(rows)
for rows.Next() {
var dep Deployment
if err := rows.Scan(&dep.UUID, &dep.GitHubURL, &dep.Branch, &dep.DocsPath, &dep.DeployURL, &dep.Status); err != nil {
log.Infof("Failed to scan deployment: %v", err)
return
}
go func() {
deploymentDir := filepath.Join(dir, ".repos", dep.UUID)
// Check if the deployment directory exists and is not empty (except for Git files)
if isEmptyOrOnlyGitFiles(deploymentDir) {
log.Infof("Repository not cloned or incomplete for UUID %s. Cloning now...", dep.UUID)
_, repoURL := extractPRID(dep.GitHubURL)
if out, err := cloneRepo(repoURL, dep.Branch, deploymentDir); err != nil {
log.Infof("Failed to clone repository for UUID %s: %v", dep.UUID, out)
_, err2 := db.Exec("UPDATE deployments SET status = ?, error = ? WHERE uuid = ?", "failed", err.Error(), dep.UUID)
if err2 != nil {
log.Infof("Failed to update status for UUID %s: %v for error %+v", dep.UUID, err2, err)
}
return
}
}
mintFilePath := filepath.Join(deploymentDir, dep.DocsPath)
if _, err := os.Stat(mintFilePath); os.IsNotExist(err) {
_, err2 := db.Exec("UPDATE deployments SET status = ?, error = ? WHERE uuid = ?", "failed", "mint.json file not found", dep.UUID)
if err2 != nil {
log.Infof("Failed to update status for UUID %s: %v for error %+v", dep.UUID, err2, err)
}
return
}
serverDir := filepath.Dir(mintFilePath)
port := extractPortFromURL(dep.DeployURL)
startMintlifyDev(dep.UUID, port, serverDir)
}()
}
}
func isEmptyOrOnlyGitFiles(dir string) bool {
entries, err := os.ReadDir(dir)
if err != nil {
// If the directory doesn't exist, it's considered "empty"
return true
}
// Check if the directory is empty or contains only Git files
for _, entry := range entries {
if entry.Name() != ".git" {
return false // Directory contains non-Git files
}
}
return true // Directory is empty or contains only Git files
}
func extractPortFromURL(deployURL string) int {
parsedURL, err := url.Parse(deployURL)
if err != nil {
log.Infof("Failed to parse URL %s: %v", deployURL, err)
return 0
}
hostParts := strings.Split(parsedURL.Host, ":")
if len(hostParts) < 2 {
log.Infof("No port found in URL %s", deployURL)
return 0
}
port, err := strconv.Atoi(hostParts[1])
if err != nil {
log.Infof("Failed to convert port to integer: %v", err)
return 0
}
return port
}