diff options
author | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2017-12-04 17:28:18 +0100 |
---|---|---|
committer | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2017-12-04 17:28:18 +0100 |
commit | 9194f6c934ab3af0a559290b9299f6be2d9b2269 (patch) | |
tree | cc58c3153fbeb5ee44abf593870c7fc82e8f66e7 /lib/agent | |
parent | 0e8a438f07a2fbb2b9e16cb9ad852211400e5aea (diff) |
Added xds-project.conf file creation.
Diffstat (limited to 'lib/agent')
-rw-r--r-- | lib/agent/apiv1-projects.go | 2 | ||||
-rw-r--r-- | lib/agent/projects.go | 25 |
2 files changed, 24 insertions, 3 deletions
diff --git a/lib/agent/apiv1-projects.go b/lib/agent/apiv1-projects.go index 6fe2238..b69474d 100644 --- a/lib/agent/apiv1-projects.go +++ b/lib/agent/apiv1-projects.go @@ -56,7 +56,7 @@ func (s *APIService) addProject(c *gin.Context) { s.Log.Debugln("Add project config: ", cfgArg) - newFld, err := s.projects.Add(cfgArg, s.sessions.GetID(c)) + newFld, err := s.projects.Add(cfgArg, s.sessions.GetID(c), c.Request.Host) if err != nil { common.APIError(c, err.Error()) return diff --git a/lib/agent/projects.go b/lib/agent/projects.go index 14f2784..a0b641a 100644 --- a/lib/agent/projects.go +++ b/lib/agent/projects.go @@ -20,12 +20,15 @@ package agent import ( "fmt" "log" + "os" + "path/filepath" "strings" "time" "github.com/franciscocpg/reflectme" "github.com/iotbzh/xds-agent/lib/syncthing" "github.com/iotbzh/xds-agent/lib/xaapiv1" + common "github.com/iotbzh/xds-common/golib" "github.com/iotbzh/xds-server/lib/xsapiv1" "github.com/syncthing/syncthing/lib/sync" ) @@ -138,12 +141,30 @@ func (p *Projects) GetProjectArrUnsafe() []xaapiv1.ProjectConfig { } // Add adds a new folder -func (p *Projects) Add(newF xaapiv1.ProjectConfig, fromSid string) (*xaapiv1.ProjectConfig, error) { - prj, err := p.createUpdate(newF, true, false) +func (p *Projects) Add(newP xaapiv1.ProjectConfig, fromSid, requestURL string) (*xaapiv1.ProjectConfig, error) { + prj, err := p.createUpdate(newP, true, false) if err != nil { return prj, err } + // Create xds-project.conf file + prjConfFile := filepath.Join(prj.ClientPath, "xds-project.conf") + if !common.Exists(prjConfFile) { + fd, err := os.OpenFile(prjConfFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) + if err != nil { + return prj, fmt.Errorf("Cannot create xds-project.conf: %v", err.Error()) + } + fd.WriteString("# XDS project settings\n") + fd.WriteString("export XDS_AGENT_URL=" + requestURL + "\n") + fd.WriteString("export XDS_PROJECT_ID=" + prj.ID + "\n") + if prj.DefaultSdk != "" { + fd.WriteString("export XDS_SDK_ID=" + prj.DefaultSdk + "\n") + } else { + fd.WriteString("#export XDS_SDK_ID=???\n") + } + fd.Close() + } + // Notify client with event if err := p.events.Emit(xaapiv1.EVTProjectAdd, *prj, fromSid); err != nil { p.Log.Warningf("Cannot notify project deletion: %v", err) |