~ccheney/ubuntu/lucid/eucalyptus/lucid-sru

« back to all changes in this revision

Viewing changes to cluster/handlers.c

  • Committer: Bazaar Package Importer
  • Author(s): Soren Hansen
  • Date: 2009-02-11 02:45:39 UTC
  • Revision ID: james.westby@ubuntu.com-20090211024539-0jhzbpg3hk6nu1yg
Tags: upstream-1.5~bzr139
ImportĀ upstreamĀ versionĀ 1.5~bzr139

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <sys/types.h>
 
4
#include <unistd.h>
 
5
#include <sys/wait.h>
 
6
#include <sys/mman.h>
 
7
#include <semaphore.h>
 
8
 
 
9
#include "axis2_skel_EucalyptusCC.h"
 
10
 
 
11
#include <server-marshal.h>
 
12
#include <handlers.h>
 
13
#include <storage.h>
 
14
#include <vnetwork.h>
 
15
#include <euca_auth.h>
 
16
#include <misc.h>
 
17
 
 
18
#include "data.h"
 
19
#include "client-marshal.h"
 
20
 
 
21
#define SUPERUSER "eucalyptus"
 
22
 
 
23
int init=0;
 
24
sem_t *initLock=NULL;
 
25
 
 
26
// to be stored in shared memory
 
27
ccConfig *config=NULL;
 
28
sem_t *configLock=NULL;
 
29
 
 
30
ccInstance *instanceCache=NULL;
 
31
sem_t *instanceCacheLock=NULL;
 
32
 
 
33
vnetConfig *vnetconfig=NULL;
 
34
sem_t *vnetConfigLock=NULL;
 
35
 
 
36
int doAttachVolume(ncMetadata *ccMeta, char *volumeId, char *instanceId, char *remoteDev, char *localDev) {
 
37
  int i, j, rc, start, stop, k, done, ret=0;
 
38
  ccInstance *myInstance, *out;
 
39
  ncStub *ncs;
 
40
  time_t op_start, op_timer;
 
41
  
 
42
  i = j = 0;
 
43
  myInstance = NULL;
 
44
  op_start = time(NULL);
 
45
  op_timer = OP_TIMEOUT;
 
46
  
 
47
  rc = init_config();
 
48
  if (rc) {
 
49
    return(1);
 
50
  }
 
51
  logprintfl(EUCADEBUG,"AttachVolume(): called\n");
 
52
  
 
53
  rc = find_instanceCacheId(instanceId, &myInstance);
 
54
  if (!rc) {
 
55
    // found the instance in the cache
 
56
    start = myInstance->ncHostIdx;
 
57
    stop = start+1;
 
58
    if (myInstance) free(myInstance);
 
59
  } else {
 
60
    start = 0;
 
61
    stop = config->numResources;
 
62
  }
 
63
  
 
64
  sem_wait(configLock);
 
65
  for (j=start; j<stop; j++) {
 
66
    // read the instance ids
 
67
    logprintfl(EUCAINFO,"AttachVolume(): calling attach volume (%s) on (%s)\n", instanceId, config->resourcePool[j].hostname);
 
68
    if (1) {
 
69
      int pid, status;
 
70
      pid = fork();
 
71
      if (pid == 0) {
 
72
        ncs = ncStubCreate(config->resourcePool[j].ncService, NULL, NULL);
 
73
        if (config->use_wssec) {
 
74
          rc = InitWSSEC(ncs->env, ncs->stub, config->policyFile);
 
75
        }
 
76
        logprintfl(EUCADEBUG, "would call attachVol on NC: %s\n",  config->resourcePool[j].hostname);
 
77
        rc = 0;
 
78
        // here
 
79
        rc = ncAttachVolumeStub(ncs, ccMeta, instanceId, volumeId, remoteDev, localDev);
 
80
        if (!rc) {
 
81
          ret = 0;
 
82
        } else {
 
83
          ret = 1;
 
84
        }
 
85
        exit(ret);
 
86
      } else {
 
87
        op_timer = OP_TIMEOUT - (time(NULL) - op_start);
 
88
        rc = timewait(pid, &status, op_timer / ((stop-start) - (j - start)));
 
89
        rc = WEXITSTATUS(status);
 
90
        logprintfl(EUCADEBUG,"\tcall complete (pid/rc): %d/%d\n", pid, rc);
 
91
      }
 
92
    }
 
93
    sem_post(configLock);
 
94
    
 
95
    if (!rc) {
 
96
      ret = 0;
 
97
    } else {
 
98
      logprintfl(EUCAERROR, "failed to attach volume '%s'\n", instanceId);
 
99
      ret = 1;
 
100
    }
 
101
  }
 
102
  
 
103
  //rc = refresh_resources(ccMeta, OP_TIMEOUT - (time(NULL) - op_start));
 
104
  
 
105
  logprintfl(EUCADEBUG,"AttachVolume(): done.\n");
 
106
  
 
107
  shawn();
 
108
  
 
109
  return(ret);
 
110
}
 
111
 
 
112
int doDetachVolume(ncMetadata *ccMeta, char *volumeId, char *instanceId, char *remoteDev, char *localDev, int force) {
 
113
  int i, j, rc, start, stop, k, done, ret=0;
 
114
  ccInstance *myInstance, *out;
 
115
  ncStub *ncs;
 
116
  time_t op_start, op_timer;
 
117
  
 
118
  i = j = 0;
 
119
  myInstance = NULL;
 
120
  op_start = time(NULL);
 
121
  op_timer = OP_TIMEOUT;
 
122
  
 
123
  rc = init_config();
 
124
  if (rc) {
 
125
    return(1);
 
126
  }
 
127
  logprintfl(EUCADEBUG,"DetachVolume(): called\n");
 
128
  
 
129
  rc = find_instanceCacheId(instanceId, &myInstance);
 
130
  if (!rc) {
 
131
    // found the instance in the cache
 
132
    start = myInstance->ncHostIdx;
 
133
    stop = start+1;
 
134
    if (myInstance) free(myInstance);
 
135
  } else {
 
136
    start = 0;
 
137
    stop = config->numResources;
 
138
  }
 
139
  
 
140
  sem_wait(configLock);
 
141
  for (j=start; j<stop; j++) {
 
142
    // read the instance ids
 
143
    logprintfl(EUCAINFO,"DetachVolume(): calling dettach volume (%s) on (%s)\n", instanceId, config->resourcePool[j].hostname);
 
144
    if (1) {
 
145
      int pid, status;
 
146
      pid = fork();
 
147
      if (pid == 0) {
 
148
        ncs = ncStubCreate(config->resourcePool[j].ncService, NULL, NULL);
 
149
        if (config->use_wssec) {
 
150
          rc = InitWSSEC(ncs->env, ncs->stub, config->policyFile);
 
151
        }
 
152
        logprintfl(EUCADEBUG, "would call dettachVol on NC: %s\n",  config->resourcePool[j].hostname);
 
153
        rc = 0;
 
154
        rc = ncDetachVolumeStub(ncs, ccMeta, instanceId, volumeId, remoteDev, localDev, force);
 
155
        if (!rc) {
 
156
          ret = 0;
 
157
        } else {
 
158
          ret = 1;
 
159
        }
 
160
        exit(ret);
 
161
      } else {
 
162
        op_timer = OP_TIMEOUT - (time(NULL) - op_start);
 
163
        rc = timewait(pid, &status, op_timer / ((stop-start) - (j - start)));
 
164
        rc = WEXITSTATUS(status);
 
165
        logprintfl(EUCADEBUG,"\tcall complete (pid/rc): %d/%d\n", pid, rc);
 
166
      }
 
167
    }
 
168
    sem_post(configLock);
 
169
    
 
170
    if (!rc) {
 
171
      ret = 0;
 
172
    } else {
 
173
      logprintfl(EUCAERROR, "failed to dettach volume '%s'\n", instanceId);
 
174
      ret = 1;
 
175
    }
 
176
  }
 
177
  
 
178
  //rc = refresh_resources(ccMeta, OP_TIMEOUT - (time(NULL) - op_start));
 
179
  
 
180
  logprintfl(EUCADEBUG,"DetachVolume(): done.\n");
 
181
  
 
182
  shawn();
 
183
  
 
184
  return(ret);
 
185
}
 
186
 
 
187
int doConfigureNetwork(ncMetadata *meta, char *type, int namedLen, char **sourceNames, char **userNames, int netLen, char **sourceNets, char *destName, char *protocol, int minPort, int maxPort) {
 
188
  int rc, i, destVlan, slashnet, fail;
 
189
  char *destUserName;
 
190
 
 
191
  rc = init_config();
 
192
  if (rc) {
 
193
    return(1);
 
194
  }
 
195
  
 
196
  logprintfl(EUCADEBUG, "ConfigureNetwork(): called\n");
 
197
  
 
198
  if (!strcmp(vnetconfig->mode, "SYSTEM") || !strcmp(vnetconfig->mode, "STATIC")) {
 
199
    fail = 0;
 
200
  } else {
 
201
    
 
202
    destUserName = meta->userId;
 
203
    
 
204
    sem_wait(vnetConfigLock);
 
205
    
 
206
    fail=0;
 
207
    for (i=0; i<namedLen; i++) {
 
208
      if (sourceNames && userNames) {
 
209
        rc = vnetTableRule(vnetconfig, type, destUserName, destName, userNames[i], NULL, sourceNames[i], protocol, minPort, maxPort);
 
210
      }
 
211
      if (rc) {
 
212
        logprintfl(EUCAERROR,"ERROR: vnetTableRule() returned error\n");
 
213
        fail=1;
 
214
      }
 
215
    }
 
216
    for (i=0; i<netLen; i++) {
 
217
      if (sourceNets) {
 
218
        rc = vnetTableRule(vnetconfig, type, destUserName, destName, NULL, sourceNets[i], NULL, protocol, minPort, maxPort);
 
219
      }
 
220
      if (rc) {
 
221
        logprintfl(EUCAERROR,"ERROR: vnetTableRule() returned error\n");
 
222
        fail=1;
 
223
      }
 
224
    }
 
225
    sem_post(vnetConfigLock);
 
226
  }
 
227
  
 
228
  logprintfl(EUCADEBUG,"ConfigureNetwork(): done\n");
 
229
  
 
230
  if (fail) {
 
231
    return(1);
 
232
  }
 
233
  return(0);
 
234
}
 
235
 
 
236
int doFlushNetwork(ncMetadata *ccMeta, char *destName) {
 
237
  int rc;
 
238
 
 
239
  if (!strcmp(vnetconfig->mode, "SYSTEM") || !strcmp(vnetconfig->mode, "STATIC")) {
 
240
    return(0);
 
241
  }
 
242
 
 
243
  sem_wait(vnetConfigLock);
 
244
  rc = vnetFlushTable(vnetconfig, ccMeta->userId, destName);
 
245
  sem_post(vnetConfigLock);
 
246
  return(rc);
 
247
}
 
248
 
 
249
int doAssignAddress(ncMetadata *ccMeta, char *src, char *dst) {
 
250
  int rc, allocated, addrdevno, ret;
 
251
  char cmd[256];
 
252
 
 
253
  rc = init_config();
 
254
  if (rc) {
 
255
    return(1);
 
256
  }
 
257
 
 
258
  ret = 0;
 
259
 
 
260
  logprintfl(EUCADEBUG,"AssignAddress(): called\n");
 
261
 
 
262
  if (!strcmp(vnetconfig->mode, "SYSTEM") || !strcmp(vnetconfig->mode, "STATIC")) {
 
263
    ret = 0;
 
264
  } else {
 
265
    
 
266
    sem_wait(vnetConfigLock);
 
267
    rc = vnetGetPublicIP(vnetconfig, src, NULL, &allocated, &addrdevno);
 
268
    if (rc) {
 
269
      logprintfl(EUCAERROR,"failed to get publicip record %s\n", src);
 
270
      ret = 1;
 
271
    } else {
 
272
      if (!allocated) {
 
273
        snprintf(cmd, 255, "%s/usr/share/eucalyptus/euca_rootwrap ip addr add %s/32 dev %s", config->eucahome, src, vnetconfig->pubInterface);
 
274
        logprintfl(EUCAINFO,"running cmd %s\n", cmd);
 
275
        rc = system(cmd);
 
276
        if (rc) {
 
277
          logprintfl(EUCAERROR,"cmd '%s' failed\n", cmd);
 
278
          ret = 1;
 
279
        } else {
 
280
          rc = vnetAssignAddress(vnetconfig, src, dst);
 
281
          if (rc) {
 
282
            logprintfl(EUCAERROR,"could not assign address\n");
 
283
            ret = 1;
 
284
          } else {
 
285
            rc = vnetAllocatePublicIP(vnetconfig, src, dst);
 
286
            if (rc) {
 
287
              logprintfl(EUCAERROR,"could not allocate public IP\n");
 
288
              ret = 1;
 
289
            }
 
290
          }
 
291
        }
 
292
      } else {
 
293
        logprintfl(EUCAWARN,"ip %s is allready assigned, ignoring\n", src);
 
294
        ret = 0;
 
295
      }
 
296
    }
 
297
    sem_post(vnetConfigLock);
 
298
  }
 
299
  
 
300
  logprintfl(EUCADEBUG,"AssignAddress(): done\n");  
 
301
  return(ret);
 
302
}
 
