kubestub.go 6.72 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
56
57
58
59
60
61
62
63
64
65
/*
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 kubestub simulates the behavior of the Kubernetes API for local execution of clonemap
// It provides an API to be used by the MAS. Functionalities are: Startup of AMS at beginning,
// start of agencies and proper termination of all MAP-parts
package kubestub

import (
	"fmt"
	"os"
	"os/signal"
	"strconv"
	"syscall"

	"git.rwth-aachen.de/acs/public/cloud/mas/clonemap/pkg/schemas"
)

// LocalStub holds context information such as a list of all started agencies
type LocalStub struct {
	// address of the local stub
	address string
	// list of all agencies; necessary in order to prevent from starting agencies with same name
	// and to stop all agencies upon termination
Stefan Dähling's avatar
Stefan Dähling committed
66
67
68
69
70
71
72
73
74
	agencies     []schemas.StubAgencyConfig
	startModules bool
	fiware       bool
	mqtt         bool
	logger       bool
	df           bool
	pnp          bool
	frontend     bool
	logLevel     string
Stefan Dähling's avatar
Stefan Dähling committed
75
76
77
78
}

// StartLocalStub starts the local stub. The AMS is started and a server for AMS interaction is
// created
79
func StartLocalStub() {
Stefan Dähling's avatar
Stefan Dähling committed
80
81
	var err error
	// initialization
82
	cntxt := &LocalStub{}
Stefan Dähling's avatar
Stefan Dähling committed
83
	_, cntxt.startModules = os.LookupEnv("CLONEMAP_START_MODULES")
84
85
86
87
88
89
	_, cntxt.mqtt = os.LookupEnv("CLONEMAP_MODULE_MQTT")
	_, cntxt.fiware = os.LookupEnv("CLONEMAP_MODULE_FIWARE")
	_, cntxt.logger = os.LookupEnv("CLONEMAP_MODULE_LOGGER")
	_, cntxt.df = os.LookupEnv("CLONEMAP_MODULE_DF")
	_, cntxt.pnp = os.LookupEnv("CLONEMAP_MODULE_PNP")
	_, cntxt.frontend = os.LookupEnv("CLONEMAP_MODULE_FRONTEND")
Stefan Dähling's avatar
Stefan Dähling committed
90
91
92
93
	cntxt.logLevel, _ = os.LookupEnv("CLONEMAP_LOG_LEVEL")
	if cntxt.logLevel == "" {
		cntxt.logLevel = "error"
	}
94

Stefan Dähling's avatar
Stefan Dähling committed
95
96
97
	if cntxt.startModules {
		fmt.Println("Create Bridge Network")
		err = cntxt.createBridge()
98
99
100
101
		if err != nil {
			fmt.Println(err)
			return
		}
Stefan Dähling's avatar
Stefan Dähling committed
102
103
		fmt.Println("Create AMS Container")
		err = cntxt.createAMS()
104
105
106
107
		if err != nil {
			fmt.Println(err)
			return
		}
Stefan Dähling's avatar
Stefan Dähling committed
108
109
110
111
112
113
114
		if cntxt.mqtt || cntxt.fiware {
			fmt.Println("Create MQTT Broker Container")
			err = cntxt.createMQTT()
			if err != nil {
				fmt.Println(err)
				return
			}
115
		}
Stefan Dähling's avatar
Stefan Dähling committed
116
117
118
119
120
121
122
		if cntxt.fiware {
			fmt.Println("Ceate Fiware Containers")
			err = cntxt.createFiware()
			if err != nil {
				fmt.Println(err)
				return
			}
123
		}
Stefan Dähling's avatar
Stefan Dähling committed
124
125
126
127
128
129
130
		if cntxt.logger {
			fmt.Println("Create Logger Container")
			err = cntxt.createLogger()
			if err != nil {
				fmt.Println(err)
				return
			}
131
		}
Stefan Dähling's avatar
Stefan Dähling committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
		if cntxt.df {
			fmt.Println("Create DF Container")
			err = cntxt.createDF()
			if err != nil {
				fmt.Println(err)
				return
			}
		}
		if cntxt.pnp {
			fmt.Println("Create Plugnplay Container")
			err = cntxt.createPnP()
			if err != nil {
				fmt.Println(err)
				return
			}
		}
		if cntxt.frontend {
			fmt.Println("Create Frontend Container")
			err = cntxt.createFrontend()
			if err != nil {
				fmt.Println(err)
				return
			}
155
		}
156
	}
Stefan Dähling's avatar
Stefan Dähling committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
	fmt.Println("Ready")

	// catch kill signal in order to terminate MAP parts before exiting
	var gracefulStop = make(chan os.Signal)
	signal.Notify(gracefulStop, syscall.SIGTERM)
	signal.Notify(gracefulStop, syscall.SIGINT)
	go cntxt.terminate(gracefulStop)

	// start API server
	err = cntxt.listen()
	if err != nil {
		fmt.Println(err)
	}
}

// terminate takes care of terminating all parts of the MAP before exiting. It is to be called as a
// goroutine and waits until an OS signal is inserted into the channel gracefulStop
func (stub *LocalStub) terminate(gracefulStop chan os.Signal) {
	var err error
	sig := <-gracefulStop
	fmt.Printf("Caught sig: %+v\n", sig)
	for i := range stub.agencies {
Stefan Dähling's avatar
fixes    
Stefan Dähling committed
179
180
		agencyName := "mas-" + strconv.Itoa(stub.agencies[i].MASID) + "-im-" +
			strconv.Itoa(stub.agencies[i].ImageGroupID) + "-agency-" +
Stefan Dähling's avatar
Stefan Dähling committed
181
182
			strconv.Itoa(stub.agencies[i].AgencyID)
		fmt.Println("Stop Agency Container " + agencyName)
Stefan Dähling's avatar
fixes    
Stefan Dähling committed
183
184
		err = stub.deleteAgency(stub.agencies[i].MASID, stub.agencies[i].ImageGroupID,
			stub.agencies[i].AgencyID)
Stefan Dähling's avatar
Stefan Dähling committed
185
186
187
188
189
		if err != nil {
			fmt.Println(err)
			// os.Exit(0)
		}
	}
Stefan Dähling's avatar
Stefan Dähling committed
190
191
192
193

	if stub.startModules {
		fmt.Println("Stop AMS Container")
		err = stub.deleteAMS()
194
195
196
197
		if err != nil {
			fmt.Println(err)
			os.Exit(0)
		}
Stefan Dähling's avatar
Stefan Dähling committed
198
199
200
201
202
203
204
		if stub.logger {
			fmt.Println("Stop Logger Container")
			err = stub.deleteLogger()
			if err != nil {
				fmt.Println(err)
				os.Exit(0)
			}
205
		}
Stefan Dähling's avatar
Stefan Dähling committed
206
207
208
209
210
211
212
		if stub.df {
			fmt.Println("Stop DF Container")
			err = stub.deleteDF()
			if err != nil {
				fmt.Println(err)
				os.Exit(0)
			}
213
		}
Stefan Dähling's avatar
Stefan Dähling committed
214
215
216
217
218
219
220
		if stub.pnp {
			fmt.Println("Stop Plugnplay Container")
			err = stub.deletePnP()
			if err != nil {
				fmt.Println(err)
				os.Exit(0)
			}
221
		}
Stefan Dähling's avatar
Stefan Dähling committed
222
223
224
225
226
227
228
		if stub.frontend {
			fmt.Println("Stop Frontend Container")
			err = stub.deleteFrontend()
			if err != nil {
				fmt.Println(err)
				os.Exit(0)
			}
229
		}
Stefan Dähling's avatar
Stefan Dähling committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
		if stub.mqtt || stub.fiware {
			fmt.Println("Stop MQTT Broker Container")
			err = stub.deleteMQTT()
			if err != nil {
				fmt.Println(err)
				os.Exit(0)
			}
		}
		if stub.fiware {
			fmt.Println("Stop FIWARE Containers")
			err = stub.deleteFiware()
			if err != nil {
				fmt.Println(err)
				os.Exit(0)
			}
		}

		fmt.Println("Delete Bridge Network")
		err = stub.deleteBridge()
249
250
251
252
		if err != nil {
			fmt.Println(err)
		}
	}
Stefan Dähling's avatar
Stefan Dähling committed
253
254
	os.Exit(0)
}