Skip to content

Commit

Permalink
Make pro the new default
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsauter committed Jul 7, 2020
1 parent 3911c15 commit fa31556
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 64 deletions.
6 changes: 0 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
crane_darwin_amd64
crane_darwin_amd64_pro
crane_linux_amd64
crane_linux_386
crane_windows_amd64.exe
crane_windows_amd64_pro.exe

crane/accelerated_mount_pro.go
crane/mac_sync_pro.go
crane/version_pro.go
6 changes: 0 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,5 @@ build-linux: imports
build-darwin: imports
GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -o crane_darwin_amd64 -v github.com/michaelsauter/crane

build-darwin-pro: imports
GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -tags pro -o crane_darwin_amd64_pro -v github.com/michaelsauter/crane

build-windows: imports
GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -o crane_windows_amd64.exe -v github.com/michaelsauter/crane

build-windows-pro: imports
GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -tags pro -o crane_windows_amd64_pro.exe -v github.com/michaelsauter/crane
1 change: 0 additions & 1 deletion crane.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ commands:
test: run crane make test
build: run crane make build
build-darwin: run crane make build-darwin
build-darwin-pro: run crane make build-darwin-pro
gofmt: run crane gofmt -w crane

accelerated-mounts:
Expand Down
199 changes: 199 additions & 0 deletions crane/accelerated_mount.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
package crane

import (
"crypto/md5"
"fmt"
"os"
"runtime"
"strconv"
"strings"
)

type AcceleratedMount interface {
Run()
Reset()
Logs(follow bool)
VolumeArg() string
Volume() string
}

type acceleratedMount struct {
RawVolume string
RawIgnore string `json:"ignore" yaml:"ignore"`
RawFlags []string `json:"flags" yaml:"flags"`
Uid int `json:"uid" yaml:"uid"`
Gid int `json:"gid" yaml:"gid"`
configPath string
volume string
_digest string
}

func (am *acceleratedMount) Volume() string {
if am.volume == "" {
am.volume = expandEnv(am.RawVolume)
}
return am.volume
}

func (am *acceleratedMount) Run() {
if !am.running() {
am.ensureDataVolume()
am.initialSync()
am.continuousSync()
}
}

func (am *acceleratedMount) VolumeArg() string {
return am.dataVolumeName() + ":" + am.bindMountContainerPart()
}

func (am *acceleratedMount) Reset() {
args := []string{"rm", "-f", am.syncContainerName()}
executeCommand("docker", args, os.Stdout, os.Stderr)
args = []string{"volume", "rm", am.dataVolumeName()}
executeCommand("docker", args, os.Stdout, os.Stderr)
}

func (am *acceleratedMount) Logs(follow bool) {
args := []string{"logs"}
if follow {
args = append(args, "-f")
}
args = append(args, am.syncContainerName())
executeCommand("docker", args, os.Stdout, os.Stderr)
}

func (am *acceleratedMount) running() bool {
return am.syncContainerExists() && inspectBool(am.syncContainerName(), "{{.State.Running}}")
}

func (am *acceleratedMount) ensureDataVolume() {
if !am.dataVolumeExists() {
printInfof("Creating volume %s ...\n", am.dataVolumeName())
args := []string{
"volume",
"create",
"--name", am.dataVolumeName(),
"--label", "com.crane-orchestration.accelerated-mount=" + am.Volume(),
}
executeHiddenCommand("docker", args)
}
}

func (am *acceleratedMount) initialSync() {
dockerArgs := []string{"run", "--rm"}
dockerArgs = append(dockerArgs, "-e", "UNISON_CHOWN=1")
dockerArgs = append(dockerArgs, am.syncContainerArgs()...)
dockerArgs = append(dockerArgs, am.initialFlags()...)
dockerArgs = append(dockerArgs, "/bind-mount", "/data-volume")
printInfof("Doing initial sync for %s ... this might take a while\n", am.bindMountHostPart())
executeHiddenCommand("docker", dockerArgs)
}

func (am *acceleratedMount) continuousSync() {
if am.syncContainerExists() {
dockerArgs := []string{"start", am.syncContainerName()}
printInfof("Starting sync for %s via %s ...\n", am.bindMountHostPart(), am.syncContainerName())
executeCommand("docker", dockerArgs, nil, nil)
} else {
dockerArgs := []string{"run", "--name", am.syncContainerName(), "-d"}
dockerArgs = append(dockerArgs, am.syncContainerArgs()...)
dockerArgs = append(dockerArgs, am.continuousFlags()...)
dockerArgs = append(dockerArgs, "/bind-mount", "/data-volume")
printInfof("Starting sync for %s via %s ...\n", am.bindMountHostPart(), am.syncContainerName())
executeCommand("docker", dockerArgs, nil, nil)
}
}

