Skip to content

Commit

Permalink
Created ddconfig script to automate installing / configuring datadog (a…
Browse files Browse the repository at this point in the history
…lgorand#238)

* Created algocfg to provide cli access to config.json

Adds `algocfg get|set|reset` commands to manipulate config.json
in a consistent and typesafe way.
Modified config.json is persisted minimally - only non-default
values are written.  Versioning is taken into account and migration
is performed as required.

This also includes a fix to SaveNonDefaultValuesToFile() which was
writing config.json files with a trailing empty line for each default
value that was skipped.

* Noticed typo in old error message

* Started - based on algocfg branch

* Completed first pass for testing

* Debugged and tested on ubuntu vm

* Removed dead script code
  • Loading branch information
David Shoots authored Aug 27, 2019
1 parent f7e8c38 commit b6d6857
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 5 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ build-race: build
GOBIN=$(GOPATH1)/bin-race go install $(GOTRIMPATH) $(GOTAGS) -race -ldflags="$(GOLDFLAGS)" ./...
GOBIN=$(GOPATH1)/bin-race go install $(GOTRIMPATH) $(GOTAGS) -ldflags="$(GOLDFLAGS)" $(SOURCES_RACE)

NONGO_BIN_FILES=$(GOPATH1)/bin/find-nodes.sh $(GOPATH1)/bin/update.sh $(GOPATH1)/bin/COPYING
NONGO_BIN_FILES=$(GOPATH1)/bin/find-nodes.sh $(GOPATH1)/bin/update.sh $(GOPATH1)/bin/COPYING $(GOPATH1)/bin/ddconfig.sh

NONGO_BIN: $(NONGO_BIN_FILES)

Expand All @@ -146,6 +146,8 @@ $(GOPATH1)/bin/update.sh: cmd/updater/update.sh

$(GOPATH1)/bin/COPYING: COPYING

$(GOPATH1)/bin/ddconfig.sh: scripts/ddconfig.sh

$(GOPATH1)/bin/%:
cp -f $< $@

Expand Down
2 changes: 1 addition & 1 deletion cmd/algocfg/setCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (

var (
setParameterArg string
setValueArg string
setValueArg string
)

func init() {
Expand Down
3 changes: 2 additions & 1 deletion installer/rpm/algorand.spec
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ This package provides an implementation of the Algorand protocol.
mkdir -p %{buildroot}/usr/bin
# NOTE: keep in sync with scripts/build_deb.sh bin_files
# NOTE: keep in sync with %files section below
for f in algocfg algod algoh algokey carpenter catchupsrv diagcfg goal kmd msgpacktool node_exporter; do
for f in algocfg algod algoh algokey carpenter catchupsrv ddconfig.sh diagcfg goal kmd msgpacktool node_exporter; do
install -m 755 ${GOPATH}/bin/${f} %{buildroot}/usr/bin/${f}
done

Expand Down Expand Up @@ -74,6 +74,7 @@ fi
/usr/bin/algokey
/usr/bin/carpenter
/usr/bin/catchupsrv
/usr/bin/ddconfig.sh
/usr/bin/diagcfg
/usr/bin/goal
/usr/bin/kmd
Expand Down
2 changes: 1 addition & 1 deletion scripts/build_deb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ mkdir -p ${PKG_ROOT}/usr/bin

if [ "${VARIATION}" = "" ]; then
# NOTE: keep in sync with installer/rpm/algorand.spec
bin_files=("algocfg" "algod" "algoh" "algokey" "carpenter" "catchupsrv" "diagcfg" "goal" "kmd" "msgpacktool" "node_exporter")
bin_files=("algocfg" "algod" "algoh" "algokey" "carpenter" "catchupsrv" "ddconfig.sh" "diagcfg" "goal" "kmd" "msgpacktool" "node_exporter")
fi

for bin in "${bin_files[@]}"; do
Expand Down
2 changes: 1 addition & 1 deletion scripts/build_package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ DEFAULT_RELEASE_NETWORK=$(./scripts/compute_branch_release_network.sh "${DEFAULT
mkdir ${PKG_ROOT}/bin

# If you modify this list, also update this list in ./cmd/updater/update.sh backup_binaries()
bin_files=("algocfg" "algod" "algoh" "algokey" "carpenter" "catchupsrv" "diagcfg" "find-nodes.sh" "goal" "kmd" "msgpacktool" "node_exporter" "update.sh" "updater" "COPYING")
bin_files=("algocfg" "algod" "algoh" "algokey" "carpenter" "catchupsrv" "ddconfig.sh" "diagcfg" "find-nodes.sh" "goal" "kmd" "msgpacktool" "node_exporter" "update.sh" "updater" "COPYING")
for bin in "${bin_files[@]}"; do
cp ${GOPATH}/bin/${bin} ${PKG_ROOT}/bin
if [ $? -ne 0 ]; then exit 1; fi
Expand Down
146 changes: 146 additions & 0 deletions scripts/ddconfig.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#!/bin/bash

set -ex

SCRIPTPATH="$(cd "$(dirname "$0")" ; pwd -P )" >/dev/null

function ShowSyntaxAndExit() {
echo "ddconfig - enable or disable DataDog for use with an Algorand host"
echo "Syntax: ddconfig enable -d <datadir> -p <port> -n <hostname> -k <apikey>"
echo "Syntax: ddconfig disable"
echo "Do not run as root / sudo - you will be prompted to elevate if necessary"
exit 2
}

function DisableAndExit() {
sudo systemctl stop datadog-agent
sudo systemctl disable datadog-agent
exit 0
}

DATADIR=
PORT=
HOSTNAME=
APIKEY=

# ddconfig enable -p 8880 -n r-aa.mainnet -k <apikey> -d ~/algorand/node

CMD="$1"

if [[ "${CMD}" = "disable" ]]; then
DisableAndExit
fi

if [[ "${CMD}" != "enable" ]]; then
ShowSyntaxAndExit
fi

shift

while [[ "$1" != "" ]]; do
case "$1" in
-d)
shift
DATADIR=$1
pushd ${DATADIR} >/dev/null
DATADIR=$(pwd -P)
popd >/dev/null
;;
-p)
shift
PORT=$1
PORT=$(echo $1 | grep -o "[0-9]*")
if [[ -z "${PORT}" ]]; then
echo "Port value does not appear to be valid. Specify just the port (eg -p 8000)"
exit 1
fi
;;
-n)
shift
HOSTNAME=$1
;;
-k)
shift
APIKEY=$1
;;
-h)
ShowSyntaxAndExit
;;
*)
echo "Unknown option:" "$1"
;;
esac
shift
done