303
 
 
304
int doDescribePublicAddresses(ncMetadata *ccMeta, publicip **outAddresses, int *outAddressesLen) {
 
305
  int i, rc, count;
 
306
  
 
307
  rc = init_config();
 
308
  if (rc) {
 
309
    return(1);
 
310
  }
 
311
  
 
312
  *outAddresses = vnetconfig->publicips;
 
313
  *outAddressesLen = NUMBER_OF_PUBLIC_IPS;
 
314
  
 
315
  return(0);
 
316
}
 
317
 
 
318
int doUnassignAddress(ncMetadata *ccMeta, char *src, char *dst) {
 
319
  int rc, allocated, addrdevno, ret, count;
 
320
  char cmd[256];
 
321
  
 
322
  rc = init_config();
 
323
  if (rc) {
 
324
    return(1);
 
325
  }
 
326
  logprintfl(EUCADEBUG,"UnassignAddress(): called\n");  
 
327
  
 
328
  if (!strcmp(vnetconfig->mode, "SYSTEM") || !strcmp(vnetconfig->mode, "STATIC")) {
 
329
    ret = 0;
 
330
  } else {
 
331
    
 
332
    sem_wait(vnetConfigLock);
 
333
    ret=0;
 
334
    rc = vnetGetPublicIP(vnetconfig, src, NULL, &allocated, &addrdevno);
 
335
    if (rc) {
 
336
      logprintfl(EUCAERROR,"failed to find publicip to unassign (%s)\n", src);
 
337
      ret=1;
 
338
    } else {
 
339
      if (allocated && dst) {
 
340
        rc = vnetUnassignAddress(vnetconfig, src, dst); 
 
341
        if (rc) {
 
342
          logprintfl(EUCAWARN,"vnetUnassignAddress() failed %d: %s/%s\n", rc, src, dst);
 
343
        }
 
344
        
 
345
        rc = vnetDeallocatePublicIP(vnetconfig, src, dst);
 
346
        if (rc) {
 
347
          logprintfl(EUCAWARN,"vnetDeallocatePublicIP() failed %d: %s\n", rc, src);
 
348
        }
 
349
      }
 
350
      
 
351
 
 
352
      snprintf(cmd, 256, "%s/usr/share/eucalyptus/euca_rootwrap ip addr del %s/32 dev %s", config->eucahome, src, vnetconfig->pubInterface);
 
353
      logprintfl(EUCADEBUG, "running cmd '%s'\n", cmd);
 
354
      rc = system(cmd);
 
355
      if (rc) {
 
356
        logprintfl(EUCAWARN,"cmd failed '%s'\n", cmd);
 
357
      }
 
358
    }
 
359
    
 
360
    sem_post(vnetConfigLock);
 
361
  }
 
362
  
 
363
  logprintfl(EUCADEBUG,"UnassignAddress(): done\n");  
 
364
  return(ret);
 
365
}
 
366
 
 
367
int doStopNetwork(ncMetadata *ccMeta, char *netName, int vlan) {
 
368
  int rc, ret;
 
369
  
 
370
  rc = init_config();
 
371
  if (rc) {
 
372
    return(1);
 
373
  }
 
374
  
 
375
  logprintfl(EUCADEBUG,"StopNetwork(): called\n");
 
376
  logprintfl(EUCADEBUG, "\t vlan:%d\n", vlan);
 
377
 
 
378
  if (!strcmp(vnetconfig->mode, "SYSTEM") || !strcmp(vnetconfig->mode, "STATIC")) {
 
379
    ret = 0;
 
380
  } else {
 
381
    
 
382
    sem_wait(vnetConfigLock);
 
383
    rc = vnetStopNetwork(vnetconfig, vlan, ccMeta->userId, netName);
 
384
    ret = rc;
 
385
    sem_post(vnetConfigLock);
 
386
  }
 
387
  
 
388
  logprintfl(EUCADEBUG,"StopNetwork(): done\n");
 
389
  
 
390
  return(ret);
 
391
}
 
392
 
 
393
int doStartNetwork(ncMetadata *ccMeta, char *netName, int vlan) {
 
394
  int rc, ret, i, status;
 
395
  time_t op_start, op_timer;
 
396
  char *brname;
 
397
  
 
398
  op_start = time(NULL);
 
399
  op_timer = OP_TIMEOUT;
 
400
 
 
401
  rc = init_config();
 
402
  if (rc) {
 
403
    return(1);
 
404
  }
 
405
  
 
406
  logprintfl(EUCADEBUG, "StartNetwork(): called\n");
 
407
  logprintfl(EUCADEBUG, "\t vlan:%d\n", vlan);
 
408
  if (!strcmp(vnetconfig->mode, "SYSTEM") || !strcmp(vnetconfig->mode, "STATIC")) {
 
409
    ret = 0;
 
410
  } else {
 
411
    sem_wait(vnetConfigLock);
 
412
    brname = NULL;
 
413
    
 
414
    rc = vnetStartNetwork(vnetconfig, vlan, ccMeta->userId, netName, &brname);
 
415
    
 
416
    //    if (brname) {
 
417
      //      vnetAddDev(vnetconfig, brname);
 
418
    //    }
 
419
    
 
420
    sem_post(vnetConfigLock);
 
421
    
 
422
    if (rc) {
 
423
      logprintfl(EUCAERROR,"StartNetwork(): ERROR return from vnetStartNetwork %d\n", rc);
 
424
      ret = 1;
 
425
    } else {
 
426
      logprintfl(EUCAINFO,"StartNetwork(): SUCCESS return from vnetStartNetwork %d\n", rc);
 
427
      ret = 0;
 
428
    }
 
429
    
 
430
    sem_wait(configLock);
 
431
    
 
432
    for (i=0; i<config->numResources; i++) {
 
433
      int pid, j, numHosts, done, k;
 
434
      ncStub *ncs=NULL;
 
435
      char *statusString=NULL, **hosts;
 
436
      
 
437
      hosts = malloc(sizeof(char *));
 
438
      numHosts = 1;
 
439
      hosts[0] = strdup("localhost");
 
440
      
 
441
      pid = fork();
 
442
      if (pid == 0) {
 
443
        ncs = ncStubCreate(config->resourcePool[i].ncService, NULL, NULL);
 
444
        if (config->use_wssec) {
 
445
          rc = InitWSSEC(ncs->env, ncs->stub, config->policyFile);
 
446
        }
 
447
        rc = ncStartNetworkStub(ncs, ccMeta, hosts, numHosts, 1234, vlan, &statusString);
 
448
        exit(rc);
 
449
      }
 
450
      op_timer = OP_TIMEOUT - (time(NULL) - op_start);
 
451
      rc = timewait(pid, &status, op_timer / (config->numResources - i));
 
452
      
 
453
      if (hosts != NULL) {
 
454
        for (j=0; j<numHosts; j++) {
 
455
          if (hosts[j]) free(hosts[j]);
 
456
        }
 
457
        if (hosts) free(hosts);
 
458
      }
 
459
      
 
460
    }
 
461
    
 
462
    sem_post(configLock);
 
463
  }
 
464
  
 
465
  logprintfl(EUCADEBUG,"StartNetwork(): done\n");
 
466
  
 
467
  shawn();
 
468
  
 
469
  return(ret);
 
470
}
 
471
 
 
472
int doDescribeResources(ncMetadata *ccMeta, virtualMachine **ccvms, int vmLen, int **outTypesMax, int **outTypesAvail, int *outTypesLen, char ***outServiceTags, int *outServiceTagsLen) {
 
473
  int i;
 
474
  ncResource *ncRes;
 
475
  int rc, diskpool, mempool, corepool;
 
476
  int *numberOfTypes, j;
 
477
  resource *res;
 
478
  ncStub *ncs;
 
479
  axis2_svc_client_t *svc_client;  
 
480
  char *ptr;
 
481
  time_t op_start, op_timer;
 
482
 
 
483
  op_start = time(NULL);
 
484
  op_timer = OP_TIMEOUT;
 
485
 
 
486
  rc = init_config();
 
487
  if (rc) {
 
488
    return(1);
 
489
  }
 
490
  logprintfl(EUCADEBUG,"DescribeResources(): called %d\n", vmLen);
 
491
  
 
492
  if (outTypesMax == NULL || outTypesAvail == NULL || outTypesLen == NULL || outServiceTags == NULL || outServiceTagsLen == NULL) {
 
493
    // input error
 
494
    return(1);
 
495
  }
 
496
  
 
497
  print_instanceCache();
 
498
 
 
499
  *outServiceTags = malloc(sizeof(char *) * config->numResources);
 
500
  *outServiceTagsLen = config->numResources;
 
501
  for (i=0; i<config->numResources; i++) {
 
502
    (*outServiceTags)[i] = strdup(config->resourcePool[i].ncService);
 
503
  }
 
504
  
 
505
  *outTypesMax = NULL;
 
506
  *outTypesAvail = NULL;
 
507
  
 
508
  *outTypesMax = malloc(sizeof(int) * vmLen);
 
509
  bzero(*outTypesMax, sizeof(int) * vmLen);
 
510
 
 
511
  *outTypesAvail = malloc(sizeof(int) * vmLen);
 
512
  bzero(*outTypesAvail, sizeof(int) * vmLen);
 
513
 
 
514
  *outTypesLen = vmLen;
 
515
 
 
516
  for (i=0; i<vmLen; i++) {
 
517
    if ((*ccvms)[i].mem <= 0 || (*ccvms)[i].cores <= 0 || (*ccvms)[i].disk <= 0) {
 
518
      logprintfl(EUCAERROR,"DescribeResources(): input error\n");
 
519
      if (*outTypesAvail) free(*outTypesAvail);
 
520
      if (*outTypesMax) free(*outTypesMax);
 
521
      *outTypesLen = 0;
 
522
      return(1);
 
523
    }
 
524
  }
 
525
  
 
526
  rc = refresh_resources(ccMeta, OP_TIMEOUT - (time(NULL) - op_start));
 
527
  if (rc) {
 
528
    logprintfl(EUCAERROR,"calling refresh_resources\n");
 
529
  }
 
530
 
 
531
  sem_wait(configLock);
 
532
  {
 
533
    for (i=0; i<config->numResources; i++) {
 
534
      res = &(config->resourcePool[i]);
 
535
      
 
536
      for (j=0; j<vmLen; j++) {
 
537
        mempool = res->availMemory;
 
538
        diskpool = res->availDisk;
 
539
        corepool = res->availCores;
 
540
        
 
541
        mempool -= (*ccvms)[j].mem;
 
542
        diskpool -= (*ccvms)[j].disk;
 
543
        corepool -= (*ccvms)[j].cores;
 
544
        while (mempool >= 0 && diskpool >= 0 && corepool >= 0) {
 
545
          (*outTypesAvail)[j]++;
 
546
          mempool -= (*ccvms)[j].mem;
 
547
          diskpool -= (*ccvms)[j].disk;
 
548
          corepool -= (*ccvms)[j].cores;
 
549
        }
 
550
        
 
551
        mempool = res->maxMemory;
 
552
        diskpool = res->maxDisk;
 
553
        corepool = res->maxCores;
 
554
        
 
555
        mempool -= (*ccvms)[j].mem;
 
556
        diskpool -= (*ccvms)[j].disk;
 
557
        corepool -= (*ccvms)[j].cores;
 
558
        while (mempool >= 0 && diskpool >= 0 && corepool >= 0) {
 
559
          (*outTypesMax)[j]++;
 
560
          mempool -= (*ccvms)[j].mem;
 
561
          diskpool -= (*ccvms)[j].disk;
 
562
          corepool -= (*ccvms)[j].cores;
 
563
        }
 
564
      }
 
565
    }
 
566
    sem_post(configLock);
 
567
  }
 
568
 
 
569
  logprintfl(EUCADEBUG,"DescribeResources(): done\n");
 
570
  
 
571
  shawn();
 
572
 
 
573
  return(0);
 
574
}
 
