Add xds-agent user systemd service file and install
authorSebastien Douheret <sebastien.douheret@iot.bzh>
Mon, 16 Oct 2017 22:04:36 +0000 (00:04 +0200)
committerSebastien Douheret <sebastien.douheret@iot.bzh>
Mon, 16 Oct 2017 22:46:57 +0000 (00:46 +0200)
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Makefile
conf.d/etc/default/xds-agent [new file with mode: 0644]
conf.d/etc/profile.d/xds-agent.sh [new file with mode: 0644]
conf.d/etc/xds-agent/config.json [moved from agent-config.json.in with 100% similarity]
conf.d/usr/lib/systemd/user/xds-agent.service [moved from xds-agent.service with 74% similarity]
lib/xdsconfig/fileconfig.go
scripts/get-syncthing.sh
scripts/install.sh [new file with mode: 0755]

index ae5a69c..eb70258 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -27,9 +27,9 @@ endif
 # for backward compatibility
 DESTDIR := $(INSTALL_DIR)
 
-# Configurable variables for installation (default /usr/local/...)
+# Configurable variables for installation (default /opt/AGL/xds/agent)
 ifeq ($(origin DESTDIR), undefined)
-       DESTDIR := /usr/local/bin
+       DESTDIR := /opt/AGL/xds/agent
 endif
 
 HOST_GOOS=$(shell go env GOOS)
@@ -82,9 +82,10 @@ build: tools/syncthing/copytobin
        @cd $(ROOT_SRCDIR); $(BUILD_ENV_FLAGS) go build $(VERBOSE_$(V)) -i -o $(LOCAL_BINDIR)/xds-agent$(EXT) -ldflags "$(GORELEASE) -X main.AppVersion=$(VERSION) -X main.AppSubVersion=$(SUB_VERSION)" -gcflags "$(GO_GCFLAGS)" .
 
 package: clean tools/syncthing vendor build
