Skip to content

Commit

Permalink
Refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
virtualzone committed Apr 23, 2022
1 parent 6c80ae6 commit 6bfb8d4
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 77 deletions.
16 changes: 10 additions & 6 deletions src/compose-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,19 @@ func (s *ComposeService) Build() bool {
return true
}

/*
func (s *ComposeService) RequiresBuild() bool {
return len(s.BuildInfo) > 0
}

func (s *ComposeService) IsWatched() bool {
return s.Instance != nil
}

// ATTENTION: docker compose restart does not use an updated image in Docker Compose V2 yet.
func (s *ComposeService) Restart() bool {
log.Println("docker", "compose", "-f", s.ComposeFile.YamlFilePath, "up", "-d", "--no-deps", s.Name)
out, err := exec.Command("docker", "compose", "-f", s.ComposeFile.YamlFilePath, "up", "-d", "--no-deps", s.Name).CombinedOutput()
err := exec.Command("docker", "compose", "-f", s.ComposeFile.YamlFilePath, "up", "-d", "--no-deps", s.Name).Run()
if err != nil {
log.Println(err)
return false
}
log.Println("-----> " + string(out))
return true
}
*/
5 changes: 5 additions & 0 deletions src/docker-image.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ func (i *DockerImage) ReadImageHash() error {
i.Hash = strings.TrimSpace(string(out))
return nil
}

func (i *DockerImage) ExistsNewerImageHash() bool {
newImage := CreateDockerImageInstance(i.ID)
return i.Hash != newImage.Hash
}
9 changes: 3 additions & 6 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@ func printHeader() {
}

