-
Notifications
You must be signed in to change notification settings - Fork 0
/
lxc-awa-agent.c
457 lines (361 loc) · 13.7 KB
/
lxc-awa-agent.c
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <awa/common.h>
#include <awa/client.h>
#include <lxc/lxccontainer.h>
#include "queue.h"
#include "LWM2M_Device_obj.h"
#include "lxc-obj-defs.h"
LIST_HEAD(container_list, container_info);
struct lxc_agent
{
AwaClientSession *session;
struct container_list *cl;
};
struct container_info
{
LIST_ENTRY(container_info) list;
struct lxc_container *c;
int instance;
char *name;
const char *status;
AwaClientExecuteSubscription *startSub;
AwaClientExecuteSubscription *stopSub;
AwaClientExecuteSubscription *destroySub;
struct lxc_agent *agent;
};
static void createCallback(const AwaExecuteArguments * arguments, void * context);
static void startCallback(const AwaExecuteArguments * arguments, void * context);
static void stopCallback(const AwaExecuteArguments * arguments, void * context);
static void destroyCallback(const AwaExecuteArguments * arguments, void * context);
static void createContainerInstance(struct lxc_agent *a, int instance, char *name);
static void destroyContainerInstance(struct container_info *ci);
static int createContainer(struct container_info *ci, char * name);
static int startContainer(struct lxc_container *c);
static int stopContainer(struct lxc_container *c);
static int destroyContainer(struct container_info *ci);
static char* instanceName(int instance)
{
char *buf;
int buflen = strlen(LXC_DEFAULT_NAME) + 10;
buf = calloc(1, buflen);
snprintf(buf, buflen, "%s-%d", LXC_DEFAULT_NAME, instance);
printf("InstanceName: %s\n", buf);
return buf;
}
static char *objectInstance(char *objId, int instance)
{
char *buf;
int buflen = strlen(objId) + 60;
buf = calloc(1, buflen);
snprintf(buf, buflen, "/%s/%d", objId, instance);
printf("objectInstance: %s\n", buf);
return buf;
}
static char *resourceInstance(char *objId, int instance, char* resId)
{
char *buf;
int buflen = strlen(objId) + strlen(resId) + 60;
buf = calloc(1, buflen);
snprintf(buf, buflen, "/%s/%d/%s", objId, instance, resId);
// printf("resourceInstance: %s\n", buf);
return buf;
}
static void CreateAgentInstance(AwaClientSession * session)
{
AwaClientSetOperation * operation = AwaClientSetOperation_New(session);
AwaClientSetOperation_CreateObjectInstance(operation, OBJECT_INSTANCE(LXC_AGENT_OBJID, 0));
AwaClientSetOperation_Perform(operation, OPERATION_PERFORM_TIMEOUT);
AwaClientSetOperation_Free(&operation);
}
static void updateContainerObjectsList(struct lxc_agent *a)
{
AwaClientSetOperation * operation = AwaClientSetOperation_New(a->session);
char *str;
struct container_info *ci;
struct lxc_container *c;
bool perform = false;
for (ci = a->cl->lh_first; ci != NULL; ci = ci->list.le_next)
{
c = ci->c;
if (c && c->is_defined(c))
{
ci->status = c->state(c);
AwaClientSetOperation_AddValueAsCString(operation, str = resourceInstance(LXC_OBJID, ci->instance, LXC_RESID_STATUS), ci->status);
free(str);
perform = true;
}
}
if (perform)
{
AwaClientSetOperation_Perform(operation, OPERATION_PERFORM_TIMEOUT);
}
AwaClientSetOperation_Free(&operation);
}
static char* getAppID(AwaClientSession *session)
{
char *str;
char *ret = NULL;
AwaClientGetOperation * operation = AwaClientGetOperation_New(session);
AwaClientGetOperation_AddPath(operation, str = resourceInstance(LXC_AGENT_OBJID, 0, LXCA_RESID_APPID));
AwaClientGetOperation_Perform(operation, OPERATION_PERFORM_TIMEOUT);
const AwaClientGetResponse * response = AwaClientGetOperation_GetResponse(operation);
if (AwaClientGetResponse_ContainsPath(response, str))
{
/* Test whether the response has a valid value for the specified path */
if (AwaClientGetResponse_HasValue(response, str))
{
/* Retrieve the value, as a C-style string */
const char * value;
AwaClientGetResponse_GetValueAsCStringPointer(response, str, &value);
ret = strdup(value);
}
}
AwaClientGetOperation_Free(&operation);
free(str);
return ret;
}
static void createContainerInstance(struct lxc_agent *a, int instance, char *name)
{
char *appId = NULL;
if (instance < LXC_MAX_INSTANCES)
{
AwaClientSetOperation * operation = AwaClientSetOperation_New(a->session);
struct container_info *ci;
char *str;
AwaClientSetOperation_CreateObjectInstance(operation, str = objectInstance(LXC_OBJID, instance));
free(str);
name = name ? name : instanceName(instance);
AwaClientSetOperation_AddValueAsCString(operation, str = resourceInstance(LXC_OBJID, instance, LXC_RESID_NAME), name);
free(str);
AwaClientSetOperation_Perform(operation, OPERATION_PERFORM_TIMEOUT);
AwaClientSetOperation_Free(&operation);
appId = getAppID(a->session);
printf("Got AppID: %s\n", appId);
free(appId);
ci = calloc(1, sizeof(struct container_info));
LIST_INSERT_HEAD(a->cl, ci, list);
createContainer(ci, name);
startContainer(ci->c);
ci->name = name;
ci->instance = instance;
ci->agent = a;
AwaClientExecuteSubscription *startSub = AwaClientExecuteSubscription_New(str = resourceInstance(LXC_OBJID, instance, LXC_RESID_START), startCallback, (void*)ci);
free(str);
AwaClientExecuteSubscription *stopSub = AwaClientExecuteSubscription_New(str = resourceInstance(LXC_OBJID, instance, LXC_RESID_STOP), stopCallback, (void*)ci);
free(str);
AwaClientExecuteSubscription *destroySub = AwaClientExecuteSubscription_New(str = resourceInstance(LXC_OBJID, instance, LXC_RESID_DESTROY), destroyCallback, (void*)ci);
free(str);
ci->startSub = startSub;
ci->stopSub = startSub;
ci->destroySub = destroySub;
/* Start listening to notifications */
AwaClientSubscribeOperation * subscribeOperation = AwaClientSubscribeOperation_New(a->session);
AwaClientSubscribeOperation_AddExecuteSubscription(subscribeOperation, startSub);
AwaClientSubscribeOperation_AddExecuteSubscription(subscribeOperation, stopSub);
AwaClientSubscribeOperation_AddExecuteSubscription(subscribeOperation, destroySub);
AwaClientSubscribeOperation_Perform(subscribeOperation, OPERATION_PERFORM_TIMEOUT);
AwaClientSubscribeOperation_Free(&subscribeOperation);
printf("Container instance: %d created\n", ci->instance);
}
}
static void destroyContainerInstance(struct container_info *ci)
{
struct lxc_container *c = ci->c;
if (c && !strncmp(c->state(c), "STOPPED", strlen(c->state(c))))
{
AwaClientSubscribeOperation *cancelSubscribeOperation = AwaClientSubscribeOperation_New(ci->agent->session);
AwaClientSubscribeOperation_AddCancelExecuteSubscription(cancelSubscribeOperation, ci->startSub);
AwaClientSubscribeOperation_AddCancelExecuteSubscription(cancelSubscribeOperation, ci->stopSub);
AwaClientSubscribeOperation_AddCancelExecuteSubscription(cancelSubscribeOperation, ci->destroySub);
AwaClientSubscribeOperation_Perform(cancelSubscribeOperation, OPERATION_PERFORM_TIMEOUT);
AwaClientSubscribeOperation_Free(&cancelSubscribeOperation);
// AwaClientExecuteSubscription_Free(&(ci->startSub));
// AwaClientExecuteSubscription_Free(&(ci->stopSub));
// AwaClientExecuteSubscription_Free(&(ci->destroySub));
AwaClientDeleteOperation *operation = AwaClientDeleteOperation_New(ci->agent->session);
AwaClientDeleteOperation_AddPath(operation, objectInstance(LXC_OBJID, ci->instance));
AwaClientDeleteOperation_Perform(operation, OPERATION_PERFORM_TIMEOUT);
AwaClientDeleteOperation_Free(&operation);
destroyContainer(ci);
LIST_REMOVE(ci, list);
printf("Container instance: %d destroyed\n", ci->instance);
}
else
{
printf("Container not stopped: %s\n", c->state(c));
}
}
static int createContainer(struct container_info *ci, char *name)
{
struct lxc_container *c;
int ret = 0;
/* Setup container struct */
c = lxc_container_new(name, NULL);
if (!c) {
fprintf(stderr, "Failed to setup lxc_container struct\n");
ret = 1;
}
ci->c = c;
if (c->is_defined(c)) {
fprintf(stderr, "Container already exists\n");
ret = 1;
}
#ifdef CI40_TEMPLATE
/* Create the container */
if (!c->createl(c, "template", NULL, NULL, LXC_CREATE_QUIET,
name, NULL)) {
fprintf(stderr, "Failed to create container rootfs\n");
ret = 1;
}
#else
/* Create the container */
if (!c->createl(c, "download", NULL, NULL, LXC_CREATE_QUIET,
"-d", "ubuntu", "-r", "trusty", "-a", "i386", NULL)) {
fprintf(stderr, "Failed to create container rootfs\n");
ret = 1;
}
#endif
if (!ret)
{
/* Query some information */
printf("Container state: %s\n", c->state(c));
}
return ret;
}
static int startContainer(struct lxc_container *c)
{
int ret = 0;
/* Start the container */
if (!c->start(c, 0, NULL)) {
fprintf(stderr, "Failed to start the container\n");
ret = 1;
}
if (!ret)
{
/* Query some information */
printf("Container state: %s\n", c->state(c));
printf("Container PID: %d\n", c->init_pid(c));
}
return ret;
}
static int stopContainer(struct lxc_container *c)
{
int ret = 0;
if (c)
{
/* Stop the container */
if (!c->shutdown(c, 30)) {
printf("Failed to cleanly shutdown the container, forcing.\n");
if (!c->stop(c)) {
fprintf(stderr, "Failed to kill the container.\n");
ret = 1;
}
}
/* Query some information */
printf("Container state: %s\n", c->state(c));
printf("Container PID: %d\n", c->init_pid(c));
}
return ret;
}
static int destroyContainer(struct container_info *ci)
{
int ret = 0;
if (ci->c)
{
/* Destroy the container */
if (!ci->c->destroy(ci->c)) {
fprintf(stderr, "Failed to destroy the container.\n");
ret = 1;
}
else
{
lxc_container_put(ci->c);
ci->c = NULL;
}
}
return ret;
}
static void createCallback(const AwaExecuteArguments * arguments, void * context)
{
struct lxc_agent *a = (struct lxc_agent*)context;
static int instance = 0;
printf("Create Callback received. Context = %p\n", a);
createContainerInstance(a, instance, NULL);
instance++;
printf("Resource executed [%zu bytes payload]\n", arguments->Size);
}
static void startCallback(const AwaExecuteArguments * arguments, void * context)
{
struct container_info *ci = (struct container_info*) context;
printf("Start Callback received. Context = %p\n", ci);
startContainer(ci->c);
printf("Resource executed [%zu bytes payload]\n", arguments->Size);
}
static void stopCallback(const AwaExecuteArguments * arguments, void * context)
{
struct container_info *ci = (struct container_info*) context;
printf("Stop Callback received. Context = %p\n", ci);
stopContainer(ci->c);
printf("Resource executed [%zu bytes payload]\n", arguments->Size);
}
static void destroyCallback(const AwaExecuteArguments * arguments, void * context)
{
struct container_info *ci = (struct container_info*) context;
printf("Destroy Callback received. Context = %p\n", ci);
destroyContainerInstance(ci);
printf("Resource executed [%zu bytes payload]\n", arguments->Size);
}
static bool stopFlag = false;
/*
* Trigger the program to exit on CTRL-C
*/
static void stop(int ignore)
{
printf("Exiting...\n");
stopFlag = true;
}
int main(void)
{
signal(SIGINT, stop);
AwaClientSession * session = AwaClientSession_New();
AwaClientSession_Connect(session);
DefineLxcAgentClientObject(session);
CreateAgentInstance(session);
DefineLxcClientObject(session);
InitDevice(session);
/* Application-specific data */
struct lxc_agent agent;
struct container_list cl_head;
LIST_INIT(&cl_head);
agent.cl = &cl_head;
agent.session = session;
AwaClientExecuteSubscription * createSub = AwaClientExecuteSubscription_New(RESOURCE_INSTANCE(LXC_AGENT_OBJID, 0, LXCA_RESID_CREATE), createCallback, (void*)&agent);
/* Start listening to notifications */
AwaClientSubscribeOperation * subscribeOperation = AwaClientSubscribeOperation_New(session);
AwaClientSubscribeOperation_AddExecuteSubscription(subscribeOperation, createSub);
AwaClientSubscribeOperation_Perform(subscribeOperation, OPERATION_PERFORM_TIMEOUT);
AwaClientSubscribeOperation_Free(&subscribeOperation);
printf("Waiting for events...\n");
while (!stopFlag)
{
sleep(1);
/* Receive notifications */
AwaClientSession_Process(session, OPERATION_PERFORM_TIMEOUT);
AwaClientSession_DispatchCallbacks(session);
updateContainerObjectsList(&agent);
// DeviceControl(session);
}
AwaClientSubscribeOperation * cancelSubscribeOperation = AwaClientSubscribeOperation_New(session);
AwaClientSubscribeOperation_AddCancelExecuteSubscription(cancelSubscribeOperation, createSub);
AwaClientSubscribeOperation_Perform(cancelSubscribeOperation, OPERATION_PERFORM_TIMEOUT);
AwaClientSubscribeOperation_Free(&cancelSubscribeOperation);
/* Free the execute subscription */
AwaClientExecuteSubscription_Free(&createSub);
AwaClientSession_Disconnect(session);
AwaClientSession_Free(&session);
return 0;
}