Merge branch 'master' into wip v0.3.0-rc1
authorSebastien Douheret <sebastien.douheret@iot.bzh>
Fri, 20 Oct 2017 12:30:33 +0000 (14:30 +0200)
committerSebastien Douheret <sebastien.douheret@iot.bzh>
Fri, 20 Oct 2017 12:36:43 +0000 (14:36 +0200)
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
1  2 
Makefile
conf.d/etc/xds-agent/config.json
lib/syncthing/st.go
lib/xdsconfig/configfile.go
scripts/install.sh

diff --cc Makefile
+++ b/Makefile
@@@ -74,15 -75,28 +74,15 @@@ els
  endif
  
  
 -all: tools/syncthing vendor build
 +all: tools/syncthing build
  
 -build: tools/syncthing/copytobin
 +.PHONY: build
 +build: vendor xds webapp
 +
 +xds: scripts tools/syncthing/copytobin
        @echo "### Build XDS agent (version $(VERSION), subversion $(SUB_VERSION)) - $(BUILD_MODE)";
-       @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)" .
+       @cd $(ROOT_SRCDIR); $(BUILD_ENV_FLAGS) go build $(VERBOSE_$(V)) -i -o $(LOCAL_BINDIR)/xds-agent$(EXT) -ldflags "$(GO_LDFLAGS) -X main.AppVersion=$(VERSION) -X main.AppSubVersion=$(SUB_VERSION)" -gcflags "$(GO_GCFLAGS)" .
  
 -package: clean tools/syncthing vendor build
 -      @mkdir -p $(PACKAGE_DIR)/xds-agent $(PACKAGE_DIR)/scripts
 -      @cp -a $(LOCAL_BINDIR)/* $(PACKAGE_DIR)/xds-agent
 -      @cp -r $(ROOT_SRCDIR)/conf.d $(ROOT_SRCDIR)/scripts $(PACKAGE_DIR)/xds-agent
 -      cd $(PACKAGE_DIR) && zip -r $(ROOT_SRCDIR)/$(PACKAGE_ZIPFILE) ./xds-agent
 -
 -.PHONY: package-all
 -package-all:
 -      @echo "# Build linux amd64..."
 -      GOOS=linux GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
 -      @echo "# Build windows amd64..."
 -      GOOS=windows GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
 -      @echo "# Build darwin amd64..."
 -      GOOS=darwin GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
 -      make -f $(ROOT_SRCDIR)/Makefile clean
 -
  test: tools/glide
        go test --race $(shell $(LOCAL_TOOLSDIR)/glide novendor)
  
@@@ -104,38 -118,17 +104,43 @@@ clean
  
  .PHONY: distclean
  distclean: clean
 -      rm -rf $(LOCAL_BINDIR) tools glide.lock vendor $(ROOT_SRCDIR)/*.zip
 +      cd $(ROOT_SRCDIR) && rm -rf $(LOCAL_BINDIR) ./tools ./glide.lock ./vendor ./*.zip ./webapp/node_modules ./webapp/dist
 +
 +webapp: webapp/install
 +      (cd webapp && gulp build)
 +
 +webapp/debug:
 +      (cd webapp && gulp watch &)
 +
 +webapp/install:
 +      (cd webapp && npm install)
 +      @if [ -d ${DESTDIR}/usr/local/etc ]; then rm -rf ${DESTDIR}/usr; fi
  
  .PHONY: install
- install: all
-       mkdir -p $(DESTDIR) && cp $(LOCAL_BINDIR)/* $(DESTDIR)
-       mkdir -p $(DESTDIR_WWW) && cp -a webapp/dist/* $(DESTDIR_WWW)
+ 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
++      @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) && export DESTDIR_WWW=$(DESTDIR_WWW) && $(ROOT_SRCDIR)/scripts/install.sh
+ .PHONY: uninstall
+ uninstall:
 -      export DESTDIR=$(DESTDIR) && $(ROOT_SRCDIR)/scripts/install.sh uninstall
++      export DESTDIR=$(DESTDIR) && export DESTDIR_WWW=$(DESTDIR_WWW) && $(ROOT_SRCDIR)/scripts/install.sh uninstall
 +
 +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 $(ROOT_SRCDIR)/scripts $(PACKAGE_DIR)/xds-agent
 +      cd $(PACKAGE_DIR) && zip -r $(ROOT_SRCDIR)/$(PACKAGE_ZIPFILE) ./xds-agent
 +
 +.PHONY: package-all
 +package-all:
 +      @echo "# Build linux amd64..."
 +      GOOS=linux GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
 +      @echo "# Build windows amd64..."
 +      GOOS=windows GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
 +      @echo "# Build darwin amd64..."
 +      GOOS=darwin GOARCH=amd64 RELEASE=1 make -f $(ROOT_SRCDIR)/Makefile package
 +      make -f $(ROOT_SRCDIR)/Makefile clean
  
  vendor: tools/glide glide.yaml
        $(LOCAL_TOOLSDIR)/glide install --strip-vendor
@@@ -1,15 -1,9 +1,15 @@@
  {
 +    "httpPort": "8000",
 +    "webAppDir": "./www",
      "logsDir": "${HOME}/.xds/agent/logs",
 -    "xds-apikey": "1234abcezam",
 +    "xdsServers": [
 +        {
 +          "url": "http://localhost:8810"
 +        }
 +    ],
      "syncthing": {
          "home": "${HOME}/.xds/agent/syncthing-config",
-         "gui-address": "http://localhost:8384",
+         "gui-address": "http://localhost:8386",
          "gui-apikey": "1234abcezam"
      }
  }
Simple merge
index 6eaaf6a,0000000..e3737f4
mode 100644,000000..100644
--- /dev/null
@@@ -1,112 -1,0 +1,109 @@@
- //  3/ <current_dir>/agent-config.json file
- //  4/ <executable dir>/agent-config.json file
 +package xdsconfig
 +
 +import (
 +      "encoding/json"
 +      "os"
 +      "path"
 +
 +      common "github.com/iotbzh/xds-common/golib"
 +)
 +
 +type SyncThingConf struct {
 +      BinDir     string `json:"binDir"`
 +      Home       string `json:"home"`
 +      GuiAddress string `json:"gui-address"`
 +      GuiAPIKey  string `json:"gui-apikey"`
 +}
 +
 +type XDSServerConf struct {
 +      URL       string `json:"url"`
 +      ConnRetry int    `json:"connRetry"`
 +
 +      // private/not exported fields
 +      ID            string `json:"-"`
 +      APIBaseURL    string `json:"-"`
 +      APIPartialURL string `json:"-"`
 +}
 +
 +type FileConfig struct {
 +      HTTPPort    string          `json:"httpPort"`
 +      WebAppDir   string          `json:"webAppDir"`
 +      LogsDir     string          `json:"logsDir"`
 +      XDSAPIKey   string          `json:"xds-apikey"`
 +      ServersConf []XDSServerConf `json:"xdsServers"`
 +      SThgConf    *SyncThingConf  `json:"syncthing"`
 +}
 +
 +// readGlobalConfig reads configuration from a config file.
 +// Order to determine which config file is used:
 +//  1/ from command line option: "--config myConfig.json"
 +//  2/ $HOME/.xds/agent/agent-config.json file
-       searchIn = append(searchIn, path.Join(common.GetExePath(), "agent-config.json"))
++//  3/ /etc/xds-agent/config.json file
 +
 +func readGlobalConfig(c *Config, confFile string) error {
 +
 +      searchIn := make([]string, 0, 3)
 +      if confFile != "" {
 +              searchIn = append(searchIn, confFile)
 +      }
 +      if homeDir := common.GetUserHome(); homeDir != "" {
 +              searchIn = append(searchIn, path.Join(homeDir, ".xds", "agent", "agent-config.json"))
 +      }
 +
 +      searchIn = append(searchIn, "/etc/xds-agent/agent-config.json")
 +
 +      var cFile *string
 +      for _, p := range searchIn {
 +              if _, err := os.Stat(p); err == nil {
 +                      cFile = &p
 +                      break
 +              }
 +      }
 +      if cFile == nil {
 +              c.Log.Infof("No config file found")
 +              return nil
 +      }
 +
 +      c.Log.Infof("Use config file: %s", *cFile)
 +
 +      // TODO move on viper package to support comments in JSON and also
 +      // bind with flags (command line options)
 +      // see https://github.com/spf13/viper#working-with-flags
 +
 +      fd, _ := os.Open(*cFile)
 +      defer fd.Close()
 +
 +      // Decode config file content and save it in a first variable
 +      fCfg := FileConfig{}
 +      if err := json.NewDecoder(fd).Decode(&fCfg); err != nil {
 +              return err
 +      }
 +
 +      // Decode config file content and overwrite default settings
 +      fd.Seek(0, 0)
 +      json.NewDecoder(fd).Decode(&c.FileConf)
 +
 +      // Disable Syncthing support when there is no syncthing field in config
 +      if fCfg.SThgConf == nil {
 +              c.FileConf.SThgConf = nil
 +      }
 +
 +      // Support environment variables (IOW ${MY_ENV_VAR} syntax) in agent-config.json
 +      vars := []*string{
 +              &c.FileConf.LogsDir,
 +              &c.FileConf.WebAppDir,
 +      }
 +      if c.FileConf.SThgConf != nil {
 +              vars = append(vars, &c.FileConf.SThgConf.Home,
 +                      &c.FileConf.SThgConf.BinDir)
 +      }
 +      for _, field := range vars {
 +              var err error
 +              *field, err = common.ResolveEnvVar(*field)
 +              if err != nil {
 +                      return err
 +              }
 +      }
 +
 +      return nil
 +}
index 0000000,6f93f2b..357c5e8
mode 000000,100755..100755
--- /dev/null
@@@ -1,0 -1,42 +1,44 @@@
+ #!/bin/bash
+ # Install XDS agent as a user systemd service
+ DESTDIR=${DESTDIR:-/opt/AGL/xds/agent}
++DESTDIR_WWW=${DESTDIR_WWW:-${DESTDIR}/www}
+ ROOT_SRCDIR=$(cd $(dirname "$0")/.. && pwd)
+ install() {
+     mkdir -p ${DESTDIR} && cp ${ROOT_SRCDIR}/bin/* ${DESTDIR} || exit 1
++    mkdir -p ${DESTDIR_WWW} && cp -a ${ROOT_SRCDIR}/webapp/dist/* ${DESTDIR_WWW} || 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