func (am *acceleratedMount) dataVolumeExists() bool {
args := []string{"volume", "inspect", am.dataVolumeName()}
_, err := commandOutput("docker", args)
return err == nil
}

// If flags is given in the config, its value is used.
// Otherwise, we check if ingore is configured, and use
// its value for the -ignore flag. Otherwise the default
// flags are sent to Unison.
func (am *acceleratedMount) flags() []string {
if len(am.RawFlags) > 0 {
f := []string{}
for _, rawFlag := range am.RawFlags {
f = append(f, expandEnv(rawFlag))
}
return f
}

ignore := "Name {.git}"
if len(am.RawIgnore) > 0 {
ignore = expandEnv(am.RawIgnore)
}
ignoreFlag := fmt.Sprintf("-ignore='%s'", ignore)

return []string{"-auto", "-batch", ignoreFlag, "-contactquietly", "-confirmbigdel=false", "-prefer=newer"}
}

func (am *acceleratedMount) initialFlags() []string {
return append(am.flags(), "-ignorearchives")
}

func (am *acceleratedMount) continuousFlags() []string {
return append(am.flags(), "-repeat=watch")
}

func (am *acceleratedMount) syncContainerExists() bool {
return containerID(am.syncContainerName()) != ""
}

func (am *acceleratedMount) syncContainerArgs() []string {
return []string{
"-v", am.bindMountHostPart() + ":/bind-mount",
"-v", am.dataVolumeName() + ":/data-volume",
"-e", "UNISON_UID=" + strconv.Itoa(am.Uid),
"-e", "UNISON_GID=" + strconv.Itoa(am.Gid),
"--label", "com.crane-orchestration.accelerated-mount=" + am.Volume(),
am.image(),
}
}

func (am *acceleratedMount) dataVolumeName() string {
return "crane_vol_" + am.digest()
}

func (am *acceleratedMount) syncContainerName() string {
return "crane_sync_" + am.digest()
}

func (am *acceleratedMount) digest() string {
if am._digest == "" {
syncIdentifierParts := []string{
am.configPath,
am.Volume(),
am.image(),
strings.Join(am.flags(), " "),
strconv.Itoa(am.Uid),
strconv.Itoa(am.Gid),
}
syncIdentifier := []byte(strings.Join(syncIdentifierParts, ":"))
am._digest = fmt.Sprintf("%x", md5.Sum(syncIdentifier))
}
return am._digest
}

func (am *acceleratedMount) image() string {
return "michaelsauter/crane-sync:3.2.0"
}

func (am *acceleratedMount) bindMountHostPart() string {
parts := strings.Split(actualVolumeArg(am.Volume()), ":")
return parts[0]
}

func (am *acceleratedMount) bindMountContainerPart() string {
parts := strings.Split(actualVolumeArg(am.Volume()), ":")
return parts[1]
}

func accelerationEnabled() bool {
return runtime.GOOS == "darwin" || runtime.GOOS == "windows"
}
44 changes: 0 additions & 44 deletions crane/accelerated_mount_basic.go

This file was deleted.

2 changes: 0 additions & 2 deletions crane/version_basic.go → crane/version.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build !pro

package crane

import (
Expand Down
9 changes: 4 additions & 5 deletions release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ echo "Running tests..."
crane cmd test

echo "Update version..."
old_version=$(grep -o "[0-9]*\.[0-9]*\.[0-9]*" crane/version_basic.go)
sed -i.bak 's/Version = "'$old_version'"/Version = "'$version'"/' crane/version_basic.go crane/version_pro.go
rm crane/version_basic.go.bak
rm crane/version_pro.go.bak
old_version=$(grep -o "[0-9]*\.[0-9]*\.[0-9]*" crane/version.go)
sed -i.bak 's/Version = "'$old_version'"/Version = "'$version'"/' crane/version.go
rm crane/version.go.bak
sed -i.bak 's/VERSION="'$old_version'"/VERSION="'$version'"/' download.sh
rm download.sh.bak
sed -i.bak 's/'$old_version'/'$version'/' README.md
Expand All @@ -36,7 +35,7 @@ echo "Build binaries..."
crane cmd build

echo "Update repository..."
git add crane/version_basic.go download.sh README.md CHANGELOG.md CONTRIBUTORS
git add crane/version.go download.sh README.md CHANGELOG.md CONTRIBUTORS
git commit -m "Bump version to ${version}"
git tag --sign --message="v$version" --force "v$version"
git tag --sign --message="latest" --force latest
Expand Down

0 comments on commit fa31556

Please sign in to comment.