Skip to content

Commit

Permalink
Complete re-factor of Peekaboo, added caching, gui written in AngularJS
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Persson committed Mar 28, 2016
1 parent 73ba2f9 commit fdcf8e7
Show file tree
Hide file tree
Showing 204 changed files with 7,608 additions and 26,757 deletions.
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Self containerd Dev. environment
# docker run -it --rm -v $HOME/go:/root/go -v /var/run/docker.sock:/var/run/docker.sock -p 5050:5050 <image id>

FROM centos:centos7

RUN set -ex ;\
yum install -y go vim-enhanced net-tools lsb lvm2 docker ;\
yum clean all

ENV GOPATH=/root/go
ENV PATH=${PATH}:${GOPATH}/bin
ENV PROJECT=${GOPATH}/src/github.com/imc-trading/peekaboo

RUN go get github.com/constabulary/gb/...

EXPOSE 5050

WORKDIR ${PROJECT}
ENTRYPOINT /bin/bash
22 changes: 11 additions & 11 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Peekaboo
Peekaboo

Copyright (c) 2015, IMC Trading B.V.
Copyright (c) 2015, IMC Trading B.V.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ NAME=peekaboo
SRCDIR=src/github.com/imc-trading/${NAME}
BUILDDIR=.build
RESDIR=/var/lib/${NAME}
VERSION:=$(shell awk -F '"' '/Version/ {print $$2}' ${SRCDIR}/version.go)
VERSION:=$(shell awk -F '"' '/Version/ {print $$2}' ${SRCDIR}/version/version.go)
RELEASE:=$(shell date -u +%Y%m%d%H%M)

all: build
Expand Down Expand Up @@ -35,7 +35,7 @@ rpm:

build-rpm: deps build
mkdir -p ${BUILDDIR}/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
cp -r bin files ${SRCDIR}/templates ${SRCDIR}/static ${BUILDDIR}/SOURCES
cp -r bin files static ${BUILDDIR}/SOURCES
sed -e "s/%NAME%/${NAME}/g" -e "s/%VERSION%/${VERSION}/g" -e "s/%RELEASE%/${RELEASE}/g" \
${NAME}.spec >${BUILDDIR}/SPECS/${NAME}.spec
rpmbuild -vv -bb --target="x86_64" --clean --define "_topdir $$(pwd)/${BUILDDIR}" ${BUILDDIR}/SPECS/${NAME}.spec
Expand Down
122 changes: 48 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<img src="img/logo.png" width="50%">
</p>

Expose hardware info using JSON/REST and provide a system HTML Front-End.
Expose hardware info using a REST API written in Go and a Front-End written in AngularJS.

**FrontEnd:**

Expand All @@ -13,53 +13,55 @@ http://myserver.example.com:5050
**JSON endpoints:**

```
/json
/cpu/json
/disks/json
/lvm/json
/lvm/log_vols/json
/lvm/phys_vols/json
/lvm/vol_grps/json
/memory/json
/mounts/json
/network/interfaces/json
/network/json
/network/json
/network/routes/json
/opsys/json
/pci/json
/sysctl/json
/api/v1/network/interfaces
/api/v1/network/routes
/api/v1/system
/api/v1/system/os
/api/v1/system/cpu
/api/v1/system/memory
/api/v1/system/sysctls (Linux only)
/api/v1/storage/disks (Linux only)
/api/v1/storage/mounts (Linux only)
/api/v1/storage/lvm/physvols (Linux only)
/api/v1/storage/lvm/logvols (Linux only)
/api/v1/storage/lvm/volgrps (Linux only)
/api/v1/docker
/api/v1/docker/containers
/api/v1/docker/images
```

**Example:**

```bash
curl http://myserver.example.com:5050/cpu/json
curl http://myserver.example.com:5050/api/v1/system
```

# Usage

```bash
Peekaboo

Usage:
peekaboo [OPTIONS]

Application Options:
-v, --verbose Verbose
--version Version
-b, --bind-addr= Bind to address (0.0.0.0)
-p, --port= Port (5050)
-s, --static-dir= Static content (static)
-t, --template-dir= Templates (templates)
-K, --kafka Enable Kafka message bus
--kafka-topic= Kafka topic (peekaboo)
--kafka-peers= Comma-delimited list of Kafka brokers
--kafka-cert= Certificate file for client authentication
--kafka-key= Key file for client client authentication
--kafka-ca= CA file for TLS client authentication
--kafka-verify Verify SSL certificate

Help Options:
-h, --help Show this help message
peekaboo daemon [--debug] [--bind=<addr>] [--static=<dir>]
peekaboo list
peekaboo get <hardware-type>
peekaboo -h | --help
peekaboo --version

Commands:
daemon Start as a daemon serving HTTP requests.
list List hardware names available.
get Return information about hardware.

Arguments:
hardware-type Name of hardware to return information about.

Options:
-h --help Show this screen.
--version Show version.
-d --debug Debug.
-b --bind=<addr> Bind to address and port. [default: 0.0.0.0:5050]
-s --static=<dir> Directory for static content. [default: static]
```

# Setup Go on Linux
Expand All @@ -72,18 +74,16 @@ export PATH=$GOPATH/bin:$PATH
go get github.com/constabulary/gb/...
```

## Build
## Build and run

```bash
gb build
sudo bin/peekaboo \
--static-dir src/github.com/mickep76/peekaboo/static \
--template-dir src/github.com/mickep76/peekaboo/templates
sudo bin/peekaboo daemon -d
```

## Build RPM

Make sure you have Docker configured.
Fiest make sure you have Docker configured.

```bash
make rpm
Expand All @@ -96,12 +96,12 @@ systemctl stop peekaboo
vi /etc/sysconfig/peekaboo
```

Add "--bind-addr" bind address, defaults to "0.0.0.0". For port add "--port", defaults to 5050.
Change port to 8080.

**Example:**

```
OPTIONS="--bind-addr 192.168.1.153 --port 8080"
OPTIONS="--bind 0.0.0.0:8080"
```

Reload SystemD and then restart Peekaboo.
Expand All @@ -110,26 +110,7 @@ Reload SystemD and then restart Peekaboo.
systemctl start peekaboo
```

