Skip to content

Commit

Permalink
feat: register new task definition
Browse files Browse the repository at this point in the history
  • Loading branch information
keidarcy committed Jul 29, 2023
1 parent 3b4bafd commit 7f1be84
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 102 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
VERSION := $(shell cat app-version)

dep:
go mod download

Expand All @@ -14,9 +16,11 @@ vet:
go vet

tag:
echo "Tagging version $(VERSION)"
git tag -a v$(VERSION) -m "Release v$(VERSION)"
git push origin v$(VERSION)


plan:
cd tests && terraform plan -var="cluster_count=10" -var="service_count=10" -var="task_count=10"

Expand Down
14 changes: 9 additions & 5 deletions api/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,13 @@ func (store *Store) ListTasks(clusterName, serviceName *string) ([]types.Task, e
}

// aws ecs register-task-definition --family ${{family}} --...
func (store *Store) RegisterTaskDefinition(family string) {
listTasksOutput, err := store.ecs.RegisterTaskDefinition(context.Background(), &ecs.RegisterTaskDefinitionInput{
Family: &family,
})
logger.Println(listTasksOutput, err)
// return registered task definition revision
func (store *Store) RegisterTaskDefinition(input *ecs.RegisterTaskDefinitionInput) (string, int32, error) {
registeredTdOutput, err := store.ecs.RegisterTaskDefinition(context.Background(), input)
if err != nil {
return "", 0, err
}
family := *registeredTdOutput.TaskDefinition.Family
revision := registeredTdOutput.TaskDefinition.Revision
return family, revision, nil
}
1 change: 1 addition & 0 deletions app-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.2
83 changes: 0 additions & 83 deletions sample/main.go

This file was deleted.

7 changes: 7 additions & 0 deletions ui/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ui

import (
"flag"
"fmt"

"github.com/aws/aws-sdk-go-v2/service/ecs/types"
"github.com/gdamore/tcell/v2"
Expand All @@ -19,6 +20,7 @@ const (
var (
logger = util.Logger
readonly = false
version = false
)

// Entity contains ECS resources to show
Expand Down Expand Up @@ -47,6 +49,7 @@ type App struct {

func init() {
flag.BoolVar(&readonly, "readonly", false, "Enable readonly mode")
flag.BoolVar(&version, "version", false, "Print e1s version")
}

func newApp() *App {
Expand All @@ -67,6 +70,10 @@ func newApp() *App {
// Entry point of the app
func Show() error {
flag.Parse()
if version {
fmt.Println("v" + util.AppVersion)
return nil
}
app := newApp()
app.initStyles()

Expand Down
42 changes: 41 additions & 1 deletion ui/modal.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
"github.com/rivo/tview"
)

// any form need at least one field
const placeholder = " (form placeholder) "

// Show update service modal and handle submit event
func (v *View) showEditServiceModal() {
if v.kind != ServicePage {
Expand All @@ -35,7 +38,17 @@ func (v *View) showAutoScaling() {
v.app.Pages.AddPage(title, v.modal(content, 100, 25), true, true)
}

const placeholder = " (form placeholder) "
// Show task definition register confirm modal
func (v *View) showTaskDefinitionConfirm(fn func()) {
if v.kind != TaskPage {
return
}
content, title := v.taskDefinitionRegisterContent(fn)
if content == nil {
return
}
v.app.Pages.AddPage(title, v.modal(content, 100, 10), true, true)
}

// Show service metrics modal(Memory/CPU)
func (v *View) showMetrics() {
Expand All @@ -49,6 +62,33 @@ func (v *View) showMetrics() {
v.app.Pages.AddPage(title, v.modal(content, 100, 15), true, true)
}

// Get task definition register content
func (v *View) taskDefinitionRegisterContent(fn func()) (*tview.Form, string) {
if v.kind != TaskPage {
return nil, ""
}

readonly := "[-:-:-](readonly) "
title := " Register edited [purple::b]task definition?" + readonly
f := v.styledForm(title)

// handle form close
f.AddButton("Cancel", func() {
v.closeModal()
})

// readonly mode has no submit button
if v.app.readonly {
return f, title
}

// handle form submit
f.AddButton("Register", func() {
fn()
})
return f, title
}

// Get service auto scaling form
func (v *View) serviceAutoScalingContent() (*tview.Form, string) {
if v.kind != ServicePage {
Expand Down
45 changes: 38 additions & 7 deletions ui/table.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package ui

import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"

"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/gdamore/tcell/v2"
"github.com/keidarcy/e1s/util"
"github.com/rivo/tview"
Expand Down Expand Up @@ -206,8 +209,14 @@ func (v *View) editTaskDefinition() {
defer os.Remove(tmpfile.Name())
defer tmpfile.Close()

jsonData, _ := json.MarshalIndent(td, "", " ")
if _, err := tmpfile.Write(jsonData); err != nil {
originalTD, err := json.MarshalIndent(td, "", " ")
if err != nil {
logger.Println("Error reading temporary file:", err)
v.errorModal(errMsg)
return
}

if _, err := tmpfile.Write(originalTD); err != nil {
logger.Println("Error writing to temporary file:", err)
v.errorModal(errMsg)
return
Expand All @@ -230,21 +239,43 @@ func (v *View) editTaskDefinition() {
return
}

file, err := os.ReadFile(tmpfile.Name())
editedTD, err := os.ReadFile(tmpfile.Name())
if err != nil {
logger.Println("Error reading temporary file:", err)
v.errorModal(errMsg)
return
}
logger.Println(string(file))

if err := json.Unmarshal(file, &taskDefinition); err != nil {
// remove edited empty line
if editedTD[len(editedTD)-1] == '\n' {
originalTD = append(originalTD, '\n')
}

// if no change do nothing
if bytes.Equal(originalTD, editedTD) {
v.flashModal(" no change", 2)
return
}

var updatedTd ecs.RegisterTaskDefinitionInput
if err := json.Unmarshal(editedTD, &updatedTd); err != nil {
logger.Println("Error unmarshaling JSON:", err)
v.errorModal(errMsg)
return
}

logger.Println("Modified JSON Data:")
logger.Println(taskDefinition)
register := func() {
family, revision, err := v.app.Store.RegisterTaskDefinition(&updatedTd)

if err != nil {
logger.Println("Error opening editor:", err)
v.errorModal(errMsg)
return
}
v.successModal(fmt.Sprintf("SUCCESS 🚀\nTaskDefinition Family: %s\nRevision: %d\n", family, revision))
}

v.showTaskDefinitionConfirm(register)

})
}
4 changes: 2 additions & 2 deletions ui/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ const (
showMetrics = "Show metrics"

editService = "Edit Service"
editTaskDefinition = "Edit Task Definition(WIP)"
editTaskDefinition = "Edit Task Definition"
openInBrowser = "Open in browser"
sshContainer = "SSH container"
toggleFullScreen = "Toggle full screen"
toggleFullScreen = "JSON Toggle full screen"
)

const (
Expand Down
19 changes: 15 additions & 4 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,35 @@ const (
clusterURLFmt = clusterFmt + regionFmt
serviceURLFmt = clusterFmt + serviceFmt + regionFmt
taskURLFmt = clusterFmt + serviceFmt + taskFmt + regionFmt
AppName = "e1s"
AppVersion = "1.0.2"

versionFilename = "app-version"
AppName = "e1s"
)

var (
Logger *log.Logger
Logger *log.Logger
AppVersion string
)

func init() {
// init basic logger
logPath := fmt.Sprintf("/tmp/%s-debug.log", AppName)
logFile, err := os.OpenFile(logPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Printf("failed to open log file path: %s, err: %v\n", logPath, err)
os.Exit(1)
}
// defer logFile.Close()

// defer logFile.Close()
Logger = log.New(logFile, "", log.LstdFlags)

// read app-version
content, err := os.ReadFile(versionFilename)
if err != nil {
AppVersion = "N/A"
}

AppVersion = string(content)
}

func ArnToName(arn *string) string {
Expand Down

0 comments on commit 7f1be84

Please sign in to comment.