Skip to content

Commit

Permalink
Adapt for go mod and vendoring:
Browse files Browse the repository at this point in the history
- add Makefile for easy build
- add future build build bin in container
- add future bulld packages in container
- adapt for go mod
- vendored some library
  • Loading branch information
kt315 committed Jan 27, 2024
1 parent 7cb9005 commit 7dda37d
Show file tree
Hide file tree
Showing 32 changed files with 520 additions and 2,776 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
pkg
pkgs_out
bin
.prepared_cmd
16 changes: 3 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
FROM gliderlabs/alpine:3.2

MAINTAINER Ryan Eschinger <ryanesc@gmail.com>

COPY . /go/src/github.com/outbrain/zookeepercli/

RUN apk add --update go git \
&& cd /go/src/github.com/outbrain/zookeepercli/ \
&& export GOPATH=/go \
&& go get \
&& go build -o /bin/zookeepercli \
&& rm -rf /go \
&& apk del --purge go git
# syntax=docker/dockerfile:experimental

FROM scratch
COPY bin/zookeepercli /bin/zookeepercli
ENTRYPOINT ["/bin/zookeepercli"]
54 changes: 54 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
.PHONY: clean

BINARY_NAME ?= zookeepercli
VERSION ?= $(shell git describe --long --tags --always --dirty --abbrev=10)
GOLANG_VERSION ?= 1.21-alpine

CURRENT_DIR := $(shell pwd)

DOCKER ?= docker

.prepare_cmd:
printf "go build -o ./bin/%s -ldflags \"-X 'main.Version=%s'\"\n" "$(BINARY_NAME)" "$(VERSION)" > .prepared_cmd

# build bin in docker, package in docker and build the docker image
all_in_docker: package_in_docker build_docker_image
@echo "All done"

# build bin and package locally
all: package
@echo "All done"

build: .prepare_cmd
sh .prepared_cmd

package: build
@ /bin/sh ./scripts/build_package.sh
@ echo "=== Packaged files: ==="
@ echo "==== in ./pkgs_out ===="
@ ls -1 ./pkgs_out
@ echo "======================="

build_in_docker: .prepare_cmd
${DOCKER} run --rm -t -v "$(CURRENT_DIR):/app" -w /app \
-e CGO_ENABLED=0 \
golang:$(GOLANG_VERSION) /bin/sh .prepared_cmd

package_in_docker: build_in_docker
${DOCKER} run --rm -t -v "$(CURRENT_DIR):/app" -w /app \
-e CGO_ENABLED=0 \
golang:$(GOLANG_VERSION) \
/bin/sh -c \
"/bin/sh ./scripts/prepare_container.sh && /bin/sh ./scripts/build_package.sh"
@ echo "=== Packaged files: ==="
@ echo "==== in ./pkgs_out ===="
@ ls -1 ./pkgs_out
@ echo "======================="

build_docker_image: build_in_docker
${DOCKER} build -t zookeepercli:$(VERSION) -t zookeepercli:latest .

clean:
rm -rf ./bin
rm -rf ./pkgs_out
rm -rf .prepared_cmd
114 changes: 63 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# zookeepercli