575
 
 
576
int refresh_resources(ncMetadata *ccMeta, int timeout) {
 
577
  int i, rc;
 
578
  int pid, status, ret;
 
579
  int filedes[2];  
 
580
  time_t op_start, op_timer;
 
581
  ncStub *ncs;
 
582
  ncResource *ncRes;
 
583
 
 
584
  if (timeout <= 0) timeout = 1;
 
585
 
 
586
  op_start = time(NULL);
 
587
  op_timer = timeout;
 
588
  logprintfl(EUCADEBUG,"refresh_resources(): called\n");
 
589
 
 
590
  sem_wait(configLock);
 
591
  for (i=0; i<config->numResources; i++) {
 
592
    rc = pipe(filedes);
 
593
 
 
594
    logprintfl(EUCADEBUG, "calling %s\n", config->resourcePool[i].ncService);
 
595
    pid = fork();
 
596
    if (pid == 0) {
 
597
      close(filedes[0]);
 
598
      ncs = ncStubCreate(config->resourcePool[i].ncService, NULL, NULL);
 
599
      if (config->use_wssec) {
 
600
        rc = InitWSSEC(ncs->env, ncs->stub, config->policyFile);
 
601
      }
 
602
      rc = ncDescribeResourceStub(ncs, ccMeta, NULL, &ncRes);
 
603
      
 
604
      if (!rc) {
 
605
        rc = write(filedes[1], ncRes, sizeof(ncResource));
 
606
        ret = 0;
 
607
      } else {
 
608
        ret = 1;
 
609
      }
 
610
      close(filedes[1]);          
 
611
      exit(ret);
 
612
    } else {
 
613
      fd_set rfds;
 
614
      struct timeval tv;
 
615
      
 
616
      close(filedes[1]);
 
617
      ncRes = malloc(sizeof(ncResource));
 
618
      bzero(ncRes, sizeof(ncResource));
 
619
      op_timer = timeout - (time(NULL) - op_start);
 
620
      logprintfl(EUCADEBUG, "\ttime left for next op: %d\n", op_timer);
 
621
      rc = timeread(filedes[0], ncRes, sizeof(ncResource), op_timer / (config->numResources - i));
 
622
      close(filedes[0]);
 
623
      if (rc <= 0) {
 
624
        // timeout or read went badly
 
625
        kill(pid, SIGKILL);
 
626
        wait(&status);
 
627
      } else {
 
628
        wait(&status);
 
629
        rc = WEXITSTATUS(status);
 
630
      }
 
631
    }
 
632
    
 
633
    config->lastResourceUpdate = time(NULL);
 
634
    if (rc != 0) {
 
635
      logprintfl(EUCAERROR,"bad return from ncDescribeResource(%s) (%d/%d)\n", config->resourcePool[i].hostname, pid, rc);
 
636
      config->resourcePool[i].maxMemory = 0;
 
637
      config->resourcePool[i].availMemory = 0;
 
638
      config->resourcePool[i].maxDisk = 0;
 
639
      config->resourcePool[i].availDisk = 0;
 
640
      config->resourcePool[i].maxCores = 0;
 
641
      config->resourcePool[i].availCores = 0;    
 
642
      config->resourcePool[i].isup = 0;
 
643
    } else {
 
644
      logprintfl(EUCAINFO,"\tnode=%s mem=%d/%d disk=%d/%d cores=%d/%d\n", config->resourcePool[i].hostname, ncRes->memorySizeMax, ncRes->memorySizeAvailable, ncRes->diskSizeMax,  ncRes->diskSizeAvailable, ncRes->numberOfCoresMax, ncRes->numberOfCoresAvailable);
 
645
      config->resourcePool[i].maxMemory = ncRes->memorySizeMax;
 
646
      config->resourcePool[i].availMemory = ncRes->memorySizeAvailable;
 
647
      config->resourcePool[i].maxDisk = ncRes->diskSizeMax;
 
648
      config->resourcePool[i].availDisk = ncRes->diskSizeAvailable;
 
649
      config->resourcePool[i].maxCores = ncRes->numberOfCoresMax;
 
650
      config->resourcePool[i].availCores = ncRes->numberOfCoresAvailable;    
 
651
      config->resourcePool[i].isup = 1;
 
652
      if (ncRes) free(ncRes);
 
653
    }
 
654
  }
 
655
  sem_post(configLock);
 
656
 
 
657
  logprintfl(EUCADEBUG,"refresh_resources(): done\n");
 
658
  return(0);
 
659
}
 
660
 
 
661
int doDescribeInstances(ncMetadata *ccMeta, char **instIds, int instIdsLen, ccInstance **outInsts, int *outInstsLen) {
 
662
  ccInstance *myInstance=NULL, *out=NULL, *cacheInstance=NULL;
 
663
  int i, j, k, numInsts, found, ncOutInstsLen, rc, pid;
 
664
  virtualMachine ccvm;
 
665
  time_t op_start, op_timer;
 
666
 
 
667
  ncInstance **ncOutInsts=NULL;
 
668
  ncStub *ncs;
 
669
 
 
670
  op_start = time(NULL);
 
671
  op_timer = OP_TIMEOUT;
 
672
 
 
673
  rc = init_config();
 
674
  if (rc) {
 
675
    return(1);
 
676
  }
 
677
 
 
678
  logprintfl(EUCADEBUG,"DescribeInstances(): called\n");
 
679
 
 
680
  *outInsts = NULL;
 
681
  out = *outInsts;
 
682
 
 
683
  *outInstsLen = 0;
 
684
  numInsts=0;
 
685
 
 
686
  sem_wait(configLock);  
 
687
  for (i=0; i<config->numResources; i++) {
 
688
    if (1) {
 
689
      int status, ret;
 
690
      int filedes[2];
 
691
      int len, j;
 
692
      
 
693
      rc = pipe(filedes);
 
694
      pid = fork();
 
695
      if (pid == 0) {
 
696
        close(filedes[0]);
 
697
        ncs = ncStubCreate(config->resourcePool[i].ncService, NULL, NULL);
 
698
        if (config->use_wssec) {
 
699
          rc = InitWSSEC(ncs->env, ncs->stub, config->policyFile);
 
700
        }
 
701
        rc = ncDescribeInstancesStub(ncs, ccMeta, instIds, instIdsLen, &ncOutInsts, &ncOutInstsLen);
 
702
        
 
703
        if (!rc) {
 
704
          len = ncOutInstsLen;
 
705
          rc = write(filedes[1], &len, sizeof(int));
 
706
          for (j=0; j<len; j++) {
 
707
            ncInstance *inst;
 
708
            inst = ncOutInsts[j];
 
709
            rc = write(filedes[1], inst, sizeof(ncInstance));
 
710
          }
 
711
          ret = 0;
 
712
        } else {
 
713
          len = 0;
 
714
          rc = write(filedes[1], &len, sizeof(int));
 
715
          ret = 1;
 
716
        }
 
717
        close(filedes[1]);
 
718
        fflush(stdout);
 
719
 
 
720
        exit(ret);
 
721
      } else {
 
722
        int len,rbytes,j;
 
723
        ncInstance *inst;
 
724
        close(filedes[1]);
 
725
        
 
726
        op_timer = OP_TIMEOUT - (time(NULL) - op_start);
 
727
        rbytes = timeread(filedes[0], &len, sizeof(int), op_timer / (config->numResources - i));
 
728
        if (rbytes <= 0) {
 
729
          // read went badly
 
730
          kill(pid, SIGKILL);
 
731
          wait(&status);
 
732
          rc = -1;
 
733
        } else {
 
734
          if (rbytes < sizeof(int)) {
 
735
            len = 0;
 
736
            ncOutInsts = NULL;
 
737
            ncOutInstsLen = 0;
 
738
          } else {
 
739
            ncOutInsts = malloc(sizeof(ncInstance *) * len);
 
740
            ncOutInstsLen = len;
 
741
            for (j=0; j<len; j++) {
 
742
              inst = malloc(sizeof(ncInstance));
 
743
              op_timer = OP_TIMEOUT - (time(NULL) - op_start);
 
744
              rbytes = timeread(filedes[0], inst, sizeof(ncInstance), op_timer / (config->numResources - i));
 
745
              ncOutInsts[j] = inst;
 
746
            }
 
747
          }
 
748
          wait(&status);
 
749
          rc = WEXITSTATUS(status);
 
750
        }
 
751
        close(filedes[0]);
 
752
      }
 
753
    }
 
754
    
 
755
    if (rc != 0) {
 
756
      logprintfl(EUCAERROR,"ncDescribeInstancesStub(%s): returned fail: (%d/%d)\n", config->resourcePool[i].ncService, pid, rc);
 
757
    } else {
 
758
      for (j=0; j<ncOutInstsLen; j++) {
 
759
        found=0;
 
760
        for (k=0; k<instIdsLen; k++) {
 
761
          if (!strcmp(ncOutInsts[j]->instanceId, instIds[k]) && (!strcmp(ncOutInsts[j]->userId, ccMeta->userId) || !strcmp(ccMeta->userId, SUPERUSER))) {
 
762
            found=1;
 
763
            k=instIdsLen;
 
764
          }
 
765
        }
 
766
        if (found || instIdsLen == 0) {
 
767
          // add it
 
768
          logprintfl(EUCAINFO,"DescribeInstances(): describing instance %s, %d\n", ncOutInsts[j]->instanceId, j);
 
769
          numInsts++;
 
770
          
 
771
          *outInsts = realloc(*outInsts, sizeof(ccInstance) * numInsts);
 
772
          out = *outInsts;
 
773
          
 
774
          // ccvm.name = TODO
 
775
          bzero(ccvm.name, 64);
 
776
          ccvm.mem = ncOutInsts[j]->params.memorySize;
 
777
          ccvm.disk = ncOutInsts[j]->params.diskSize;
 
778
          ccvm.cores = ncOutInsts[j]->params.numberOfCores;
 
779
          
 
780
          myInstance = &(out[numInsts-1]);
 
781
          bzero(myInstance, sizeof(ccInstance));
 
782
          //      strncpy(myInstance->instanceId, ncOutInsts[j]->instanceId, 16);
 
783
          cacheInstance=NULL;
 
784
          find_instanceCacheId(ncOutInsts[j]->instanceId, &cacheInstance);
 
785
          if (cacheInstance) {
 
786
            logprintfl(EUCADEBUG, "\t%s in cache\n", ncOutInsts[j]->instanceId);
 
787
            memcpy(myInstance, cacheInstance, sizeof(ccInstance));
 
788
          }
 
789
          
 
790
          rc = ccInstance_to_ncInstance(myInstance, ncOutInsts[j]);
 
791
 
 
792
          /*
 
793
          strncpy(myInstance->keyName, ncOutInsts[j]->keyName, 1024);
 
794
          strncpy(myInstance->ownerId, ncOutInsts[j]->userId, 16);
 
795
          strncpy(myInstance->reservationId, ncOutInsts[j]->reservationId, 16);  
 
796
          strncpy(myInstance->amiId, ncOutInsts[j]->imageId, 16);
 
797
          strncpy(myInstance->kernelId, ncOutInsts[j]->kernelId, 16);
 
798
          strncpy(myInstance->ramdiskId, ncOutInsts[j]->ramdiskId, 16);
 
799
          strncpy(myInstance->state, ncOutInsts[j]->stateName, 16);
 
800
          myInstance->ts = ncOutInsts[j]->launchTime;
 
801
          
 
802
          myInstance->ccnet.vlan = ncOutInsts[j]->ncnet.vlan;
 
803
          strncpy(myInstance->ccnet.publicIp, ncOutInsts[j]->ncnet.publicIp, 24);
 
804
          strncpy(myInstance->ccnet.privateIp, ncOutInsts[j]->ncnet.privateIp, 24);
 
805
          strncpy(myInstance->ccnet.publicMac, ncOutInsts[j]->ncnet.publicMac, 24);
 
806
          strncpy(myInstance->ccnet.privateMac, ncOutInsts[j]->ncnet.privateMac, 24);
 
807
          */
 
808
 
 
809
          // instance info that the CC maintains
 
810
          myInstance->ncHostIdx = i;
 
811
          strncpy(myInstance->serviceTag, config->resourcePool[i].ncService, 64);
 
812
          memcpy(&(myInstance->ccvm), &ccvm, sizeof(virtualMachine));
 
813
          
 
814
          /*
 
815
            if (cacheInstance) {
 
816
            // see if I remember the IPs
 
817
            memcpy(&(myInstance->ccvm), &(cacheInstance->ccvm), sizeof(virtualMachine));
 
818
            
 
819
            if (!strcmp(myInstance->ccnet.publicIp, "0.0.0.0") && strcmp(cacheInstance->ccnet.publicIp, "0.0.0.0")) {
 
820
              // found cached publicIp
 
821
              strncpy(myInstance->ccnet.publicIp, cacheInstance->ccnet.publicIp, 24);
 
822
            }
 
823
            if (!strcmp(myInstance->ccnet.privateIp, "0.0.0.0") && strcmp(cacheInstance->ccnet.privateIp, "0.0.0.0")) {
 
824
              // found cached publicIp
 
825
              strncpy(myInstance->ccnet.privateIp, cacheInstance->ccnet.privateIp, 24);
 
826
            }
 
827
          }
 
828
          */
 
829
          
 
830
          {
 
831
            char *ip;
 
832
            
 
833
            if (!strcmp(myInstance->ccnet.publicIp, "0.0.0.0")) {
 
834
              rc = discover_mac(vnetconfig, myInstance->ccnet.publicMac, &ip);
 
835
              if (!rc) {
 
836
                strncpy(myInstance->ccnet.publicIp, ip, 24);
 
837
              }
 
838
            }
 
839
            if (!strcmp(myInstance->ccnet.privateIp, "0.0.0.0")) {
 
840
              rc = discover_mac(vnetconfig, myInstance->ccnet.privateMac, &ip);
 
841
              if (!rc) {
 
842
                strncpy(myInstance->ccnet.privateIp, ip, 24);
 
843
              }
 
844
            }
 
845
          }
 
846
          if (cacheInstance) free(cacheInstance);
 
847
          
 
848
          refresh_instanceCache(myInstance->instanceId, myInstance);
 
849
        }
 
850
      }
 
851
      for (j=0; j<ncOutInstsLen; j++) {
 
852
        free_instance(&(ncOutInsts[j]));
 
853
      }
 
854
      if (ncOutInsts) free(ncOutInsts);
 
855
    }
 
856
  }
 
857
  sem_post(configLock);
 
858
  
 
859
  *outInstsLen = numInsts;
 
860
  logprintfl(EUCADEBUG,"DescribeInstances(): done\n");
 
861
 
 
862
  shawn();
 
863
      
 
864
  return(0);
 
865
}
 