-       @mkdir -p $(PACKAGE_DIR)/xds-agent
-       @cp agent-config.json.in $(PACKAGE_DIR)/xds-agent/agent-config.json
+       @mkdir -p $(PACKAGE_DIR)/xds-agent $(PACKAGE_DIR)/scripts
        @cp -a $(LOCAL_BINDIR)/* $(PACKAGE_DIR)/xds-agent
+       cp -r $(ROOT_SRCDIR)/conf.d $(PACKAGE_DIR)/xds-agent
+       cp -r $(ROOT_SRCDIR)/scripts $(PACKAGE_DIR)/scripts
        cd $(PACKAGE_DIR) && zip -r $(ROOT_SRCDIR)/$(PACKAGE_ZIPFILE) ./xds-agent
 
 .PHONY: package-all
@@ -121,8 +122,10 @@ distclean: clean
        rm -rf $(LOCAL_BINDIR) tools glide.lock vendor $(ROOT_SRCDIR)/*.zip
 
 .PHONY: install
-install: all
-       mkdir -p $(DESTDIR) && cp $(LOCAL_BINDIR)/* $(DESTDIR)
+install:
+       @test -e $(LOCAL_BINDIR)/xds-agent$(EXT) || { echo "Please execute first: make all\n"; exit 1; }
+       @test -e $(LOCAL_BINDIR)/syncthing$(EXT) -a -e $(LOCAL_BINDIR)/syncthing-inotify$(EXT) || { echo        "Please execute first: make all\n"; exit 1; }
+       export DESTDIR=$(DESTDIR) && $(ROOT_SRCDIR)/scripts/install.sh
 
 vendor: tools/glide glide.yaml
        $(LOCAL_TOOLSDIR)/glide install --strip-vendor
diff --git a/conf.d/etc/default/xds-agent b/conf.d/etc/default/xds-agent
new file mode 100644 (file)
index 0000000..93d56cb
--- /dev/null
@@ -0,0 +1,5 @@
+# defaults file for XDS Agent
+# this file is used for service environment in /usr/lib/systemd/user/xds-agent.service
+
+# Logging level (supported levels: panic, fatal, error, warn, info, debug)
+LOG_LEVEL=info
diff --git a/conf.d/etc/profile.d/xds-agent.sh b/conf.d/etc/profile.d/xds-agent.sh
new file mode 100644 (file)
index 0000000..78eff4f
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+#----------  AGL xds-agent tool options Start ---------"
+[ ":${PATH}:" != *":%%XDS_INSTALL_BIN_DIR%%:"* ] && export PATH=%%XDS_INSTALL_BIN_DIR%%:${PATH}
similarity index 74%
rename from xds-agent.service
rename to conf.d/usr/lib/systemd/user/xds-agent.service
index 66788dc..1fd038b 100644 (file)
@@ -2,7 +2,7 @@
 Description=XDS Agent
 
 [Service]
-User=claneys
+EnvironmentFile=-/etc/default/xds-agent
 ExecStart=/opt/AGL/xds/agent/xds-agent &
 
 [Install]
index efe94bf..d936bbe 100644 (file)
@@ -5,7 +5,6 @@ import (
        "os"
        "os/user"
        "path"
-       "path/filepath"
 
        common "github.com/iotbzh/xds-common/golib"
 )
@@ -28,8 +27,7 @@ type FileConfig struct {
 // Order to determine which config file is used:
 //  1/ from command line option: "--config myConfig.json"
 //  2/ $HOME/.xds/agent/agent-config.json file
-//  3/ <current_dir>/agent-config.json file
-//  4/ <executable dir>/agent-config.json file
+//  3/ /etc/xds-agent/config.json file
 
 func updateConfigFromFile(c *Config, confFile string) (*FileConfig, error) {
 
@@ -41,20 +39,7 @@ func updateConfigFromFile(c *Config, confFile string) (*FileConfig, error) {
                searchIn = append(searchIn, path.Join(usr.HomeDir, ".xds", "agent", "agent-config.json"))
        }
 
-       searchIn = append(searchIn, "/etc/xds-agent/agent-config.json")
-
-       exePath := os.Args[0]
-       ee, _ := os.Executable()
-       exeAbsPath, err := filepath.Abs(ee)
-       if err == nil {
-               exePath, err = filepath.EvalSymlinks(exeAbsPath)
-               if err == nil {
-                       exePath = filepath.Dir(ee)
-               } else {
-                       exePath = filepath.Dir(exeAbsPath)
-               }
-       }
-       searchIn = append(searchIn, path.Join(exePath, "agent-config.json"))
+       searchIn = append(searchIn, "/etc/xds-agent/config.json")
 
        var cFile *string
        for _, p := range searchIn {
index 3455122..772c09a 100755 (executable)
@@ -43,7 +43,7 @@ if [ "$?" != 0 ]; then
     exit 1
 fi
 
-gpg -q --keyserver pool.sks-keyservers.net --recv-keys 37C84554E7E0A261E4F76E1ED26E6ED000654A3E || exit 1
+${GPG} -q --keyserver pool.sks-keyservers.net --recv-keys 37C84554E7E0A261E4F76E1ED26E6ED000654A3E || exit 1
 
 tarball="syncthing-${GOOS_ST}-${GOARCH}-v${SYNCTHING_VERSION}.${TB_EXT}" \
        && curl -sfSL "https://github.com/syncthing/syncthing/releases/download/v${SYNCTHING_VERSION}/${tarball}" -O \
@@ -94,4 +94,4 @@ else
     fi
 fi
 
-echo "DONE: syncthing and syncthing-inotify successfuly installed in ${DESTDIR}"
\ No newline at end of file
+echo "DONE: syncthing and syncthing-inotify successfuly installed in ${DESTDIR}"
diff --git a/scripts/install.sh b/scripts/install.sh
new file mode 100755 (executable)
index 0000000..6f93f2b
--- /dev/null
@@ -0,0 +1,42 @@
+#!/bin/bash
+
+# Install XDS agent as a user systemd service
+
+DESTDIR=${DESTDIR:-/opt/AGL/xds/agent}
+
+ROOT_SRCDIR=$(cd $(dirname "$0")/.. && pwd)
+
+install() {
+    mkdir -p ${DESTDIR} && cp ${ROOT_SRCDIR}/bin/* ${DESTDIR} || exit 1
+
+    cp ${ROOT_SRCDIR}/conf.d/etc/xds-agent /etc/ || exit 1
+    cp ${ROOT_SRCDIR}/conf.d/etc/default/xds-agent /etc/default/ || exit 1
+
+    FILE=/etc/profile.d/xds-agent.sh
+    sed -e "s;%%XDS_INSTALL_BIN_DIR%%;${DESTDIR};g" ${ROOT_SRCDIR}/conf.d/${FILE} > ${FILE} || exit 1
+
+    FILE=/usr/lib/systemd/user/xds-agent.service
+    sed -e "s;/opt/AGL/xds/agent;${DESTDIR};g" ${ROOT_SRCDIR}/conf.d/${FILE} > ${FILE} || exit 1
+
+    echo ""
+    echo "To enable xds-agent service, execute:      systemctl --user enable xds-agent"
+    echo "and to start xds-agent service, execute:   systemctl --user start xds-agent"
+}
+
+uninstall() {
+    rm -rf "${DESTDIR}"
+    rm -f /etc/xds-agent /etc/profile.d/xds-agent.sh /usr/lib/systemd/user/xds-agent.service
+}
+
+if [ "$1" == "uninstall" ]; then
+    echo -n "Are-you sure you want to remove ${DESTDIR} [y/n]? "
+    read answer
+    if [ "${answer}" = "y" ]; then
+        uninstall
+        echo "xds-agent sucessfully uninstalled."
+    else
+        echo "Uninstall canceled."
+    fi
+else
+    install
+fi