diff options
author | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2017-10-06 17:53:51 +0200 |
---|---|---|
committer | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2017-11-06 15:57:03 +0100 |
commit | 0e72ccd733207877bd8edca55fd6aed0877139e5 (patch) | |
tree | 948fd6eebea259c8a09b313427e0112433418d30 /lib | |
parent | ac9e80f7f32a1c04c171a4d208f0ab7d3a5ca154 (diff) |
Cleanup and improvements
- Save server data into file
- FolderType is now a string
- cleanup useless code
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/apiv1/agent.go | 70 | ||||
-rw-r--r-- | lib/apiv1/apiv1.go | 1 | ||||
-rw-r--r-- | lib/apiv1/version.go | 2 | ||||
-rw-r--r-- | lib/folder/folder-interface.go | 11 | ||||
-rw-r--r-- | lib/model/folders.go | 3 | ||||
-rw-r--r-- | lib/syncthing/folder-st.go | 2 | ||||
-rw-r--r-- | lib/syncthing/st.go | 1 | ||||
-rw-r--r-- | lib/xdsconfig/builderconfig.go | 5 | ||||
-rw-r--r-- | lib/xdsconfig/config.go | 25 | ||||
-rw-r--r-- | lib/xdsconfig/data.go | 87 | ||||
-rw-r--r-- | lib/xdsconfig/fileconfig.go | 17 |
11 files changed, 127 insertions, 97 deletions
diff --git a/lib/apiv1/agent.go b/lib/apiv1/agent.go deleted file mode 100644 index 925f12b..0000000 --- a/lib/apiv1/agent.go +++ /dev/null @@ -1,70 +0,0 @@ -package apiv1 - -import ( - "net/http" - "path" - "strings" - - "path/filepath" - - "github.com/gin-gonic/gin" - common "github.com/iotbzh/xds-common/golib" -) - -// XDSAgentTarball . -type XDSAgentTarball struct { - OS string `json:"os"` - Arch string `json:"arch"` - Version string `json:"version"` - RawVersion string `json:"raw-version"` - FileURL string `json:"fileUrl"` -} - -// XDSAgentInfo . -type XDSAgentInfo struct { - Tarballs []XDSAgentTarball `json:"tarballs"` -} - -// getXdsAgentInfo : return various information about Xds Agent -func (s *APIService) getXdsAgentInfo(c *gin.Context) { - - res := XDSAgentInfo{} - tarballURL := "assets/xds-agent-tarballs" - tarballDir := filepath.Join(s.cfg.FileConf.WebAppDir, "assets", "xds-agent-tarballs") - if common.Exists(tarballDir) { - files, err := filepath.Glob(path.Join(tarballDir, "xds-agent_*.zip")) - if err != nil { - s.log.Debugf("Error while retrieving xds-agent tarballs: dir=%s, error=%v", tarballDir, err) - } - for _, ff := range files { - file := filepath.Base(ff) - // Assume that tarball name format is: xds-agent_OS-ARCH-RAWVERSION.zip - fs := strings.TrimSuffix(strings.TrimPrefix(file, "xds-agent_"), ".zip") - f := strings.Split(fs, "-") - - if len(f) >= 3 { - vers := strings.Split(f[2], "_") - ver := f[2] - if len(vers) > 1 { - ver = vers[0] - } - - newT := XDSAgentTarball{ - OS: f[0], - Arch: f[1], - Version: ver, - RawVersion: f[2], - FileURL: filepath.Join(tarballURL, file), - } - - s.log.Infof("Added XDS-Agent tarball: %s", file) - res.Tarballs = append(res.Tarballs, newT) - - } else { - s.log.Debugf("Error while retrieving xds-agent, decoding failure: file:%v", ff) - } - } - } - - c.JSON(http.StatusOK, res) -} diff --git a/lib/apiv1/apiv1.go b/lib/apiv1/apiv1.go index 262f513..0a96311 100644 --- a/lib/apiv1/apiv1.go +++ b/lib/apiv1/apiv1.go @@ -34,7 +34,6 @@ func New(r *gin.Engine, sess *session.Sessions, cfg *xdsconfig.Config, mfolders } s.apiRouter.GET("/version", s.getVersion) - s.apiRouter.GET("/xdsagent/info", s.getXdsAgentInfo) s.apiRouter.GET("/config", s.getConfig) s.apiRouter.POST("/config", s.setConfig) diff --git a/lib/apiv1/version.go b/lib/apiv1/version.go index e022441..8f928ec 100644 --- a/lib/apiv1/version.go +++ b/lib/apiv1/version.go @@ -7,6 +7,7 @@ import ( ) type version struct { + ID string `json:"id"` Version string `json:"version"` APIVersion string `json:"apiVersion"` VersionGitTag string `json:"gitTag"` @@ -15,6 +16,7 @@ type version struct { // getInfo : return various information about server func (s *APIService) getVersion(c *gin.Context) { response := version{ + ID: s.cfg.ServerUID, Version: s.cfg.Version, APIVersion: s.cfg.APIVersion, VersionGitTag: s.cfg.VersionGitTag, diff --git a/lib/folder/folder-interface.go b/lib/folder/folder-interface.go index 4beccb8..5036c4d 100644 --- a/lib/folder/folder-interface.go +++ b/lib/folder/folder-interface.go @@ -1,12 +1,12 @@ package folder // FolderType definition -type FolderType int +type FolderType string const ( - TypePathMap = 1 - TypeCloudSync = 2 - TypeCifsSmb = 3 + TypePathMap = "PathMap" + TypeCloudSync = "CloudSync" + TypeCifsSmb = "CIFS" ) // Folder Status definition @@ -65,6 +65,5 @@ type PathMapConfig struct { // CloudSyncConfig CloudSync (AKA Syncthing) specific data type CloudSyncConfig struct { - SyncThingID string `json:"syncThingID"` - BuilderSThgID string `json:"builderSThgID"` + SyncThingID string `json:"syncThingID"` } diff --git a/lib/model/folders.go b/lib/model/folders.go index 576c4a2..24ac48c 100644 --- a/lib/model/folders.go +++ b/lib/model/folders.go @@ -168,8 +168,7 @@ func (f *Folders) GetConfigArr() []folder.FolderConfig { // getConfigArrUnsafe Same as GetConfigArr without mutex protection func (f *Folders) getConfigArrUnsafe() []folder.FolderConfig { - var conf []folder.FolderConfig - + conf := []folder.FolderConfig{} for _, v := range f.folders { conf = append(conf, (*v).GetConfig()) } diff --git a/lib/syncthing/folder-st.go b/lib/syncthing/folder-st.go index 7e1fe55..ae95b27 100644 --- a/lib/syncthing/folder-st.go +++ b/lib/syncthing/folder-st.go @@ -57,8 +57,6 @@ func (f *STFolder) Add(cfg folder.FolderConfig) (*folder.FolderConfig, error) { f.fConfig = cfg - f.fConfig.DataCloudSync.BuilderSThgID = f.st.MyID // FIXME - should be removed after local ST config rework - // Update Syncthing folder // (expect if status is ErrorConfig) // TODO: add cache to avoid multiple requests on startup diff --git a/lib/syncthing/st.go b/lib/syncthing/st.go index 99a17a1..e8e9f44 100644 --- a/lib/syncthing/st.go +++ b/lib/syncthing/st.go @@ -227,7 +227,6 @@ func (s *SyncThing) Start() (*exec.Cmd, error) { env := []string{ "STNODEFAULTFOLDER=1", "STNOUPGRADE=1", - "STNORESTART=1", // FIXME SEB remove ? } s.STCmd, err = s.startProc("syncthing", args, env, &s.exitSTChan) diff --git a/lib/xdsconfig/builderconfig.go b/lib/xdsconfig/builderconfig.go index c64fe9c..6fc1814 100644 --- a/lib/xdsconfig/builderconfig.go +++ b/lib/xdsconfig/builderconfig.go @@ -28,10 +28,7 @@ func NewBuilderConfig(stID string) (BuilderConfig, error) { return b, nil } -// Copy makes a real copy of BuilderConfig -func (c *BuilderConfig) Copy(n BuilderConfig) { - // TODO -} +/*** Private ***/ func getLocalIP() (string, error) { addrs, err := net.InterfaceAddrs() diff --git a/lib/xdsconfig/config.go b/lib/xdsconfig/config.go index 84e0778..2f1fa96 100644 --- a/lib/xdsconfig/config.go +++ b/lib/xdsconfig/config.go @@ -13,10 +13,12 @@ import ( // Config parameters (json format) of /config command type Config struct { - Version string `json:"version"` - APIVersion string `json:"apiVersion"` - VersionGitTag string `json:"gitTag"` - Builder BuilderConfig `json:"builder"` + ServerUID string `json:"id"` + Version string `json:"version"` + APIVersion string `json:"apiVersion"` + VersionGitTag string `json:"gitTag"` + SupportedSharing map[string]bool `json:"supportedSharing"` + Builder BuilderConfig `json:"builder"` // Private (un-exported fields in REST GET /config route) Options Options `json:"-"` @@ -55,12 +57,19 @@ func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) { dfltSTHomeDir = resDir } + uuid, err := ServerIDGet() + if err != nil { + return nil, err + } + // Define default configuration c := Config{ - Version: cliCtx.App.Metadata["version"].(string), - APIVersion: DefaultAPIVersion, - VersionGitTag: cliCtx.App.Metadata["git-tag"].(string), - Builder: BuilderConfig{}, + ServerUID: uuid, + Version: cliCtx.App.Metadata["version"].(string), + APIVersion: DefaultAPIVersion, + VersionGitTag: cliCtx.App.Metadata["git-tag"].(string), + Builder: BuilderConfig{}, + SupportedSharing: map[string]bool{ /*FIXME USE folder.TypePathMap*/ "PathMap": true}, Options: Options{ ConfigFile: cliCtx.GlobalString("config"), diff --git a/lib/xdsconfig/data.go b/lib/xdsconfig/data.go new file mode 100644 index 0000000..65e0fc6 --- /dev/null +++ b/lib/xdsconfig/data.go @@ -0,0 +1,87 @@ +package xdsconfig + +import ( + "encoding/xml" + "fmt" + "os" + + common "github.com/iotbzh/xds-common/golib" + uuid "github.com/satori/go.uuid" + "github.com/syncthing/syncthing/lib/sync" +) + +// xmlServerData contains persistent data stored/loaded by server +type xmlServerData struct { + XMLName xml.Name `xml:"XDS-Server"` + Version string `xml:"version,attr"` + Data ServerData `xml:"server-data"` +} + +type ServerData struct { + ID string `xml:"id"` +} + +var sdMutex = sync.NewMutex() + +// ServerIDGet +func ServerIDGet() (string, error) { + var f string + var err error + + d := ServerData{} + if f, err = ServerDataFilenameGet(); err != nil { + return "", err + } + if err = serverDataRead(f, &d); err != nil || d.ID == "" { + // Create a new uuid when not found + d.ID = uuid.NewV1().String() + if err := serverDataWrite(f, d); err != nil { + return "", err + } + } + return d.ID, nil +} + +// serverDataRead reads data saved on disk +func serverDataRead(file string, data *ServerData) error { + if !common.Exists(file) { + return fmt.Errorf("No folder config file found (%s)", file) + } + + sdMutex.Lock() + defer sdMutex.Unlock() + + fd, err := os.Open(file) + defer fd.Close() + if err != nil { + return err + } + + xsd := xmlServerData{} + err = xml.NewDecoder(fd).Decode(&xsd) + if err == nil { + *data = xsd.Data + } + return err +} + +// serverDataWrite writes persistant data to disk +func serverDataWrite(file string, data ServerData) error { + sdMutex.Lock() + defer sdMutex.Unlock() + + fd, err := os.OpenFile(file, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) + defer fd.Close() + if err != nil { + return err + } + + xsd := &xmlServerData{ + Version: "1", + Data: data, + } + + enc := xml.NewEncoder(fd) + enc.Indent("", " ") + return enc.Encode(xsd) +} diff --git a/lib/xdsconfig/fileconfig.go b/lib/xdsconfig/fileconfig.go index 2651caf..39560a4 100644 --- a/lib/xdsconfig/fileconfig.go +++ b/lib/xdsconfig/fileconfig.go @@ -16,6 +16,8 @@ const ( ConfigDir = ".xds-server" // GlobalConfigFilename Global config filename GlobalConfigFilename = "config.json" + // ServerDataFilename Server data filename + ServerDataFilename = "server-data.xml" // FoldersConfigFilename Folders config filename FoldersConfigFilename = "server-config_folders.xml" ) @@ -146,11 +148,20 @@ func readGlobalConfig(c *Config, confFile string) error { return nil } -// FoldersConfigFilenameGet -func FoldersConfigFilenameGet() (string, error) { +func configFilenameGet(cfgFile string) (string, error) { usr, err := user.Current() if err != nil { return "", err } - return path.Join(usr.HomeDir, ConfigDir, FoldersConfigFilename), nil + return path.Join(usr.HomeDir, ConfigDir, cfgFile), nil +} + +// FoldersConfigFilenameGet +func FoldersConfigFilenameGet() (string, error) { + return configFilenameGet(FoldersConfigFilename) +} + +// ServerDataFilenameGet +func ServerDataFilenameGet() (string, error) { + return configFilenameGet(ServerDataFilename) } |