Skip to content

Commit

Permalink
Merge pull request #151 from kthcloud/dev
Browse files Browse the repository at this point in the history
add zone support, rework auth model and bug fixes
  • Loading branch information
saffronjam authored Aug 21, 2023
2 parents 3256536 + 5153991 commit 9b4d9de
Show file tree
Hide file tree
Showing 76 changed files with 1,535 additions and 904 deletions.
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"flag"
"go-deploy/pkg/app"
"log"
Expand Down Expand Up @@ -59,9 +60,12 @@ func main() {
log.Println("no workers specified, starting all")
}

server := app.Start(options)
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
server := app.Start(ctx, options)
if server != nil {
defer func() {
cancel()
app.Stop(server)
}()
}
Expand Down
28 changes: 14 additions & 14 deletions models/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,18 @@ func findCollection(collectionName string) *mongo.Collection {
}

func Shutdown() {
makeError := func(err error) error {
return fmt.Errorf("failed to shutdown database. details: %s", err)
}

DeploymentCollection = nil
VmCollection = nil
GpuCollection = nil
UserCollection = nil
JobCollection = nil

err := client.Disconnect(context.Background())
if err != nil {
log.Fatalln(makeError(err))
}
//makeError := func(err error) error {
// return fmt.Errorf("failed to shutdown database. details: %s", err)
//}
//
//DeploymentCollection = nil
//VmCollection = nil
//GpuCollection = nil
//UserCollection = nil
//JobCollection = nil
//
//err := client.Disconnect(context.Background())
//if err != nil {
// log.Fatalln(makeError(err))
//}
}
21 changes: 13 additions & 8 deletions models/dto/body/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type DeploymentCreate struct {
Token string `json:"token" binding:"required,min=1,max=1000"`
RepositoryID int64 `json:"repositoryId" binding:"required"`
} `json:"github" binding:"omitempty,dive"`
Zone *string `json:"zone" binding:"omitempty"`
}