866
 
 
867
int ccInstance_to_ncInstance(ccInstance *dst, ncInstance *src) {
 
868
  int i;
 
869
  
 
870
  strncpy(dst->instanceId, src->instanceId, 16);
 
871
  strncpy(dst->reservationId, src->reservationId, 16);
 
872
  strncpy(dst->ownerId, src->userId, 16);
 
873
  strncpy(dst->amiId, src->imageId, 16);
 
874
  strncpy(dst->kernelId, src->kernelId, 16);
 
875
  strncpy(dst->ramdiskId, src->ramdiskId, 16);
 
876
  strncpy(dst->launchIndex, src->launchIndex, 64);
 
877
  strncpy(dst->userData, src->userData, 64);
 
878
  for (i=0; i<src->groupNamesSize || i >= 64; i++) {
 
879
    snprintf(dst->groupNames[i], 32, "%s", src->groupNames[i]);
 
880
  }
 
881
  strncpy(dst->state, src->stateName, 16);
 
882
  dst->ccnet.vlan = src->ncnet.vlan;
 
883
  strncpy(dst->ccnet.publicMac, src->ncnet.publicMac, 24);
 
884
  strncpy(dst->ccnet.privateMac, src->ncnet.privateMac, 24);
 
885
  if (strcmp(src->ncnet.publicIp, "0.0.0.0") || dst->ccnet.publicIp[0] == '\0') strncpy(dst->ccnet.publicIp, src->ncnet.publicIp, 16);
 
886
  if (strcmp(src->ncnet.privateIp, "0.0.0.0") || dst->ccnet.privateIp[0] == '\0') strncpy(dst->ccnet.privateIp, src->ncnet.privateIp, 16);
 
887
 
 
888
  memcpy(dst->volumes, src->volumes, sizeof(ncVolume) * EUCA_MAX_VOLUMES);
 
889
  dst->volumesSize = src->volumesSize;
 
890
 
 
891
  return(0);
 
892
}
 
893
 
 
894
int schedule_instance(virtualMachine *vm, int *outresid) {
 
895
  
 
896
  if (config->schedPolicy == SCHEDGREEDY) {
 
897
    return(schedule_instance_greedy(vm, outresid));
 
898
  } else if (config->schedPolicy == SCHEDROUNDROBIN) {
 
899
    return(schedule_instance_roundrobin(vm, outresid));
 
900
  }
 
901
  
 
902
  return(schedule_instance_greedy(vm, outresid));
 
903
}
 
904
 
 
905
int schedule_instance_roundrobin(virtualMachine *vm, int *outresid) {
 
906
  int i, rc, done, start, found, resid=0;
 
907
  resource *res;
 
908
 
 
909
  *outresid = 0;
 
910
 
 
911
  logprintfl(EUCAINFO, "scheduler using ROUNDROBIN policy to find next resource\n");
 
912
 
 
913
  // find the best 'resource' on which to run the instance
 
914
  done=found=0;
 
915
  start = config->schedState;
 
916
  i = start;
 
917
  
 
918
  logprintfl(EUCADEBUG, "scheduler state starting at resource %d\n", config->schedState);
 
919
  while(!done) {
 
920
    int mem, disk, cores;
 
921
    
 
922
    res = &(config->resourcePool[i]);
 
923
    if (res->isup) {
 
924
      mem = res->availMemory - vm->mem;
 
925
      disk = res->availDisk - vm->disk;
 
926
      cores = res->availCores - vm->cores;
 
927
      
 
928
      if (mem >= 0 && disk >= 0 && cores >= 0) {
 
929
        resid = i;
 
930
        found=1;
 
931
        done++;
 
932
      }
 
933
    }
 
934
    i++;
 
935
    if (i >= config->numResources) {
 
936
      i = 0;
 
937
    }
 
938
    if (i == start) {
 
939
      done++;
 
940
    }
 
941
  }
 
942
 
 
943
  if (!found) {
 
944
    // didn't find a resource
 
945
    return(1);
 
946
  }
 
947
 
 
948
  *outresid = resid;
 
949
  config->schedState = i;
 
950
  logprintfl(EUCADEBUG, "scheduler state finishing at resource %d\n", config->schedState);
 
951
 
 
952
  return(0);
 
953
}
 
954
 
 
955
int schedule_instance_greedy(virtualMachine *vm, int *outresid) {
 
956
  int i, rc, done, resid=0;
 
957
  resource *res;
 
958
 
 
959
  *outresid = 0;
 
960
 
 
961
  logprintfl(EUCAINFO, "scheduler using GREEDY policy to find next resource\n");
 
962
 
 
963
  // find the best 'resource' on which to run the instance
 
964
  done=0;
 
965
  for (i=0; i<config->numResources && !done; i++) {
 
966
    int mem, disk, cores;
 
967
    
 
968
    // new fashion way
 
969
    res = &(config->resourcePool[i]);
 
970
    if (res->isup) {
 
971
      mem = res->availMemory - vm->mem;
 
972
      disk = res->availDisk - vm->disk;
 
973
      cores = res->availCores - vm->cores;
 
974
      
 
975
      if (mem >= 0 && disk >= 0 && cores >= 0) {
 
976
        resid = i;
 
977
        done++;
 
978
      }
 
979
    }
 
980
  }
 
981
  
 
982
  if (!done) {
 
983
    // didn't find a resource
 
984
    return(1);
 
985
  }
 
986
  *outresid = resid;
 
987
  return(0);
 
988
}
 