if [[ -z "${DATADIR}" || -z "${HOSTNAME}" || -z "${APIKEY}" ]]; then
ShowSyntaxAndExit
fi

ENDPOINT=$(${SCRIPTPATH}/algocfg get -p EndpointAddress -d "$DATADIR")
ADDRESS=$(echo ${ENDPOINT} | grep -o "[0-9\.]*:" | tr -d ":")

if [[ -z "${PORT}" ]]; then
PORT=$(echo ${ENDPOINT} | grep -o ":[0-9]*" | tr -d ":")
if [[ -z "${PORT}" || "${PORT}" = "0" ]]; then
echo "Port not specified and not already configured - please specify a port to use with `-p`"
exit 1
fi
fi

# Validate the APIKEY - should be 32 alphanum (lowercase) chars
if [[ "${#APIKEY}" != "32" ]]; then
echo "API Key specified should be 32 characters long"
exit 1
fi
FILTEREDKEY=$(echo ${APIKEY} | grep -o "[0-9a-f]*")
if [[ "${APIKEY}" != "${FILTEREDKEY}" ]]; then
echo "API Key specified should contain only lowercase characters or numbers"
exit 1
fi

# At this point we should have valid PORT, HOSTNAME, APIKEY, and DATADIR
# Apply the algod changes and restart it
# Install DataDog agent, configure it, and restart it

${SCRIPTPATH}/diagcfg metric disable -d "${DATADIR}"

${SCRIPTPATH}/algocfg set -p EndpointAddress -d "$DATADIR" -v "${ADDRESS}:${PORT}"

${SCRIPTPATH}/goal node stop -d ${DATADIR}
pkill node_exporter || true

if [[ ! -f "${DATADIR}/algod.token" ]]; then
${SCRIPTPATH}/goal node generatetoken -d "${DATADIR}"
fi

ALGOD_TOKEN=$(cat "${DATADIR}/algod.token")
${SCRIPTPATH}/goal node start -d "${DATADIR}"


# Install DataDog Agent
DD_API_KEY=${APIKEY} bash -c "$(curl -L https://raw.githubusercontent.com/DataDog/datadog-agent/master/cmd/agent/install_script.sh)"

# Remove existing "hostname:" line if any, then append the new one
sudo sed /[[:space:]#]hostname:/d /etc/datadog-agent/datadog.yaml | sudo sed /^hostname:/d > ~/datadog.yaml.tmp
sudo echo "hostname: $HOSTNAME" >> ~/datadog.yaml.tmp
sudo mv ~/datadog.yaml.tmp /etc/datadog-agent/datadog.yaml

sudo mkdir -p /etc/datadog-agent/conf.d/prometheus.d

cat <<EOF>~/conf.yaml.tmp
init_config:
 
instances:
  - prometheus_url: http://localhost:${PORT}/metrics
    extra_headers:
      X-Algo-API-Token: ${ALGOD_TOKEN}
    namespace: algod
    metrics:
      - algod*
EOF
sudo mv ~/conf.yaml.tmp /etc/datadog-agent/conf.d/prometheus.d/conf.yaml
# Restart datadog agent to pick up hostname and prometheus settings
sudo systemctl restart datadog-agent

0 comments on commit b6d6857

Please sign in to comment.