Skip to content

Commit

Permalink
Merged release/0.7 into master
Browse files Browse the repository at this point in the history
  • Loading branch information
aacebedo committed Feb 10, 2016
2 parents 4faebbf + d9eb67a commit a637efe
Show file tree
Hide file tree
Showing 17 changed files with 347 additions and 355 deletions.
29 changes: 8 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Logs can be outputed in stdout or syslog. Check the help for more information ab
#### Instructions
```sh
$ docker build -t cfdnsupdaterbuild ./environments/build
$ docker run -t -v <output_path_on_host>:/out cfdnsupdaterbuild <ARCH> [-b <BRANCH or TAG>]
$ docker run -t -v <output_path_on_host>:/out cfdnsupdaterbuild -a <ARCH> [-b <BRANCH or TAG>]
```

### Installation
Expand All @@ -26,31 +26,18 @@ $ tar xvzf ./cfdnsupdater.<ARCH>.<VERSION>.tar.gz
Note: I also provide a systemd file you can manually install wherever in the appropriate directory ("/etc/systemd/system" for instance).

### Use
There are two ways to use this script:
##### Inline config use
Starting with a configuration passeed in the command line:
```sh
$ cfdnsupdater inlineconfig -e <cloudflare_account_email> -a <cloudflare_apikey> -t <record_types> <domain>
```
##### File config use
Starting with a file containing the configuration:
##### Inline config use##### File config use
```sh
$ cfdnsupdater fileconfig -c <path_to_configuration_file>
```
Here is an example of a configuration file (also included in the distribution):
```sh
{
"email":"[email protected]",
"apikey":"get_your_api_key_from_your_cloud_flare_a_account",
"period":60,
"instances" : [
{
"domain":"foo.com",
"types":["A"],
"names":["bar"]
}
]
}
foo.com:
email: [email protected]
apikey: get_your_api_key_from_your_cloud_flare_a_account
period: 60
record_types: []
record_names: []
```
##### More Help
```sh
Expand Down
7 changes: 2 additions & 5 deletions environments/build/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from alpine
RUN apk add --update go git python3 bash

ENV GOPATH /go:/out/cfdnsupdater
RUN go get gopkg.in/alecthomas/kingpin.v2
RUN go get github.com/op/go-logging
RUN go get github.com/levigross/grequests
ENV GOPATH /go
ENV CGO_ENABLED=0

ADD ./build.py /usr/bin
Expand All @@ -13,7 +10,7 @@ RUN chmod u+x /usr/bin/build.py
WORKDIR /out
VOLUME ["/out"]

ENTRYPOINT ["build.py"]
ENTRYPOINT ["build.py", "-p", "aacebedo/cfdnsupdater", "-n", "cfdnsupdater"]



73 changes: 49 additions & 24 deletions environments/build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,58 @@
import subprocess
import sys
import os
import tarfile
from subprocess import Popen, PIPE

parser = argparse.ArgumentParser(prog="build",
description='Builder.')
def parseArguments(raw_args):
parser = argparse.ArgumentParser(prog="build",
description='Project Builder')
parser.add_argument('-project','-p',required=True,help="Github project", type=str)
parser.add_argument('-binname','-n',required=True,help="Binary name project", type=str)

parser.add_argument('-arch','-a',required=True, help='Architecture to build', type=str)

parser.add_argument('--branch', '-b', help='Git branch to build', default="master", type=str)

return parser.parse_args(raw_args)

parser.add_argument('arch', help='Architecture to build', type=str)

parser.add_argument('--branch', '-b', help='Git branch to build', type=str)

def build(project, branch, arch, bin_name):
if not os.path.exists(os.path.join("/", "out", "project")) :
os.makedirs(os.path.join("/", "out","project"), exist_ok=True)
process = None
process = subprocess.Popen(["git", "clone", "-b", branch,
"https://github.com/{}".format(project),
os.path.join("/","out","project")],
shell=False, stdout=PIPE)
stdout,err = process.communicate()
if err != None:
os.exit("Error while cloning project: {}".format(err))

args = parser.parse_args(sys.argv[1:])

if not os.path.exists(os.path.join("/", "out","cfdnsupdater")) :
os.makedirs(os.path.join("/", "out"), exist_ok=True)
process = None
if args.branch == None:
process = subprocess.Popen(["git", "clone", "https://github.com/aacebedo/cfdnsupdater"],
cwd=os.path.join("/", "out"), shell=False, stdout=PIPE)
else:
process = subprocess.Popen(["git", "clone", "https://github.com/aacebedo/cfdnsupdater",
"-b", args.branch], cwd=os.path.join("/", "out"),
shell=False, stdout=PIPE)

stdout = process.communicate()[0]
print("{}".format(stdout))
print("Project already exists, branch name is ignored")

go_path = "{}:/out/project".format(os.environ["GOPATH"])
process = subprocess.Popen(["go", "get", "-d", "./..."],
cwd=os.path.join("/", "out", "project"), shell=False,
env=dict(os.environ, GOARCH=arch, GOPATH=go_path), stdout=PIPE)
stdout,err = process.communicate()
if err != None:
os.exit("Error while getting dependencies: {}".format(err))

process = subprocess.Popen(["go", "build", '-o', os.path.join('bin', bin_name), bin_name],
cwd=os.path.join("/", "out", "project"), shell=False,
env=dict(os.environ, GOARCH=arch, GOPATH=go_path), stdout=PIPE)
stdout,err = process.communicate()
if err != None:
os.exit("Error while building project: {}".format(err))

with tarfile.open(os.path.join("/", "out","project", "{}.{}.{}.tar.gz".format(bin_name, branch, arch)), "w:gz") as tar:
tar.add(os.path.join("/", "out", "project", "bin", bin_name))


process = subprocess.Popen(["go", "install", "./..."],
cwd=os.path.join("/", "out", "cfdnsupdater"), shell=False,
env=dict(os.environ,GOARCH=args.arch), stdout=PIPE)
stdout = process.communicate()[0]
print("{}".format(stdout))
if __name__ == "__main__":
parsed_args = parseArguments(sys.argv[1:])
build(parsed_args.project,parsed_args.branch,parsed_args.arch,parsed_args.binname)

18 changes: 6 additions & 12 deletions resources/etc/default/cfdnsupdater.conf.sample
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
{
"email":"[email protected]",
"apikey":"get_your_api_key_from_your_cloud_flare_a_account",
"period":60,
"instances" : [
{
"domain":"foo.com",
"types":["A"],
"names":["bar"]
}
]
}
foo.com:
email: [email protected]
apikey: get_your_api_key_from_your_cloud_flare_a_account
period: 60
record_types: ["A"]
record_names: []
53 changes: 53 additions & 0 deletions src/cfdnsupdater/configuration/cmdline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package configuration

import (
"errors"
"fmt"
"gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/yaml.v2"
"io/ioutil"
"math"
)

const (
MIN_UPDATE_PERIOD = 10
MAX_UPDATE_PERIOD = 60 * 60
VERSION = "0.7"
)

type CommandLine struct{}

func (self *CommandLine) ParseParameters(rawParams []string) (res *CFDNSUpdaterConfiguration, err error) {
app := kingpin.New("cfdnsupdater", "Automtatic DNS updater for cloudflare.")
app.Version(VERSION)
app.HelpFlag.Short('h')
fileconfig_path := app.Flag("config", "Configuration file.").Short('c').Required().String()
verbose := app.Flag("verbose", "Verbose mode.").Short('v').Bool()
quiet := app.Flag("quiet", "Quiet mode.").Short('q').Bool()
syslog := app.Flag("syslog", "Output logs to syslog.").Short('s').Bool()

kingpin.MustParse(app.Parse(rawParams))

res = &CFDNSUpdaterConfiguration{}
res.Verbose = *verbose
res.Quiet = *quiet
res.Syslog = *syslog

fileContent, rerr := ioutil.ReadFile(*fileconfig_path)
if rerr != nil {
err = errors.New(fmt.Sprintf("Unable to read configuration file '%s'", *fileconfig_path))
return
}

err = yaml.Unmarshal([]byte(fileContent), &res.DomainConfigs)
if err != nil {
err = errors.New(fmt.Sprintf("Unable to parse configuration file '%s'", *fileconfig_path))
return
}
logger.Debugf("%v",res.DomainConfigs)
for _, domain := range res.DomainConfigs {
domain.Period = int(math.Min(MAX_UPDATE_PERIOD, float64(domain.Period)))
domain.Period = int(math.Max(MIN_UPDATE_PERIOD, float64(domain.Period)))
}
return
}
9 changes: 9 additions & 0 deletions src/cfdnsupdater/configuration/loggers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package configuration

import (
"github.com/op/go-logging"
)

var logger = logging.MustGetLogger("cfdnsupdater.configuration")


21 changes: 21 additions & 0 deletions src/cfdnsupdater/configuration/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package configuration

import (
"cfdnsupdater/core"
)


type DomainConfiguration struct {
Email string `yaml:"email"`
ApiKey string `yaml:"apikey"`
Period int `yaml:"period"`
RecordNames []string `yaml:"record_names"`
RecordTypes core.RecordTypeSlice `yaml:"record_types"`
}

type CFDNSUpdaterConfiguration struct {
Verbose bool
Quiet bool
Syslog bool
DomainConfigs map[string]DomainConfiguration
}
8 changes: 0 additions & 8 deletions src/cfdnsupdater/core/CFDNSUpdaterConfiguration.go

This file was deleted.

85 changes: 0 additions & 85 deletions src/cfdnsupdater/core/CmdLine.go

This file was deleted.

11 changes: 0 additions & 11 deletions src/cfdnsupdater/core/DomainConfiguration.go

This file was deleted.

Loading

0 comments on commit a637efe

Please sign in to comment.