aboutsummaryrefslogtreecommitdiffstats
path: root/lib/agent/projects.go
blob: d5f809f0ce31a853f8e7db80639c6032350a31d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * 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/lib/syncthing"
	"gerrit.automotivelinux.org/gerrit/src/xds/xds-server.git/lib/xsapiv1"

	"gerrit.automotivelinux.org/gerrit/src/xds/xds-agent/lib/xaapiv1"
	common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib"
	"github.com/franciscocpg/reflectme"
	"github.com/syncthing/syncthing/lib/sync"
)

// Projects Represent a an XDS Projects
type Projects struct {
	*Context
	SThg     *st.SyncThing
	projects map[string]*IPROJECT
}

// Mutex to make add/delete atomic
var pjMutex = sync.NewMutex()

// NewProjects Create a new instance of Project Model
func NewProjects(ctx *Context, st *st.SyncThing) *Projects {
	return &Projects{
		Context:  ctx,
		SThg:     st,
		projects: make(map[string]*IPROJECT),
	}
}

// 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 {
		svrList = p.xdsServers
	} else {
		svrList[server.ID] = server
	}
	errMsg := ""
	for _, svr := range svrList {
		if svr.Disabled {
			continue
		}
		xFlds := []xsapiv1.FolderConfig{}
		if err := svr.GetFolders(&xFlds); err != nil {
			errMsg += fmt.Sprintf("Cannot retrieve folders config of XDS server ID %s : %v \n", svr.ID, err.Error())
			continue
		}
		p.Log.Debugf("Connected to XDS Server %s, %d projects detected", svr.ID, len(xFlds))
		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
			}
		}
	}

	p.Log.Infof("Number of loaded Projects: %d", len(p.projects))

	if errMsg != "" {
		return fmt.Errorf(errMsg)
	}
	return nil
}

// ResolveID Complete a Project ID (helper for user that can use partial ID value)
func (p *Projects) ResolveID(id string) (string, error) {
	if id == "" {
		return "", nil
	}

	match := []string{}
	for iid := range p.projects {
		if strings.HasPrefix(iid, id) {
			match = append(match, iid)
		}
	}

	if len(match) == 1 {
		return match[0], nil
	} else if len(match) == 0 {
		return id, fmt.Errorf("Unknown id")
	}
	return id, fmt.Errorf("Multiple IDs found with provided prefix: " + id)
}

// Get returns the folder config or nil if not existing
func (p *Projects) Get(id string) *IPROJECT {
	if id == "" {
		return nil
	}
	fc, exist := p.projects[id]
	if !exist {
		return nil
	}
	return fc
}

// GetProjectArr returns the config of all folders as an array
func (p *Projects) GetProjectArr() []xaapiv1.ProjectConfig {
	pjMutex.Lock()
	defer pjMutex.Unlock()

	return p.GetProjectArrUnsafe()
}

// GetProjectArrUnsafe Same as GetProjectArr without mutex protection
func (p *Projects) GetProjectArrUnsafe() []xaapiv1.ProjectConfig {
	conf := []xaapiv1.ProjectConfig{}
	for _, v := range p.projects {
		prj := (*v).GetProject()
		conf = append(conf, *prj)
	}
	return conf
}

// Add adds a new folder
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.WriteString("\n# Only disable reset of LD_LIBRARY_PATH if you are absolutely know what you are doing!\n")
		fd.WriteString("export LD_LIBRARY_PATH=''\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)
	}

	return prj, err
}