type DeploymentUpdate struct {
Expand Down Expand Up @@ -43,15 +44,19 @@ type DeploymentUpdated struct {
}

type DeploymentRead struct {
ID string `json:"id"`
Name string `json:"name"`
OwnerID string `json:"ownerId"`
Status string `json:"status"`
URL *string `json:"url,omitempty"`
Envs []Env `json:"envs"`
Private bool `json:"private"`
ID string `json:"id"`
Name string `json:"name"`
OwnerID string `json:"ownerId"`
Zone string `json:"zone"`

URL *string `json:"url,omitempty"`
Envs []Env `json:"envs"`
Private bool `json:"private"`

Status string `json:"status"`
PingResult *int `json:"pingResult,omitempty"`

Integrations []string `json:"integrations"`
PingResult *int `json:"pingResult,omitempty"`
}

type CiConfig struct {
Expand Down
9 changes: 8 additions & 1 deletion models/dto/body/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ type Quota struct {
CpuCores int `json:"cpuCores"`
RAM int `json:"ram"`
DiskSize int `json:"diskSize"`
Snapshots int `json:"snapshots"`
}

type Role struct {
Name string `json:"name"`
Description string `json:"description"`
}

type UserRead struct {
ID string `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
Roles []string `json:"roles"`
Role Role `json:"role"`
Admin bool `json:"admin"`
Quota Quota `json:"quota"`
Usage Quota `json:"usage"`
PublicKeys []PublicKey `json:"publicKeys"`
Expand Down
13 changes: 7 additions & 6 deletions models/dto/body/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ type VmRead struct {
}

type VmCreate struct {
Name string `json:"name" binding:"required,rfc1035,min=3,max=30"`
SshPublicKey string `json:"sshPublicKey" binding:"required,ssh_public_key"`
Ports []Port `json:"ports" binding:"omitempty,port_list_names,port_list_numbers,dive,min=0,max=1000"`
CpuCores int `json:"cpuCores" binding:"required,min=1"`
RAM int `json:"ram" binding:"required,min=1"`
DiskSize int `json:"diskSize" binding:"required,min=20"`
Name string `json:"name" binding:"required,rfc1035,min=3,max=30"`
SshPublicKey string `json:"sshPublicKey" binding:"required,ssh_public_key"`
Ports []Port `json:"ports" binding:"omitempty,port_list_names,port_list_numbers,dive,min=0,max=1000"`
CpuCores int `json:"cpuCores" binding:"required,min=1"`
RAM int `json:"ram" binding:"required,min=1"`
DiskSize int `json:"diskSize" binding:"required,min=20"`
Zone *string `json:"zone" binding:"omitempty"`
}

type VmUpdate struct {
Expand Down
7 changes: 7 additions & 0 deletions models/dto/body/zone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package body

type ZoneRead struct {
Name string `json:"name"`
Description string `json:"description"`
Type string `json:"type"`
}
5 changes: 5 additions & 0 deletions models/dto/query/zone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package query

type ZoneList struct {
Type *string `form:"type" binding:"omitempty,oneof=deployment vm"`
}
6 changes: 6 additions & 0 deletions models/dto/uri/zone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package uri

type ZoneGet struct {
Name string `uri:"name" binding:"required,rfc1035,min=1,max=30"`
Type string `uri:"type" binding:"required,oneof=deployment vm"`
}
28 changes: 19 additions & 9 deletions models/sys/deployment/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,19 @@ func (deployment *Deployment) ToDTO(url *string) body.DeploymentRead {
}

return body.DeploymentRead{
ID: deployment.ID,
Name: deployment.Name,
OwnerID: deployment.OwnerID,
Status: deployment.StatusMessage,
URL: fullURL,
Envs: envs,
Private: deployment.Private,
ID: deployment.ID,
Name: deployment.Name,
OwnerID: deployment.OwnerID,
Zone: deployment.Zone,

URL: fullURL,
Envs: envs,
Private: deployment.Private,

Status: deployment.StatusMessage,
PingResult: pingResult,

Integrations: integrations,
PingResult: pingResult,
}
}

Expand All @@ -70,7 +74,7 @@ func (p *UpdateParams) FromDTO(dto *body.DeploymentUpdate) {
p.ExtraDomains = dto.ExtraDomains
}

func (p *CreateParams) FromDTO(dto *body.DeploymentCreate) {
func (p *CreateParams) FromDTO(dto *body.DeploymentCreate, fallbackZone *string) {
p.Name = dto.Name
p.Private = dto.Private
p.Envs = make([]Env, len(dto.Envs))
Expand All @@ -87,6 +91,12 @@ func (p *CreateParams) FromDTO(dto *body.DeploymentCreate) {
RepositoryID: dto.GitHub.RepositoryID,
}
}

if dto.Zone != nil {
p.Zone = *dto.Zone
} else {
p.Zone = *fallbackZone
}
}

func (p *BuildParams) FromDTO(dto *body.DeploymentBuild) {
Expand Down
1 change: 1 addition & 0 deletions models/sys/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type Deployment struct {
ID string `bson:"id"`
Name string `bson:"name"`
OwnerID string `bson:"ownerId"`
Zone string `bson:"zone"`

CreatedAt time.Time `bson:"createdAt"`
UpdatedAt time.Time `bson:"updatedAt"`
Expand Down
15 changes: 15 additions & 0 deletions models/sys/deployment/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func CreateDeployment(deploymentID, ownerID string, params *CreateParams) (bool,

StatusCode: status_codes.ResourceBeingCreated,
StatusMessage: status_codes.GetMsg(status_codes.ResourceBeingCreated),

Zone: params.Zone,
}

result, err := models.DeploymentCollection.UpdateOne(context.TODO(), bson.D{{"name", params.Name}}, bson.D{
Expand Down Expand Up @@ -296,6 +298,19 @@ func RemoveActivity(deploymentID, activity string) error {
return nil
}

func ClearActivities(deploymentID string) error {
_, err := models.DeploymentCollection.UpdateOne(context.TODO(),
bson.D{{"id", deploymentID}},
bson.D{{"$set", bson.D{{"activities", bson.A{}}}}},
)
if err != nil {
err = fmt.Errorf("failed to clear activities from deployment %s. details: %s", deploymentID, err)
return err
}

return nil
}

func MarkRepaired(deploymentID string) error {
filter := bson.D{{"id", deploymentID}}
update := bson.D{
Expand Down
1 change: 1 addition & 0 deletions models/sys/deployment/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type CreateParams struct {
Private bool `json:"private" bson:"private"`
Envs []Env `json:"envs" bson:"envs"`
GitHub *GitHubCreateParams `json:"github,omitempty" bson:"github,omitempty"`
Zone string `json:"zone,omitempty" bson:"zoneId,omitempty"`
}

type BuildParams struct {
Expand Down
45 changes: 45 additions & 0 deletions models/sys/enviroment/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package enviroment

import roleModel "go-deploy/models/sys/enviroment/role"

func (e *Environment) GetRole(roleName string) *roleModel.Role {
for _, role := range e.Roles {
if role.Name == roleName {
return &role
}
}

return nil
}

func (e *Environment) GetRolesByIamGroups(iamGroups []string) []roleModel.Role {
var roles []roleModel.Role

for _, role := range e.Roles {
for _, iamGroup := range iamGroups {
if role.IamGroup == iamGroup {
roles = append(roles, role)
}
}
}

return roles
}

func (d *Deployment) GetZone(name string) *DeploymentZone {
for _, zone := range d.Zones {
if zone.Name == name {
return &zone
}
}
return nil
}

func (v *VM) GetZone(name string) *VmZone {
for _, zone := range v.Zones {
if zone.Name == name {
return &zone
}
}
return nil
}
Loading

0 comments on commit 9b4d9de

Please sign in to comment.