[![downloads](https://img.shields.io/github/downloads/outbrain/zookeepercli/total.svg)](https://github.com/outbrain/zookeepercli/releases)
[![downloads](https://img.shields.io/github/downloads/outbrain/zookeepercli/total.svg)](https://github.com/outbrain/zookeepercli/releases)

Simple, lightweight, dependable CLI for ZooKeeper

Expand All @@ -9,109 +9,126 @@ Simple, lightweight, dependable CLI for ZooKeeper
* Basic CRUD-like operations: `create`, `set`, `delete` (aka `rm`), `exists`, `get`, `ls` (aka `children`).
* Extended operations: `lsr` (ls recursive), `creater` (create recursively), `deleter` (aka `rmr`, delete recursively)
* Well formatted and controlled output: supporting either `txt` or `json` format
* Single, no-dependencies binary file, based on a native Go ZooKeeper library
* Single, no-dependencies binary file, based on a native Go ZooKeeper library
by [github.com/samuel/go-zookeeper](http://github.com/samuel/go-zookeeper) ([LICENSE](https://github.com/outbrain/zookeepercli/blob/master/go-zookeeper-LICENSE))

### Download & Install
### Build:
Clone the repository:
```
git clone --depth 1 https://github.com/openark/zookeepercli.git && zookeepercli
```

There are [pre built binaries](https://github.com/outbrain/zookeepercli/releases) for download.
You can find `RPM` and `deb` packages, as well as pre-compiled, dependency free `zookeepercli` executable binary.
In fact, the only file installed by the pre-built `RPM` and `deb` packages is said executable binary file.
Build :
```
make build # build binary with local goland
make package # previous + build deb, rpm, apk, tar, zip packages
make all # all previous steps
Otherwise the source code is freely available; you will need `git` installed as well as `go`, and you're on your own.
make build_in_docker # build binary with using docker container (golang-alpine)
make package_in_docker # previous + build deb, rpm, apk, tar, zip packages with using docker container
make build_docker_image # build docker image with zookeepercli
make all_in_docker # all previous steps

### Usage:
make clean # clean all build artifacts
```

For build packages locally you need requires fpm: https://github.com/jordansissel/fpm.

You can find the binary in the `bin` directory. \
Also you can find `deb`, `rpm`, `apk`, `tar` and `zip` packages in pkgs_out.

### Usage:
```
$ zookeepercli --help
Usage of zookeepercli:
-acls="31": optional, csv list [1|,2|,4|,8|,16|,31]
-auth_pwd="": optional, digest scheme, pwd
-auth_usr="": optional, digest scheme, user
-c="": command (exists|get|ls|lsr|create|creater|set|delete|rm|deleter|rmr|getacl|setacl)
-debug=false: debug mode (very verbose)
-force=false: force operation
-format="txt": output format (txt|json)
-servers="": srv1[:port1][,srv2[:port2]...]
-stack=false: add stack trace upon error
-acls="31": optional, csv list [1|,2|,4|,8|,16|,31]
-auth_pwd="": optional, digest scheme, pwd
-auth_usr="": optional, digest scheme, user
-c="": command (exists|get|ls|lsr|create|creater|set|delete|rm|deleter|rmr|getacl|setacl)
-debug=false: debug mode (very verbose)
-force=false: force operation
-format="txt": output format (txt|json)
-servers="": srv1[:port1][,srv2[:port2]...]
-stack=false: add stack trace upon error
-verbose=false: verbose

-version: print version and exit
```

### Examples:


$ zookeepercli --servers srv-1,srv-2,srv-3 -c create /demo_only some_value

# Default port is 2181. The above is equivalent to:
$ zookeepercli --servers srv-1:2181,srv-2:2181,srv-3:2181 -c create /demo_only some_value

$ zookeepercli --servers srv-1,srv-2,srv-3 --format=txt -c get /demo_only
some_value

# Same as above, JSON format output:
$ zookeepercli --servers srv-1,srv-2,srv-3 --format=json -c get /demo_only
"some_value"
# exists exits with exit code 0 when path exists, 1 when path does not exist

# exists exits with exit code 0 when path exists, 1 when path does not exist
$ zookeepercli --servers srv-1,srv-2,srv-3 -c exists /demo_only
true

$ zookeepercli --servers srv-1,srv-2,srv-3 -c set /demo_only another_value

$ zookeepercli --servers srv-1,srv-2,srv-3 --format=json -c get /demo_only
"another_value"

$ zookeepercli --servers srv-1,srv-2,srv-3 -c delete /demo_only

$ zookeepercli --servers srv-1,srv-2,srv-3 -c get /demo_only
2014-09-15 04:07:16 FATAL zk: node does not exist

$ zookeepercli --servers srv-1,srv-2,srv-3 -c create /demo_only "path placeholder"
$ zookeepercli --servers srv-1,srv-2,srv-3 -c create /demo_only/key1 "value1"
$ zookeepercli --servers srv-1,srv-2,srv-3 -c create /demo_only/key2 "value2"
$ zookeepercli --servers srv-1,srv-2,srv-3 -c create /demo_only/key3 "value3"

$ zookeepercli --servers srv-1,srv-2,srv-3 -c ls /demo_only
key3
key2
key1

# Same as above, JSON format output:
$ zookeepercli --servers srv-1,srv-2,srv-3 --format=json -c ls /demo_only
["key3","key2","key1"]

$ zookeepercli --servers srv-1,srv-2,srv-3 -c delete /demo_only
2014-09-15 08:26:31 FATAL zk: node has children

$ zookeepercli --servers srv-1,srv-2,srv-3 -c delete /demo_only/key1
$ zookeepercli --servers srv-1,srv-2,srv-3 -c delete /demo_only/key2
$ zookeepercli --servers srv-1,srv-2,srv-3 -c delete /demo_only/key3
$ zookeepercli --servers srv-1,srv-2,srv-3 -c delete /demo_only

# /demo_only path now does not exist.

# Create recursively a path:
$ zookeepercli --servers=srv-1,srv-2,srv-3 -c creater "/demo_only/child/key1" "val1"
$ zookeepercli --servers=srv-1,srv-2,srv-3 -c creater "/demo_only/child/key2" "val2"

# "-c creater" is same as "-c create --force"

$ zookeepercli --servers=srv-1,srv-2,srv-3 -c get "/demo_only/child/key1"
val1

# This path was auto generated due to recursive create:
$ zookeepercli --servers=srv-1,srv-2,srv-3 -c get "/demo_only"
$ zookeepercli --servers=srv-1,srv-2,srv-3 -c get "/demo_only"
zookeepercli auto-generated

# ls recursively a path and all sub children:
$ zookeepercli --servers=srv-1,srv-2,srv-3 -c lsr "/demo_only"
$ zookeepercli --servers=srv-1,srv-2,srv-3 -c lsr "/demo_only"
child
child/key1
child/key2

# set value with read and write acl using digest authentication
$ zookeepercli --servers 192.168.59.103 --auth_usr "someuser" --auth_pwd "pass" --acls 1,2 -c create /secret4 value4

# get value using digest authentication
$ zookeepercli --servers 192.168.59.103 --auth_usr "someuser" --auth_pwd "pass" -c get /secret4

Expand All @@ -132,17 +149,17 @@ Otherwise the source code is freely available; you will need `git` installed as
# set an acl with world and digest authentication creating the node if it doesn't exist
$ zookeepercli --servers srv-1,srv-2,srv-3 -force -c setacl /demo_acl_create "world:anyone:rw,digest:someuser:hashedpw:crdwa"

The tool was built in order to allow with shell scripting seamless integration with ZooKeeper.
There is another, official command line tool for ZooKeeper that the author found inadequate
in terms of output format and output control, as well as large footprint.
The tool was built in order to allow with shell scripting seamless integration with ZooKeeper.
There is another, official command line tool for ZooKeeper that the author found inadequate
in terms of output format and output control, as well as large footprint.
**zookeepercli** overcomes those limitations and provides with quick, well formatted output as well as
enhanced functionality.
enhanced functionality.

### Docker

You can also build and run **zookeepercli** in a Docker container. To build the image:

$ docker build -t zookeepercli .
$ make build_docker_image

Now, you can run **zookeepercli** from a container. Examples:

Expand All @@ -158,9 +175,4 @@ Now, you can run **zookeepercli** from a container. Examples:
Release under the [Apache 2.0 license](https://github.com/outbrain/zookeepercli/blob/master/LICENSE)

Authored by [Shlomi Noach](https://github.com/shlomi-noach) at [Outbrain](https://github.com/outbrain)






8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/openark/zookeepercli

go 1.21.5

require (
github.com/outbrain/golib v0.0.0-20200503083229-2531e5dbcc71
github.com/samuel/go-zookeeper v0.0.0-20201211165307-7117e9ea2414
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/outbrain/golib v0.0.0-20200503083229-2531e5dbcc71 h1:5FSwz/q8DhpkUsq8cqRN7gRVWWnfXfjeOeB8Bhj5ARc=
github.com/outbrain/golib v0.0.0-20200503083229-2531e5dbcc71/go.mod h1:JDhu//MMvcPVPH889Xr7DyamEbTLumgDBALGUyXrz1g=
github.com/samuel/go-zookeeper v0.0.0-20201211165307-7117e9ea2414 h1:AJNDS0kP60X8wwWFvbLPwDuojxubj9pbfK7pjHw0vKg=
github.com/samuel/go-zookeeper v0.0.0-20201211165307-7117e9ea2414/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E=
15 changes: 12 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@ package main
import (
"flag"
"fmt"
"github.com/outbrain/golib/log"
"github.com/outbrain/zookeepercli/output"
"github.com/outbrain/zookeepercli/zk"
"io/ioutil"
"math/rand"
"os"
"sort"
"strings"
"time"

"github.com/openark/zookeepercli/output"
"github.com/openark/zookeepercli/zk"
"github.com/outbrain/golib/log"
)

var Version = "undefined-dev-version"

// main is the application's entry point.
func main() {
servers := flag.String("servers", "", "srv1[:port1][,srv2[:port2]...]")
Expand All @@ -43,8 +46,14 @@ func main() {
authUser := flag.String("auth_usr", "", "optional, digest scheme, user")
authPwd := flag.String("auth_pwd", "", "optional, digest scheme, pwd")
acls := flag.String("acls", "31", "optional, csv list [1|,2|,4|,8|,16|,31]")
version := flag.Bool("version", false, "print version and exit")
flag.Parse()

if *version {
fmt.Println("zookeepercli version:", Version)
os.Exit(0)
}

log.SetLevel(log.ERROR)
if *verbose {
log.SetLevel(log.INFO)
Expand Down
File renamed without changes.
36 changes: 36 additions & 0 deletions scripts/build_package.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/sh -e
# Build deb package for zookeepercli
# Sergei Kraev <skraev@tradingview.com>
#

echo "========= Preparing packing start =========";
PKGNAME="zookeepercli"
VER=$(git describe --long --tags --always --abbrev=10 | sed 's/^[^0-9]//ig')
ARCH=$(arch | sed s/aarch64/arm64/ | sed s/x86_64/amd64/)
RDIR="$(pwd)/pkgs_out/"

rm -rf "${RDIR}"
mkdir -p "$RDIR"

OUTTYPES="rpm deb apk tar zip"

for OUTTYPE in $OUTTYPES; do
echo "========= Packing ${OUTTYPE} start =========";
fpm \
--output-type "${OUTTYPE}" \
--input-type dir \
--force \
\
--name "${PKGNAME}" \
--package "${RDIR}" \
--version "${VER}" \
--architecture "${ARCH}" \
--maintainer 'Shlomi Noach <shlomi-noach@github.com>' \
--url 'https://github.com/openark/zookeepercli' \
--description 'Zookeeper client console' \
--license 'Apache 2.0' \
--category 'universe/net' \
--no-depends --no-auto-depends \
--prefix /usr/local/bin \
--chdir "./bin" .
done
16 changes: 16 additions & 0 deletions scripts/prepare_container.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh -e
# Build deb package for zookeepercli
# Sergei Kraev <skraev@tradingview.com>
#

echo "========= Preparing packing start =========";
apk update
apk add --no-cache \
git \
ruby \
rpm-dev \
tar \
zip

git config --global --add safe.directory "$(pwd)"
gem install fpm
Loading

0 comments on commit 7dda37d

Please sign in to comment.