Skip to content

Commit

Permalink
Merge pull request #3 from arshamalh/rm-rename-conts-imgs
Browse files Browse the repository at this point in the history
Remove and Rename functionality for Images and Containers
  • Loading branch information
arshamalh committed Nov 18, 2023
2 parents d94170e + 2d36845 commit 20837ef
Show file tree
Hide file tree
Showing 28 changed files with 903 additions and 274 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TOKEN="telgram-bot-token"
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
.env
log.json
.github
.vscode/
vendor
log
/.idea/

30 changes: 0 additions & 30 deletions api/api.go

This file was deleted.

3 changes: 1 addition & 2 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"os"
"time"

"github.com/arshamalh/dockeroller/contracts"
"github.com/arshamalh/dockeroller/docker"
"github.com/arshamalh/dockeroller/log"
"github.com/arshamalh/dockeroller/repo/ephemeral"
Expand Down Expand Up @@ -53,7 +52,7 @@ func start(token string, whitelistedIDs []int64) {
startTelegram(docker, token, whitelistedIDs)
}

func startTelegram(docker contracts.Docker, token string, whitelistedIDs []int64) {
func startTelegram(docker docker.Docker, token string, whitelistedIDs []int64) {
bot, err := telebot.NewBot(telebot.Settings{
Token: token,
Poller: &telebot.LongPoller{Timeout: 10 * time.Second},
Expand Down
18 changes: 0 additions & 18 deletions contracts/docker.go

This file was deleted.

11 changes: 11 additions & 0 deletions docker/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,14 @@ func (d *docker) ContainerStart(containerID string) error {
func (d *docker) ContainerStop(containerID string) error {
return d.cli.ContainerStop(context.TODO(), containerID, nil)
}

func (d *docker) ContainerRemove(containerID string, removeForm *models.ContainerRemoveForm) error {
return d.cli.ContainerRemove(context.TODO(), containerID, types.ContainerRemoveOptions{
RemoveVolumes: removeForm.RemoveVolumes,
Force: removeForm.Force,
})
}

func (d *docker) ContainerRename(containerID, newName string) error {
return d.cli.ContainerRename(context.TODO(), containerID, newName)
}
19 changes: 19 additions & 0 deletions docker/docker.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
package docker

import (
"context"
"io"

"github.com/arshamalh/dockeroller/log"
"github.com/arshamalh/dockeroller/models"
"github.com/moby/moby/client"
)

type Docker interface {
GetContainer(containerID string) (*models.Container, error)
ContainersList() []*models.Container
ContainerLogs(containerID string) (io.ReadCloser, error)
ContainerStats(containerID string) (io.ReadCloser, error)
ContainerStart(containerID string) error
ContainerStop(containerID string) error
ContainerRemove(containerID string, removeForm *models.ContainerRemoveForm) error
ContainerRename(containerID, newName string) error

ImagesList() []*models.Image
ImageTag(ctx context.Context, imageID, newTag string) error
ImageRemove(ctx context.Context, imageID string, force, pruneChildren bool) error
}

func New() *docker {
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
Expand Down
54 changes: 49 additions & 5 deletions docker/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,63 @@ package docker

import (
"context"
"fmt"
"time"

"github.com/arshamalh/dockeroller/models"
"github.com/docker/docker/api/types"
)

func (d docker) ImagesList() (images []*models.Image) {
func (d *docker) ImagesList() (images []*models.Image) {
raw_images, _ := d.cli.ImageList(context.TODO(), types.ImageListOptions{All: true})
for _, rimg := range raw_images {
for _, raw_img := range raw_images {
status := d.getImageStatus(context.TODO(), raw_img)
images = append(images, &models.Image{
ID: rimg.ID,
Size: rimg.Size,
Tags: rimg.RepoTags,
ID: raw_img.ID,
Size: raw_img.Size,
Tags: raw_img.RepoTags,
Status: models.ImageStatus(status),
CreatedAt: fmt.Sprint(time.Unix(raw_img.Created, 0).Format("2006-01-02 15:04:05")),
})
}
return
}

func (d *docker) ImageTag(ctx context.Context, imageID, newTag string) error {
return d.cli.ImageTag(ctx, imageID, newTag)
}

func (d *docker) ImageRemove(ctx context.Context, imageID string, force, pruneChildren bool) error {
_, err := d.cli.ImageRemove(ctx, imageID,
types.ImageRemoveOptions{
Force: force,
PruneChildren: pruneChildren,
},
)
return err
}

func (d *docker) getImageStatus(ctx context.Context, image types.ImageSummary) (status string) {
if len(image.RepoTags) == 0 {
status = string(models.ImageStatusUnUsedDangling)
}

containers, _ := d.cli.ContainerList(ctx, types.ContainerListOptions{})
newImgs := make(map[string][]string)
for _, cont := range containers {
if cont.ImageID != image.ID {
status = string(models.ImageStatusUnUsed)
} else {
newSlice := newImgs[image.ID]
if newSlice == nil {
newSlice = make([]string, 0)
}
newSlice = append(newSlice, cont.ID)
newImgs[image.ID] = newSlice

status = string(models.ImageStatusInUse)
}
}

return
}
23 changes: 5 additions & 18 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,20 @@ require (
)

require (
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.10.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
golang.org/x/text v0.3.6 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
golang.org/x/time v0.4.0 // indirect
gotest.tools/v3 v3.5.1 // indirect
)

require (
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/gin-gonic/gin v1.8.1
github.com/gogo/protobuf v1.3.2 // indirect
github.com/moby/moby v20.10.18+incompatible
github.com/opencontainers/go-digest v1.0.0 // indirect
Expand All @@ -45,5 +32,5 @@ require (
github.com/spf13/cobra v1.7.0
go.uber.org/zap v1.26.0
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
golang.org/x/sys v0.1.0 // indirect
)
Loading

0 comments on commit 20837ef

Please sign in to comment.