func mainLoop() {
updater := &Updater{}
for {
PerformComposeUpdates()
if GlobalSettings.Cleanup {
log.Println("Removing unused images...")
CleanUp()
}
log.Println("Done.")
updater.PerformComposeUpdates()
if GlobalSettings.Once {
return
}
Expand All @@ -54,6 +50,7 @@ func main() {
if GlobalSettings.PrintSettings {
GlobalSettings.Print()
}
CreateEventBus()
initLogger(GlobalSettings.UpdateLog)
mainLoop()
}
55 changes: 55 additions & 0 deletions src/update-event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import "log"

type UpdateEvent struct{}

var EventBus *UpdateEvent = nil

func CreateEventBus() {
EventBus = &UpdateEvent{}
}

func (e *UpdateEvent) OnPerformUpdatesStart() {
log.Println("Gathering details about running containers...")
}

func (e *UpdateEvent) OnPerformUpdatesComplete() {
log.Println("Done.")
}

func (e *UpdateEvent) OnProcessComposeFileStart(composeFile *ComposeFile) {
log.Printf("Checking for updates of services in %s...\n", composeFile.YamlFilePath)
}

func (e *UpdateEvent) OnSkipRestartComposeFileDryMode(composeFile *ComposeFile) {
log.Printf("Dry-Mode enabled, not restarting services in %s\n", composeFile.YamlFilePath)
}

func (e *UpdateEvent) OnSkipRestartComposeFileNoUpdates(composeFile *ComposeFile) {
log.Printf("No need to restart services in %s\n", composeFile.YamlFilePath)
}

func (e *UpdateEvent) OnRestartComposeFile(composeFile *ComposeFile) {
log.Printf("Restarting services in %s...\n", composeFile.YamlFilePath)
}

func (e *UpdateEvent) OnRestartComposeFileComplete(composeFile *ComposeFile) {
log.Printf("Restarted services in %s\n", composeFile.YamlFilePath)
}

func (e *UpdateEvent) OnProcessServiceStart(service *ComposeService) {
log.Printf("Processing service %s (requires build: %t, watched: %t)...\n", service.Name, service.RequiresBuild(), service.IsWatched())
}

func (e *UpdateEvent) OnServiceNewImageBuilt(service *ComposeService) {
log.Printf("Built new image for service %s\n", service.Name)
}

func (e *UpdateEvent) OnServiceNewImagePulled(service *ComposeService) {
log.Printf("Pulled new image %s for service %s\n", service.ImageName, service.Name)
}

func (e *UpdateEvent) OnImagePruneStart() {
log.Println("Removing unused images...")
}
103 changes: 54 additions & 49 deletions src/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,71 @@ import (
"os/exec"
)

func PerformComposeUpdates() {
log.Println("Gathering details about running containers...")
composeFiles := createComposeFileContainerMapping()
type Updater struct{}

func (u *Updater) PerformComposeUpdates() {
EventBus.OnPerformUpdatesStart()
composeFiles := u.createComposeFileContainerMapping()
for _, composeFile := range composeFiles {
compositionRestart := false
log.Printf("Checking for updates of services in %s...\n", composeFile.YamlFilePath)
for _, service := range composeFile.Services {
if service.Instance == nil {
continue
}
requiresBuild := len(service.BuildInfo) > 0
log.Printf("Processing service %s (requires build: %t)...\n", service.Name, requiresBuild)
if !requiresBuild {
service.Pull()
} else if GlobalSettings.Build {
service.Build()
}
newImage := CreateDockerImageInstance(service.Instance.Image.ID)
if service.Instance.Image.Hash != newImage.Hash {
if requiresBuild {
log.Printf("Built new image for service %s\n", service.Name)
} else {
log.Printf("Pulled new image %s for service %s\n", service.ImageName, service.Name)
}
/*
Not working with Docker Compose V2 yet
if !GlobalSettings.CompleteStop {
log.Printf("Restarting service %s in %s...\n", service.Name, composeFile.YamlFilePath)
service.Restart()
} else {
compositionRestart = true
}
*/
compositionRestart = true
}
}
if compositionRestart {
if GlobalSettings.Dry {
log.Printf("Dry-Mode enabled, not restarting services in %s\n", composeFile.YamlFilePath)
} else {
log.Printf("Restarting services in %s...\n", composeFile.YamlFilePath)
composeFile.Down()
composeFile.Up()
log.Printf("Restarted services in %s\n", composeFile.YamlFilePath)
}
} else {
log.Printf("No need to restart services in %s\n", composeFile.YamlFilePath)
}
u.processComposeFile(composeFile)
}
if GlobalSettings.Cleanup {
u.CleanUp()
}
EventBus.OnPerformUpdatesComplete()
}

func CleanUp() bool {
func (u *Updater) CleanUp() bool {
EventBus.OnImagePruneStart()
err := exec.Command("docker", "image", "prune", "-a", "-f").Run()
if err != nil {
return false
}
return true
}

func createComposeFileContainerMapping() []*ComposeFile {
func (u *Updater) processComposeFile(composeFile *ComposeFile) {
EventBus.OnProcessComposeFileStart(composeFile)
compositionRestart := false
for _, service := range composeFile.Services {
compositionRestart = u.processService(service) || compositionRestart
}
if compositionRestart {
if GlobalSettings.Dry {
EventBus.OnSkipRestartComposeFileDryMode(composeFile)
} else {
EventBus.OnRestartComposeFile(composeFile)
composeFile.Down()
composeFile.Up()
EventBus.OnRestartComposeFileComplete(composeFile)
}
} else {
EventBus.OnSkipRestartComposeFileNoUpdates(composeFile)
}
}

func (u *Updater) processService(service *ComposeService) bool {
EventBus.OnProcessServiceStart(service)
if !service.IsWatched() {
return false
}
if !service.RequiresBuild() {
service.Pull()
} else if GlobalSettings.Build {
service.Build()
}
if service.Instance.Image.ExistsNewerImageHash() {
if service.RequiresBuild() {
EventBus.OnServiceNewImageBuilt(service)
} else {
EventBus.OnServiceNewImagePulled(service)
}
return true
}
return false
}

func (u *Updater) createComposeFileContainerMapping() []*ComposeFile {
containers := GetWatchedRunningContainers()
cache := make(map[string]*ComposeFile)
for _, container := range containers {
Expand Down
2 changes: 1 addition & 1 deletion src/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main

// BuildVersion is the version
const BuildVersion = "2.0.1"
const BuildVersion = "2.0.2"
2 changes: 2 additions & 0 deletions test/c2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ services:
labels:
- "docker-compose-watcher.watch=1"
- "docker-compose-watcher.dir=${PWD}/c2"
test23:
image: nginx:alpine
33 changes: 18 additions & 15 deletions test/run-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ function testShouldFindNoUpdates() {
runComposeUpdateAndLog
checkLogContains "${TESTNAME} / check c1 found" "Checking for updates of services in ${WORKDIR}/c1/compose1.yaml" 1
checkLogContains "${TESTNAME} / check c2 found" "Checking for updates of services in ${WORKDIR}/c2/docker-compose.yml" 1
checkLogContains "${TESTNAME} / check service test11 found" "Processing service test11" 1
checkLogContains "${TESTNAME} / check service test12 found" "Processing service test12" 1
checkLogContains "${TESTNAME} / check service test13 found" "Processing service test13" 1
checkLogContains "${TESTNAME} / check service test21 found" "Processing service test21" 1
checkLogContains "${TESTNAME} / check service test22 found" "Processing service test22" 1
checkLogContains "${TESTNAME} / check service test11 found" "Processing service test11 (requires build: false, watched: true)" 1
checkLogContains "${TESTNAME} / check service test12 found" "Processing service test12 (requires build: true, watched: true)" 1
checkLogContains "${TESTNAME} / check service test13 found" "Processing service test13 (requires build: true, watched: true)" 1
checkLogContains "${TESTNAME} / check service test21 found" "Processing service test21 (requires build: false, watched: true)" 1
checkLogContains "${TESTNAME} / check service test22 found" "Processing service test22 (requires build: false, watched: true)" 1
checkLogContains "${TESTNAME} / check service test23 found" "Processing service test23 (requires build: false, watched: false)" 1
checkLogContains "${TESTNAME} / check no pulls" "Pulled new image" 0
checkLogContains "${TESTNAME} / check no builds" "Built new image" 0
checkLogContains "${TESTNAME} / check no service restarts in c1" "Restarting services in ${WORKDIR}/c1/compose1.yaml" 0
Expand All @@ -73,11 +74,12 @@ function testShouldFindUpdateC1() {
runComposeUpdateAndLog
checkLogContains "${TESTNAME} / check c1 found" "Checking for updates of services in ${WORKDIR}/c1/compose1.yaml" 1
checkLogContains "${TESTNAME} / check c2 found" "Checking for updates of services in ${WORKDIR}/c2/docker-compose.yml" 1
checkLogContains "${TESTNAME} / check service test11 found" "Processing service test11" 1
checkLogContains "${TESTNAME} / check service test12 found" "Processing service test12" 1
checkLogContains "${TESTNAME} / check service test13 found" "Processing service test13" 1
checkLogContains "${TESTNAME} / check service test21 found" "Processing service test21" 1
checkLogContains "${TESTNAME} / check service test22 found" "Processing service test22" 1
checkLogContains "${TESTNAME} / check service test11 found" "Processing service test11 (requires build: false, watched: true)" 1
checkLogContains "${TESTNAME} / check service test12 found" "Processing service test12 (requires build: true, watched: true)" 1
checkLogContains "${TESTNAME} / check service test13 found" "Processing service test13 (requires build: true, watched: true)" 1
checkLogContains "${TESTNAME} / check service test21 found" "Processing service test21 (requires build: false, watched: true)" 1
checkLogContains "${TESTNAME} / check service test22 found" "Processing service test22 (requires build: false, watched: true)" 1
checkLogContains "${TESTNAME} / check service test23 found" "Processing service test23 (requires build: false, watched: false)" 1
checkLogContains "${TESTNAME} / check one pull" "Pulled new image" 1
checkLogContains "${TESTNAME} / check no builds" "Built new image" 0
checkLogContains "${TESTNAME} / check no service restarts in c1" "Restarting services in ${WORKDIR}/c1/compose1.yaml" 1
Expand All @@ -90,11 +92,12 @@ function testShouldFindUpdateC2() {
runComposeUpdateAndLog
checkLogContains "${TESTNAME} / check c1 found" "Checking for updates of services in ${WORKDIR}/c1/compose1.yaml" 1
checkLogContains "${TESTNAME} / check c2 found" "Checking for updates of services in ${WORKDIR}/c2/docker-compose.yml" 1
checkLogContains "${TESTNAME} / check service test11 found" "Processing service test11" 1
checkLogContains "${TESTNAME} / check service test12 found" "Processing service test12" 1
checkLogContains "${TESTNAME} / check service test13 found" "Processing service test13" 1
checkLogContains "${TESTNAME} / check service test21 found" "Processing service test21" 1
checkLogContains "${TESTNAME} / check service test22 found" "Processing service test22" 1
checkLogContains "${TESTNAME} / check service test11 found" "Processing service test11 (requires build: false, watched: true)" 1
checkLogContains "${TESTNAME} / check service test12 found" "Processing service test12 (requires build: true, watched: true)" 1
checkLogContains "${TESTNAME} / check service test13 found" "Processing service test13 (requires build: true, watched: true)" 1
checkLogContains "${TESTNAME} / check service test21 found" "Processing service test21 (requires build: false, watched: true)" 1
checkLogContains "${TESTNAME} / check service test22 found" "Processing service test22 (requires build: false, watched: true)" 1
checkLogContains "${TESTNAME} / check service test23 found" "Processing service test23 (requires build: false, watched: false)" 1
checkLogContains "${TESTNAME} / check one pull" "Pulled new image" 1
checkLogContains "${TESTNAME} / check no builds" "Built new image" 0
checkLogContains "${TESTNAME} / check no service restarts in c1" "Restarting services in ${WORKDIR}/c1/compose1.yaml" 0
Expand Down

0 comments on commit 6bfb8d4

Please sign in to comment.