Fix container duplication

This commit is contained in:
2020-11-08 01:32:45 +01:00
parent f44caa8ad8
commit 0bf7e54ad7
6 changed files with 69 additions and 28 deletions

21
Dockerfile Normal file
View File

@ -0,0 +1,21 @@
FROM golang:alpine as builder
# Application Directory
RUN mkdir /app
WORKDIR /app
# First handle dependencies as those probably are more stable than rest of codebase
COPY ./go.mod /app/
COPY ./go.sum /app/
RUN go mod download
# Copy source and build app
COPY . /app
RUN go build .
FROM alpine
# Copy over the app from the builder image
COPY --from=builder /app/lookbuilding /lookbuilding
ENTRYPOINT ["/lookbuilding"]

View File

@ -4,10 +4,8 @@ import (
"context"
"fmt"
"strings"
"errors"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
)
@ -111,12 +109,6 @@ func (lc LabeledContainer) UpdateTo(cli *client.Client, tag Tag) error {
return err
}
if len(lc.Container.Names) != 1 {
return errors.New("containers with more (or fewer) than 1 name are not supported")
}
name := lc.Container.Names[0]
tmpOldName := fmt.Sprintf("%s.lb.old", name)
fmt.Printf("Stopping container %s\n", lc.Container.ID)
// TODO: hopefully this is blocking
err = cli.ContainerStop(ctx, lc.Container.ID, nil)
@ -124,6 +116,20 @@ func (lc LabeledContainer) UpdateTo(cli *client.Client, tag Tag) error {
return err
}
oldContainer, err := cli.ContainerInspect(ctx, lc.Container.ID)
if err != nil {
return err
}
name := oldContainer.Name
tmpOldName := fmt.Sprintf("%s.lb.old", name)
config := oldContainer.Config
config.Image = image
hostConfig := oldContainer.HostConfig
hostConfig.VolumesFrom = []string{tmpOldName}
fmt.Printf("Renaming container %s\n", lc.Container.ID)
err = cli.ContainerRename(ctx, lc.Container.ID, tmpOldName)
if err != nil {
@ -131,29 +137,22 @@ func (lc LabeledContainer) UpdateTo(cli *client.Client, tag Tag) error {
}
fmt.Printf("Creating new container\n")
body, err := cli.ContainerCreate(ctx, &container.Config{
Image: image,
//Cmd: []string{"echo", "hello world"},
Labels: lc.Container.Labels,
Tty: false,
}, &container.HostConfig{
VolumesFrom: []string{tmpOldName},
}, &network.NetworkingConfig{
EndpointsConfig: lc.Container.NetworkSettings.Networks,
new, err := cli.ContainerCreate(ctx, oldContainer.Config, hostConfig, &network.NetworkingConfig{
EndpointsConfig: oldContainer.NetworkSettings.Networks,
}, name)
if err != nil {
return err
}
fmt.Printf("Starting new container id: %s\n", body.ID)
err = cli.ContainerStart(ctx, body.ID, types.ContainerStartOptions{})
fmt.Printf("Starting new container id: %s\n", new.ID)
err = cli.ContainerStart(ctx, new.ID, types.ContainerStartOptions{})
if err != nil {
return err
}
fmt.Printf("Removing old container\n")
err = cli.ContainerRemove(ctx, tmpOldName, types.ContainerRemoveOptions{
err = cli.ContainerRemove(ctx, oldContainer.ID, types.ContainerRemoveOptions{
RemoveVolumes: false,
RemoveLinks: false,
Force: false,

26
main.go
View File

@ -2,12 +2,12 @@ package main
import (
"fmt"
"net/http"
//"github.com/sirupsen/logrus" TODO
"github.com/docker/docker/client"
)
func main() {
func checkForUpdates() {
cli, err := client.NewEnvClient()
if err != nil {
panic(err)
@ -56,6 +56,28 @@ func main() {
if err != nil {
panic(err)
}
} else {
fmt.Println(" no update available")
}
}
fmt.Println("all done")
}
func main() {
addr := "0.0.0.0:8000"
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
go checkForUpdates()
fmt.Fprintf(w, "OK")
})
fs := http.FileServer(http.Dir("static/"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
fmt.Printf("Listening on %s\n", addr)
err := http.ListenAndServe(addr, nil)
if err != nil {
panic(err)
}
}

View File

@ -16,7 +16,7 @@ type Tag struct {
}
func anonymousClient() (*registry.Registry, error) {
url := "https://registry-1.docker.io/"
url := "https://registry-1.docker.io/"
username := "" // anonymous
password := "" // anonymous
@ -43,7 +43,7 @@ func getDockerRepoTags(hub *registry.Registry, maybe_owner *string, repository s
svt := parseTagAsSemVer(tag)
out = append(out, Tag { tag, svt, digest })
out = append(out, Tag{tag, svt, digest})
}
return out, nil

View File

@ -12,7 +12,7 @@ var (
)
type SemVerTag struct {
prefix string
prefix string
version semver.Version
}
@ -31,7 +31,7 @@ func parseTagAsSemVer(tag string) *SemVerTag {
return nil
}
svt := SemVerTag {
svt := SemVerTag{
prefix,
*version,
}
@ -42,4 +42,3 @@ func parseTagAsSemVer(tag string) *SemVerTag {
func (SemVerTag) asTag() string {
return ""
}

View File

@ -42,7 +42,7 @@ func (SemVerMajor) ShouldUpdate(currentTag string, availableTags []Tag) *Tag {
semverTags := make([]Tag, 0)
for _, tag := range availableTags {
if tag.SemVer != nil {
if tag.SemVer != nil && currentSemVer.version.LessThan(tag.SemVer.version) {
semverTags = append(semverTags, tag);
}
}