Skip to content
This repository was archived by the owner on Sep 2, 2021. It is now read-only.

Commit a732cbf

Browse files
authored
Merge pull request #1 from emcniece/feat/dockerize
Feat/dockerize
2 parents 17fac57 + ba1cdd5 commit a732cbf

File tree

14 files changed

+474
-159
lines changed

14 files changed

+474
-159
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,9 @@ bin
33
pkg
44
out
55
.idea
6-
gsyncd.iml
6+
gsyncd.iml
7+
build
8+
.env
9+
/Godeps
10+
/vendor
11+
/demo

Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM busybox:ubuntu-14.04
2+
MAINTAINER CausticLab
3+
4+
ENV FILESYNC_RELEASE v0.0.1
5+
6+
ADD https://github.com/CausticLab/filesync/releases/download/${RGON_EXEC_RELEASE}/filesync-linux-amd64.tar.gz /tmp/filesync.tar.gz
7+
RUN tar -zxvf /tmp/filesync -C /usr/local/bin \
8+
&& mv /usr/local/bin/filesync-linux-amd64 /usr/local/bin/filesync \
9+
&& chmod +x /usr/local/bin/filesync \
10+
&& rm /tmp/filesync
11+
RUN mkdir /share
12+
13+
ENTRYPOINT ["/usr/local/bin/filesync"]

Dockerfile.dev

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM busybox:ubuntu-14.04
2+
MAINTAINER CausticLab
3+
4+
ADD build/filesync-linux-amd64 /usr/local/bin/filesync
5+
RUN chmod +x /usr/local/bin/filesync
6+
RUN mkdir /share
7+
8+
ENTRYPOINT ["/usr/local/bin/filesync"]

