Skip to content

Commit

Permalink
Remove and Edit support for CLI (#2)
Browse files Browse the repository at this point in the history
* Added remove and edit support to cli
* Run test cases in docker
* Fix test case container linkage
* Fix installer
* Updated docs [ci skip]
  • Loading branch information
euforia committed Aug 22, 2016
1 parent 59e88be commit cf93495
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 78 deletions.
29 changes: 29 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
language: go
sudo: required
go:
- 1.6.3
services:
- docker
before_install:
- docker pull progrium/consul
- docker pull golang:1.6.3
install:
- sudo service docker restart ; sleep 10
- docker run -d -p 127.0.0.1:8500:8500 --name consul progrium/consul -server -bootstrap
- true
before_script:
- sleep 10
- make .docker-test
script:
- make .docker-build

deploy:
provider: releases
skip_cleanup: true
api_key:
secure: VKajzP3u6dZh15/OV7tCWmoKsblg0k/B3dy9M3fU1wIZ/A+wttMZtCaqNzI65q94MBpu+9i8Yqb7apQMGz+scYtj5VmJ6FJRiczIDZeZ6pHbVlNudsCkPbjCXhC2f3MWgTCXf4W7Mw1MHket38WKnBTt3AIozeZfIMF5Vks7qUqqPO8U/sSnFn+ByONdFZmhkJzav8R2hfjqXC515tg31pnYpEloUKohvZGen8GG34cRp0VGNRq7P919Kpo5MLSC0HrXrUqtPxpMvdagnGGNQ4P+NdP1+JULzCep80r5TDsTE16AJxUKLarc11mtLsCWtMgD58bcZiwr2zI+9eKdDMrRBRO0wAjl1VwO/8xs2rgByBMNOfkYP6mcO6KgLEN96+UjhjxMTEjaMo/Z7Kze+ikPGq4h02h843BIwp8A4QovVGnV1bwLJoVVhpnJVU0lR365EYQ1o1Cj38CWRCe7gJcy9q6OBfCYK/e5BpUDSZZ+8QVCLFXLG5u4m2ARUI6aj/7xRnmeg/XHI9753Rmv0eoDihI7WJ2hKKXHQyJOthWN4qMLJ/3noeAO7jirYyEdtP3VwgunXCALNLIDu1Mx7Hq4KE+YVmq03nMnB70MOiY079rPJl4v7O+IacA2Xxw4Tjc0/gKQSolVgSaoheY1OfVd1qaEya0IIOz71Rmc99k=
file: build/voletc-*.tgz
file_glob: true
on:
tag: true
repo: ipkg/voletc
74 changes: 42 additions & 32 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ NAMESPACE = ipkg
NAME = voletc
VERSION = $(shell grep "const VERSION" version.go | cut -d "\"" -f 2)

INST_PKG_NAME = $(NAME)-$(VERSION)-$(shell go env GOOS).tgz

PROJPATH = github.com/$(NAMESPACE)/$(NAME)

BRANCH = $(shell git rev-parse --abbrev-ref HEAD)
Expand All @@ -31,51 +29,63 @@ export VOLETC_INSTALL
clean:
rm -rf ./build
rm -f ./coverage.out
rm -rf ./testrun
go clean -i ./...
rm -f $(NAME)-installer

.PHONY: test
test: clean
docker run -d -p 127.0.0.1:8500:8500 --name consul progrium/consul -server -bootstrap
@sleep 3;
go test -coverprofile=coverage.out ./...; docker rm -f consul

# docker run -d -p 127.0.0.1:8500:8500 --name consul progrium/consul -server -bootstrap
.PHONY: test
test:
go test -coverprofile=coverage.out ./...

.PHONY: deps
deps:
go get -d -v ./...

build: clean voletc.conf install.sh
[ -d ./build ] || mkdir ./build
CGO_ENABLED=0 go build -a -tags netgo -installsuffix netgo -ldflags="-X main.branch=${BRANCH} -X main.commit=${COMMIT} -X main.buildtime=${BUILDTIME} -w" .
mv ${NAME} ./build/
.linux-build: voletc.conf
GOOS=linux CGO_ENABLED=0 go build -a -tags netgo -installsuffix netgo -ldflags="-X main.branch=${BRANCH} -X main.commit=${COMMIT} -X main.buildtime=${BUILDTIME} -w" -o ./build/linux/$(NAME) .

.docker-build:
docker run --rm -v $(shell pwd):/go/src/${PROJPATH} -w /go/src/${PROJPATH} golang:1.6.3 make clean deps build
.darwin-build:
if [ -e ./build/darwin ]; then rm -rf ./build/darwin; fi
mkdir -p ./build/darwin

# Assemble image
.docker-image:
docker build --no-cache -t $(NAMESPACE)/$(NAME):$(VERSION) .

# Complete docker build
.PHONY: docker
docker: .docker-build .docker-image
GOOS=darwin CGO_ENABLED=0 go build -a -tags netgo -installsuffix netgo -ldflags="-X main.branch=${BRANCH} -X main.commit=${COMMIT} -X main.buildtime=${BUILDTIME} -w" -o ./build/darwin/$(NAME) .

.PHONY: install.sh
install.sh:
[ -d ./build ] || mkdir ./build
cd ./build && echo $${VOLETC_INSTALL} > install.sh
chmod +x ./build/install.sh
cd ./build/linux && echo $${VOLETC_INSTALL} > install.sh
chmod +x ./build/linux/install.sh
cp ./build/linux/install.sh ./build/darwin/

.PHONY: voletc.conf
voletc.conf:
[ -d ./build ] || mkdir ./build
cd ./build && echo $${VOLETC_STARTUP} > voletc.conf
rm -rf ./build/linux
mkdir -p ./build/linux
cd ./build/linux && echo $${VOLETC_STARTUP} > voletc.conf

# Should be run after make all
.PHONY: installer
installer: build
sea ./build/ $(NAME)-installer voletc ./install.sh
tar -czvf $(INST_PKG_NAME) $(NAME)-installer
mv $(NAME)-installer ./build/
mv $(INST_PKG_NAME) ./build/

installer:
sea ./build/linux/ $(NAME)-installer voletc ./install.sh
mv $(NAME)-installer ./build/linux
cd ./build/linux && tar -czvf $(NAME)-$(VERSION)-linux.tgz $(NAME)-installer && mv $(NAME)-$(VERSION)-linux.tgz ../

sea ./build/darwin/ $(NAME)-installer voletc ./install.sh
mv $(NAME)-installer ./build/darwin
cd ./build/darwin && tar -czvf $(NAME)-$(VERSION)-darwin.tgz $(NAME)-installer && mv $(NAME)-$(VERSION)-darwin.tgz ../

all: .darwin-build .linux-build install.sh

.docker-test:
docker run --link consul:consul --rm -v $(shell pwd):/go/src/${PROJPATH} -w /go/src/${PROJPATH} golang:1.6.3 make clean deps test

.docker-build:
docker run --rm -v $(shell pwd):/go/src/${PROJPATH} -w /go/src/${PROJPATH} golang:1.6.3 make clean deps all

# Assemble image
.docker-image:
docker build --no-cache -t $(NAMESPACE)/$(NAME):$(VERSION) .

# Complete docker build
.PHONY: docker
docker: .docker-build .docker-image
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# voletc
# voletc [![Build Status](https://travis-ci.org/ipkg/voletc.svg?branch=master)](https://travis-ci.org/ipkg/voletc) [![Release](https://img.shields.io/github/release/ipkg/voletc.svg)](https://github.com/ipkg/voletc/releases)

voletc (pronounced vol etc) is a Docker Volume Plugin that allows to create volumes containing application configurations that can be accessed on any of your docker nodes.

Once created the application no longer needs to worry about obtaining application specific configurations. The mounted volume will contain all of config file/s based on the templates and keys you've specified during volume creation.
Expand Down
17 changes: 10 additions & 7 deletions appconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"io/ioutil"
"log"
"strings"
)

