X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=lib%2Fagent%2Fprojects.go;h=a2d8fe123fc36f9c8caa1979322f9979c6153456;hb=d0d64817590d38d182faeb0e040861d3d3cb9f3b;hp=1d21931e1189359b486428917477a8acedc47f8f;hpb=7c7d90a781082c6bd22d12a5e2451ca61a5198af;p=src%2Fxds%2Fxds-agent.git diff --git a/lib/agent/projects.go b/lib/agent/projects.go index 1d21931..a2d8fe1 100644 --- a/lib/agent/projects.go +++ b/lib/agent/projects.go @@ -1,14 +1,34 @@ +/* + * Copyright (C) 2017 "IoT.bzh" + * Author Sebastien Douheret + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + 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" ) @@ -121,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) @@ -210,10 +248,12 @@ func (p *Projects) createUpdate(newF xaapiv1.ProjectConfig, create bool, initial // Force sync after creation // (need to defer to be sure that WS events will arrive after HTTP creation reply) - go func() { - time.Sleep(time.Millisecond * 500) - fld.Sync() - }() + if create { + go func() { + time.Sleep(time.Millisecond * 500) + fld.Sync() + }() + } return newPrj, nil }