ams.go 14 KB
Newer Older
Stefan Dähling's avatar
Stefan Dähling committed
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
/*
Copyright 2020 Institute for Automation of Complex Power Systems,
E.ON Energy Research Center, RWTH Aachen University

This project is licensed under either of
- Apache License, Version 2.0
- MIT License
at your option.

Apache License, Version 2.0:

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.

MIT License:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

// Package ams provides functionality for the Agent Management System. It provides an API for user
// interaction as well as for other MAP components. It also takes care of interacting with the
// underlying cluster (local or Kubernetes) and stores MAP related information (local or etcd)
package ams

import (
	"errors"
	"io/ioutil"
	"log"
	"net/http"
	"os"
56
	"strconv"
Stefan Dähling's avatar
Stefan Dähling committed
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
	"time"

	agcli "git.rwth-aachen.de/acs/public/cloud/mas/clonemap/pkg/agency/client"
	dfcli "git.rwth-aachen.de/acs/public/cloud/mas/clonemap/pkg/df/client"
	"git.rwth-aachen.de/acs/public/cloud/mas/clonemap/pkg/schemas"
)

// AMS contains storage and deployment object
type AMS struct {
	stor     storage     // interface for local or distributed storage
	depl     deployment  // interface for local or cloud deployment
	logInfo  *log.Logger // logger for info logging
	logError *log.Logger // logger for error logging
}

// StartAMS starts an AMS instance. It initializes the cluster and storage object and starts API
// server.
func StartAMS() (err error) {
	ams := &AMS{logError: log.New(os.Stderr, "[ERROR] ", log.LstdFlags)}
	// create storage and deployment object according to specified deployment type
	err = ams.init()
	if err != nil {
		return
	}
	cmap := schemas.CloneMAP{
		Version: "v0.1",
		Uptime:  time.Now(),
	}
	ams.stor.setCloneMAPInfo(cmap)
	// start to listen and serve requests
	err = ams.listen()
	return
}

// init initializes deployment and storage. The deployment type is read from an environment
92
// variable.
Stefan Dähling's avatar
Stefan Dähling committed
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
func (ams *AMS) init() (err error) {
	//fmt.Println("Getting deployment type")
	// ams.addrSuffix = os.Getenv("CLONEMAP_SUFFIX")
	logType := os.Getenv("CLONEMAP_LOG_LEVEL")
	switch logType {
	case "info":
		ams.logInfo = log.New(os.Stdout, "[INFO] ", log.LstdFlags)
	case "error":
		ams.logInfo = log.New(ioutil.Discard, "", log.LstdFlags)
	default:
		err = errors.New("Wrong log type: " + logType)
		return
	}
	ams.logInfo.Println("Starting AMS")

	deplType := os.Getenv("CLONEMAP_DEPLOYMENT_TYPE")
	switch deplType {
	case "local":
		ams.logInfo.Println("Local deployment")
		ams.depl, err = newLocalDeployment()
	case "minikube":
		ams.logInfo.Println("Kubernetes deployment")
		ams.depl, err = newKubeDeployment(deplType)
	case "production":
		ams.logInfo.Println("Kubernetes deployment")
		ams.depl, err = newKubeDeployment(deplType)
	default:
		err = errors.New("Wrong deployment type: " + deplType)
		return
	}
	storType := os.Getenv("CLONEMAP_STORAGE_TYPE")
	switch storType {
	case "local":
		ams.logInfo.Println("Local storage")
		ams.stor = newLocalStorage()
	case "etcd":
		if deplType == "local" {
			err = errors.New("etcd storage can not be used with local deployment")
			return
		}
		ams.logInfo.Println("ectd storage")
		ams.stor, err = newEtcdStorage(ams.logError)
135
136
137
	case "fiware":
		ams.logInfo.Println("FiWare storage")
		ams.stor, err = newFiwareStorage(ams.logError)
Stefan Dähling's avatar
Stefan Dähling committed
138
139
140
141
142
143
144
145
146
147
148
149
	default:
		err = errors.New("Wrong storage type: " + storType)
	}
	return
}

// getCloneMAPInfo returns info about clonemap
func (ams *AMS) getCloneMAPInfo() (ret schemas.CloneMAP, err error) {
	ret, err = ams.stor.getCloneMAPInfo()
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
150
151
152
153
154
155
// getMASsShort returns specs of all MAS
func (ams *AMS) getMASsShort() (ret []schemas.MASInfoShort, err error) {
	ret, err = ams.stor.getMASsShort()
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
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
// getMASs returns specs of all MAS
func (ams *AMS) getMASs() (ret schemas.MASs, err error) {
	ret, err = ams.stor.getMASs()
	return
}

// getMASInfo returns info of one MAS
func (ams *AMS) getMASInfo(masID int) (ret schemas.MASInfo, err error) {
	ret, err = ams.stor.getMASInfo(masID)
	return
}

// getAgents returns specs of all agents in MAS
func (ams *AMS) getAgents(masID int) (ret schemas.Agents, err error) {
	ret, err = ams.stor.getAgents(masID)
	return
}

// getAgentInfo returns info of one or multiple agents
func (ams *AMS) getAgentInfo(masID int, agentID int) (ret schemas.AgentInfo, err error) {
	ret, err = ams.stor.getAgentInfo(masID, agentID)
	return
}

// getAgentAddress returns address of one agent
func (ams *AMS) getAgentAddress(masID int, agentID int) (ret schemas.Address, err error) {
	ret, err = ams.stor.getAgentAddress(masID, agentID)
	return
}

// updateAgentAddress sets address of agent
func (ams *AMS) updateAgentAddress(masID int, agentID int, address schemas.Address) (err error) {
	err = ams.stor.setAgentAddress(masID, agentID, address)
	return
}

192
193
194
195
196
197
198
199
200
201
// updateAgentCustom sets custom config of agent and sends PUT to agency
func (ams *AMS) updateAgentCustom(masID int, agentID int, custom string) (err error) {
	err = ams.stor.setAgentCustom(masID, agentID, custom)
	if err != nil {
		return
	}
	// ToDo Post new custom to agency
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
202
203
204
205
206
207
// getAgencies returns specs of all agencies in MAS
func (ams *AMS) getAgencies(masID int) (ret schemas.Agencies, err error) {
	ret, err = ams.stor.getAgencies(masID)
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
208
// getAgencyInfoFull returns status of one agency
Stefan Dähling's avatar
Stefan Dähling committed
209
210
211
func (ams *AMS) getAgencyInfoFull(masID int, imID int, agencyID int) (ret schemas.AgencyInfoFull,
	err error) {
	ret, err = ams.stor.getAgencyInfoFull(masID, imID, agencyID)
212
213
214
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
215
// createMAS creates a new mas according to masconfig
Stefan Dähling's avatar
Stefan Dähling committed
216
func (ams *AMS) createMAS(masSpec schemas.MASSpec) (err error) {
Stefan Dähling's avatar
Stefan Dähling committed
217
	// fill masInfo
Stefan Dähling's avatar
Stefan Dähling committed
218
	var masInfo schemas.MASInfo
219
	var numAgencies []int
Stefan Dähling's avatar
Stefan Dähling committed
220
221
222
223
	masInfo, numAgencies, err = ams.configureMAS(masSpec)
	if err != nil {
		return
	}
Stefan Dähling's avatar
Stefan Dähling committed
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238

	// safe mas in storage and get ID
	var masID int
	masID, err = ams.stor.registerMAS()
	ams.logInfo.Println("Create new MAS with ID ", masID)
	if err != nil {
		return
	}

	go ams.startMAS(masID, masInfo, numAgencies)

	return
}

// startMAS starts the MAS
239
func (ams *AMS) startMAS(masID int, masInfo schemas.MASInfo, numAgencies []int) (err error) {
Stefan Dähling's avatar
Stefan Dähling committed
240
241
242
243
244
245
246
	err = ams.stor.storeMAS(masID, masInfo)
	if err != nil {
		ams.logError.Println(err.Error())
		return
	}
	ams.logInfo.Println("Stored MAS data")
	if os.Getenv("CLONEMAP_DEPLOYMENT_TYPE") == "local" {
Stefan Dähling's avatar
Stefan Dähling committed
247
		_, err = dfcli.PostGraph(masID, masInfo.Graph)
Stefan Dähling's avatar
Stefan Dähling committed
248
		if err != nil {
Stefan Dähling's avatar
Stefan Dähling committed
249
250
251
			ams.logInfo.Println(err.Error())
			// return
			err = nil
Stefan Dähling's avatar
Stefan Dähling committed
252
253
254
255
		}
	}

	// deploy containers
256
	err = ams.depl.newMAS(masID, masInfo.ImageGroups, masInfo.Config.Logging,
Stefan Dähling's avatar
Stefan Dähling committed
257
		masInfo.Config.MQTT, masInfo.Config.DF)
Stefan Dähling's avatar
Stefan Dähling committed
258
259
260
261
262
263
264
265
266
267
	if err != nil {
		ams.logError.Println(err.Error())
		return
	}
	ams.logInfo.Println("Started agencies")

	return
}

// configureMAS fills the missing configuration as agencies, agent ids and addresses
268
269
func (ams *AMS) configureMAS(masSpec schemas.MASSpec) (masInfo schemas.MASInfo,
	numAgencies []int, err error) {
Stefan Dähling's avatar
Stefan Dähling committed
270
	// extract all image groups
Stefan Dähling's avatar
Stefan Dähling committed
271
272
	imageTemp := make(map[string]interface{})
	for i := range masSpec.ImageGroups {
Stefan Dähling's avatar
Stefan Dähling committed
273
		if _, ok := imageTemp[masSpec.ImageGroups[i].Config.Image]; ok {
Stefan Dähling's avatar
Stefan Dähling committed
274
275
276
277
278
			// image already exists
			err = errors.New("invalid masSpec; two or more groups with same image")
			return
		}
		imGroupInfo := schemas.ImageGroupInfo{
Stefan Dähling's avatar
Stefan Dähling committed
279
280
			Config: masSpec.ImageGroups[i].Config,
			ID:     i,
Stefan Dähling's avatar
Stefan Dähling committed
281
		}
Stefan Dähling's avatar
Stefan Dähling committed
282
		masInfo.ImageGroups.Inst = append(masInfo.ImageGroups.Inst, imGroupInfo)
Stefan Dähling's avatar
Stefan Dähling committed
283
284
		masInfo.ImageGroups.Counter++
		imageTemp[masSpec.ImageGroups[i].Config.Image] = nil
Stefan Dähling's avatar
Stefan Dähling committed
285
286
	}

Stefan Dähling's avatar
Stefan Dähling committed
287
	// MAS configuration
Stefan Dähling's avatar
Stefan Dähling committed
288
289
290
291
292
	masInfo.Config = masSpec.Config
	masInfo.Graph = masSpec.Graph

	// total number of agents and total number of agencies
	masInfo.Agents.Counter = 0
Stefan Dähling's avatar
fixes    
Stefan Dähling committed
293
	numAgencies = make([]int, masInfo.ImageGroups.Counter, masInfo.ImageGroups.Counter)
Stefan Dähling's avatar
Stefan Dähling committed
294
295
	for i := range masSpec.ImageGroups {
		masInfo.Agents.Counter += len(masSpec.ImageGroups[i].Agents)
Stefan Dähling's avatar
Stefan Dähling committed
296
		num := len(masSpec.ImageGroups[i].Agents) / masSpec.Config.NumAgentsPerAgency
Stefan Dähling's avatar
Stefan Dähling committed
297
		if len(masSpec.ImageGroups[i].Agents)%masSpec.Config.NumAgentsPerAgency > 0 {
Stefan Dähling's avatar
Stefan Dähling committed
298
			num++
Stefan Dähling's avatar
Stefan Dähling committed
299
		}
Stefan Dähling's avatar
Stefan Dähling committed
300
		masInfo.ImageGroups.Inst[i].Agencies.Inst = make([]schemas.AgencyInfo, num,
Stefan Dähling's avatar
Stefan Dähling committed
301
			num)
Stefan Dähling's avatar
Stefan Dähling committed
302
		masInfo.ImageGroups.Inst[i].Agencies.Counter = num
Stefan Dähling's avatar
Stefan Dähling committed
303
		numAgencies[i] = num
Stefan Dähling's avatar
Stefan Dähling committed
304
	}
Stefan Dähling's avatar
Stefan Dähling committed
305
	masInfo.Agents.Inst = make([]schemas.AgentInfo, masInfo.Agents.Counter,
Stefan Dähling's avatar
Stefan Dähling committed
306
		masInfo.Agents.Counter)
Stefan Dähling's avatar
Stefan Dähling committed
307
308
309
310

	// empty graph?
	if len(masInfo.Graph.Node) == 0 {
		masInfo.Graph.Node = append(masInfo.Graph.Node, schemas.Node{ID: 0})
Stefan Dähling's avatar
Stefan Dähling committed
311
312
313
	}

	// agent configuration
Stefan Dähling's avatar
Stefan Dähling committed
314
315
316
	agentID := 0
	for i := range masSpec.ImageGroups {
		for j := range masSpec.ImageGroups[i].Agents {
Stefan Dähling's avatar
Stefan Dähling committed
317
318
319
320
321
			masInfo.Agents.Inst[agentID].Spec = masSpec.ImageGroups[i].Agents[j]
			masInfo.Agents.Inst[agentID].ID = agentID
			masInfo.Agents.Inst[agentID].AgencyID = j / masSpec.Config.NumAgentsPerAgency
			masInfo.Agents.Inst[agentID].ImageGroupID = i
			masInfo.Agents.Inst[agentID].Address.Agency = "-im-" + strconv.Itoa(i) + "-agency-" +
322
				strconv.Itoa(j/masSpec.Config.NumAgentsPerAgency)
Stefan Dähling's avatar
Stefan Dähling committed
323
			for j := range masInfo.Graph.Node {
Stefan Dähling's avatar
Stefan Dähling committed
324
				if masInfo.Graph.Node[j].ID == masInfo.Agents.Inst[i].Spec.NodeID {
Stefan Dähling's avatar
Stefan Dähling committed
325
					masInfo.Graph.Node[j].Agent = append(masInfo.Graph.Node[j].Agent,
Stefan Dähling's avatar
Stefan Dähling committed
326
						masInfo.Agents.Inst[i].ID)
Stefan Dähling's avatar
Stefan Dähling committed
327
328
					break
				}
Stefan Dähling's avatar
Stefan Dähling committed
329
			}
Stefan Dähling's avatar
Stefan Dähling committed
330
			agentID++
Stefan Dähling's avatar
Stefan Dähling committed
331
332
333
334
		}
	}

	// agency configuration
Stefan Dähling's avatar
Stefan Dähling committed
335
336
337
338
339
340
	agentCounterTot := 0
	for i := range masSpec.ImageGroups {
		agentCounter := 0
		for j := 0; j < numAgencies[i]; j++ {
			agencyInfo := schemas.AgencyInfo{
				ImageGroupID: i,
Stefan Dähling's avatar
Stefan Dähling committed
341
				ID:           j,
Stefan Dähling's avatar
Stefan Dähling committed
342
				Logger:       masSpec.Config.Logger,
343
				Name:         "-im-" + strconv.Itoa(i) + "-agency-" + strconv.Itoa(j),
Stefan Dähling's avatar
Stefan Dähling committed
344
345
346
347
348
349
350
351
			}
			for k := 0; k < masSpec.Config.NumAgentsPerAgency; k++ {
				if agentCounter >= len(masSpec.ImageGroups[i].Agents) {
					break
				}
				agencyInfo.Agents = append(agencyInfo.Agents, agentCounterTot)
				agentCounter++
				agentCounterTot++
Stefan Dähling's avatar
Stefan Dähling committed
352
			}
Stefan Dähling's avatar
Stefan Dähling committed
353
			masInfo.ImageGroups.Inst[i].Agencies.Inst[j] = agencyInfo
Stefan Dähling's avatar
Stefan Dähling committed
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
		}
	}
	return
}

// removeMAS removes specified mas if it exists
func (ams *AMS) removeMAS(masID int) (err error) {
	err = ams.depl.deleteMAS(masID)
	if err != nil {
		return
	}
	err = ams.stor.deleteMAS(masID)
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
369
370
// createAgents creates new agents and adds them to an existing mas
func (ams *AMS) createAgents(masID int, groupSpecs []schemas.ImageGroupSpec) (err error) {
371
372
373
374
375
376
377
	for i := range groupSpecs {
		var newGroup bool
		var imID int
		newGroup, imID, err = ams.stor.registerImageGroup(masID, groupSpecs[i].Config)
		if err != nil {
			return
		}
Stefan Dähling's avatar
Stefan Dähling committed
378
		var newAgencies []int
379
380
381
		for j := range groupSpecs[i].Agents {
			var newAgency bool
			var agentID int
Stefan Dähling's avatar
Stefan Dähling committed
382
383
			var agencyID int
			newAgency, agentID, agencyID, err = ams.stor.addAgent(masID, imID,
384
385
386
387
388
				groupSpecs[i].Agents[j])
			if err != nil {
				return
			}
			if newGroup {
Stefan Dähling's avatar
Stefan Dähling committed
389
390
				// continue if group is new group
				continue
391
			} else if newAgency {
Stefan Dähling's avatar
Stefan Dähling committed
392
				newAgencies = append(newAgencies, agencyID)
393
			} else {
Stefan Dähling's avatar
Stefan Dähling committed
394
				// post agent to running agency if agency is not new
Stefan Dähling's avatar
fix    
Stefan Dähling committed
395
396
				var agentInfo schemas.AgentInfo
				agentInfo, err = ams.stor.getAgentInfo(masID, agentID)
397
398
399
				if err != nil {
					return
				}
Stefan Dähling's avatar
Stefan Dähling committed
400
401
402
403
404
405
406
407
408
				for k := range newAgencies {
					if agentInfo.AgencyID == newAgencies[k] {
						newAgency = true
					}
				}
				if newAgency {
					// continue if agency is new agency
					continue
				}
409
				err = ams.postAgentToAgency(agentInfo)
Stefan Dähling's avatar
Stefan Dähling committed
410
411
412
413
414
415
416
417
418
419
420
				if err != nil {
					return
				}
			}
		}
		if newGroup {
			var groupInfo schemas.ImageGroupInfo
			groupInfo, err = ams.stor.getGroupInfo(masID, imID)
			if err != nil {
				return
			}
421
422
423
424
425
426
427
			var masInfo schemas.MASInfo
			masInfo, err = ams.stor.getMASInfo(masID)
			if err != nil {
				return
			}
			err = ams.depl.newImageGroup(masID, groupInfo, masInfo.Config.Logging,
				masInfo.Config.MQTT, masInfo.Config.DF)
Stefan Dähling's avatar
Stefan Dähling committed
428
429
430
431
432
433
434
435
			if err != nil {
				return
			}
		} else {
			numNewAgencies := len(newAgencies)
			err = ams.depl.scaleImageGroup(masID, imID, numNewAgencies)
			if err != nil {
				return
436
437
438
			}
		}
	}
Stefan Dähling's avatar
Stefan Dähling committed
439
440
441
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
442
443
444
445
446
// createAgent creates a new agent and adds it to an existing mas
func (ams *AMS) createAgent(masID int, agentSpec schemas.AgentSpec) (err error) {
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
447
448
// removeAgent removes an agent from the MAS
func (ams *AMS) removeAgent(masID int, agentID int) (err error) {
Stefan Dähling's avatar
Stefan Dähling committed
449
450
451
452
453
	var addr schemas.Address
	addr, err = ams.stor.getAgentAddress(masID, agentID)
	if err != nil {
		return
	}
Stefan Dähling's avatar
Stefan Dähling committed
454
455
456
457
	err = ams.stor.deleteAgent(masID, agentID)
	if err != nil {
		return
	}
Stefan Dähling's avatar
Stefan Dähling committed
458
459
	_, err = agcli.DeleteAgent(addr.Agency, agentID)

Stefan Dähling's avatar
Stefan Dähling committed
460
461
462
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// postAgentToAgency sends a post request to agency with info about agent to start
func (ams *AMS) postAgentToAgency(agentInfo schemas.AgentInfo) (err error) {
	var httpStatus int
	httpStatus, err = agcli.PostAgent(agentInfo.Address.Agency, agentInfo)
	if err != nil {
		return
	}
	if httpStatus == http.StatusCreated {
		// stat := schemas.Status{
		// 	Code:       status.Starting,
		// 	LastUpdate: time.Now(),
		// }
		// ams.updateAgentStatus(agentInfo.Spec.MASID, agentInfo.Spec.ID, stat)
	} else {
		err = errors.New("error posting to agency")
	}
	return
}