989
 
 
990
int doRunInstances(ncMetadata *ccMeta, char *amiId, char *kernelId, char *ramdiskId, char *amiURL, char *kernelURL, char *ramdiskURL, char **instIds, int instIdsLen, char **netNames, int netNamesLen, char **macAddrs, int macAddrsLen, int minCount, int maxCount, char *ownerId, char *reservationId, virtualMachine *ccvm, char *keyName, int vlan, char *userData, char *launchIndex, ccInstance **outInsts, int *outInstsLen) {
 
991
  int rc, i, j, done, runCount, resid;
 
992
  ccInstance *myInstance=NULL, 
 
993
    *retInsts=NULL;
 
994
  char *instId=NULL, 
 
995
    *mac=NULL, 
 
996
    *pubip=NULL, 
 
997
    *privip=NULL, 
 
998
    *brname=NULL;
 
999
  time_t op_start, op_timer;
 
1000
 
 
1001
  ncInstance *outInst=NULL;
 
1002
  ncInstParams ncvm;
 
1003
  ncStub *ncs=NULL;
 
1004
 
 
1005
  op_start = time(NULL);
 
1006
  op_timer = OP_TIMEOUT;
 
1007
  
 
1008
  rc = init_config();
 
1009
  if (rc) {
 
1010
    return(1);
 
1011
  }
 
1012
  logprintfl(EUCADEBUG,"RunInstances(): called\n");
 
1013
  
 
1014
  *outInstsLen = 0;
 
1015
  
 
1016
  if (minCount <= 0 || maxCount <= 0 || instIdsLen < maxCount) {
 
1017
    logprintfl(EUCAERROR,"RunInstances(): bad min or max count, or not enough instIds (%d, %d, %d)\n", minCount, maxCount, instIdsLen);
 
1018
    return(-1);
 
1019
  }
 
1020
  
 
1021
  retInsts = malloc(sizeof(ccInstance) * maxCount);  
 
1022
  runCount=0;
 
1023
  
 
1024
  // get updated resource information
 
1025
  rc = refresh_resources(ccMeta, OP_TIMEOUT - (time(NULL) - op_start));
 
1026
  
 
1027
  for (i=0; i<maxCount; i++) {
 
1028
    resource *res;
 
1029
    char mac[32], privip[32], pubip[32];
 
1030
    int foundnet=0;
 
1031
 
 
1032
    logprintfl(EUCAINFO,"\trunning instance %d with emiId %s...\n", i, amiId);
 
1033
    
 
1034
    // generate new mac
 
1035
    bzero(mac, 32);
 
1036
    bzero(pubip, 32);
 
1037
    bzero(privip, 32);
 
1038
    
 
1039
    strncpy(pubip, "0.0.0.0", 32);
 
1040
    strncpy(privip, "0.0.0.0", 32);
 
1041
    strncpy(mac, macAddrs[i], 32);
 
1042
    
 
1043
    sem_wait(vnetConfigLock);
 
1044
    
 
1045
    // define/get next mac and allocate IP
 
1046
    if (!strcmp(vnetconfig->mode, "STATIC")) {
 
1047
      // get the next valid mac/ip pairing for this vlan
 
1048
      bzero(mac, 32);
 
1049
      rc = vnetGetNextHost(vnetconfig, mac, privip, 0);
 
1050
      if (!rc) {
 
1051
        foundnet = 1;
 
1052
      }
 
1053
    } else if (!strcmp(vnetconfig->mode, "SYSTEM")) {
 
1054
      foundnet = 1;
 
1055
    } else if (!strcmp(vnetconfig->mode, "MANAGED") || !strcmp(vnetconfig->mode, "MANAGED-NOVLAN")) {
 
1056
      
 
1057
      // add the mac address to the virtual network
 
1058
      rc = vnetAddHost(vnetconfig, mac, NULL, vlan);
 
1059
      if (!rc) {
 
1060
        // get the next valid mac/ip pairing for this vlan
 
1061
        rc = vnetGetNextHost(vnetconfig, mac, privip, vlan);
 
1062
        if (!rc) {
 
1063
          foundnet = 1;
 
1064
        }
 
1065
      }
 
1066
    }
 
1067
    sem_post(vnetConfigLock);
 
1068
    
 
1069
    logprintfl(EUCAINFO,"\tassigning MAC/IP: %s/%s\n", mac, privip);
 
1070
    
 
1071
    if (mac[0] == '\0' || !foundnet) {
 
1072
      logprintfl(EUCAERROR,"could not find any free network address, failing doRunInstances()\n");
 
1073
    } else {
 
1074
      // "run" the instance
 
1075
      instId = strdup(instIds[i]);
 
1076
      ncvm.memorySize = ccvm->mem;
 
1077
      ncvm.diskSize = ccvm->disk;
 
1078
      ncvm.numberOfCores = ccvm->cores;
 
1079
      
 
1080
      sem_wait(configLock);
 
1081
      
 
1082
      resid = 0;
 
1083
      rc = schedule_instance(ccvm, &resid);
 
1084
      
 
1085
      res = &(config->resourcePool[resid]);
 
1086
      if (rc) {
 
1087
        // could not find resource
 
1088
        logprintfl(EUCAERROR, "scheduler could not find resource to run the instance on\n");
 
1089
        // couldn't run this VM, remove networking information from system
 
1090
        sem_wait(vnetConfigLock);
 
1091
        
 
1092
        vnetDisableHost(vnetconfig, mac, NULL, 0);
 
1093
        if (!strcmp(vnetconfig->mode, "MANAGED") || !strcmp(vnetconfig->mode, "MANAGED-NOVLAN")) {
 
1094
          vnetDelHost(vnetconfig, mac, NULL, vlan);
 
1095
        }
 
1096
        
 
1097
        sem_post(vnetConfigLock);
 
1098
      } else {
 
1099
        int pid, status, ret, rbytes;
 
1100
        int filedes[2];
 
1101
        
 
1102
        // try to run the instance on the chosen resource
 
1103
        logprintfl(EUCAINFO, "\tscheduler decided to run instance '%s' on resource '%s'\n", instId, res->ncService);
 
1104
        outInst=NULL;
 
1105
        
 
1106
        rc = pipe(filedes);
 
1107
        pid = fork();
 
1108
        if (pid == 0) {
 
1109
          close(filedes[0]);
 
1110
          ncs = ncStubCreate(res->ncService, NULL, NULL);
 
1111
          if (config->use_wssec) {
 
1112
            rc = InitWSSEC(ncs->env, ncs->stub, config->policyFile);
 
1113
          }
 
1114
          logprintfl(EUCAINFO,"\tclient (%s) running instance: %s %s %s %s %d %s\n", res->ncService, instId, amiId, mac, mac, vlan, keyName);
 
1115
          logprintfl(EUCAINFO,"\tasking for virtual hardware (mem/disk/cores): %d/%d/%d\n", ncvm.memorySize, ncvm.diskSize, ncvm.numberOfCores);
 
1116
          rc = ncRunInstanceStub(ncs, ccMeta, instId, reservationId, &ncvm, amiId, amiURL, kernelId, kernelURL, ramdiskId, ramdiskURL, keyName, mac, mac, vlan, userData, launchIndex, netNames, netNamesLen, &outInst);
 
1117
          if (!rc) {
 
1118
            rc = write(filedes[1], outInst, sizeof(ncInstance));
 
1119
            ret = 0;
 
1120
          } else {
 
1121
            ret = 1;
 
1122
          }
 
1123
          close(filedes[1]);      
 
1124
          exit(ret);
 
1125
        } else {
 
1126
          close(filedes[1]);
 
1127
          outInst = malloc(sizeof(ncInstance));
 
1128
          op_timer = OP_TIMEOUT - (time(NULL) - op_start);
 
1129
          logprintfl(EUCADEBUG, "\ttime left for op: %d\n", op_timer / (maxCount - i));
 
1130
          rbytes = timeread(filedes[0], outInst, sizeof(ncInstance), op_timer / (maxCount - i));
 
1131
          close(filedes[0]);
 
1132
          if (rbytes <= 0) {
 
1133
            // read went badly
 
1134
            kill(pid, SIGKILL);
 
1135
            wait(&status);
 
1136
            rc = -1;
 
1137
          } else {
 
1138
            wait(&status);
 
1139
            rc = WEXITSTATUS(status);
 
1140
          }
 
1141
          logprintfl(EUCAINFO,"\tcall complete (pid/rc): %d/%d\n", pid, rc);
 
1142
        }
 
1143
        if (rc != 0) {
 
1144
          // problem
 
1145
          logprintfl(EUCAERROR, "tried to run the VM, but runInstance() failed; marking resource '%s' as down\n", res->ncService);
 
1146
          res->isup = 0;
 
1147
          i--;
 
1148
          // couldn't run this VM, remove networking information from system
 
1149
          sem_wait(vnetConfigLock);
 
1150
          vnetDisableHost(vnetconfig, mac, NULL, 0);
 
1151
          if (!strcmp(vnetconfig->mode, "MANAGED") || !strcmp(vnetconfig->mode, "MANAGED-NOVLAN")) {
 
1152
            vnetDelHost(vnetconfig, mac, NULL, vlan);
 
1153
          }
 
1154
          sem_post(vnetConfigLock);
 
1155
        } else {
 
1156
          res->availMemory -= ccvm->mem;
 
1157
          res->availDisk -= ccvm->disk;
 
1158
          res->availCores -= ccvm->cores;
 
1159
          
 
1160
          myInstance = &(retInsts[runCount]);
 
1161
          
 
1162
          bzero(myInstance, sizeof(ccInstance));
 
1163
          
 
1164
          // stuff from NC
 
1165
          rc = ccInstance_to_ncInstance(myInstance, outInst);
 
1166
 
 
1167
          /*
 
1168
          strncpy(myInstance->instanceId, outInst->instanceId, 16);
 
1169
          strncpy(myInstance->reservationId, outInst->reservationId, 16);
 
1170
          strncpy(myInstance->ownerId, outInst->userId, 16);
 
1171
          strncpy(myInstance->amiId, outInst->imageId, 16);
 
1172
          strncpy(myInstance->kernelId, outInst->kernelId, 16);
 
1173
          strncpy(myInstance->ramdiskId, outInst->ramdiskId, 16);
 
1174
          strncpy(myInstance->launchIndex, outInst->launchIndex, 64);
 
1175
          strncpy(myInstance->userData, outInst->userData, 64);
 
1176
          for (i=0; i<outInst->groupNamesSize || i >= 64; i++) {
 
1177
            snprintf(myInstance->groupNames[i], 32, "%s", outInst->groupNames[i]);
 
1178
          }
 
1179
          strncpy(myInstance->state, outInst->stateName, 16);
 
1180
          myInstance->ccnet.vlan = outInst->ncnet.vlan;
 
1181
          strncpy(myInstance->ccnet.publicMac, outInst->ncnet.publicMac, 24);
 
1182
          strncpy(myInstance->ccnet.privateMac, outInst->ncnet.privateMac, 24);
 
1183
          if (strcmp(pubip, "0.0.0.0")) {
 
1184
            strncpy(myInstance->ccnet.publicIp, pubip, 16);
 
1185
          } else {
 
1186
            strncpy(myInstance->ccnet.publicIp, outInst->ncnet.publicIp, 16);
 
1187
          }
 
1188
          
 
1189
          if (strcmp(privip, "0.0.0.0")) {
 
1190
          strncpy(myInstance->ccnet.privateIp, privip, 16);
 
1191
          } else {
 
1192
          strncpy(myInstance->ccnet.privateIp, outInst->ncnet.privateIp, 16);
 
1193
            }
 
1194
          */
 
1195
          
 
1196
          // instance info that CC has
 
1197
          myInstance->ts = time(NULL);
 
1198
          if (strcmp(pubip, "0.0.0.0")) {
 
1199
            strncpy(myInstance->ccnet.publicIp, pubip, 16);
 
1200
          }
 
1201
          if (strcmp(privip, "0.0.0.0")) {
 
1202
            strncpy(myInstance->ccnet.privateIp, privip, 16);
 
1203
          }
 
1204
          myInstance->ncHostIdx = resid;
 
1205
          if (ccvm) memcpy(&(myInstance->ccvm), ccvm, sizeof(virtualMachine));
 
1206
          if (config->resourcePool[resid].ncService) strncpy(myInstance->serviceTag, config->resourcePool[resid].ncService, 64);
 
1207
          //      if (kernelId) strncpy(myInstance->kernelId, kernelId, 16);
 
1208
          //      if (ramdiskId) strncpy(myInstance->ramdiskId, ramdiskId, 16);
 
1209
          //      if (userData) strncpy(myInstance->userData, userData, 64);
 
1210
          //      if (launchIndex) strncpy(myInstance->launchIndex, launchIndex, 64);
 
1211
          /*
 
1212
          {
 
1213
            int i;
 
1214
            for (i=0; i<netNamesLen; i++) {
 
1215
              if (i >= 64) break;
 
1216
              if (netNames[i]) snprintf(myInstance->groupNames[i], 32, "%s", netNames[i]);
 
1217
            }
 
1218
          }
 
1219
          */
 
1220
          // start up DHCP
 
1221
          rc = vnetKickDHCP(vnetconfig);
 
1222
          if (rc) {
 
1223
            logprintfl(EUCAERROR, "cannot start DHCP daemon, please check your network settings\n");
 
1224
          }
 
1225
          
 
1226
          // add the instance to the cache, and continue on
 
1227
          add_instanceCache(myInstance->instanceId, myInstance);
 
1228
          free_instance(&outInst);
 
1229
          runCount++;
 
1230
        }
 
1231
        sem_post(configLock);
 
1232
      }
 
1233
    }
 
1234
  }
 
1235
  *outInstsLen = runCount;
 
1236
  *outInsts = retInsts;
 
1237
  
 
1238
  logprintfl(EUCADEBUG,"RunInstances(): done\n");
 
1239
  
 
1240
  shawn();
 
1241
  if (instId) free(instId);
 
1242
  return(0);
 
1243
}
 
1244
 
 
1245
int doGetConsoleOutput(ncMetadata *meta, char *instId, char **outConsoleOutput) {
 
1246
  int i, j, rc, numInsts, start, stop, k, done, ret;
 
1247
  ccInstance *myInstance;
 
1248
  ncStub *ncs;
 
1249
  char *consoleOutput;
 
1250
  time_t op_start, op_timer;
 
1251
 
 
1252
  i = j = numInsts = 0;
 
1253
  op_start = time(NULL);
 
1254
  op_timer = OP_TIMEOUT;
 
1255
 
 
1256
  myInstance = NULL;
 
1257
  
 
1258
  *outConsoleOutput = NULL;
 
1259
 
 
1260
  rc = init_config();
 
1261
  if (rc) {
 
1262
    return(1);
 
1263
  }
 
1264
 
 
1265
  logprintfl(EUCADEBUG,"GetConsoleOutput(): called\n");
 
1266
  
 
1267
  rc = find_instanceCacheId(instId, &myInstance);
 
1268
  if (!rc) {
 
1269
    // found the instance in the cache
 
1270
    start = myInstance->ncHostIdx;
 
1271
    stop = start+1;      
 
1272
    free(myInstance);
 
1273
  } else {
 
1274
    start = 0;
 
1275
    stop = config->numResources;
 
1276
  }
 
1277
  
 
1278
  sem_wait(configLock);
 
1279
  done=0;
 
1280
  for (j=start; j<stop && !done; j++) {
 
1281
    // read the instance ids
 
1282
    logprintfl(EUCAINFO,"getConsoleOutput(): calling GetConsoleOutput for instance (%s) on (%s)\n", instId, config->resourcePool[j].hostname);
 
1283
    if (1) {
 
1284
      int pid, status, ret, rbytes, len;
 
1285
      int filedes[2];
 
1286
      rc = pipe(filedes);
 
1287
      pid = fork();
 
1288
      if (pid == 0) {
 
1289
        close(filedes[0]);
 
1290
        ncs = ncStubCreate(config->resourcePool[j].ncService, NULL, NULL);
 
1291
        if (config->use_wssec) {
 
1292
          rc = InitWSSEC(ncs->env, ncs->stub, config->policyFile);
 
1293
        }
 
1294
 
 
1295
        rc = ncGetConsoleOutputStub(ncs, meta, instId, &consoleOutput);
 
1296
        if (!rc && consoleOutput) {
 
1297
          len = strlen(consoleOutput) + 1;
 
1298
          rc = write(filedes[1], &len, sizeof(int));
 
1299
          rc = write(filedes[1], consoleOutput, sizeof(char) * len);
 
1300
          ret = 0;
 
1301
        } else {
 
1302
          len = 0;
 
1303
          rc = write(filedes[1], &len, sizeof(int));
 
1304
          ret = 1;
 
1305
        }
 
1306
        close(filedes[1]);        
 
1307
        exit(ret);
 
1308
      } else {
 
1309
        close(filedes[1]);
 
1310
        op_timer = OP_TIMEOUT - (time(NULL) - op_start);
 
1311
        rbytes = timeread(filedes[0], &len, sizeof(int), op_timer / ((stop-start) - (j - start)));
 
1312
        if (rbytes <= 0) {
 
1313
          // read went badly
 
1314
          kill(pid, SIGKILL);
 
1315
          wait(&status);
 
1316
          rc = -1;
 
1317
        } else {
 
1318
          consoleOutput = malloc(sizeof(char) * len);
 
1319
          op_timer = OP_TIMEOUT - (time(NULL) - op_start);
 
1320
          rbytes = timeread(filedes[0], consoleOutput, len, op_timer / ((stop-start) - (j-start)));
 
1321
          if (rbytes <= 0) {
 
1322
            // read went badly
 
1323
            kill(pid, SIGKILL);
 
1324
            wait(&status);
 
1325
            rc = -1;
 
1326
          } else {
 
1327
            wait(&status);
 
1328
            rc = WEXITSTATUS(status);
 
1329
          }
 
1330
        }
 
1331
        close(filedes[0]);
 
1332
 
 
1333
        logprintfl(EUCAINFO,"\tcall complete (pid/rc): %d/%d\n", pid, rc);
 
1334
        if (!rc) {
 
1335
          done++;
 
1336
        }
 
1337
      }
 
1338
    }
 
1339
  }
 
1340
  sem_post(configLock);
 
1341
  
 
1342
  logprintfl(EUCADEBUG,"GetConsoleOutput(): done.\n");
 
1343
  
 
1344
  shawn();
 
1345
  
 
1346
  if (consoleOutput) {
 
1347
    *outConsoleOutput = strdup(consoleOutput);
 
1348
    ret = 0;
 
1349
  } else {
 
1350
    *outConsoleOutput = NULL;
 
1351
    ret = 1;
 
1352
  }
 
1353
  if (consoleOutput) free(consoleOutput);
 
1354
  return(ret);
 
1355
}
 
