initial commit
This commit is contained in:
177
server/deploy.go
Normal file
177
server/deploy.go
Normal file
@@ -0,0 +1,177 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type DeployRequest struct {
|
||||
Config multipart.File `form:"config"`
|
||||
Code multipart.File `form:"code"`
|
||||
}
|
||||
|
||||
type DeployResponse struct {
|
||||
AppID int64 `json:"app_id"`
|
||||
}
|
||||
|
||||
type ProjectConfig struct {
|
||||
Name string `json:"name"`
|
||||
Urls []string `json:"urls"`
|
||||
Port int `json:"port"`
|
||||
EnvFile string `json:"env_file"`
|
||||
Environment []string `json:"environment"`
|
||||
}
|
||||
|
||||
func (s *FluxServer) DeployHandler(w http.ResponseWriter, r *http.Request) {
|
||||
err := r.ParseMultipartForm(10 << 30) // 10 GiB
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// bind to DeployRequest struct
|
||||
var deployRequest DeployRequest
|
||||
deployRequest.Config, _, err = r.FormFile("config")
|
||||
if err != nil {
|
||||
http.Error(w, "No flux.json found", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer deployRequest.Config.Close()
|
||||
|
||||
deployRequest.Code, _, err = r.FormFile("code")
|
||||
if err != nil {
|
||||
http.Error(w, "No code archive found", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer deployRequest.Code.Close()
|
||||
|
||||
var projectConfig ProjectConfig
|
||||
if err := json.NewDecoder(deployRequest.Config).Decode(&projectConfig); err != nil {
|
||||
log.Printf("Failed to decode config: %v", err)
|
||||
http.Error(w, "Invalid flux.json", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if projectConfig.Name == "" {
|
||||
http.Error(w, "No project name specified", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if projectConfig.Urls == nil || len(projectConfig.Urls) == 0 {
|
||||
http.Error(w, "No deployment urls specified", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if projectConfig.Port == 0 {
|
||||
http.Error(w, "No port specified", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Deploying project %s to %s", projectConfig.Name, projectConfig.Urls)
|
||||
|
||||
projectPath, err := s.UploadAppCode(deployRequest.Code, projectConfig)
|
||||
if err != nil {
|
||||
log.Printf("Failed to upload code: %v", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
prepareCmd := exec.Command("go", "generate")
|
||||
prepareCmd.Dir = projectPath
|
||||
err = prepareCmd.Run()
|
||||
if err != nil {
|
||||
log.Printf("Failed to prepare project: %s", err)
|
||||
http.Error(w, fmt.Sprintf("Failed to prepare project: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
imageName := fmt.Sprintf("%s-image", projectConfig.Name)
|
||||
buildCmd := exec.Command("pack", "build", imageName, "--builder", s.config.Builder)
|
||||
buildCmd.Dir = projectPath
|
||||
err = buildCmd.Run()
|
||||
if err != nil {
|
||||
log.Printf("Failed to build image: %s", err)
|
||||
http.Error(w, fmt.Sprintf("Failed to build image: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
containerID, err := s.containerManager.DeployContainer(r.Context(), imageName, projectConfig.Name, projectPath, projectConfig)
|
||||
if err != nil {
|
||||
log.Printf("Failed to deploy container: %v", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
deploymentResult, err := s.db.Exec("INSERT INTO deployments (urls) VALUES (?)", strings.Join(projectConfig.Urls, ","))
|
||||
if err != nil {
|
||||
log.Printf("Failed to insert deployment: %v", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
deploymentID, err := deploymentResult.LastInsertId()
|
||||
if err != nil {
|
||||
log.Printf("Failed to get deployment id: %v", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = s.db.Exec("INSERT INTO containers (container_id, deployment_id, status) VALUES (?, ?, ?)", containerID, deploymentID, "pending")
|
||||
if err != nil {
|
||||
log.Printf("Failed to get container id: %v", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
appExists := s.db.QueryRow("SELECT * FROM apps WHERE name = ?", projectConfig.Name)
|
||||
configBytes, err := json.Marshal(projectConfig)
|
||||
if err != nil {
|
||||
log.Printf("Failed to marshal project config: %v", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
var appResult sql.Result
|
||||
|
||||
if appExists.Err() == sql.ErrNoRows {
|
||||
// create app in the database
|
||||
appResult, err = s.db.Exec("INSERT INTO apps (name, image, project_path, project_config, deployment_id) VALUES (?, ?, ?, ?, ?)", projectConfig.Name, imageName, projectPath, configBytes, deploymentID)
|
||||
if err != nil {
|
||||
log.Printf("Failed to insert app: %v", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// update app in the database
|
||||
appResult, err = s.db.Exec("UPDATE apps SET project_config = ?, deployment_id = ? WHERE name = ?", configBytes, deploymentID, projectConfig.Name)
|
||||
if err != nil {
|
||||
log.Printf("Failed to update app: %v", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
appId, err := appResult.LastInsertId()
|
||||
if err != nil {
|
||||
log.Printf("Failed to get app id: %v", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(DeployResponse{
|
||||
AppID: appId,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *FluxServer) ListAppsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// Implement app listing logic
|
||||
apps := s.db.QueryRow("SELECT * FROM apps")
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(apps)
|
||||
}
|
||||
121
server/docker.go
Normal file
121
server/docker.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/docker/go-connections/nat"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
type ContainerManager struct {
|
||||
dockerClient *client.Client
|
||||
}
|
||||
|
||||
func NewContainerManager() *ContainerManager {
|
||||
dockerClient, err := client.NewClientWithOpts(client.FromEnv)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create Docker client: %v", err)
|
||||
}
|
||||
|
||||
return &ContainerManager{
|
||||
dockerClient: dockerClient,
|
||||
}
|
||||
}
|
||||
|
||||
func (cm *ContainerManager) DeployContainer(ctx context.Context, imageName, containerPrefix, projectPath string, projectConfig ProjectConfig) (string, error) {
|
||||
containerName := fmt.Sprintf("%s-%s", containerPrefix, time.Now().Format("20060102-150405"))
|
||||
|
||||
existingContainers, err := cm.findExistingContainers(ctx, containerPrefix)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Failed to find existing containers: %v", err)
|
||||
}
|
||||
|
||||
// TODO: swap containers if they are running and have the same image so that we can have a constant uptime
|
||||
for _, existingContainer := range existingContainers {
|
||||
log.Printf("Stopping existing container: %s", existingContainer)
|
||||
|
||||
if err := cm.dockerClient.ContainerStop(ctx, existingContainer, container.StopOptions{}); err != nil {
|
||||
return "", fmt.Errorf("Failed to stop existing container: %v", err)
|
||||
}
|
||||
|
||||
if err := cm.dockerClient.ContainerRemove(ctx, existingContainer, container.RemoveOptions{}); err != nil {
|
||||
return "", fmt.Errorf("Failed to remove existing container: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if projectConfig.EnvFile != "" {
|
||||
envBytes, err := os.Open(filepath.Join(projectPath, projectConfig.EnvFile))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Failed to open env file: %v", err)
|
||||
}
|
||||
defer envBytes.Close()
|
||||
|
||||
envVars, err := godotenv.Parse(envBytes)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Failed to parse env file: %v", err)
|
||||
}
|
||||
|
||||
for key, value := range envVars {
|
||||
projectConfig.Environment = append(projectConfig.Environment, fmt.Sprintf("%s=%s", key, value))
|
||||
}
|
||||
}
|
||||
|
||||
resp, err := cm.dockerClient.ContainerCreate(ctx, &container.Config{
|
||||
Image: imageName,
|
||||
Env: projectConfig.Environment,
|
||||
ExposedPorts: nat.PortSet{
|
||||
nat.Port(fmt.Sprintf("%d/tcp", projectConfig.Port)): {},
|
||||
},
|
||||
},
|
||||
&container.HostConfig{
|
||||
PortBindings: nat.PortMap{
|
||||
nat.Port(fmt.Sprintf("%d/tcp", projectConfig.Port)): []nat.PortBinding{
|
||||
{
|
||||
HostIP: "0.0.0.0",
|
||||
HostPort: strconv.Itoa(projectConfig.Port),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
nil,
|
||||
nil,
|
||||
containerName,
|
||||
)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Failed to create container: %v", err)
|
||||
}
|
||||
|
||||
if err := cm.dockerClient.ContainerStart(ctx, resp.ID, container.StartOptions{}); err != nil {
|
||||
return "", fmt.Errorf("Failed to start container: %v", err)
|
||||
}
|
||||
|
||||
log.Printf("Deployed new container: %s", containerName)
|
||||
return resp.ID, nil
|
||||
}
|
||||
|
||||
func (cm *ContainerManager) findExistingContainers(ctx context.Context, containerPrefix string) ([]string, error) {
|
||||
containers, err := cm.dockerClient.ContainerList(ctx, container.ListOptions{
|
||||
All: true,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var existingContainers []string
|
||||
for _, container := range containers {
|
||||
if strings.HasPrefix(container.Names[0], fmt.Sprintf("/%s", containerPrefix)) {
|
||||
existingContainers = append(existingContainers, container.ID)
|
||||
}
|
||||
}
|
||||
|
||||
return existingContainers, nil
|
||||
}
|
||||
25
server/schema.sql
Normal file
25
server/schema.sql
Normal file
@@ -0,0 +1,25 @@
|
||||
CREATE TABLE IF NOT EXISTS apps (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
image TEXT NOT NULL,
|
||||
project_path TEXT NOT NULL,
|
||||
project_config TEXT NOT NULL,
|
||||
deployment_id INTEGER,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY(deployment_id) REFERENCES deployments(id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS containers (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
container_id TEXT NOT NULL,
|
||||
deployment_id INTEGER NOT NULL,
|
||||
status TEXT NOT NULL,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY(deployment_id) REFERENCES deployments(id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS deployments (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
urls TEXT NOT NULL,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
160
server/server.go
Normal file
160
server/server.go
Normal file
@@ -0,0 +1,160 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"compress/gzip"
|
||||
"database/sql"
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
//go:embed schema.sql
|
||||
var schema embed.FS
|
||||
|
||||
var DefaultConfig = FluxServerConfig{
|
||||
Builder: "paketobuildpacks/builder-jammy-tiny",
|
||||
}
|
||||
|
||||
type FluxServerConfig struct {
|
||||
Builder string `json:"builder"`
|
||||
}
|
||||
|
||||
type FluxServer struct {
|
||||
containerManager *ContainerManager
|
||||
config FluxServerConfig
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
var rootDir string
|
||||
|
||||
func init() {
|
||||
rootDir = os.Getenv("FLUXD_ROOT_DIR")
|
||||
if rootDir == "" {
|
||||
rootDir = "/var/fluxd"
|
||||
}
|
||||
}
|
||||
|
||||
func NewServer() *FluxServer {
|
||||
containerManager := NewContainerManager()
|
||||
|
||||
var serverConfig FluxServerConfig
|
||||
|
||||
// parse config, if it doesnt exist, create it and use the default config
|
||||
configPath := filepath.Join(rootDir, "config.json")
|
||||
if _, err := os.Stat(configPath); err != nil {
|
||||
if err := os.MkdirAll(rootDir, 0755); err != nil {
|
||||
log.Fatalf("Failed to create fluxd directory: %v", err)
|
||||
}
|
||||
|
||||
configBytes, err := json.Marshal(DefaultConfig)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to marshal default config: %v", err)
|
||||
}
|
||||
|
||||
log.Printf("Config file not found, creating default config file at %s", configPath)
|
||||
if err := os.WriteFile(configPath, configBytes, 0644); err != nil {
|
||||
log.Fatalf("Failed to write config file: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
configFile, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to read config file: %v", err)
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(configFile, &serverConfig); err != nil {
|
||||
log.Fatalf("Failed to parse config file: %v", err)
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(filepath.Join(rootDir, "apps"), 0755); err != nil {
|
||||
log.Fatalf("Failed to create apps directory: %v", err)
|
||||
}
|
||||
|
||||
db, err := sql.Open("sqlite3", filepath.Join(rootDir, "fluxd.db"))
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open database: %v", err)
|
||||
}
|
||||
|
||||
// create database schema
|
||||
schemaBytes, err := schema.ReadFile("schema.sql")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to read schema file: %v", err)
|
||||
}
|
||||
|
||||
_, err = db.Exec(string(schemaBytes))
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create database schema: %v", err)
|
||||
}
|
||||
|
||||
return &FluxServer{
|
||||
containerManager: containerManager,
|
||||
config: serverConfig,
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *FluxServer) UploadAppCode(code io.Reader, projectConfig ProjectConfig) (string, error) {
|
||||
projectPath := filepath.Join(rootDir, "apps", projectConfig.Name)
|
||||
if err := os.MkdirAll(projectPath, 0755); err != nil {
|
||||
log.Printf("Failed to create project directory: %v", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
gzReader, err := gzip.NewReader(code)
|
||||
if err != nil {
|
||||
log.Printf("Failed to create gzip reader: %v", err)
|
||||
return "", err
|
||||
}
|
||||
defer gzReader.Close()
|
||||
|
||||
tarReader := tar.NewReader(gzReader)
|
||||
|
||||
// Extract files
|
||||
for {
|
||||
header, err := tarReader.Next()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
log.Printf("Failed to read tar header: %v", 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 {
|
||||
log.Printf("Failed to extract directory: %v", err)
|
||||
return "", err
|
||||
}
|
||||
case tar.TypeReg:
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
||||
log.Printf("Failed to extract directory: %v", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
outFile, err := os.Create(path)
|
||||
if err != nil {
|
||||
log.Printf("Failed to extract file: %v", err)
|
||||
return "", err
|
||||
}
|
||||
defer outFile.Close()
|
||||
|
||||
if _, err := io.Copy(outFile, tarReader); err != nil {
|
||||
log.Printf("Failed to copy file during extraction: %v", err)
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return projectPath, nil
|
||||
}
|
||||
Reference in New Issue
Block a user