Expand Down Expand Up @@ -66,16 +65,21 @@ func (ac *AppConfig) AddTemplate(t *Template) error {
// add template keys
for k, _ := range keys {
if _, ok := ac.Keys[k]; !ok {
ac.Keys[k] = []byte{}
ac.Keys[k] = nil
}
}
}

return nil
}

func (c *AppConfig) HasKeys() bool {
return len(c.Keys) > 0
func (c *AppConfig) HasMappedKeys() bool {
for _, v := range c.Keys {
if v != nil {
return true
}
}
return false
}

func (c *AppConfig) Metadata() map[string]interface{} {
Expand Down Expand Up @@ -115,10 +119,7 @@ func (a *AppConfig) Commit() error {
func (a *AppConfig) Generate(basedir string) error {
err := a.Load()
if err == nil {

keys := a.Keys.ToString()
log.Println("Keys from store:", keys)

for _, t := range a.Templates {
rendered, err := t.Render(keys)
if err == nil {
Expand Down Expand Up @@ -146,6 +147,7 @@ func (a *AppConfig) Set(data map[string][]byte) error {
k := strings.TrimPrefix(key, a.getOpaque(""))

switch {

case strings.HasPrefix(k, "templates"):
if t := NewTemplateFromKey(k); t != nil {
t.SetBody(v)
Expand All @@ -157,6 +159,7 @@ func (a *AppConfig) Set(data map[string][]byte) error {

}
}

return nil
}

Expand Down
93 changes: 76 additions & 17 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
//"io/ioutil"
//"log"
"bufio"
"os"
//"path/filepath"
"strings"
)
Expand All @@ -20,15 +22,9 @@ var (
dataPrefix = flag.String("prefix", driverName, "Path prefix to store data under")
backendUri = flag.String("uri", defaultConsulUri, "Backend uri")
listenAddr = flag.String("b", "127.0.0.1:8989", "Service bind address")
//showVersion = flag.Bool("version", false, "Show version")

// Use for template operations
//tfile = flag.String("t", "", "Template file")
//appName = flag.String("a", "", "Name of app ( <name>-<verison>-<env> )")
//render = flag.Bool("r", false, "Render template")
//commitConf = flag.Bool("commit", false, "Commit app config to backend")

dryrun = false
// Set when cli is parsed
dryrun = false
answerYes = new(bool)
)

type cli struct {
Expand Down Expand Up @@ -74,6 +70,59 @@ func (c *cli) Run(args []string) (bool, error) {
}
}

case "rm":
if len(args) < 2 || args[1] == "" {
err = errInvalidConfName
break
}

var vol *AppConfig
if vol, err = c.ve.Get(args[1]); err == nil {
printDataStructue(vol)
parseCliKeyValues(args[2:])

if !*answerYes {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("Are you sure you want to destroy '%s' [y/n]? : ", vol.QualifiedName())
ans, _ := reader.ReadString('\n')
ans = strings.TrimSuffix(ans, "\n")

if strings.ToLower(ans) != "y" && strings.ToLower(ans) != "yes" {
break
}
}

fmt.Printf("Destroying volume (%s)...\n", vol.QualifiedName())
err = vol.Destroy()

}

case "edit":
if len(args) < 2 || args[1] == "" {
err = errInvalidConfName
break
}

var vol *AppConfig
if vol, err = c.ve.Get(args[1]); err == nil {

if len(args[2:]) < 1 {
err = fmt.Errorf("no data provided")
break
}

ckvs := parseCliKeyValues(args[2:])
var reqOpts map[string][]byte
if reqOpts, err = parseCreateReqOptions(ckvs); err == nil {
vol.Set(reqOpts)
if !dryrun {
err = vol.Commit()
}
printDataStructue(vol)
}

}

case "create":
if len(args) < 2 || args[1] == "" {
err = errInvalidConfName
Expand Down Expand Up @@ -136,9 +185,14 @@ func parseCliKeyValues(arr []string) map[string]string {
for _, s := range arr {
// Treat keys starting with - specially.
if strings.HasPrefix(s, "-") {
if strings.HasSuffix(s, "-dryrun") {
switch {
case strings.HasSuffix(s, "-dryrun"):
dryrun = true

case strings.HasSuffix(s, "-y"):
*answerYes = true
}

continue
}

Expand All @@ -157,8 +211,7 @@ func printDataStructue(v interface{}) {
fmt.Printf("%s\n", b)
}

func printUsage() {
fmt.Println(`
var usageHeader = `
Usage:
voletc [options] <cmd> [name] [key=value] [key=value]
Expand All @@ -169,18 +222,24 @@ Commands:
ls List volumes
create Create new volume
edit Edit volume configurations
info Show volume info
rm Destroy volume i.e. remove all keys
render Show rendered volume templates
version Show version
Options:
`)
flag.PrintDefaults()
fmt.Println(`Rules:
`

var usageFooter = `Rules:
- Volume names: <name>-<version>-<env>
- Template keys: template:<name_of_file>=<content_or_filepath>
- File paths must begin with '/' or './' in order to be recognized.
`)
//os.Exit(0)
`

func printUsage() {
fmt.Println(usageHeader)
flag.PrintDefaults()
fmt.Println(usageFooter)
}
26 changes: 22 additions & 4 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func Test_cli(t *testing.T) {
t.Fatal(err)
}

printUsage()
//printUsage()

if _, err := cl.Run([]string{"create", "test2-0.1.0-dev",
"db/name=dbname", "template:config.json=./testdata/config.json"}); err != nil {
Expand All @@ -23,18 +23,36 @@ func Test_cli(t *testing.T) {
t.Fatal(err)
}
if _, err := cl.Run([]string{"info", "test3-0.1.0-dev"}); err == nil {
t.Fatal("info should fail")
t.Log("should fail")
t.Fail()
}

if _, err := cl.Run([]string{"info", "test2-0.1.0-dev"}); err != nil {
t.Fatal(err)
t.Log(err)
t.Fail()
}

if _, err := cl.Run([]string{"render", "test2-0.1.0-dev"}); err != nil {
t.Fatal(err)
t.Log(err)
t.Fail()
}

if _, err = cl.Run([]string{"ls"}); err != nil {
t.Log(err)
t.Fail()
}

if _, err := cl.Run([]string{"edit", "test2-0.1.0-dev",
"db/username=dbuser,db/password=dbpasswd"}); err != nil {
t.Log(err)
t.Fail()
}
if _, err := cl.Run([]string{"edit"}); err == nil {
t.Log("should fail")
t.Fail()
}

if _, err := cl.Run([]string{"rm", "test2-0.1.0-dev", "-y"}); err != nil {
t.Fatal(err)
}

Expand Down
Loading

0 comments on commit cf93495

Please sign in to comment.