1356
 
 
1357
int doRebootInstances(ncMetadata *meta, char **instIds, int instIdsLen) {
 
1358
  int i, j, rc, numInsts, start, stop, k, done, ret;
 
1359
  char *instId;
 
1360
  ccInstance *myInstance, *out;
 
1361
  ncStub *ncs;
 
1362
  time_t op_start, op_timer;
 
1363
 
 
1364
  i = j = numInsts = 0;
 
1365
  instId = NULL;
 
1366
  myInstance = NULL;
 
1367
  op_start = time(NULL);
 
1368
  op_timer = OP_TIMEOUT;
 
1369
 
 
1370
  rc = init_config();
 
1371
  if (rc) {
 
1372
    return(1);
 
1373
  }
 
1374
  logprintfl(EUCADEBUG,"RebootInstances(): called\n");
 
1375
  
 
1376
  for (i=0; i<instIdsLen; i++) {
 
1377
    instId = instIds[i];
 
1378
    rc = find_instanceCacheId(instId, &myInstance);
 
1379
    if (!rc) {
 
1380
      // found the instance in the cache
 
1381
      start = myInstance->ncHostIdx;
 
1382
      stop = start+1;      
 
1383
      free(myInstance);
 
1384
    } else {
 
1385
      start = 0;
 
1386
      stop = config->numResources;
 
1387
    }
 
1388
    
 
1389
    sem_wait(configLock);
 
1390
    done=0;
 
1391
    for (j=start; j<stop && !done; j++) {
 
1392
      // read the instance ids
 
1393
      logprintfl(EUCAINFO,"RebootInstances(): calling reboot instance (%s) on (%s)\n", instId, config->resourcePool[j].hostname);
 
1394
      if (1) {
 
1395
        int pid, status, ret, rbytes;
 
1396
        pid = fork();
 
1397
        if (pid == 0) {
 
1398
          ncs = ncStubCreate(config->resourcePool[j].ncService, NULL, NULL);
 
1399
          if (config->use_wssec) {
 
1400
            rc = InitWSSEC(ncs->env, ncs->stub, config->policyFile);
 
1401
          }
 
1402
          
 
1403
          rc = 0;
 
1404
          rc = ncRebootInstanceStub(ncs, meta, instId);
 
1405
          
 
1406
          if (!rc) {
 
1407
            ret = 0;
 
1408
          } else {
 
1409
            ret = 1;
 
1410
          }
 
1411
          exit(ret);
 
1412
        } else {
 
1413
          op_timer = OP_TIMEOUT - (time(NULL) - op_start);
 
1414
          rc = timewait(pid, &status, op_timer / ((stop-start) - (j-start)));
 
1415
          rc = WEXITSTATUS(status);
 
1416
          logprintfl(EUCAINFO,"\tcall complete (pid/rc): %d/%d\n", pid, rc);
 
1417
        }
 
1418
      }
 
1419
      sem_post(configLock);
 
1420
      
 
1421
      if (!rc) {
 
1422
        done++;
 
1423
      }
 
1424
    }
 
1425
  }
 
1426
  
 
1427
  logprintfl(EUCADEBUG,"RebootInstances(): done.\n");
 
1428
 
 
1429
  shawn();
 
1430
 
 
1431
  return(0);
 
1432
}
 
1433
 
 
1434
int doTerminateInstances(ncMetadata *ccMeta, char **instIds, int instIdsLen, int **outStatus) {
 
1435
  int i, j, shutdownState, previousState, rc, start, stop, k, done;
 
1436
  char *instId;
 
1437
  ccInstance *myInstance, *out;
 
1438
  ncStub *ncs;
 
1439
  time_t op_start, op_timer;
 
1440
 
 
1441
  i = j = 0;
 
1442
  instId = NULL;
 
1443
  myInstance = NULL;
 
1444
  op_start = time(NULL);
 
1445
  op_timer = OP_TIMEOUT;
 
1446
 
 
1447
  rc = init_config();
 
1448
  if (rc) {
 
1449
    return(1);
 
1450
  }
 
1451
  logprintfl(EUCADEBUG,"TerminateInstances(): called\n");
 
1452
  
 
1453
  for (i=0; i<instIdsLen; i++) {
 
1454
    instId = instIds[i];
 
1455
    rc = find_instanceCacheId(instId, &myInstance);
 
1456
    if (!rc) {
 
1457
      // found the instance in the cache
 
1458
      start = myInstance->ncHostIdx;
 
1459
      stop = start+1;
 
1460
      
 
1461
      // remove private network info from system
 
1462
      sem_wait(vnetConfigLock);
 
1463
      
 
1464
      vnetDisableHost(vnetconfig, myInstance->ccnet.privateMac, NULL, 0);
 
1465
      if (!strcmp(vnetconfig->mode, "MANAGED") || !strcmp(vnetconfig->mode, "MANAGED-NOVLAN")) {
 
1466
        vnetDelHost(vnetconfig, myInstance->ccnet.privateMac, NULL, myInstance->ccnet.vlan);
 
1467
      }
 
1468
      
 
1469
      sem_post(vnetConfigLock);
 
1470
      
 
1471
      if (myInstance) free(myInstance);
 
1472
    } else {
 
1473
      start = 0;
 
1474
      stop = config->numResources;
 
1475
    }
 
1476
    
 
1477
    sem_wait(configLock);
 
1478
    for (j=start; j<stop; j++) {
 
1479
      // read the instance ids
 
1480
 
 
1481
      logprintfl(EUCAINFO,"TerminateInstances(): calling terminate instance (%s) on (%s)\n", instId, config->resourcePool[j].hostname);
 
1482
      if (1) {
 
1483
        int pid, status, ret, rbytes;
 
1484
        int filedes[2];
 
1485
        rc = pipe(filedes);
 
1486
        pid = fork();
 
1487
        if (pid == 0) {
 
1488
          close(filedes[0]);
 
1489
          ncs = ncStubCreate(config->resourcePool[j].ncService, NULL, NULL);
 
1490
          if (config->use_wssec) {
 
1491
            rc = InitWSSEC(ncs->env, ncs->stub, config->policyFile);
 
1492
          }
 
1493
          rc = ncTerminateInstanceStub(ncs, ccMeta, instId, &shutdownState, &previousState);
 
1494
          
 
1495
          if (!rc) {
 
1496
            ret = 0;
 
1497
          } else {
 
1498
            ret = 1;
 
1499
          }
 
1500
          close(filedes[1]);      
 
1501
          exit(ret);
 
1502
        } else {
 
1503
          close(filedes[1]);
 
1504
          close(filedes[0]);
 
1505
          
 
1506
          op_timer = OP_TIMEOUT - (time(NULL) - op_start);
 
1507
          rc = timewait(pid, &status, op_timer / ((stop-start) - (j - start)));
 
1508
          rc = WEXITSTATUS(status);
 
1509
          logprintfl(EUCADEBUG,"\tcall complete (pid/rc): %d/%d\n", pid, rc);
 
1510
        }
 
1511
      }
 
1512
      sem_post(configLock);
 
1513
 
 
1514
      if (!rc) {
 
1515
        del_instanceCacheId(instId);
 
1516
        (*outStatus)[i] = 1;
 
1517
        logprintfl(EUCAWARN, "failed to terminate '%s': instance may not exist any longer\n", instId);
 
1518
      } else {
 
1519
        (*outStatus)[i] = 0;
 
1520
      }
 
1521
    }
 
1522
  }
 
1523
  
 
1524
  rc = refresh_resources(ccMeta, OP_TIMEOUT - (time(NULL) - op_start));
 
1525
  
 
1526
  logprintfl(EUCADEBUG,"TerminateInstances(): done.\n");
 
1527
  
 
1528
  shawn();
 
1529
 
 
1530
  return(0);
 
1531
}
 
1532
 
 
1533
int setup_shared_buffer(void **buf, char *bufname, size_t bytes, sem_t **lock, char *lockname) {
 
1534
  int shd, rc;
 
1535
  sem_t *thelock;
 
1536
  
 
1537
  // create a lock and grab it
 
1538
  *lock = sem_open(lockname, O_CREAT, 0644, 1);    
 
1539
  sem_wait(*lock);
 
1540
  
 
1541
  // set up shared memory segment for config
 
1542
  shd = shm_open(bufname, O_CREAT | O_RDWR | O_EXCL, 0644);
 
1543
  if (shd >= 0) {
 
1544
    // if this is the first process to create the config, init to 0
 
1545
    rc = ftruncate(shd, bytes);
 
1546
  } else {
 
1547
    shd = shm_open(bufname, O_CREAT | O_RDWR, 0644);
 
1548
  }
 
1549
  if (shd < 0) {
 
1550
    printf("cannot initialize shared memory segment\n");
 
1551
    sem_post(*lock);
 
1552
    sem_close(*lock);
 
1553
    return(1);
 
1554
  }
 
1555
  *buf = mmap(0, bytes, PROT_READ | PROT_WRITE, MAP_SHARED, shd, 0);
 
1556
  sem_post(*lock);
 
1557
  return(0);
 
1558
}
 
1559
 
 
1560
int sem_timepost(sem_t *sem) {
 
1561
  int rc;
 
1562
  rc = sem_post(sem);
 
1563
  if (rc == 0) {
 
1564
    //    sem_getvalue(sem, &rc);
 
1565
    //    logprintfl(EUCADEBUG, "dropped sem %d %d %08X\n", getpid(), rc, sem);
 
1566
  }
 
1567
  return(rc);
 
1568
}
 
1569
 
 
1570
int sem_timewait(sem_t *sem, time_t seconds) {
 
1571
  int rc;
 
1572
  struct timespec to;
 
1573
 
 
1574
  to.tv_sec = time(NULL) + seconds + 1;
 
1575
  to.tv_nsec = 0;
 
1576
  
 
1577
  rc = sem_timedwait(sem, &to);
 
1578
  if (rc < 0) {
 
1579
    perror("SEM");
 
1580
    logprintfl(EUCAERROR, "timeout waiting for semaphore\n");
 
1581
  } else {
 
1582
  }
 
1583
  return(rc);
 
1584
}
 
