Skip to content

Commit

Permalink
gh-96: rewrite using docker sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
viggy28 committed Mar 23, 2022
1 parent b855585 commit 585d955
Show file tree
Hide file tree
Showing 21 changed files with 1,048 additions and 716 deletions.
374 changes: 44 additions & 330 deletions api/create.go

Large diffs are not rendered by default.

76 changes: 0 additions & 76 deletions api/helpers.go

This file was deleted.

61 changes: 17 additions & 44 deletions api/list_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"io/fs"
"log"
"net/http"
"os"

"github.com/spinup-host/config"
"github.com/spinup-host/internal/metastore"
"github.com/spinup-host/misc"

_ "modernc.org/sqlite"
)
Expand All @@ -26,57 +27,29 @@ func ListCluster(w http.ResponseWriter, req *http.Request) {
var err error
config.Cfg.UserID, err = config.ValidateUser(authHeader, apiKeyHeader)
if err != nil {
log.Printf(err.Error())
log.Println("ERROR: validating user: ", err)
http.Error(w, "error validating user", http.StatusUnauthorized)
return
}
dbPath := config.Cfg.Common.ProjectDir + "/" + config.Cfg.UserID
clusterInfos := ReadClusterInfo(dbPath, config.Cfg.UserID)
clusterByte, err := json.Marshal(clusterInfos)
path := config.Cfg.Common.ProjectDir + "/" + config.Cfg.UserID + "/" + config.Cfg.UserID + ".db"
db, err := metastore.NewDb(path)
if err != nil {
log.Printf("ERROR: marshalling clusterInfos %v", err)
http.Error(w, "Internal server error ", 500)
misc.ErrorResponse(w, "error accessing sqlite database", 500)
return
}
w.Write(clusterByte)
}

type clusterInfo struct {
ID int `json:"id"`
ClusterID string `json:"cluster_id"`
Name string `json:"name"`
Port int `json:"port"`
Username string `json:"username"`
}

func ReadClusterInfo(path, dbName string) []clusterInfo {
dsn := path + "/" + dbName + ".db"
if _, err := os.Stat(dsn); errors.Is(err, fs.ErrNotExist) {
log.Printf("INFO: no sqlite database")
return nil
}
db, err := sql.Open("sqlite", dsn)
clustersInfo, err := metastore.ClustersInfo(db)
if err != nil {
log.Fatal(err)
log.Println("ERROR: reading from clusterInfo table: ", err)
http.Error(w, "reading from clusterInfo", http.StatusUnauthorized)
return
}
defer db.Close()
rows, err := db.Query("select id, clusterId, name, username, port from clusterInfo")
clusterByte, err := json.Marshal(clustersInfo)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var clusterIds []string
var clusterInfos []clusterInfo
var cluster clusterInfo
for rows.Next() {
err = rows.Scan(&cluster.ID, &cluster.ClusterID, &cluster.Name, &cluster.Username, &cluster.Port)
if err != nil {
log.Fatal(err)
}
clusterInfos = append(clusterInfos, cluster)
log.Printf("ERROR: marshalling clusterInfos %v", err)
http.Error(w, "Internal server error ", 500)
return
}
fmt.Println(clusterIds)
return clusterInfos
w.Write(clusterByte)
}

func GetCluster(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -133,8 +106,8 @@ func GetCluster(w http.ResponseWriter, r *http.Request) {
})
}

func getClusterFromDb(clusterId string) (clusterInfo, error) {
var ci clusterInfo
func getClusterFromDb(clusterId string) (config.ClusterInfo, error) {
var ci config.ClusterInfo
path := config.Cfg.Common.ProjectDir + "/" + config.Cfg.UserID
dsn := path + "/" + config.Cfg.UserID + ".db"
if _, err := os.Stat(dsn); errors.Is(err, fs.ErrNotExist) {
Expand Down
25 changes: 0 additions & 25 deletions backup/backup.go

This file was deleted.

59 changes: 59 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"strings"
"time"

"github.com/golang-jwt/jwt"
)
Expand All @@ -26,6 +27,64 @@ type Configuration struct {

var Cfg Configuration

type Service struct {
Duration time.Duration
UserID string
// one of arm64v8 or arm32v7 or amd64
Architecture string
//Port uint
Db dbCluster
DockerNetwork string
Version version
BackupEnabled bool
Backup backupConfig
}

type version struct {
Maj uint
Min uint
}
type dbCluster struct {
Name string
ID string
Type string
Port int
Username string
Password string

Memory string
Storage string
Monitoring string
}

type backupConfig struct {
// https://man7.org/linux/man-pages/man5/crontab.5.html
Schedule map[string]interface{}
Dest Destination `json:"Dest"`
}

type Destination struct {
Name string
BucketName string
ApiKeyID string
ApiKeySecret string
}
type serviceResponse struct {
HostName string
Port int
ContainerID string
}

type ClusterInfo struct {
ID int `json:"id"`
ClusterID string `json:"cluster_id"`
Name string `json:"name"`
Port int `json:"port"`
Username string `json:"username"`
MajVersion int `json:"majversion"`
MinVersion int `json:"minversion"`
}

func ValidateUser(authHeader string, apiKeyHeader string) (string, error) {
if authHeader == "" && apiKeyHeader == "" {
return "", errors.New("no authorization keys found")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.16
require (
github.com/containerd/containerd v1.5.7 // indirect
github.com/docker/docker v20.10.9+incompatible
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-connections v0.4.0
github.com/golang-jwt/jwt v3.2.1+incompatible
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.4.2
Expand Down
Loading

0 comments on commit 585d955

Please sign in to comment.