# Setup Go on Mac OS X

```bash
brew install go
mkdir ~/go
export GOPATH=~/go
export PATH=$GOPATH/bin:$PATH
go get github.com/constabulary/gb/...
```

## Build

```bash
gb build
sudo bin/peekaboo \
--static-dir src/github.com/mickep76/peekaboo/static \
--template-dir src/github.com/mickep76/peekaboo/templates
```

## Install using Brew
## Install using Brew on Mac OS X

```bash
brew tap mickep76/funk-gnarge
Expand All @@ -145,21 +126,14 @@ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.peekaboo.plist
vi ~/Library/LaunchAgents/homebrew.mxcl.peekaboo.plist
```

Add "--bind-addr" to change bind address, defaults to "0.0.0.0". To change port add "--port", defaults to 5050.
Change port to 8080.

**Example:**

```
...
<string>/usr/local/Cellar/peekaboo/0.2.1/bin/peekaboo</string>
<string>--static-dir</string>
<string>/usr/local/Cellar/peekaboo/0.2.1/peekaboo/static</string>
<string>--template-dir</string>
<string>/usr/local/Cellar/peekaboo/0.2.1/peekaboo/templates</string>
<string>--bind-addr</string>
<string>192.168.1.153</string>
<string>--port</string>
<string>8080</string>
<string>--bind</string>
<string>0.0.0.0:8080</string>
..
```

Expand Down
Empty file modified build
100755 → 100644
Empty file.
2 changes: 1 addition & 1 deletion files/peekaboo.service
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Type=simple
EnvironmentFile=/etc/sysconfig/peekaboo
User=root
Group=root
ExecStart=/usr/bin/peekaboo -s /var/lib/peekaboo/static -t /var/lib/peekaboo/templates $OPTIONS
ExecStart=/usr/bin/peekaboo -s /var/lib/peekaboo/static $OPTIONS

[Install]
WantedBy=multi-user.target
1 change: 0 additions & 1 deletion peekaboo.spec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ AutoReqProv: no
mkdir -p %{buildroot}/usr/bin
cp %{sources}/bin/* %{buildroot}/usr/bin
mkdir -p %{buildroot}/var/lib/%{name}
cp -r %{sources}/templates %{buildroot}/var/lib/%{name}
cp -r %{sources}/static %{buildroot}/var/lib/%{name}
mkdir -p %{buildroot}/etc/systemd/system
cp %{sources}/files/%{name}.service %{buildroot}/etc/systemd/system/%{name}.service
Expand Down
85 changes: 85 additions & 0 deletions src/github.com/imc-trading/peekaboo/daemon/daemon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package daemon

import (
"net/http"
"strings"
"time"

"github.com/gorilla/mux"

"github.com/imc-trading/peekaboo/log"
)

var apiURL = "/api/v1"

type Daemon interface {
Run(string, string) error
}

type daemon struct {
data map[string]interface{}
cache map[string]cache
router *mux.Router
}

type cache struct {
LastUpdated time.Time `json:"lastUpdated"`
Timeout int `json:"timeoutSec"`
FromCache bool `json:"fromCache"`
}

func (d *daemon) addStaticRoute(endpoint, path string) {
endpoint = strings.TrimRight(endpoint, "/") + "/"
log.Infof("Add static endpoint: %s path: %s", endpoint, path)
d.router.PathPrefix(endpoint).Handler(http.StripPrefix(endpoint, http.FileServer(http.Dir(path))))
http.Handle(endpoint, d.router)
}

func (d *daemon) funcAPIRoute(endpoint string, getHwType func() (interface{}, error)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
// Initialize or refresh cache when it has expired.
expire := d.cache[endpoint].LastUpdated.Add(time.Duration(d.cache[endpoint].Timeout) * time.Second)
if strings.ToLower(r.URL.Query().Get("refresh")) == "true" || d.cache[endpoint].LastUpdated.IsZero() || expire.Before(time.Now()) {
var err error
d.data[endpoint], err = getHwType()
if err != nil {
writeJSONError(w, r, d.data[endpoint], err.Error(), http.StatusInternalServerError)
return
}
d.cache[endpoint] = cache{
Timeout: d.cache[endpoint].Timeout,
LastUpdated: time.Now(),
FromCache: false,
}

writeJSON(w, r, d.data[endpoint], d.cache[endpoint])
return
}

d.cache[endpoint] = cache{
Timeout: d.cache[endpoint].Timeout,
LastUpdated: d.cache[endpoint].LastUpdated,
FromCache: true,
}

writeJSON(w, r, d.data[endpoint], d.cache[endpoint])
return
}
}

func (d *daemon) addAPIRoute(endpoint string, getHwType func() (interface{}, error)) {
log.Infof("Add API endpoint: %s method: GET", endpoint)
d.router.HandleFunc(endpoint, d.funcAPIRoute(endpoint, getHwType)).Methods("GET")
}

/*
func (d *daemon) addAPIPurgeRoute(endpoint string, getHwType func() (interface{}, error)) {
log.Infof("Add API purge cache endpoint: %s method: PURGE", endpoint)
d.router.HandleFunc(endpoint, d.funcAPIRoute(endpoint).Methods("PURGE")
}
func apiPurgeCache(forceUpdate func() error) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
}
}
*/
Loading

0 comments on commit fdcf8e7

Please sign in to comment.