1585
 
 
1586
int init_config(void) {
 
1587
  resource *res=NULL;
 
1588
  char *tmpstr=NULL, **hosts=NULL, *hostname=NULL, *ncservice=NULL, *dhcp_deamon;
 
1589
  int ncport, rd, shd, val, rc, i, numHosts, tcount, use_wssec, loglevel, schedPolicy;
 
1590
  
 
1591
  char configFile[1024], netPath[1024], logFile[1024], eucahome[1024], policyFile[1024], buf[1024], *home=NULL, cmd[256];
 
1592
  
 
1593
  axutil_env_t *env = NULL;
 
1594
  FILE *FH=NULL;
 
1595
  
 
1596
  if (init) {
 
1597
    return(0);
 
1598
  }
 
1599
 
 
1600
  initLock = sem_open("/eucalyptusCCinitLock", O_CREAT, 0644, 1);    
 
1601
  sem_wait(initLock);
 
1602
 
 
1603
  if (config == NULL) {
 
1604
    rc = setup_shared_buffer((void **)&config, "/eucalyptusCCConfig", sizeof(ccConfig), &configLock, "/eucalyptusCCConfigLock");
 
1605
    if (rc != 0) {
 
1606
      fprintf(stderr, "Cannot set up shared memory region for ccConfig, exiting...\n");
 
1607
      sem_post(initLock);
 
1608
      exit(1);
 
1609
    }
 
1610
  }
 
1611
  
 
1612
  if (instanceCache == NULL) {
 
1613
    rc = setup_shared_buffer((void **)&instanceCache, "/eucalyptusCCInstanceCache", sizeof(ccInstance) * MAXINSTANCES, &instanceCacheLock, "/eucalyptusCCInstanceCacheLock");
 
1614
    if (rc != 0) {
 
1615
      fprintf(stderr, "Cannot set up shared memory region for ccInstanceCache, exiting...\n");
 
1616
      sem_post(initLock);
 
1617
      exit(1);
 
1618
    }
 
1619
  }
 
1620
  
 
1621
  if (vnetconfig == NULL) {
 
1622
    rc = setup_shared_buffer((void **)&vnetconfig, "/eucalyptusCCVNETConfig", sizeof(vnetConfig), &vnetConfigLock, "/eucalyptusCCVNETConfigLock");
 
1623
    if (rc != 0) {
 
1624
      fprintf(stderr, "Cannot set up shared memory region for ccVNETConfig, exiting...\n");
 
1625
      sem_post(initLock);
 
1626
      exit(1);
 
1627
    }
 
1628
  }
 
1629
  sem_post(initLock);
 
1630
  srand(time(NULL));
 
1631
  
 
1632
  // read in base config information
 
1633
  home = strdup(getenv(EUCALYPTUS_ENV_VAR_NAME));
 
1634
  if (!home) {
 
1635
    home = strdup("");
 
1636
  }
 
1637
  
 
1638
  bzero(configFile, 1024);
 
1639
  bzero(netPath, 1024);
 
1640
  bzero(logFile, 1024);
 
1641
  bzero(policyFile, 1024);
 
1642
 
 
1643
  snprintf(configFile, 1024, EUCALYPTUS_CONF_LOCATION, home);
 
1644
  snprintf(netPath, 1024, CC_NET_PATH_DEFAULT, home);
 
1645
  snprintf(logFile, 1024, "%s/var/log/eucalyptus/cc.log", home);
 
1646
  snprintf(policyFile, 1024, "%s/var/eucalyptus/keys/nc-client-policy.xml", home);
 
1647
  snprintf(eucahome, 1024, "%s/", home);
 
1648
  free(home);
 
1649
  
 
1650
  // now start reading the config file
 
1651
  rc = get_conf_var(configFile, "LOGLEVEL", &tmpstr);
 
1652
  if (rc != 1) {
 
1653
    loglevel = EUCADEBUG;
 
1654
  } else {
 
1655
    if (!strcmp(tmpstr,"DEBUG")) {loglevel=EUCADEBUG;}
 
1656
    else if (!strcmp(tmpstr,"INFO")) {loglevel=EUCAINFO;}
 
1657
    else if (!strcmp(tmpstr,"WARN")) {loglevel=EUCAWARN;}
 
1658
    else if (!strcmp(tmpstr,"ERROR")) {loglevel=EUCAERROR;}
 
1659
    else if (!strcmp(tmpstr,"FATAL")) {loglevel=EUCAFATAL;}
 
1660
    else {loglevel=EUCADEBUG;}
 
1661
  }
 
1662
  if (tmpstr) free(tmpstr);
 
1663
 
 
1664
  // set up logfile
 
1665
  logfile(logFile, loglevel);
 
1666
  
 
1667
  logprintfl(EUCADEBUG,"init_config(): initializing CC configutation\n");  
 
1668
  
 
1669
  // DHCP configuration section
 
1670
  {
 
1671
    char *daemon=NULL,
 
1672
      *dhcpuser=NULL,
 
1673
      *numaddrs=NULL,
 
1674
      *pubmode=NULL,
 
1675
      *pubmacmap=NULL,
 
1676
      *pubips=NULL,
 
1677
      *pubInterface=NULL,
 
1678
      *pubSubnet=NULL,
 
1679
      *pubSubnetMask=NULL,
 
1680
      *pubBroadcastAddress=NULL,
 
1681
      *pubRouter=NULL,
 
1682
      *pubDNS=NULL,
 
1683
      *pubRangeMin=NULL,
 
1684
      *pubRangeMax=NULL,
 
1685
      *privSubnet=NULL,
 
1686
      *privSubnetMask=NULL,
 
1687
      *privBroadcastAddress=NULL;
 
1688
    int initFail=0;
 
1689
    
 
1690
    // DHCP Daemon Configuration Params
 
1691
    daemon = getConfString(configFile, "VNET_DHCPDAEMON");
 
1692
    if (!daemon) {
 
1693
      logprintfl(EUCAWARN,"no VNET_DHCPDAEMON defined in config, using default\n");
 
1694
      daemon = NULL;
 
1695
    }
 
1696
 
 
1697
    dhcpuser = getConfString(configFile, "VNET_DHCPUSER");
 
1698
    if (!dhcpuser) {
 
1699
      dhcpuser = strdup("root");
 
1700
    }
 
1701
 
 
1702
    pubmode = getConfString(configFile, "VNET_MODE");
 
1703
    if (!pubmode) {
 
1704
      logprintfl(EUCAWARN,"VNET_MODE is not defined, defaulting to 'SYSTEM'\n");
 
1705
      pubmode = strdup("SYSTEM");
 
1706
    }
 
1707
    
 
1708
    pubInterface = getConfString(configFile, "VNET_INTERFACE");
 
1709
    if (!pubInterface) {
 
1710
      logprintfl(EUCAWARN,"VNET_INTERFACE is not defined, defaulting to 'eth0'\n");
 
1711
      pubInterface = strdup("eth0");
 
1712
    }
 
1713
    
 
1714
    if (!strcmp(pubmode, "STATIC")) {
 
1715
      pubSubnet = getConfString(configFile, "VNET_SUBNET");
 
1716
      pubSubnetMask = getConfString(configFile, "VNET_NETMASK");
 
1717
      pubBroadcastAddress = getConfString(configFile, "VNET_BROADCAST");
 
1718
      pubRouter = getConfString(configFile, "VNET_ROUTER");
 
1719
      pubDNS = getConfString(configFile, "VNET_DNS");
 
1720
      pubmacmap = getConfString(configFile, "VNET_MACMAP");
 
1721
 
 
1722
      if (!pubSubnet || !pubSubnetMask || !pubBroadcastAddress || !pubRouter || !pubDNS || !pubmacmap) {
 
1723
        logprintfl(EUCAFATAL,"in 'STATIC' network mode, you must specify values for 'VNET_SUBNET, VNET_NETMASK, VNET_BROADCAST, VNET_ROUTER, VNET_DNS, and VNET_MACMAP'\n");
 
1724
        initFail = 1;
 
1725
      } else {
 
1726
      }
 
1727
    } else if (!strcmp(pubmode, "MANAGED") || !strcmp(pubmode, "MANAGED-NOVLAN")) {
 
1728
      numaddrs = getConfString(configFile, "VNET_ADDRSPERNET");
 
1729
      pubSubnet = getConfString(configFile, "VNET_SUBNET");
 
1730
      pubSubnetMask = getConfString(configFile, "VNET_NETMASK");
 
1731
      pubDNS = getConfString(configFile, "VNET_DNS");
 
1732
      pubips = getConfString(configFile, "VNET_PUBLICIPS");
 
1733
      if (!pubSubnet || !pubSubnetMask || !pubDNS || !numaddrs) {
 
1734
        logprintfl(EUCAFATAL,"in 'MANAGED' or 'MANAGED-NOVLAN' network mode, you must specify values for 'VNET_SUBNET, VNET_NETMASK, VNET_ADDRSPERNET, and VNET_DNS'\n");
 
1735
        initFail = 1;
 
1736
      }
 
1737
    }
 
1738
    
 
1739
    if (initFail) {
 
1740
      logprintfl(EUCAFATAL, "bad network parameters, must fix before system will work\n");
 
1741
      return(1);
 
1742
    }
 
1743
    
 
1744
    sem_wait(vnetConfigLock);
 
1745
    
 
1746
    vnetInit(vnetconfig, pubmode, eucahome, netPath, CLC, pubInterface, numaddrs, pubSubnet, pubSubnetMask, pubBroadcastAddress, pubDNS, pubRouter, daemon, dhcpuser, NULL);
 
1747
 
 
1748
    vnetAddDev(vnetconfig, vnetconfig->pubInterface);
 
1749
 
 
1750
    if (pubmacmap) {
 
1751
      char *mac=NULL, *ip=NULL, *ptra=NULL, *toka=NULL, *ptrb=NULL, *tokb=NULL;
 
1752
      toka = strtok_r(pubmacmap, " ", &ptra);
 
1753
      while(toka) {
 
1754
        mac = ip = NULL;
 
1755
        mac = strtok_r(toka, "=", &ptrb);
 
1756
        ip = strtok_r(NULL, "=", &ptrb);
 
1757
        if (mac && ip) {
 
1758
          vnetAddHost(vnetconfig, mac, ip, 0);
 
1759
        }
 
1760
        toka = strtok_r(NULL, " ", &ptra);
 
1761
      }
 
1762
      vnetGenerateDHCP(vnetconfig);
 
1763
      vnetKickDHCP(vnetconfig);
 
1764
    } else if (pubips) {
 
1765
      char *ip, *ptra, *toka;
 
1766
      toka = strtok_r(pubips, " ", &ptra);
 
1767
      while(toka) {
 
1768
        ip = toka;
 
1769
        if (ip) {
 
1770
          rc = vnetAddPublicIP(vnetconfig, ip);
 
1771
          if (rc) {
 
1772
            logprintfl(EUCAERROR, "could not add public IP '%s'\n", ip);
 
1773
          }
 
1774
        }
 
1775
        toka = strtok_r(NULL, " ", &ptra);
 
1776
      }
 
1777
    }
 
1778
    
 
1779
    //    vnetPrintNets(vnetconfig);
 
1780
    sem_post(vnetConfigLock);
 
1781
  }
 
1782
  
 
1783
  rc = get_conf_var(configFile, CONFIG_NC_SERVICE, &tmpstr);
 
1784
  if (rc != 1) {
 
1785
    // error
 
1786
    logprintfl(EUCAFATAL,"parsing config file (%s) for NC_SERVICE\n", configFile);
 
1787
    return(1);
 
1788
  } else {
 
1789
    ncservice = strdup(tmpstr);
 
1790
  }
 
1791
  if (tmpstr) free(tmpstr);
 
1792
 
 
1793
  rc = get_conf_var(configFile, "SCHEDPOLICY", &tmpstr);
 
1794
  if (rc != 1) {
 
1795
    // error
 
1796
    logprintfl(EUCAWARN,"parsing config file (%s) for SCHEDPOLICY, defaulting to GREEDY\n", configFile);
 
1797
    schedPolicy = SCHEDGREEDY;
 
1798
  } else {
 
1799
    if (!strcmp(tmpstr, "GREEDY")) schedPolicy = SCHEDGREEDY;
 
1800
    else if (!strcmp(tmpstr, "ROUNDROBIN")) schedPolicy = SCHEDROUNDROBIN;
 
1801
    else schedPolicy = SCHEDGREEDY;
 
1802
  }
 
1803
  if (tmpstr) free(tmpstr);
 
1804
 
 
1805
  rc = get_conf_var(configFile, CONFIG_NC_PORT, &tmpstr);
 
1806
  if (rc != 1) {
 
1807
    // error
 
1808
    logprintfl(EUCAFATAL,"parsing config file (%s) for NC_PORT\n", configFile);
 
1809
    return(1);
 
1810
  } else {
 
1811
    ncport = atoi(tmpstr);
 
1812
  }
 
1813
  if (tmpstr) free(tmpstr);
 
1814
 
 
1815
  rc = get_conf_var(configFile, "ENABLE_WS_SECURITY", &tmpstr);
 
1816
  if (rc != 1) {
 
1817
    // error
 
1818
    logprintfl(EUCAFATAL,"parsing config file (%s) for ENABLE_WS_SECURITY\n", configFile);
 
1819
    return(1);
 
1820
  } else {
 
1821
    if (!strcmp(tmpstr, "Y")) {
 
1822
      use_wssec = 1;
 
1823
    } else {
 
1824
      use_wssec = 0;
 
1825
    }
 
1826
  }
 
1827
  if (tmpstr) free(tmpstr);
 
1828
  
 
1829
  rc = get_conf_var(configFile, CONFIG_NODES, &tmpstr);
 
1830
  if (rc != 1) {
 
1831
    // error
 
1832
    logprintfl(EUCAFATAL,"parsing config file (%s) for NODES\n", configFile);
 
1833
    return(1);
 
1834
  } else {
 
1835
    hosts = from_var_to_char_list(tmpstr);
 
1836
    if (hosts == NULL) {
 
1837
      logprintfl(EUCAFATAL,"parsing config file (%s) for NODES from substring (%s)\n", configFile, tmpstr);
 
1838
      if (tmpstr) free(tmpstr);
 
1839
      return(1);
 
1840
    }
 
1841
    res = NULL;
 
1842
    numHosts = 0;
 
1843
    i = 0;
 
1844
    while(hosts[i] != NULL) {
 
1845
      numHosts++;
 
1846
      res = realloc(res, sizeof(resource) * numHosts);
 
1847
      strncpy(res[numHosts-1].hostname, hosts[i], 96);  
 
1848
      snprintf(res[numHosts-1].ncService, 128, "http://%s:%d/%s", hosts[i], ncport, ncservice); 
 
1849
      free(hosts[i]);
 
1850
      i++;
 
1851
    }
 
1852
  }
 
1853
  if (hosts) free(hosts);
 
1854
  if (tmpstr) free(tmpstr);
 
1855
  
 
1856
  sem_wait(configLock);
 
1857
  // set up the current config   
 
1858
  strncpy(config->eucahome, eucahome, 1024);
 
1859
  strncpy(config->policyFile, policyFile, 1024);
 
1860
  config->use_wssec = use_wssec;
 
1861
  config->schedPolicy = schedPolicy;
 
1862
  config->numResources = numHosts;
 
1863
  memcpy(config->resourcePool, res, sizeof(resource) * numHosts);
 
1864
  if (res) free(res);
 
1865
  config->lastResourceUpdate = 0;
 
1866
  config->instanceCacheUpdate = time(NULL);
 
1867
  config->initialized = 1;
 
1868
  
 
1869
  sem_post(configLock);
 
1870
  
 
1871
  logprintfl(EUCADEBUG,"init_config(): done\n");
 
1872
  init=1;
 
1873
  
 
1874
  return(0);
 
1875
}
 