// CreateUpdate creates or update a folder
func (p *Projects) createUpdate(newF xaapiv1.ProjectConfig, create bool, initial bool) (*xaapiv1.ProjectConfig, error) {
	var err error

	pjMutex.Lock()
	defer pjMutex.Unlock()

	// Sanity check
	if _, exist := p.projects[newF.ID]; create && exist {
		return nil, fmt.Errorf("ID already exists")
	}
	if newF.ClientPath == "" {
		return nil, fmt.Errorf("ClientPath must be set")
	}
	if newF.ServerID == "" {
		return nil, fmt.Errorf("Server ID must be set")
	}
	var svr *XdsServer
	var exist bool
	if svr, exist = p.xdsServers[newF.ServerID]; !exist {
		return nil, fmt.Errorf("Unknown Server ID %s", newF.ServerID)
	}

	// Check type supported
	b, exist := svr.ServerConfig.SupportedSharing[string(newF.Type)]
	if !exist || !b {
		return nil, fmt.Errorf("Server doesn't support project type %s", newF.Type)
	}

	// Create a new folder object
	var fld IPROJECT
	switch newF.Type {
	// SYNCTHING
	case xaapiv1.TypeCloudSync:
		if p.SThg != nil {
			fld = NewProjectST(p.Context, svr)
		} else {
			return nil, fmt.Errorf("Cloud Sync project not supported")
		}

	// PATH MAP
	case xaapiv1.TypePathMap:
		fld = NewProjectPathMap(p.Context, svr)
	default:
		return nil, fmt.Errorf("Unsupported folder type")
	}

	var newPrj *xaapiv1.ProjectConfig
	if create {
		// Add project on server
		if newPrj, err = fld.Add(newF); err != nil {
			newF.Status = xaapiv1.StatusErrorConfig
			log.Printf("ERROR Adding project: %v\n", err)
			return newPrj, err
		}
	} else {
		// Just update project config
		if newPrj, err = fld.Setup(newF); err != nil {
			newF.Status = xaapiv1.StatusErrorConfig
			log.Printf("ERROR Updating project: %v\n", err)
			return newPrj, err
		}
	}

	// Sanity check
	if newPrj.ID == "" {
		log.Printf("ERROR project ID empty: %v", newF)
		return newPrj, fmt.Errorf("Project ID empty")
	}

	// Add to folders list
	p.projects[newPrj.ID] = &fld

	// 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)
		fld.Sync()
	}()

	return newPrj, nil
}

// Delete deletes a specific folder
func (p *Projects) Delete(id, fromSid string) (xaapiv1.ProjectConfig, error) {
	var err error

	pjMutex.Lock()
	defer pjMutex.Unlock()

	fld := xaapiv1.ProjectConfig{}
	fc, exist := p.projects[id]
	if !exist {
		return fld, fmt.Errorf("Unknown id")
	}

	prj := (*fc).GetProject()

	if err = (*fc).Delete(); err != nil {
		return *prj, err
	}

	delete(p.projects, id)

	// Notify client with event
	if err := p.events.Emit(xaapiv1.EVTProjectDelete, *prj, fromSid); err != nil {
		p.Log.Warningf("Cannot notify project deletion: %v", err)
	}

	return *prj, err
}

// ForceSync Force the synchronization of a folder
func (p *Projects) ForceSync(id string) error {
	fc := p.Get(id)
	if fc == nil {
		return fmt.Errorf("Unknown id")
	}
	return (*fc).Sync()
}

// IsProjectInSync Returns true when folder is in sync
func (p *Projects) IsProjectInSync(id string) (bool, error) {
	fc := p.Get(id)
	if fc == nil {
		return false, fmt.Errorf("Unknown id")
	}
	return (*fc).IsInSync()
}

// Update Update some field of a project
func (p *Projects) Update(id string, prj xaapiv1.ProjectConfig, fromSid string) (*xaapiv1.ProjectConfig, error) {

	pjMutex.Lock()
	defer pjMutex.Unlock()

	fc, exist := p.projects[id]
	if !exist {
		return nil, fmt.Errorf("Unknown id")
	}

	// Copy current in a new object to change nothing in case of an error rises
	newFld := xaapiv1.ProjectConfig{}
	reflectme.Copy((*fc).GetProject(), &newFld)

	// Only update some fields
	dirty := false
	for _, fieldName := range xaapiv1.ProjectConfigUpdatableFields {
		valNew, err := reflectme.GetField(prj, fieldName)
		if err == nil {
			valCur, err := reflectme.GetField(newFld, fieldName)
			if err == nil && valNew != valCur {
				err = reflectme.SetField(&newFld, fieldName, valNew)
				if err != nil {
					return nil, err
				}
				dirty = true
			}
		}
	}

	if !dirty {
		return &newFld, nil
	}

	upPrj, err := (*fc).Update(newFld)
	if err != nil {
		return nil, err
	}

	// Notify client with event
	if err := p.events.Emit(xaapiv1.EVTProjectChange, *upPrj, fromSid); err != nil {
		p.Log.Warningf("Cannot notify project change: %v", err)
	}
	return upPrj, err
}