Makefile

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# These env vars have to be set in the CI
2+
# GITHUB_TOKEN
3+
# DOCKER_HUB_TOKEN
4+
5+
.PHONY: build deps test release clean push image ci-compile build-dir ci-dist dist-dir ci-release version help
6+
7+
PROJECT := filesync
8+
PLATFORM := linux
9+
ARCH := amd64
10+
DOCKER_IMAGE := causticlab/$(PROJECT)
11+
12+
VERSION := $(shell cat VERSION)
13+
GITSHA := $(shell git rev-parse --short HEAD)
14+
15+
all: help
16+
17+
help:
18+
@echo "make build - build binary for the target environment"
19+
@echo "make deps - install build dependencies"
20+
@echo "make vet - run vet & gofmt checks"
21+
@echo "make test - run tests"
22+
@echo "make clean - Duh!"
23+
@echo "make release - tag with version and trigger CI release build"
24+
@echo "make image - build release image"
25+
@echo "make dev-image - build development image"
26+
@echo "make dockerhub - build and push image to Docker Hub"
27+
@echo "make version - show app version"
28+
29+
build: build-dir
30+
CGO_ENABLED=1 GOOS=$(PLATFORM) GOARCH=$(ARCH) godep go build -ldflags "-X main.Version=$(VERSION) -X main.GitSHA=$(GITSHA)" -o build/$(PROJECT)-$(PLATFORM)-$(ARCH) -v
31+
32+
deps:
33+
go get golang.org/x/sys/unix
34+
go get github.com/tools/godep
35+
go get github.com/mattn/go-sqlite3
36+
go get github.com/bitly/go-simplejson
37+
go get github.com/howeyc/fsnotify
38+
go get github.com/codegangsta/martini
39+
go get github.com/codegangsta/martini-contrib/encoder
40+
godep save
41+
42+
release:
43+
git tag `cat VERSION`
44+
git push origin master --tags
45+
46+
clean:
47+
go clean
48+
rm -fr ./build
49+
rm -fr ./dist
50+
51+
dockerhub: image
52+
@echo "Pushing $(DOCKER_IMAGE):$(VERSION)"
53+
docker push $(DOCKER_IMAGE):$(VERSION)
54+
55+
image:
56+
docker build -t $(DOCKER_IMAGE):$(VERSION) -f Dockerfile .
57+
58+
dev-image:
59+
docker build -t $(DOCKER_IMAGE):dev -f Dockerfile.dev .
60+
61+
version:
62+
@echo $(VERSION) $(GITSHA)
63+
64+
ci-compile: build-dir
65+
CGO_ENABLED=1 GOOS=$(PLATFORM) GOARCH=$(ARCH) go build -ldflags "-X main.Version=$(VERSION) -X main.GitSHA=$(GITSHA) -w -s" -a -o build/$(PROJECT)-$(PLATFORM)-$(ARCH)/$(PROJECT)
66+
67+
build-dir:
68+
@rm -rf build && mkdir build
69+
70+
dist-dir:
71+
@rm -rf dist && mkdir dist
72+
73+
ci-dist: ci-compile dist-dir
74+
$(eval FILES := $(shell ls build))
75+
@for f in $(FILES); do \
76+
(cd $(shell pwd)/build/$$f && tar -cvzf ../../dist/$$f.tar.gz *); \
77+
(cd $(shell pwd)/dist && shasum -a 256 $$f.tar.gz > $$f.sha256); \
78+
(cd $(shell pwd)/dist && md5sum $$f.tar.gz > $$f.md5); \
79+
echo $$f; \
80+
done
81+
@cp -r $(shell pwd)/dist/* $(CIRCLE_ARTIFACTS)
82+
ls $(CIRCLE_ARTIFACTS)
83+
84+
ci-release:
85+
@previous_tag=$$(git describe --abbrev=0 --tags $(VERSION)^); \
86+
comparison="$$previous_tag..HEAD"; \
87+
if [ -z "$$previous_tag" ]; then comparison=""; fi; \
88+
changelog=$$(git log $$comparison --oneline --no-merges --reverse); \
89+
github-release $(CIRCLE_PROJECT_USERNAME)/$(CIRCLE_PROJECT_REPONAME) $(VERSION) master "**Changelog**<br/>$$changelog" 'dist/*'

README.md

Lines changed: 112 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,115 @@
1-
Filesync
2-
===
1+
# Filesync
2+
33
Filesync is a utility written in Golang which helps you to keep the files on the client up to date with the files on the server. Only the changed parts of files on the server are downloaded. Therefore it's great to synchronize your huge, and frequently changing files.
44

5-
Installation
6-
===
7-
`go get github.com/elgs/filesync/gsync`
8-
9-
Server
10-
===
11-
Run
12-
---
13-
`gsync server.json`
14-
Configuration
15-
---
16-
server.json
17-
```json
18-
{
19-
"mode": "server",
20-
"ip": "0.0.0.0",
21-
"port": 6776,
22-
"monitors": {
23-
"home_elgs_desktop_a": "/home/elgs/Desktop/a",
24-
"home_elgs_desktop_b": "/home/elgs/Desktop/b"
25-
}
26-
}
27-
```
28-
29-
30-
Client
31-
===
32-
Run
33-
---
34-
`gsync client.json`
35-
Configuration
36-
---
37-
client.json
38-
```json
39-
{
40-
"mode": "client",
41-
"ip": "127.0.0.1",
42-
"port": 6776,
43-
"monitors": {
44-
"home_elgs_desktop_a": "/home/elgs/Desktop/c",
45-
"home_elgs_desktop_b": "/home/elgs/Desktop/d"
46-
}
47-
}
5+
Forked from github.com/elgs/filesync/gsync
6+
7+
## Requirements
8+
9+
Needs access to `glibc` to compile properly, and so `busybox:ubuntu-14.04` is the selected base image.
10+
11+
## Local Usage
12+
13+
Install dependencies:
14+
15+
```sh
16+
make deps
17+
```
18+
19+
Run locally with config files (modify paths before running):
20+
21+
```sh
22+
# Server
23+
go run gsync.go gsync/server.json
24+
25+
# Client
26+
go run gsync.go gsync/client.json
27+
```
28+
29+
Alternatively, set environment variables (examples in [/.env](/.env)):
30+
31+
```sh
32+
# Server
33+
export FILESYNC_MODE=server
34+
export FILESYNC_PORT=6776
35+
export FILESYNC_IP=0.0.0.0
36+
export FILESYNC_PATH=/tmp/share
37+
38+
go run gsync.go
39+
```
40+
41+
Build package (requires `glibc`, won't work on OSX):
42+
43+
```sh
44+
make build
45+
```
46+
47+
Build Docker image:
48+
49+
```sh
50+
make dev-image
51+
make image
4852
```
53+
54+
## Demo
55+
56+
The [docker-compose.yml](/docker-compose.yml) details a setup with a server and 2 clients. It will create a `./demo/` directory with 3 subfolders: `server`, `client1`, and `client2`. Each subfolder is volumed as `/share/` inside each container.
57+
58+
As files in `./demo/server` are modified, they will be altered in `./demo/client1` and `./demo/client2`.
59+
60+
Run Docker-Compose cluster:
61+
62+
```sh
63+
docker-compose up -d
64+
```
65+
66+
Check directories:
67+
68+
```sh
69+
ls -al ./demo/server
70+
ls -al ./demo/client1
71+
ls -al ./demo/client2
72+
```
73+
74+
Add a file to server volume:
75+
76+
```sh
77+
echo "testing 123" > ./demo/server/test1
78+
```
79+
80+
Check directories again:
81+
82+
```sh
83+
ls -al ./demo/server
84+
ls -al ./demo/client1
85+
ls -al ./demo/client2
86+
```
87+
88+
At this point, the `./demo/client*` directories should contain a `test1` file.
89+
90+
## Notes
91+
92+
### Docker Environment
93+
94+
While the server configuration can be set to an IP of `0.0.0.0` (accepts traffic from anywhere), the clients need a specific address to connect to. If running locally, the clients can be set to connect to `127.0.0.1` - but this will not work in a Dockerized environment.
95+
96+
The Docker-Compose.yml file contains `links: [fs-server:fs-server]` which enables the clients to contact the server container at `http://fs-server`. This is supported in a Rancher environment as well.
97+
98+
### Rancher Environment
99+
100+
Filesync can be used as a way to share files between hosts. Container deployment can be controlled by assigning labels and using the Rancher scheduling system.
101+
102+
By labelling the primary host with `filesync=server`, these labels can be used to add a Filesync server and multiple clients:
103+
104+
```yml
105+
# Server
106+
labels:
107+
io.rancher.scheduler.affinity:host_label: filesync=server
108+
109+
# Client
110+
labels:
111+
io.rancher.scheduler.global: 'true'
112+
io.rancher.scheduler.affinity:host_label_ne: filesync=server
113+
```
114+
115+
The host labelled with `filesync=server` will receive a Filesync server container, and all other hosts (`io.rancher.scheduler.global: 'true'`) not labelled with this (`host_label_ne: filesync=server`) will receive a client container.

VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v0.0.1

api/api.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package api
33
import (
44
"database/sql"
55
"fmt"
6+
"log"
67
"github.com/codegangsta/martini"
78
"github.com/codegangsta/martini-contrib/encoder"
8-
"github.com/elgs/filesync/index"
9+
"filesync/index"
910
"io"
1011
"net/http"
1112
"os"
@@ -37,7 +38,7 @@ func RunWeb(ip string, port int, monitors map[string]interface{}) {
3738
route.Get("/dirs", func(enc encoder.Encoder, req *http.Request) (int, []byte) {
3839
defer func() {
3940
if err := recover(); err != nil {
40-
fmt.Println(err)
41+
log.Println(err)
4142
}
4243
}()
4344
monitored := req.Header.Get("MONITORED")
@@ -114,5 +115,5 @@ func RunWeb(ip string, port int, monitors map[string]interface{}) {
114115
})
115116

116117
m.Action(route.Handle)
117-
fmt.Println(http.ListenAndServe(fmt.Sprint(ip, ":", port), m))
118+
log.Println(http.ListenAndServe(fmt.Sprint(ip, ":", port), m))
118119
}

0 commit comments

Comments
 (0)