1876
 
 
1877
void shawn() {
 
1878
  int p=1, status;
 
1879
 
 
1880
  // clean up any orphaned child processes
 
1881
  while(p > 0) {
 
1882
    p = waitpid(-1, &status, WNOHANG);
 
1883
  }
 
1884
  if (time(NULL) - config->instanceCacheUpdate > 86400) {
 
1885
    config->instanceCacheUpdate = time(NULL);
 
1886
  }
 
1887
  
 
1888
  //  deadlock detection
 
1889
  //  rc = sem_getvalue(configLock,&status);
 
1890
}
 
1891
 
 
1892
int timeread(int fd, void *buf, size_t bytes, int timeout) {
 
1893
  int rc;
 
1894
  fd_set rfds;
 
1895
  struct timeval tv;
 
1896
 
 
1897
  if (timeout <= 0) timeout = 1;
 
1898
 
 
1899
  FD_ZERO(&rfds);
 
1900
  FD_SET(fd, &rfds);
 
1901
  
 
1902
  tv.tv_sec = timeout;
 
1903
  tv.tv_usec = 0;
 
1904
  
 
1905
  rc = select(fd+1, &rfds, NULL, NULL, &tv);
 
1906
  if (rc <= 0) {
 
1907
    // timeout
 
1908
    logprintfl(EUCAERROR, "select() timed out for read: timeout=%d\n", timeout);
 
1909
    return(-1);
 
1910
  }
 
1911
  rc = read(fd, buf, bytes);
 
1912
  return(rc);
 
1913
}
 
1914
 
 
1915
pid_t timewait(pid_t pid, int *status, int timeout) {
 
1916
  time_t timer=0;
 
1917
  int rc;
 
1918
 
 
1919
  if (timeout <= 0) timeout = 1;
 
1920
 
 
1921
  *status = 1;
 
1922
  rc = waitpid(pid, status, WNOHANG);
 
1923
  while(rc <= 0 && timer < (timeout * 1000000)) {
 
1924
    usleep(50000);
 
1925
    timer += 50000;
 
1926
    rc = waitpid(pid, status, WNOHANG);
 
1927
  }
 
1928
  if (rc < 0) {
 
1929
    logprintfl(EUCAERROR, "waitpid() timed out: pid=%d\n", pid);
 
1930
  }
 
1931
  return(rc);
 
1932
}
 
1933
 
 
1934
int allocate_ccInstance(ccInstance *out, char *id, char *amiId, char *kernelId, char *ramdiskId, char *amiURL, char *kernelURL, char *ramdiskURL, char *ownerId, char *state, time_t ts, char *reservationId, netConfig *ccnet, virtualMachine *ccvm, int ncHostIdx, char *keyName, char *serviceTag, char *userData, char *launchIndex, char groupNames[][32], ncVolume *volumes, int volumesSize) {
 
1935
  if (out != NULL) {
 
1936
    bzero(out, sizeof(ccInstance));
 
1937
    if (id) strncpy(out->instanceId, id, 16);
 
1938
    if (amiId) strncpy(out->amiId, amiId, 16);
 
1939
    if (kernelId) strncpy(out->kernelId, kernelId, 16);
 
1940
    if (ramdiskId) strncpy(out->ramdiskId, ramdiskId, 16);
 
1941
    
 
1942
    if (amiURL) strncpy(out->amiURL, amiURL, 64);
 
1943
    if (kernelURL) strncpy(out->kernelURL, kernelURL, 64);
 
1944
    if (ramdiskURL) strncpy(out->ramdiskURL, ramdiskURL, 64);
 
1945
    
 
1946
    if (state) strncpy(out->state, state, 16);
 
1947
    if (ownerId) strncpy(out->ownerId, ownerId, 16);
 
1948
    if (reservationId) strncpy(out->reservationId, reservationId, 16);
 
1949
    if (keyName) strncpy(out->keyName, keyName, 1024);
 
1950
    out->ts = ts;
 
1951
    out->ncHostIdx = ncHostIdx;
 
1952
    if (serviceTag) strncpy(out->serviceTag, serviceTag, 64);
 
1953
    if (userData) strncpy(out->userData, userData, 64);
 
1954
    if (launchIndex) strncpy(out->launchIndex, launchIndex, 64);
 
1955
    if (groupNames) {
 
1956
      int i;
 
1957
      for (i=0; i<64; i++) {
 
1958
        if (groupNames[i]) {
 
1959
          strncpy(out->groupNames[i], groupNames[i], 32);
 
1960
        }
 
1961
      }
 
1962
    }
 
1963
 
 
1964
    if (volumes) {
 
1965
      memcpy(out->volumes, volumes, sizeof(ncVolume) * EUCA_MAX_VOLUMES);
 
1966
    }
 
1967
    out->volumesSize = volumesSize;
 
1968
 
 
1969
    if (ccnet) allocate_netConfig(&(out->ccnet), ccnet->privateMac, ccnet->publicMac, ccnet->privateIp, ccnet->publicIp, ccnet->vlan);
 
1970
    if (ccvm) allocate_virtualMachine(&(out->ccvm), ccvm->mem, ccvm->disk, ccvm->cores, ccvm->name);    
 
1971
  }
 
1972
  return(0);
 
1973
}
 
1974
 
 
1975
int allocate_netConfig(netConfig *out, char *pvMac, char *pbMac, char *pvIp, char *pbIp, int vlan) {
 
1976
  if (out != NULL) {
 
1977
    if (pvMac) strncpy(out->privateMac,pvMac,24);
 
1978
    if (pbMac) strncpy(out->publicMac,pbMac,24);
 
1979
    if (pvIp) strncpy(out->privateIp,pvIp,24);
 
1980
    if (pbIp) strncpy(out->publicIp,pbIp,24);
 
1981
    out->vlan = vlan;
 
1982
  }
 
1983
  return(0);
 
1984
}
 
1985
 
 
1986
int allocate_virtualMachine(virtualMachine *out, int mem, int disk, int cores, char *name) {
 
1987
  if (out != NULL) {
 
1988
    out->mem = mem;
 
1989
    out->disk = disk;
 
1990
    out->cores = cores;
 
1991
    snprintf(out->name, 64, "%s", name);
 
1992
  }
 
1993
  return(0);
 
1994
}
 
1995
 
 
1996
void print_instanceCache(void) {
 
1997
  int i;
 
1998
  for (i=0; i<MAXINSTANCES; i++) {
 
1999
    if (instanceCache[i].instanceId[0] != '\0') {
 
2000
      logprintfl(EUCADEBUG,"\tcache: %s\n", instanceCache[i].instanceId);
 
2001
    }
 
2002
  }
 
2003
}
 
2004
 
 
2005
void invalidate_instanceCache(void) {
 
2006
  int i;
 
2007
  for (i=0; i<MAXINSTANCES; i++) {
 
2008
    if (instanceCache[i].instanceId[0] != '\0') {
 
2009
      // del from cache
 
2010
      bzero(&(instanceCache[i]), sizeof(ccInstance));
 
2011
    }
 
2012
  }
 
2013
}
 
2014
 
 
2015
int refresh_instanceCache(char *instanceId, ccInstance *in){
 
2016
  int i, done, firstNull;
 
2017
 
 
2018
  if (!instanceId || !in) {
 
2019
    return(1);
 
2020
  }
 
2021
  
 
2022
  done=0;
 
2023
  for (i=0; i<MAXINSTANCES && !done; i++) {
 
2024
    if (instanceCache[i].instanceId[0] != '\0') {
 
2025
      if (!strcmp(instanceCache[i].instanceId, instanceId)) {
 
2026
        // in cache
 
2027
        logprintfl(EUCADEBUG, "refreshing instance '%s'\n", instanceId);
 
2028
        memcpy(&(instanceCache[i]), in, sizeof(ccInstance));
 
2029
        return(0);
 
2030
      }
 
2031
    }
 
2032
  }
 
2033
  return(0);
 
2034
}
 
2035
 
 
2036
int add_instanceCache(char *instanceId, ccInstance *in){
 
2037
  int i, done, firstNull=0;
 
2038
 
 
2039
  if (!instanceId || !in) {
 
2040
    return(1);
 
2041
  }
 
2042
  
 
2043
  done=0;
 
2044
  for (i=0; i<MAXINSTANCES && !done; i++) {
 
2045
    if (instanceCache[i].instanceId[0] != '\0') {
 
2046
      if (!strcmp(instanceCache[i].instanceId, instanceId)) {
 
2047
        // already in cache
 
2048
        return(0);
 
2049
      }
 
2050
    } else {
 
2051
      firstNull = i;
 
2052
      done++;
 
2053
    }
 
2054
  }
 
2055
  if (!done) {
 
2056
  }
 
2057
  allocate_ccInstance(&(instanceCache[firstNull]), in->instanceId, in->amiId, in->kernelId, in->ramdiskId, in->amiURL, in->kernelURL, in->ramdiskURL, in->ownerId, in->state, in->ts, in->reservationId, &(in->ccnet), &(in->ccvm), in->ncHostIdx, in->keyName, in->serviceTag, in->userData, in->launchIndex, in->groupNames, in->volumes, in->volumesSize);
 
2058
  return(0);
 
2059
}
 
2060
 
 
2061
int del_instanceCacheId(char *instanceId) {
 
2062
  int i;
 
2063
 
 
2064
  for (i=0; i<MAXINSTANCES; i++) {
 
2065
    if (instanceCache[i].instanceId[0] != '\0') {
 
2066
      if (!strcmp(instanceCache[i].instanceId, instanceId)) {
 
2067
        // del from cache
 
2068
        bzero(&(instanceCache[i]), sizeof(ccInstance));
 
2069
        return(0);
 
2070
      }
 
2071
    }
 
2072
  }
 
2073
  return(0);
 
2074
}
 
2075
 
 
2076
int find_instanceCacheId(char *instanceId, ccInstance **out) {
 
2077
  int i, done;
 
2078
  
 
2079
  if (!instanceId || !out) {
 
2080
    return(1);
 
2081
  }
 
2082
  
 
2083
  *out = NULL;
 
2084
  done=0;
 
2085
  for (i=0; i<MAXINSTANCES && !done; i++) {
 
2086
    if (instanceCache[i].instanceId[0] != '\0') {
 
2087
      if (!strcmp(instanceCache[i].instanceId, instanceId)) {
 
2088
        // found it
 
2089
        *out = malloc(sizeof(ccInstance));
 
2090
        allocate_ccInstance(*out, instanceCache[i].instanceId,instanceCache[i].amiId, instanceCache[i].kernelId, instanceCache[i].ramdiskId, instanceCache[i].amiURL, instanceCache[i].kernelURL, instanceCache[i].ramdiskURL, instanceCache[i].ownerId, instanceCache[i].state,instanceCache[i].ts, instanceCache[i].reservationId, &(instanceCache[i].ccnet), &(instanceCache[i].ccvm), instanceCache[i].ncHostIdx, instanceCache[i].keyName, instanceCache[i].serviceTag, instanceCache[i].userData, instanceCache[i].launchIndex, instanceCache[i].groupNames, instanceCache[i].volumes, instanceCache[i].volumesSize);
 
2091
        done++;
 
2092
      }
 
2093
    }
 
2094
  }
 
2095
  if (done) {
 
2096
    return(0);
 
2097
  }
 
2098
  return(1);
 
2099
}
 
2100
 
 
2101
int find_instanceCacheIP(char *ip, ccInstance **out) {
 
2102
  int i, done;
 
2103
  
 
2104
  if (!ip || !out) {
 
2105
    return(1);
 
2106
  }
 
2107
  
 
2108
  *out = NULL;
 
2109
  done=0;
 
2110
  for (i=0; i<MAXINSTANCES && !done; i++) {
 
2111
    if (instanceCache[i].ccnet.publicIp[0] != '\0' || instanceCache[i].ccnet.privateIp[0] != '\0') {
 
2112
      if (!strcmp(instanceCache[i].ccnet.publicIp, ip) || !strcmp(instanceCache[i].ccnet.privateIp, ip)) {
 
2113
        // found it
 
2114
        *out = malloc(sizeof(ccInstance));
 
2115
        allocate_ccInstance(*out, instanceCache[i].instanceId,instanceCache[i].amiId, instanceCache[i].kernelId, instanceCache[i].ramdiskId, instanceCache[i].amiURL, instanceCache[i].kernelURL, instanceCache[i].ramdiskURL, instanceCache[i].ownerId, instanceCache[i].state,instanceCache[i].ts, instanceCache[i].reservationId, &(instanceCache[i].ccnet), &(instanceCache[i].ccvm), instanceCache[i].ncHostIdx, instanceCache[i].keyName, instanceCache[i].serviceTag, instanceCache[i].userData, instanceCache[i].launchIndex, instanceCache[i].groupNames, instanceCache[i].volumes, instanceCache[i].volumesSize);
 
2116
        done++;
 
2117
      }
 
2118
    }
 
2119
  }
 
2120
  if (done) {
 
2121
    return(0);
 
2122
  }
 
2123
  return(1);
 
2124
}
 
2125