Skip to content

Commit

Permalink
Merge pull request #24 from meltred/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
KunalSin9h committed Nov 29, 2023
2 parents 6b713db + 2eedf11 commit 1fa0d8f
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 15 deletions.
10 changes: 9 additions & 1 deletion cmd/meltcd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func getAllApplications(_ *cobra.Command, _ []string) error {
table.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)

for _, v := range resPayload.Data {
table.AddRow(v.ID[:7], v.Name, v.Health, v.LastSyncedAt.Format(time.RFC822), v.CreatedAt.Format(time.RFC822), v.UpdatedAT.Format(time.RFC822))
table.AddRow(v.ID, v.Name, v.Health, v.LastSyncedAt.Format(time.RFC822), v.CreatedAt.Format(time.RFC822), v.UpdatedAT.Format(time.RFC822))
}

table.Print()
Expand All @@ -153,6 +153,14 @@ func getSpecFromData(cmd *cobra.Command, args []string) (application.Spec, error
info("Application with Specification file")
// Creating application without application name
// means using a file

// if user has specified --repo then he/she must have forgotten the app name
// and come here
repo, err := cmd.Flags().GetString("repo")
if repo != "" && err == nil {
return application.Spec{}, errors.New("missing application name")
}

file, err := cmd.Flags().GetString("file")
if err != nil {
return application.Spec{}, err
Expand Down
4 changes: 2 additions & 2 deletions examples/service.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "my_app",
"refresh_timer": "2m0s",
"source": {
"repoURL": "https://github.com/kunalsin9h/tiddi.git",
"path": "compose.yml",
"repoURL": "https://github.com/k9exp/infra-test.git",
"path": "service.yml",
"targetRevision": "HEAD"
}
}
4 changes: 2 additions & 2 deletions examples/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ name: My-Application
refresh_timer: "3m0s"

source:
repoURL: https://github.com/kunalsin9h/tiddi.git
path: compose.yml
repoURL: https://github.com/k9exp/infra-test.git
path: service.yml
targetRevision: HEAD
35 changes: 32 additions & 3 deletions internal/core/application/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,25 @@ import (
"bytes"
"context"
"errors"
"strings"
"time"

"github.com/meltred/meltcd/spec"

"github.com/charmbracelet/log"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/api/types/volume"
"github.com/docker/docker/client"
"github.com/go-git/go-billy/v5/memfs"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/storage/memory"
"gopkg.in/yaml.v2"
)

type Application struct {
ID string `json:"id"`
ID uint32 `json:"id"`
Name string `json:"name"`
Source Source `json:"source"`
RefreshTimer string `json:"refresh_timer"` // Timer to check for Sync format of "3m50s"
Expand Down Expand Up @@ -170,10 +173,16 @@ func (app *Application) GetState() (string, error) {
// TODO: Improvement
// GET the name and commit also
// so that we can show it in the ui or something
ref := plumbing.HEAD
if app.Source.TargetRevision != "HEAD" {
ref = plumbing.NewBranchReferenceName(app.Source.TargetRevision)
}

_, err := git.Clone(storage, fs, &git.CloneOptions{
URL: app.Source.RepoURL,
URL: app.Source.RepoURL,
ReferenceName: ref,
})

if errors.Is(err, git.ErrRepositoryAlreadyExists) {
// fetch & pull request
// don't clone again
Expand Down Expand Up @@ -211,6 +220,27 @@ func (app *Application) Apply(targetState string) error {
return err
}

// TODO use volOpts
// create volume
for volName, volOpts := range swarmSpec.Volumes {
labels := make(map[string]string)
for _, l := range volOpts.Labels {
tokens := strings.Split(l, "=")
if len(tokens) != 2 {
return errors.New("invalid labels in volume")
}

labels[tokens[0]] = tokens[1]
}

cli.VolumeCreate(context.Background(), volume.CreateOptions{
Name: volName,
Driver: volOpts.Driver,
DriverOpts: volOpts.DriverOpts,
Labels: labels, // TODO labels are not working
})
}

services, err := swarmSpec.GetServiceSpec(app.Name)
if err != nil {
return err
Expand Down Expand Up @@ -252,7 +282,6 @@ func (app *Application) Apply(targetState string) error {
log.Warn("New Service Create give warnings", "warnings", res.Warnings)
}

app.ID = res.ID
app.LastSyncedAt = time.Now()
}

Expand Down
6 changes: 3 additions & 3 deletions internal/core/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ type AppList struct {
}

type AppStatus struct {
ID string `json:"id"`
ID uint32 `json:"id"`
Name string `json:"name"`
Health string `json:"health"`
CreatedAt time.Time `json:"created_at"`
Expand All @@ -104,9 +104,9 @@ type AppStatus struct {
func List() AppList {
var res AppList

for _, app := range Applications {
for index, app := range Applications {
res.Data = append(res.Data, AppStatus{
ID: app.ID,
ID: uint32(index),
Name: app.Name,
Health: app.Health.ToString(),
CreatedAt: app.CreatedAt,
Expand Down
10 changes: 6 additions & 4 deletions spec/dockerSwarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ type Network struct {
}

type Volume struct {
Name string `yaml:"name"`
Driver string `yaml:"driver"`
Options map[string]string `yaml:"options"`
Name string `yaml:"name"`
Driver string `yaml:"driver"`
DriverOpts map[string]string `yaml:"driver_opts"`
Labels []string `yaml:"labels"`
Options map[string]string `yaml:"options"`
}

func (d *DockerSwarm) GetServiceSpec(appName string) ([]swarm.ServiceSpec, error) {
Expand Down Expand Up @@ -88,7 +90,7 @@ func (d *DockerSwarm) GetServiceSpec(appName string) ([]swarm.ServiceSpec, error
}

targetSpec.TaskTemplate.ContainerSpec.Mounts = append(targetSpec.TaskTemplate.ContainerSpec.Mounts, mount.Mount{
Type: mount.TypeBind,
Type: mount.TypeVolume,
Source: tokens[0],
Target: tokens[1],
})
Expand Down

0 comments on commit 1fa0d8f

Please sign in to comment.