Use go module as dependency tool instead of glide
[src/xds/xds-agent.git] / lib / agent / projects.go
index 1d21931..d316c20 100644 (file)
@@ -1,15 +1,36 @@
+/*
+ * Copyright (C) 2017-2018 "IoT.bzh"
+ * Author Sebastien Douheret <sebastien@iot.bzh>
+ *
+ * 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"
 
+       st "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/syncthing"
+       "gerrit.automotivelinux.org/gerrit/src/xds/xds-server.git/lib/xsapiv1"
+
+       "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/xaapiv1"
+       common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git"
        "github.com/franciscocpg/reflectme"
-       "github.com/iotbzh/xds-agent/lib/syncthing"
-       "github.com/iotbzh/xds-agent/lib/xaapiv1"
-       "github.com/iotbzh/xds-server/lib/xsapiv1"
        "github.com/syncthing/syncthing/lib/sync"
 )
 
@@ -34,6 +55,7 @@ func NewProjects(ctx *Context, st *st.SyncThing) *Projects {
 
 // Init Load Projects configuration
 func (p *Projects) Init(server *XdsServer) error {
+
        svrList := make(map[string]*XdsServer)
        // If server not set, load for all servers
        if server == nil {
@@ -55,6 +77,11 @@ func (p *Projects) Init(server *XdsServer) error {
                for _, prj := range xFlds {
                        newP := svr.FolderToProject(prj)
                        if _, err := p.createUpdate(newP, false, true); err != nil {
+                               // Don't consider that as an error (allow support config without CloudSync support)
+                               if p.Context.SThg == nil && strings.Contains(err.Error(), "Server doesn't support project type CloudSync") {
+                                       continue
+                               }
+
                                errMsg += "Error while creating project id " + prj.ID + ": " + err.Error() + "\n "
                                continue
                        }
@@ -121,12 +148,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)
@@ -208,7 +253,7 @@ func (p *Projects) createUpdate(newF xaapiv1.ProjectConfig, create bool, initial
        // Add to folders list
        p.projects[newPrj.ID] = &fld
 
-       // Force sync after creation
+       // Force sync to get an initial sync status
        // (need to defer to be sure that WS events will arrive after HTTP creation reply)
        go func() {
                time.Sleep(time.Millisecond * 500)