int BEE::Room::set_instance(int index, InstanceData* new_instance) { if (instances.find(index) != instances.end()) { // if the instance exists, overwrite it remove_instance(index); } instances.insert(std::pair<int,InstanceData*>(index, new_instance)); return 0; }
int BEE::Room::reset_properties() { for (auto& i : instances) { i.second->object->update(i.second); i.second->object->destroy(i.second); remove_instance(i.second->id); } instances.clear(); instances_sorted.clear(); destroyed_instances.clear(); next_instance_id = 0; tree_x = 0; tree_y = 0; tree_width = get_width(); collision_tree->game = game; collision_tree->reset(tree_x, tree_y, tree_width); // Reset background data for (auto& b : backgrounds) { b.second->background->set_time_update(); delete b.second; } backgrounds.clear(); for (auto& p : particles) { p.second->clear(); } particles.clear(); return 0; }
static void vlc_did_destroy(PP_Instance pp) { instance_t* instance = get_instance(pp); if (instance == NULL) { return; /* TODO: log */ } if(remove_instance(instance) == PP_FALSE) { assert(PP_FALSE && "failed to remove an instance"); return; } }
int BEE::Room::destroy() { for (auto& i : destroyed_instances) { i->object->update(i); i->object->destroy(i); remove_instance(i->id); } //instances_sorted.clear(); destroyed_instances.clear(); return 0; }
void Profile::start_shared_instance_withkey(std::string id, std::string name, std::string key_b64) { BM_SK* instance = dynamic_cast<BM_SK*>(load_instance(id)); if (instance) { // remove instance remove_instance(id); } instance = new BM_SK(id, key_b64); // Record this instance with a new storage InstanceStore *store = new InstanceStore(name, instance); // Insert the record to known instances instances.insert(id, store); // Keep the instances loaded loaded_instances.insert(id,instance); }
/* ** This is the process spawned by pthread_create, into which we pass StateInfo. ** Using pthread_self, we relocate the appropriate instance and set up some ** variables for the game, log some stuff to log, send a welcome message, etc. ** We then enter the main game loop, waiting for messages from the client ** which we then handle with game_step. Once the game ends, we remove the ** appropriate instance, close the socket and print to log before finally ** exiting the thread. */ void *run_instance(void *param) { /* ** Because we can only pass one thing into this function, we pass the ** thing with the greatest amount of useful info, the Instances struct. ** We then find again the target Instance by using the thread_id, ** which we can find out using pthread_self() passed to get_instance(). */ StateInfo *state_info = (StateInfo*)param; Instance *instance = get_instance(state_info, pthread_self()); char *correct = instance->code; int sock_id = instance->s; char *ip4 = instance->ip4; char log_buf[LOG_MSG_LEN]; send_welcome(sock_id); // Add our own sentinel for printing purposes. char msg[CODE_LENGTH+1]; // Main game loop. We don't have to worry about the len that recv returns // because we know we will get 4 chars and these are all that concern us. while (recv(sock_id, &msg, CODE_LENGTH, 0)) { msg[CODE_LENGTH] = '\0'; if (game_step(msg, correct, instance) == 0) break; } remove_instance(state_info, instance->t); close(sock_id); sprintf(log_buf, "(%s)(%d) Client disconnected.\n", ip4, sock_id); write_log(log_buf); pthread_exit(NULL); }
int remove_step(const char *anme, int height, int width) { int ch; curs_set(0); attron(A_BOLD); mvprintw(0, 0, name); mvprintw(0, width - strlen(vers), vers); mvprintw(height - 1, width - strlen(quit) - 1, quit); mvprintw(6, 10, "Removing '%s' instance...", anme); attroff(A_BOLD); if (remove_instance(anme)) { move(8, 10); clrtoeol(); mvprintw(8, 10, "Cleaning up configuration... Done."); move(6, 10); clrtoeol(); attron(A_BOLD); mvprintw(6, 10, "Removing '%s' instance... Done.", anme); attroff(A_BOLD); } ch = getch(); curs_set(1); return ch; }
//! //! Handles the instance migration request. //! //! @param[in] nc a pointer to the node controller (NC) state //! @param[in] pMeta a pointer to the node controller (NC) metadata structure //! @param[in] instances metadata for the instance to migrate to destination //! @param[in] instancesLen number of instances in the instance list //! @param[in] action IP of the destination Node Controller //! @param[in] credentials credentials that enable the migration //! //! @return EUCA_OK on success or EUCA_*ERROR on failure //! //! @pre //! //! @post static int doMigrateInstances(struct nc_state_t *nc, ncMetadata * pMeta, ncInstance ** instances, int instancesLen, char *action, char *credentials, char ** resourceLocations, int resourceLocationsLen) { int ret = EUCA_OK; int credentials_prepared = 0; char *libvirt_xml_modified = NULL; if (instancesLen <= 0) { LOGERROR("called with invalid instancesLen (%d)\n", instancesLen); pMeta->replyString = strdup("internal error (invalid instancesLen)"); return (EUCA_INVALID_ERROR); } LOGDEBUG("verifying %d instance[s] for migration...\n", instancesLen); for (int inst_idx = 0; inst_idx < instancesLen; inst_idx++) { LOGDEBUG("verifying instance # %d...\n", inst_idx); if (instances[inst_idx]) { ncInstance *instance_idx = instances[inst_idx]; LOGDEBUG("[%s] proposed migration action '%s' (%s > %s) [creds=%s]\n", SP(instance_idx->instanceId), SP(action), SP(instance_idx->migration_src), SP(instance_idx->migration_dst), (instance_idx->migration_credentials == NULL) ? "UNSET" : "present"); } else { pMeta->replyString = strdup("internal error (instance count mismatch)"); LOGERROR("Mismatch between migration instance count (%d) and length of instance list\n", instancesLen); return (EUCA_ERROR); } } // TO-DO: Optimize the location of this loop, placing it inside various conditionals below? for (int inst_idx = 0; inst_idx < instancesLen; inst_idx++) { ncInstance *instance_req = instances[inst_idx]; char *sourceNodeName = instance_req->migration_src; char *destNodeName = instance_req->migration_dst; LOGDEBUG("[%s] processing instance # %d (%s > %s)\n", instance_req->instanceId, inst_idx, instance_req->migration_src, instance_req->migration_dst); // this is a call to the source of migration if (!strcmp(pMeta->nodeName, sourceNodeName)) { // locate the instance structure ncInstance *instance; sem_p(inst_sem); { instance = find_instance(&global_instances, instance_req->instanceId); } sem_v(inst_sem); if (instance == NULL) { LOGERROR("[%s] cannot find instance\n", instance_req->instanceId); pMeta->replyString = strdup("failed to locate instance to migrate"); return (EUCA_NOT_FOUND_ERROR); } if (strcmp(action, "prepare") == 0) { sem_p(inst_sem); instance->migration_state = MIGRATION_PREPARING; euca_strncpy(instance->migration_src, sourceNodeName, HOSTNAME_SIZE); euca_strncpy(instance->migration_dst, destNodeName, HOSTNAME_SIZE); euca_strncpy(instance->migration_credentials, credentials, CREDENTIAL_SIZE); instance->migrationTime = time(NULL); update_resource_locations(&(instance->params), resourceLocations, resourceLocationsLen); save_instance_struct(instance); copy_instances(); sem_v(inst_sem); // Establish migration-credential keys if this is the first instance preparation for this host. LOGINFO("[%s] migration source preparing %s > %s [creds=%s]\n", SP(instance->instanceId), SP(instance->migration_src), SP(instance->migration_dst), (instance->migration_credentials == NULL) ? "UNSET" : "present"); if (!credentials_prepared) { if (generate_migration_keys(sourceNodeName, credentials, TRUE, instance) != EUCA_OK) { pMeta->replyString = strdup("internal error (migration credentials generation failed)"); return (EUCA_SYSTEM_ERROR); } else { credentials_prepared++; } } sem_p(inst_sem); instance->migration_state = MIGRATION_READY; save_instance_struct(instance); copy_instances(); sem_v(inst_sem); } else if (strcmp(action, "commit") == 0) { sem_p(inst_sem); if (instance->migration_state == MIGRATION_IN_PROGRESS) { LOGWARN("[%s] duplicate request to migration source to initiate %s > %s (already migrating)\n", instance->instanceId, instance->migration_src, instance->migration_dst); sem_v(inst_sem); return (EUCA_DUPLICATE_ERROR); } else if (instance->migration_state != MIGRATION_READY) { LOGERROR("[%s] request to commit migration %s > %s when source migration_state='%s' (not 'ready')\n", instance->instanceId, SP(sourceNodeName), SP(destNodeName), migration_state_names[instance->migration_state]); sem_v(inst_sem); return (EUCA_UNSUPPORTED_ERROR); } instance->migration_state = MIGRATION_IN_PROGRESS; outgoing_migrations_in_progress++; LOGINFO("[%s] migration source initiating %s > %s [creds=%s] (1 of %d active outgoing migrations)\n", instance->instanceId, instance->migration_src, instance->migration_dst, (instance->migration_credentials == NULL) ? "UNSET" : "present", outgoing_migrations_in_progress); save_instance_struct(instance); copy_instances(); sem_v(inst_sem); // since migration may take a while, we do them in a thread pthread_t tcb = { 0 }; if (pthread_create(&tcb, NULL, migrating_thread, (void *)instance)) { LOGERROR("[%s] failed to spawn a migration thread\n", instance->instanceId); return (EUCA_THREAD_ERROR); } set_corrid_pthread(get_corrid() != NULL ? get_corrid()->correlation_id : NULL, tcb); if (pthread_detach(tcb)) { LOGERROR("[%s] failed to detach the migration thread\n", instance->instanceId); return (EUCA_THREAD_ERROR); } } else if (strcmp(action, "rollback") == 0) { if ((instance->migration_state == MIGRATION_READY) || (instance->migration_state == MIGRATION_PREPARING)) { LOGINFO("[%s] rolling back migration (%s > %s) on source\n", instance->instanceId, instance->migration_src, instance->migration_dst); sem_p(inst_sem); migration_rollback(instance); sem_v(inst_sem); } else { LOGINFO("[%s] ignoring request to roll back migration on source with instance in state %s(%s) -- duplicate rollback request?\n", instance->instanceId, instance->stateName, migration_state_names[instance->migration_state]); } } else { LOGERROR("[%s] action '%s' is not valid\n", instance->instanceId, action); return (EUCA_INVALID_ERROR); } } else if (!strcmp(pMeta->nodeName, destNodeName)) { // this is a migrate request to destination if (!strcmp(action, "commit")) { LOGERROR("[%s] action '%s' for migration (%s > %s) is not valid on destination node\n", instance_req->instanceId, action, SP(sourceNodeName), SP(destNodeName)); return (EUCA_UNSUPPORTED_ERROR); } else if (!strcmp(action, "rollback")) { LOGINFO("[%s] rolling back migration (%s > %s) on destination\n", instance_req->instanceId, SP(sourceNodeName), SP(destNodeName)); sem_p(inst_sem); { ncInstance *instance = find_instance(&global_instances, instance_req->instanceId); if (instance != NULL) { LOGDEBUG("[%s] marked for cleanup\n", instance->instanceId); change_state(instance, SHUTOFF); instance->migration_state = MIGRATION_CLEANING; save_instance_struct(instance); } } sem_v(inst_sem); return EUCA_OK; } else if (strcmp(action, "prepare") != 0) { LOGERROR("[%s] action '%s' is not valid or not implemented\n", instance_req->instanceId, action); return (EUCA_INVALID_ERROR); } // Everything from here on is specific to "prepare" on a destination. // allocate a new instance struct ncInstance *instance = clone_instance(instance_req); if (instance == NULL) { LOGERROR("[%s] could not allocate instance struct\n", instance_req->instanceId); goto failed_dest; } sem_p(inst_sem); instance->migration_state = MIGRATION_PREPARING; instance->migrationTime = time(NULL); //In preparing state, so set migrationTime. euca_strncpy(instance->migration_src, sourceNodeName, HOSTNAME_SIZE); euca_strncpy(instance->migration_dst, destNodeName, HOSTNAME_SIZE); euca_strncpy(instance->migration_credentials, credentials, CREDENTIAL_SIZE); update_resource_locations(&(instance->params), resourceLocations, resourceLocationsLen); sem_v(inst_sem); // Establish migration-credential keys. LOGINFO("[%s] migration destination preparing %s > %s [creds=%s]\n", instance->instanceId, SP(instance->migration_src), SP(instance->migration_dst), (instance->migration_credentials == NULL) ? "UNSET" : "present"); // First, call config-file modification script to authorize source node. LOGDEBUG("[%s] authorizing migration source node %s\n", instance->instanceId, instance->migration_src); if (authorize_migration_keys("-a", instance->migration_src, instance->migration_credentials, instance, TRUE) != EUCA_OK) { goto failed_dest; } // Then, generate keys and restart libvirtd. if (generate_migration_keys(instance->migration_dst, instance->migration_credentials, TRUE, instance) != EUCA_OK) { goto failed_dest; } int error; //Fix for EUCA-10433, need instance struct in global_instances prior to doing volume ops //The monitor thread will now pick up the instance, so the migrationTime must be set sem_p(inst_sem); save_instance_struct(instance); error = add_instance(&global_instances, instance); copy_instances(); sem_v(inst_sem); if (error) { if (error == EUCA_DUPLICATE_ERROR) { LOGINFO("[%s] instance struct already exists (from previous migration?), deleting and re-adding...\n", instance->instanceId); error = remove_instance(&global_instances, instance); if (error) { LOGERROR("[%s] could not replace (remove) instance struct, failing...\n", instance->instanceId); goto failed_dest; } error = add_instance(&global_instances, instance); if (error) { LOGERROR("[%s] could not replace (add) instance struct, failing...\n", instance->instanceId); goto failed_dest; } } else { LOGERROR("[%s] could not add instance struct, failing...\n", instance->instanceId); goto failed_dest; } } if (vbr_parse(&(instance->params), pMeta) != EUCA_OK) { goto failed_dest; } // set up networking char brname[IF_NAME_LEN] = ""; if (!strcmp(nc->pEucaNet->sMode, NETMODE_MANAGED)) { snprintf(brname, IF_NAME_LEN, "%s", instance->groupIds[0]); } else { snprintf(brname, IF_NAME_LEN, "%s", nc->pEucaNet->sBridgeDevice); } euca_strncpy(instance->params.guestNicDeviceName, brname, sizeof(instance->params.guestNicDeviceName)); // TODO: move stuff in startup_thread() into a function? set_instance_params(instance); if ((error = create_instance_backing(instance, TRUE)) // create files that back the disks || (error = gen_instance_xml(instance)) // create euca-specific instance XML file || (error = gen_libvirt_instance_xml(instance))) { // transform euca-specific XML into libvirt XML LOGERROR("[%s] failed to prepare images for migrating instance (error=%d)\n", instance->instanceId, error); goto failed_dest; } // attach any volumes for (int v = 0; v < EUCA_MAX_VOLUMES; v++) { ncVolume *volume = &instance->volumes[v]; if (strcmp(volume->stateName, VOL_STATE_ATTACHED) && strcmp(volume->stateName, VOL_STATE_ATTACHING)) continue; // skip the entry unless attached or attaching LOGDEBUG("[%s] volumes [%d] = '%s'\n", instance->instanceId, v, volume->stateName); ebs_volume_data *vol_data = NULL; char *libvirt_xml = NULL; char serial[128]; char bus[16]; set_serial_and_bus(volume->volumeId, volume->devName, serial, sizeof(serial), bus, sizeof(bus)); if ((ret = connect_ebs(volume->devName, serial, bus, nc, instance->instanceId, volume->volumeId, volume->attachmentToken, &libvirt_xml, &vol_data)) != EUCA_OK) { goto unroll; } // update the volume struct with connection string obtained from SC euca_strncpy(volume->connectionString, vol_data->connect_string, sizeof(volume->connectionString)); // save volume info into vol-XXX-libvirt.xml for future detach if (create_vol_xml(instance->instanceId, volume->volumeId, libvirt_xml, &libvirt_xml_modified) != EUCA_OK) { goto unroll; } continue; unroll: ret = EUCA_ERROR; // @TODO: unroll all previous ones // for (int uv = v - 1; uv >= 0; uv--) { // disconnect_ebs(nc, instance->instanceId, volume->volumeId, ) // } goto failed_dest; } // build any secondary network interface xml files for (int w=0; w < EUCA_MAX_NICS; w++) { if (strlen(instance->secNetCfgs[w].interfaceId) == 0) continue; gen_libvirt_nic_xml(instance->instancePath, instance->secNetCfgs[w].interfaceId); } sem_p(inst_sem); instance->migration_state = MIGRATION_READY; instance->migrationTime = 0; //Reset the timer, to ensure monitoring thread handles this properly. This is required when setting BOOTING state instance->bootTime = time(NULL); // otherwise nc_state.booting_cleanup_threshold will kick in change_state(instance, BOOTING); // not STAGING, since in that mode we don't poll hypervisor for info LOGINFO("[%s] migration destination ready %s > %s\n", instance->instanceId, instance->migration_src, instance->migration_dst); save_instance_struct(instance); copy_instances(); sem_v(inst_sem); continue; failed_dest: sem_p(inst_sem); // Just making sure... if (instance != NULL) { LOGERROR("[%s] setting instance to Teardown(cleaning) after destination failure to prepare for migration\n", instance->instanceId); // Set state to Teardown(cleaning) so source won't wait until timeout to roll back. instance->migration_state = MIGRATION_CLEANING; instance->terminationTime = time(NULL); change_state(instance, TEARDOWN); save_instance_struct(instance); add_instance(&global_instances, instance); // OK if this fails--that should mean it's already been added. copy_instances(); } // If no remaining incoming or pending migrations, deauthorize all clients. // TO-DO: Consolidate with similar sequence in handlers.c into a utility function? if (!incoming_migrations_in_progress) { int incoming_migrations_pending = 0; LOGINFO("[%s] no remaining active incoming migrations -- checking to see if there are any pending migrations\n", instance->instanceId); bunchOfInstances *head = NULL; for (head = global_instances; head; head = head->next) { if ((head->instance->migration_state == MIGRATION_PREPARING) || (head->instance->migration_state == MIGRATION_READY)) { LOGINFO("[%s] is pending migration, state='%s', deferring deauthorization of migration keys\n", head->instance->instanceId, migration_state_names[head->instance->migration_state]); incoming_migrations_pending++; } } // TO-DO: Add belt and suspenders? if (!incoming_migrations_pending) { LOGINFO("[%s] no remaining incoming or pending migrations -- deauthorizing all migration client keys\n", instance->instanceId); authorize_migration_keys("-D -r", NULL, NULL, NULL, FALSE); } } sem_v(inst_sem); // Set to generic EUCA_ERROR unless already set to a more-specific error. if (ret == EUCA_OK) { ret = EUCA_ERROR; } } else { LOGERROR("unexpected migration request (node %s is neither source nor destination)\n", pMeta->nodeName); ret = EUCA_ERROR; } } return ret; }
int main (int argc, char **argv) { printf ("=====> testing misc.c\n"); test_command("date"); test_command("ls / -l | sort"); test_command("/foo"); { char c = 0; long l = 0; int i = 0; long long ll = 0; sscanf_lines ("a1\na\na2\n", "a%d", &i); assert (i==1); sscanf_lines ("a\nab3\na 4\na5", "a %d", &i); assert (i==4); sscanf_lines ("", "%d", &i); sscanf_lines ("\n\n\n", "%d", &i); sscanf_lines ("abcdefg6", "g%d", &i); assert (i!=6); sscanf_lines ("abcdefg", "ab%cdefg", &c); assert (c=='c'); sscanf_lines ("a\na 7\na\n", "a %ld", &l); assert (l==7L); sscanf_lines ("a\n8a\na9\n", "a %lld", &ll); assert (ll==9L); } printf ("=====> testing data.c\n"); { #define INSTS 50 bunchOfInstances * bag = NULL; ncInstance * inst = NULL; ncInstance * Insts[INSTS]; int i, n; printf ("========> testing instance struct management\n"); free_instance (NULL); free_instance (&inst); inst = allocate_instance ("the-uuid", "i1", NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0); assert(inst!=NULL); free_instance (&inst); assert(inst==NULL); n = total_instances (&bag); assert(n==0); bag=NULL; inst = find_instance(&bag, "foo"); assert(inst==NULL); bag=NULL; n = remove_instance(&bag, NULL); assert(n!=0); bag=NULL; for (i=0; i<INSTS; i++) { char id[10]; sprintf(id, "i-%d", i); inst = Insts[i] = allocate_instance ("the-uuid", id, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0); assert (inst!=NULL); n = add_instance(&bag, inst); assert (n==0); } n = total_instances (&bag); assert (n==INSTS); n = remove_instance(&bag, Insts[0]); assert (n==0); n = remove_instance(&bag, Insts[INSTS-1]); assert (n==0); n = total_instances (&bag); assert (n==INSTS-2); printf ("========> testing volume struct management\n"); ncVolume * v; inst = allocate_instance ("the-uuid", "i2", NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0); assert(inst!=NULL); for (i=0; i<EUCA_MAX_VOLUMES; i++) { char id[10]; sprintf (id, "v-%d", i); v = save_volume (inst, id, "rd", "ld", "ldr", VOL_STATE_ATTACHED); assert (v!=NULL); } assert (is_volume_used (v)); assert (save_volume (inst, "too-much", "rd", "ld", "ldr", VOL_STATE_ATTACHED)==NULL); assert (save_volume (inst, v->volumeId, NULL, NULL, NULL, NULL)!=NULL); assert (save_volume (inst, v->volumeId, "RD", NULL, NULL, NULL)!=NULL); assert (save_volume (inst, v->volumeId, NULL, "LD", NULL, NULL)!=NULL); assert (save_volume (inst, v->volumeId, NULL, NULL, "LDR", NULL)!=NULL); assert (save_volume (inst, v->volumeId, NULL, NULL, NULL, VOL_STATE_DETACHED)!=NULL); assert (strcmp (v->remoteDev, "RD") == 0); assert (save_volume (inst, "v-x1", NULL, NULL, NULL, VOL_STATE_ATTACHING)!=NULL); assert (save_volume (inst, "v-x2", NULL, NULL, NULL, VOL_STATE_ATTACHING)==NULL); assert (save_volume (inst, "v-x1", NULL, NULL, NULL, VOL_STATE_DETACHING)!=NULL); assert (save_volume (inst, "v-x2", NULL, NULL, NULL, VOL_STATE_ATTACHING)==NULL); assert (save_volume (inst, "v-x1", NULL, NULL, NULL, VOL_STATE_DETACHING_FAILED)!=NULL); assert (save_volume (inst, "v-x2", NULL, NULL, NULL, VOL_STATE_ATTACHING)==NULL); assert (free_volume (inst, "v-x1")!=NULL); for (i=0; i<EUCA_MAX_VOLUMES-1; i++) { char id[10]; sprintf (id, "v-%d", i); v = free_volume (inst, id); assert (v!=NULL); } free_instance (&inst); assert(inst==NULL); } printf ("OK\n"); return 0; }
void * monitoring_thread (void *arg) { int i; struct nc_state_t *nc; if (arg == NULL) { logprintfl (EUCAFATAL, "NULL parameter!\n"); return NULL; } nc = (struct nc_state_t*)arg; logprintfl (EUCADEBUG, "Starting monitoring thread\n!\n"); for (;;) { bunchOfInstances *head; time_t now = time(NULL); sem_p (inst_sem); for ( head = global_instances; head; head = head->next ) { ncInstance * instance = head->instance; /* query for current state, if any */ refresh_instance_info (nc, instance); /* don't touch running or canceled threads */ if (instance->state!=STAGING && instance->state!=BOOTING && instance->state!=SHUTOFF && instance->state!=SHUTDOWN && instance->state!=TEARDOWN) continue; if (instance->state==TEARDOWN) { /* it's been long enough, we can forget the instance */ if ((now - instance->terminationTime)>teardown_state_duration) { remove_instance (&global_instances, instance); logprintfl (EUCAINFO, "forgetting about instance %s\n", instance->instanceId); free_instance (&instance); break; /* need to get out since the list changed */ } continue; } // time out logic for STAGING or BOOTING instances if (instance->state==STAGING && (now - instance->launchTime) < staging_cleanup_threshold) continue; // hasn't been long enough, spare it if (instance->state==BOOTING && (now - instance->bootTime) < booting_cleanup_threshold) continue; /* ok, it's been condemned => destroy the files */ if (!nc_state.save_instance_files) { logprintfl (EUCAINFO, "cleaning up state for instance %s\n", instance->instanceId); if (scCleanupInstanceImage(instance->userId, instance->instanceId)) { logprintfl (EUCAWARN, "warning: failed to cleanup instance image %s\n", instance->instanceId); } } else { logprintfl (EUCAINFO, "cleaning up state for instance %s (but keeping the files)\n", instance->instanceId); } /* check to see if this is the last instance running on vlan */ int left = 0; bunchOfInstances * vnhead; for (vnhead = global_instances; vnhead; vnhead = vnhead->next ) { ncInstance * vninstance = vnhead->instance; if (vninstance->ncnet.vlan == (instance->ncnet).vlan && strcmp(instance->instanceId, vninstance->instanceId)) { left++; } } if (left==0) { logprintfl (EUCAINFO, "stopping the network (vlan=%d)\n", (instance->ncnet).vlan); vnetStopNetwork (nc_state.vnetconfig, (instance->ncnet).vlan, NULL, NULL); } change_state (instance, TEARDOWN); /* TEARDOWN = no more resources */ instance->terminationTime = time (NULL); } sem_v (inst_sem); if (head) { /* we got out because of modified list, no need to sleep * now */ continue; } sleep (MONITORING_PERIOD); } return NULL; }
static int doRunInstance( struct nc_state_t *nc, ncMetadata *meta, char *instanceId, char *reservationId, virtualMachine *params, char *imageId, char *imageURL, char *kernelId, char *kernelURL, char *ramdiskId, char *ramdiskURL, char *keyName, // char *privMac, char *privIp, int vlan, netConfig *netparams, char *userData, char *launchIndex, char **groupNames, int groupNamesSize, ncInstance **outInst) { ncInstance * instance = NULL; * outInst = NULL; pid_t pid; netConfig ncnet; int error; memcpy(&ncnet, netparams, sizeof(netConfig)); /* check as much as possible before forking off and returning */ sem_p (inst_sem); instance = find_instance (&global_instances, instanceId); sem_v (inst_sem); if (instance) { logprintfl (EUCAFATAL, "Error: instance %s already running\n", instanceId); return 1; /* TODO: return meaningful error codes? */ } if (!(instance = allocate_instance (instanceId, reservationId, params, imageId, imageURL, kernelId, kernelURL, ramdiskId, ramdiskURL, instance_state_names[PENDING], PENDING, meta->userId, &ncnet, keyName, userData, launchIndex, groupNames, groupNamesSize))) { logprintfl (EUCAFATAL, "Error: could not allocate instance struct\n"); return 2; } change_state(instance, STAGING); sem_p (inst_sem); error = add_instance (&global_instances, instance); sem_v (inst_sem); if ( error ) { free_instance (&instance); logprintfl (EUCAFATAL, "Error: could not save instance struct\n"); return error; } instance->launchTime = time (NULL); /* instance->params.mem = params->mem; instance->params.cores = params->cores; instance->params.disk = params->disk; strcpy (instance->ncnet.privateIp, "0.0.0.0"); strcpy (instance->ncnet.publicIp, "0.0.0.0"); */ /* do the potentially long tasks in a thread */ pthread_attr_t* attr = (pthread_attr_t*) malloc(sizeof(pthread_attr_t)); if (!attr) { free_instance (&instance); logprintfl (EUCAFATAL, "Warning: out of memory\n"); return 1; } pthread_attr_init(attr); pthread_attr_setdetachstate(attr, PTHREAD_CREATE_DETACHED); if ( pthread_create (&(instance->tcb), attr, startup_thread, (void *)instance) ) { pthread_attr_destroy(attr); logprintfl (EUCAFATAL, "failed to spawn a VM startup thread\n"); sem_p (inst_sem); remove_instance (&global_instances, instance); sem_v (inst_sem); free_instance (&instance); if (attr) free(attr); return 1; } pthread_attr_destroy(attr); if (attr) free(attr); * outInst = instance; return 0; }
//! //! Main entry point of the application //! //! @param[in] argc the number of parameter passed on the command line //! @param[in] argv the list of arguments //! //! @return Always return 0 //! int main(int argc, char **argv) { printf("=====> testing misc.c\n"); test_command("date"); test_command("ls / -l | sort"); test_command("/foo"); { char c = 0; long l = 0; int i = 0; long long ll = 0; euca_lscanf("a1\na\na2\n", "a%d", &i); assert(i == 1); euca_lscanf("a\nab3\na 4\na5", "a %d", &i); assert(i == 4); euca_lscanf("", "%d", &i); euca_lscanf("\n\n\n", "%d", &i); euca_lscanf("abcdefg6", "g%d", &i); assert(i != 6); euca_lscanf("abcdefg", "ab%cdefg", &c); assert(c == 'c'); euca_lscanf("a\na 7\na\n", "a %ld", &l); assert(l == 7L); euca_lscanf("a\n8a\na9\n", "a %lld", &ll); assert(ll == 9L); } printf("=====> testing auth.c\n"); { //int decrypt_string_with_node_and_symmetric_key(char *in_buffer, char *key_buffer, char**out_buffer) euca_init_cert(); char *encrypted_symm_key = "YCvEugV7FynwQnyDbNX8hVFstseIpzhBCi6nxWZOUhVbivEf9Frkk7evxvQeSn9GUIvyNSE15Stts/CQmIGNsAKpAmjUoDzkkB/8EUCO9bgVi6IQl6SOzZCqX1hGCMZT1Ir32HXcwZcEbtDl0WpfLahjT9pZQAtDnnRT7H8+qysxUbEoL9Z2Yw5GneF5hRDWcA9Kce617oNbPFiZjLnnQr2iNsxyWb8LVj7KnBweJ8UJumgcWp26VsWoBUUCK/3adPAvOk/kalgrXZAk6fo/TqGUqdokjj1tz3rG7dcvTRuB0qwY1P8KfgRg3HABQcHjriKNvBYTVDhhEFDP2v0fiA=="; char *encrypted_string = "yuDM7HLybe+7DdkYFt+7zXYCeB/WHIdXSw7eF14L8YONEWmZdTEJwgXcGYdRpfDlY821xYi3asgURP6k9gFtPxjDHv/VcEKENJk9i/RbiAp0rEZ+gmkN3HQE9PjfNmS83AS1OyUnDiK2VcFRID7QKHcMdGrL6EXg/0QT1cDuwF0VbL71tdSddpFUHanaHQy2PzqKVPjfAqKWDBanMZWXckhYuHAMDCFkASvx8tU/7TrCJ7O697sScCBUQXqBZFY7930se2Jscg2u0tGlyKEK11gPNAnrIihAmgMVH2ZUzN1Ds9kFtGPrxoyLZbaxwYD5yzrd0qRBRrErZEWvj63nV4SUXKsmI7xmWBBUbosWmQzTp3mcsmnU40mowoCbOpoWt33XrlTJTfxA+dI1pnDWZY00lgISZe9IgQTAgF5o8GECw7uTZoK5b6LCTOSJPZPE57BjH9naeOnKMaXEe/Zl+s3j26hYhA8tP3IQIzOjqHcAhRGe/o2nkgVzLAYfBnaqsppKa8wJ2+qMUyh2f00c0W0GBHaSAAVh1PzcQiTOoLXYx9HDNxxtaZqL0TFfWxXkKm+/VjSQLqZ6HBqZAqiWIatHkD4UU83gCBlfvxw6ML8Z9A0VDUVbuuXTMCA1Z48e+gHK7/m1+S0/h0tW8wTBJeO9G1JkYdCNjaszey9Dl32v0jgGKw3BSgIwM+//lNL3yEdPx3pdhFxsYV0++bLxbFqZ2xpeFXupGmrVAEUyeqo5xcehMCpmKlJmQINTAav5vo2GtXBPvbNJOF7oIRakbr6jDF811V5jU5Dlwu6A9Yn8cPpZplB23Wo+1hsg3DjUCz3RxOwi5uIyF3IRbqqyt9mx3QlaxrXfjA0HweHE7O5TkeWnLWiPTefHR4ldHgKfDV9KScJLpmhEIznuwfEQaO855Z6JovWq6nKyPYXRToajiMwotrf6Sjkod9mQTZkbt/KSK4/PlIex3GhFHAFkUOwKJkI+Qenf1B8hPVDvoJ6PPZTUo7h127USsiUaZfUISgH2mep1YUyAdXlg+obK5iLprLhJqZwXh7dsR4Efn91YFcqEsVtO284CxWASh5Mid+pJD6FxVU8/pbX/EEi963fQgw42SiQxVXCaMySVB/9zlBfPg0dwP8yxwD+SiJz5In+oTDmr+OhpY0vQzkt/UnhORFpvJM7i1scY/rjAz2j6LjsHca4bt/x0JYkM+Vr+tZ90iiS01+fQX/fgFih6l9RoLWsbnilTx39dtwI8pOPitO90glIQ3EvLLmvZN9nvx9JfWTuinAkpOqrlkbc9ccKsAWa1zvvfCzGgqONoJ499rGFsY6H81jXR4h8K2o9n/Xs/8Sp8I8IqWfmS/JKH21JquJi4gLpmmZ38Kv6V4R7j5YcB9YwnIfav/uAJTN9VD9NKOyFwfhDA2YT4ErzMk5UL3sfQIX6UCzaKCiOeH3nKHzM7X4aHSqtSbvVNVxQ0tMYvm2oVEojmaXWGHmWyoylXJ1v0eaK38SDzwdpef6UKXESPEMpnR0krqH8bC7lZyMx2OyKpse9hJbjhzOvgv91lkUG3kidPxKraTUwXUmxVlUgxfcCgknO93U+kopjiffJDe/xILawvpEq0KWBFfbFN679znyz+8AEud2WSYdz7KnYuoX3RmViid/Oj0w=="; char *decrypted = NULL; int out_len = -1; if (decrypt_string_with_node_and_symmetric_key(encrypted_string, encrypted_symm_key, &decrypted, &out_len) != EUCA_OK) { printf("failed to decrypt using symmetric key\n"); } else { printf("decrypted length: %d\n", out_len); } } printf("=====> testing data.c\n"); { #define INSTS 50 bunchOfInstances *bag = NULL; ncInstance *inst = NULL; ncInstance *Insts[INSTS]; int i, n; printf("========> testing instance struct management\n"); free_instance(NULL); free_instance(&inst); inst = allocate_instance("the-uuid", "i1", NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0); assert(inst != NULL); free_instance(&inst); assert(inst == NULL); n = total_instances(&bag); assert(n == 0); bag = NULL; inst = find_instance(&bag, "foo"); assert(inst == NULL); bag = NULL; n = remove_instance(&bag, NULL); assert(n != EUCA_OK); bag = NULL; for (i = 0; i < INSTS; i++) { char id[10]; sprintf(id, "i-%d", i); inst = Insts[i] = allocate_instance("the-uuid", id, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0); assert(inst != NULL); n = add_instance(&bag, inst); assert(n == EUCA_OK); } n = total_instances(&bag); assert(n == INSTS); n = remove_instance(&bag, Insts[0]); assert(n == EUCA_OK); n = remove_instance(&bag, Insts[INSTS - 1]); assert(n == EUCA_OK); n = total_instances(&bag); assert(n == INSTS - 2); printf("========> testing volume struct management\n"); ncVolume *v; inst = allocate_instance("the-uuid", "i2", NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0); assert(inst != NULL); for (i = 0; i < EUCA_MAX_VOLUMES; i++) { char id[10]; sprintf(id, "v-%d", i); v = save_volume(inst, id, "tok", "rd", "ld", "ldr", VOL_STATE_ATTACHED); assert(v != NULL); } assert(is_volume_used(v)); assert(save_volume(inst, "too-much", "tok", "rd", "ld", "ldr", VOL_STATE_ATTACHED) == NULL); assert(save_volume(inst, v->volumeId, NULL, NULL, NULL, NULL, NULL) != NULL); assert(save_volume(inst, v->volumeId, NULL, "RD", NULL, NULL, NULL) != NULL); assert(save_volume(inst, v->volumeId, NULL, NULL, "LD", NULL, NULL) != NULL); assert(save_volume(inst, v->volumeId, NULL, NULL, NULL, "LDR", NULL) != NULL); assert(save_volume(inst, v->volumeId, NULL, NULL, NULL, NULL, VOL_STATE_DETACHED) != NULL); assert(strcmp(v->attachmentToken, "RD") == 0); assert(save_volume(inst, "v-x1", NULL, NULL, NULL, NULL, VOL_STATE_ATTACHING) != NULL); assert(save_volume(inst, "v-x2", NULL, NULL, NULL, NULL, VOL_STATE_ATTACHING) == NULL); assert(save_volume(inst, "v-x1", NULL, NULL, NULL, NULL, VOL_STATE_DETACHING) != NULL); assert(save_volume(inst, "v-x2", NULL, NULL, NULL, NULL, VOL_STATE_ATTACHING) == NULL); assert(save_volume(inst, "v-x1", NULL, NULL, NULL, NULL, VOL_STATE_DETACHING_FAILED) != NULL); assert(save_volume(inst, "v-x2", NULL, NULL, NULL, NULL, VOL_STATE_ATTACHING) == NULL); assert(free_volume(inst, "v-x1") != NULL); for (i = 0; i < EUCA_MAX_VOLUMES - 1; i++) { char id[10]; sprintf(id, "v-%d", i); v = free_volume(inst, id); assert(v != NULL); } free_instance(&inst); assert(inst == NULL); } printf("OK\n"); return 0; }
int main(int argc, char** argv) { WINDOW *lic = NULL; int x, y, mr; int opt = 0, cnt = 0; char remname[256]; char *licpath = NULL; FILE *fout = NULL; char *version = NULL; if (am_bin_path(instance_path, sizeof (instance_path)) == -1) { return 0; } else { if (am_whitespace(instance_path) > 0) { fprintf(stderr, "Whitespace in path: %s \n", instance_path); return 0; } snprintf(log_path, sizeof (log_path), "%s/../logs/install_%s.log", instance_path, TIMESTAMPLONG); snprintf(lic_path, sizeof (lic_path), "%s/../legal-notices/license.txt", instance_path); fout = fopen(log_path, "a"); if (fout != NULL) { fprintf(fout, "\n\n==================================================="); print_version(fout, NULL); fprintf(fout, "\r\n"); fclose(fout); } strcat(instance_path, "../instances"); snprintf(int_conf_path, sizeof (int_conf_path), "%s/.agents", instance_path); } net_initialize(); /* cli mode */ if (argc > 1) { while ((opt = opts(argc, argv, "e:vlxr:o:a:i:p:c:")) != -1) switch (opt) { case 'e': { char encryptpasswd[1024] = ""; char origpasswd[1024] = ""; char *keystr; char bkeystr[1024] = ""; strcpy(origpasswd, oarg); am_trim(origpasswd); keystr = am_random_key(); memset(bkeystr, 0, 1024); am_b64encode(keystr, bkeystr); encrypt_base64(origpasswd, encryptpasswd, bkeystr); fprintf(stderr, "\nEncrypted password:\n%s\n\nKey:\n%s\n\n", encryptpasswd, bkeystr); net_shutdown(); return (EXIT_SUCCESS); } break; case 'l': { fprintf(stderr, "Agent instances:\n"); int n; am_conf_p inst = NULL, temp; if ((n = am_read_instances(instance_path, &inst)) > 0) { temp = inst; while (temp != NULL) { fprintf(stderr, "%s\n", temp->name); temp = temp->next; } am_free(inst); } else fprintf(stderr, "There are no agent instances registered.\n"); net_shutdown(); return (EXIT_SUCCESS); } break; case 'v': { print_version(stdout, NULL); net_shutdown(); return (EXIT_SUCCESS); } break; case 'r': { fprintf(stderr, "Removing \"%s\" instance...\n", oarg); if (remove_instance(oarg)) { fprintf(stderr, "Instance \"%s\" removed.\n", oarg); } else fprintf(stderr, "Error removing \"%s\" instance.\n", oarg); net_shutdown(); return (EXIT_SUCCESS); } break; case 'o': sprintf(openam_url, oarg); cnt = 1; break; case 'a': sprintf(agent_url, oarg); cnt = 1; break; case 'i': sprintf(agent_id, oarg); cnt = 1; break; case 'p': sprintf(agent_pass_file, oarg); cnt = 1; break; case 'c': sprintf(web_conf_path, oarg); cnt = 1; break; case 'x': { asprintf(&licpath, "%s/.license", instance_path); if (licpath) { am_setup_conf(licpath, NULL); free(licpath); licpath = NULL; } net_shutdown(); return (EXIT_SUCCESS); } break; case '?': if (oopt == 'e' || oopt == 'r' || oopt == 'o' || oopt == 'a' || oopt == 'i' || oopt == 'p' || oopt == 'c') fprintf(stderr, "\nError: option -%c requires an argument.\n", oopt); else if (isprint(oopt)) fprintf(stderr, "\nError: unknown option `-%c'.\n", oopt); else fprintf(stderr, "\nnError: unknown option character `\\x%x'.\n", oopt); opt = -1; default: opt = -1; } if (cnt == 1) { asprintf(&licpath, "%s/.license", instance_path); if (licpath && am_file_exists(licpath) == 0) { am_free(licpath); fprintf(stderr, "\nYou have to accept ForgeRock Web Policy Agent license terms to continue.\n" "Please run agentadmin with -x option or interactively to view and accept the license.\n\n"); net_shutdown(); return (EXIT_FAILURE); } am_free(licpath); licpath = NULL; } if (cnt == 1) { url_t u, ua; am_trim(openam_url); u = URL(openam_url); if (u.error == 0) { if (validate_am_host(&u) != 0) { fprintf(stderr, "Error validating OpenAM URL\n"); net_shutdown(); return (EXIT_FAILURE); } } else { fprintf(stderr, "Invalid OpenAM URL value\n"); net_shutdown(); return (EXIT_FAILURE); } am_trim(agent_url); ua = URL(agent_url); if (ua.error != 0) { fprintf(stderr, "Invalid Agent URL value\n"); net_shutdown(); return (EXIT_FAILURE); } else { am_trim(agent_id); am_trim(agent_pass_file); if (am_read_password(agent_pass_file, agent_pass) == 0) { fprintf(stderr, "Error reading password file\n"); net_shutdown(); return (EXIT_FAILURE); } if (validate_agent(&u, agent_id, agent_pass) != 0) { fprintf(stderr, "%s\n", PROFILE_NOT_FOUND); net_shutdown(); return (EXIT_FAILURE); } } am_trim(web_conf_path); if (web_conf_path == NULL || web_conf_path[0] == '\0') { fprintf(stderr, "Varnish vmod directory must not be empty\n"); net_shutdown(); return (EXIT_FAILURE); } else { char *t = NULL; asprintf(&t, "%s/libvmod_am.so", web_conf_path); if (am_whitespace(web_conf_path) > 0) { fprintf(stderr, "Path to Varnish modules directory must not contain spaces\n"); am_free(t); net_shutdown(); return (EXIT_FAILURE); } else if (am_file_writeable(web_conf_path) == 0) { fprintf(stderr, "Error opening Varnish modules directory\n"); am_free(t); net_shutdown(); return (EXIT_FAILURE); } else if (am_file_exists(t) == 1) { fprintf(stderr, "This Varnish instance is already configured\n"); am_free(t); net_shutdown(); return (EXIT_FAILURE); } am_free(t); } create_varnish_instance(ua); fprintf(stderr, "\nVarnish and agent configuration files are here:\n%s\nCheck installation log %s for any errors.\n\n", web_conf_path_out, log_path); } else { display_usage(); } net_shutdown(); return (EXIT_SUCCESS); } print_version(NULL, &version); if (version != NULL) { snprintf(vers, sizeof (vers), "Version: %s", version); free(version); } initscr(); cbreak(); noecho(); keypad(stdscr, TRUE); start_color(); init_pair(1, COLOR_RED, COLOR_BLACK); getmaxyx(stdscr, y, x); asprintf(&licpath, "%s/.license", instance_path); if (am_file_exists(licpath) == 0) { lic = license(y, x, &mr); wrefresh(lic); delwin(lic); lic = NULL; if (mr == KEY_F(4)) { goto all_done; } else { am_setup_conf(licpath, NULL); } } ret_to_menu: refresh(); mr = mainmenu(y, x); if (mr == 0) { int cr = configure_step(y, x); if (cr == KEY_F(4)) { goto ret_to_menu; } } else if (mr >= 2 && mr < menu_size - 1) { memset(remname, 0, sizeof (remname)); if (sscanf(menu_values[mr], "Remove '%[^''']' instance", remname) == 1) { if (remove_step(remname, y, x) == KEY_F(4)) goto ret_to_menu; } goto ret_to_menu; } else if (mr == 1) { encrypt_step(y, x); goto ret_to_menu; } all_done: am_free(licpath); clean_main_menu(); move(0, 0); clrtoeol(); refresh(); endwin(); net_shutdown(); return (EXIT_SUCCESS); }
// TODO don't leak shit. static PP_Bool vlc_did_create(PP_Instance instance, uint32_t _argc, const char *_argn[], const char *_argv[]) { vlc_setPPAPI_InitializingInstance(instance); VLC_UNUSED(_argc); VLC_UNUSED(_argn); VLC_UNUSED(_argv); if(vlc_PPAPI_InitializeInstance(instance) != VLC_SUCCESS) { return PP_FALSE; } PP_Bool ret = PP_FALSE; libvlc_instance_t* vlc_inst = NULL; libvlc_media_player_t* media_player = NULL; libvlc_media_list_player_t* media_list_player = NULL; libvlc_media_list_t* playlist = NULL; instance_t* new_inst = add_instance(instance); if(new_inst == NULL) { vlc_ppapi_log_error(instance, "failed to create the plugin instance object!\n"); goto error; } vlc_inst = libvlc_new(0, NULL); if(vlc_inst == NULL) { vlc_ppapi_log_error(instance, "failed to create the vlc instance!\n"); goto error; } else { new_inst->vlc = vlc_inst; } libvlc_log_set(vlc_inst, libvlc_logging_callback, (void*)new_inst); media_player = libvlc_media_player_new(vlc_inst); if(media_player == NULL) { vlc_ppapi_log_error(instance, "libvlc_media_player_t creation failed"); goto error; } else { new_inst->media_player = media_player; } var_Create(media_player, "ppapi-instance", VLC_VAR_INTEGER); var_SetInteger(media_player, "ppapi-instance", instance); var_SetString(media_player, "vout", "ppapi_vout_graphics3d"); if(-1 == libvlc_add_intf(vlc_inst, "ppapi_control")) { vlc_ppapi_log_error(instance, "failed to start `ppapi-control`"); goto error; } ret = PP_TRUE; goto done; error: ret = PP_FALSE; if(new_inst != NULL) { remove_instance(new_inst); } done: vlc_setPPAPI_InitializingInstance(0); return ret; }
//! //! Handles the instance migration request. //! //! @param[in] nc a pointer to the node controller (NC) state //! @param[in] pMeta a pointer to the node controller (NC) metadata structure //! @param[in] instances metadata for the instance to migrate to destination //! @param[in] instancesLen number of instances in the instance list //! @param[in] action IP of the destination Node Controller //! @param[in] credentials credentials that enable the migration //! //! @return EUCA_OK on success or EUCA_*ERROR on failure //! //! @pre //! //! @post static int doMigrateInstances(struct nc_state_t *nc, ncMetadata * pMeta, ncInstance ** instances, int instancesLen, char *action, char *credentials) { int ret = EUCA_OK; int credentials_prepared = 0; if (instancesLen <= 0) { LOGERROR("called with invalid instancesLen (%d)\n", instancesLen); pMeta->replyString = strdup("internal error (invalid instancesLen)"); return (EUCA_INVALID_ERROR); } LOGDEBUG("verifying %d instance[s] for migration...\n", instancesLen); for (int inst_idx = 0; inst_idx < instancesLen; inst_idx++) { LOGDEBUG("verifying instance # %d...\n", inst_idx); if (instances[inst_idx]) { ncInstance *instance_idx = instances[inst_idx]; LOGDEBUG("[%s] proposed migration action '%s' (%s > %s) [creds=%s]\n", SP(instance_idx->instanceId), SP(action), SP(instance_idx->migration_src), SP(instance_idx->migration_dst), (instance_idx->migration_credentials == NULL) ? "UNSET" : "present"); } else { pMeta->replyString = strdup("internal error (instance count mismatch)"); LOGERROR("Mismatch between migration instance count (%d) and length of instance list\n", instancesLen); return (EUCA_ERROR); } } // TO-DO: Optimize the location of this loop, placing it inside various conditionals below? for (int inst_idx = 0; inst_idx < instancesLen; inst_idx++) { ncInstance *instance_req = instances[inst_idx]; char *sourceNodeName = instance_req->migration_src; char *destNodeName = instance_req->migration_dst; LOGDEBUG("[%s] processing instance # %d (%s > %s)\n", instance_req->instanceId, inst_idx, instance_req->migration_src, instance_req->migration_dst); // this is a call to the source of migration if (!strcmp(pMeta->nodeName, sourceNodeName)) { // locate the instance structure ncInstance *instance; sem_p(inst_sem); { instance = find_instance(&global_instances, instance_req->instanceId); } sem_v(inst_sem); if (instance == NULL) { LOGERROR("[%s] cannot find instance\n", instance_req->instanceId); pMeta->replyString = strdup("failed to locate instance to migrate"); return (EUCA_NOT_FOUND_ERROR); } if (strcmp(action, "prepare") == 0) { sem_p(inst_sem); instance->migration_state = MIGRATION_PREPARING; euca_strncpy(instance->migration_src, sourceNodeName, HOSTNAME_SIZE); euca_strncpy(instance->migration_dst, destNodeName, HOSTNAME_SIZE); euca_strncpy(instance->migration_credentials, credentials, CREDENTIAL_SIZE); instance->migrationTime = time(NULL); save_instance_struct(instance); copy_instances(); sem_v(inst_sem); // Establish migration-credential keys if this is the first instance preparation for this host. LOGINFO("[%s] migration source preparing %s > %s [creds=%s]\n", SP(instance->instanceId), SP(instance->migration_src), SP(instance->migration_dst), (instance->migration_credentials == NULL) ? "UNSET" : "present"); if (!credentials_prepared) { if (generate_migration_keys(sourceNodeName, credentials, TRUE, instance) != EUCA_OK) { pMeta->replyString = strdup("internal error (migration credentials generation failed)"); return (EUCA_SYSTEM_ERROR); } else { credentials_prepared++; } } sem_p(inst_sem); instance->migration_state = MIGRATION_READY; save_instance_struct(instance); copy_instances(); sem_v(inst_sem); } else if (strcmp(action, "commit") == 0) { sem_p(inst_sem); if (instance->migration_state == MIGRATION_IN_PROGRESS) { LOGWARN("[%s] duplicate request to migration source to initiate %s > %s (already migrating)\n", instance->instanceId, instance->migration_src, instance->migration_dst); sem_v(inst_sem); return (EUCA_DUPLICATE_ERROR); } else if (instance->migration_state != MIGRATION_READY) { LOGERROR("[%s] request to commit migration %s > %s when source migration_state='%s' (not 'ready')\n", instance->instanceId, SP(sourceNodeName), SP(destNodeName), migration_state_names[instance->migration_state]); sem_v(inst_sem); return (EUCA_UNSUPPORTED_ERROR); } instance->migration_state = MIGRATION_IN_PROGRESS; outgoing_migrations_in_progress++; LOGINFO("[%s] migration source initiating %s > %s [creds=%s] (1 of %d active outgoing migrations)\n", instance->instanceId, instance->migration_src, instance->migration_dst, (instance->migration_credentials == NULL) ? "UNSET" : "present", outgoing_migrations_in_progress); save_instance_struct(instance); copy_instances(); sem_v(inst_sem); // since migration may take a while, we do them in a thread pthread_t tcb = { 0 }; if (pthread_create(&tcb, NULL, migrating_thread, (void *)instance)) { LOGERROR("[%s] failed to spawn a migration thread\n", instance->instanceId); return (EUCA_THREAD_ERROR); } set_corrid_pthread( get_corrid()!=NULL ? get_corrid()->correlation_id : NULL , tcb); if (pthread_detach(tcb)) { LOGERROR("[%s] failed to detach the migration thread\n", instance->instanceId); return (EUCA_THREAD_ERROR); } } else if (strcmp(action, "rollback") == 0) { if ((instance->migration_state == MIGRATION_READY) || (instance->migration_state == MIGRATION_PREPARING)) { LOGINFO("[%s] rolling back migration (%s > %s) on source\n", instance->instanceId, instance->migration_src, instance->migration_dst); sem_p(inst_sem); migration_rollback(instance); sem_v(inst_sem); } else { LOGINFO("[%s] ignoring request to roll back migration on source with instance in state %s(%s) -- duplicate rollback request?\n", instance->instanceId, instance->stateName, migration_state_names[instance->migration_state]); } } else { LOGERROR("[%s] action '%s' is not valid\n", instance->instanceId, action); return (EUCA_INVALID_ERROR); } } else if (!strcmp(pMeta->nodeName, destNodeName)) { // this is a migrate request to destination if (!strcmp(action, "commit")) { LOGERROR("[%s] action '%s' for migration (%s > %s) is not valid on destination node\n", instance_req->instanceId, action, SP(sourceNodeName), SP(destNodeName)); return (EUCA_UNSUPPORTED_ERROR); } else if (!strcmp(action, "rollback")) { LOGINFO("[%s] rolling back migration (%s > %s) on destination\n", instance_req->instanceId, SP(sourceNodeName), SP(destNodeName)); sem_p(inst_sem); { ncInstance *instance = find_instance(&global_instances, instance_req->instanceId); if (instance != NULL) { LOGDEBUG("[%s] marked for cleanup\n", instance->instanceId); change_state(instance, SHUTOFF); instance->migration_state = MIGRATION_CLEANING; save_instance_struct(instance); } } sem_v(inst_sem); return EUCA_OK; } else if (strcmp(action, "prepare") != 0) { LOGERROR("[%s] action '%s' is not valid or not implemented\n", instance_req->instanceId, action); return (EUCA_INVALID_ERROR); } // Everything from here on is specific to "prepare" on a destination. // allocate a new instance struct ncInstance *instance = clone_instance(instance_req); if (instance == NULL) { LOGERROR("[%s] could not allocate instance struct\n", instance_req->instanceId); goto failed_dest; } sem_p(inst_sem); instance->migration_state = MIGRATION_PREPARING; euca_strncpy(instance->migration_src, sourceNodeName, HOSTNAME_SIZE); euca_strncpy(instance->migration_dst, destNodeName, HOSTNAME_SIZE); euca_strncpy(instance->migration_credentials, credentials, CREDENTIAL_SIZE); save_instance_struct(instance); sem_v(inst_sem); // Establish migration-credential keys. LOGINFO("[%s] migration destination preparing %s > %s [creds=%s]\n", instance->instanceId, SP(instance->migration_src), SP(instance->migration_dst), (instance->migration_credentials == NULL) ? "UNSET" : "present"); // First, call config-file modification script to authorize source node. LOGDEBUG("[%s] authorizing migration source node %s\n", instance->instanceId, instance->migration_src); if (authorize_migration_keys("-a", instance->migration_src, instance->migration_credentials, instance, TRUE) != EUCA_OK) { goto failed_dest; } // Then, generate keys and restart libvirtd. if (generate_migration_keys(instance->migration_dst, instance->migration_credentials, TRUE, instance) != EUCA_OK) { goto failed_dest; } int error; if (vbr_parse(&(instance->params), pMeta) != EUCA_OK) { goto failed_dest; } // set up networking char *brname = NULL; if ((error = vnetStartNetwork(nc->vnetconfig, instance->ncnet.vlan, NULL, NULL, NULL, &brname)) != EUCA_OK) { LOGERROR("[%s] start network failed for instance, terminating it\n", instance->instanceId); EUCA_FREE(brname); goto failed_dest; } euca_strncpy(instance->params.guestNicDeviceName, brname, sizeof(instance->params.guestNicDeviceName)); EUCA_FREE(brname); // TODO: move stuff in startup_thread() into a function? set_instance_params(instance); if ((error = create_instance_backing(instance, TRUE)) // create files that back the disks || (error = gen_instance_xml(instance)) // create euca-specific instance XML file || (error = gen_libvirt_instance_xml(instance))) { // transform euca-specific XML into libvirt XML LOGERROR("[%s] failed to prepare images for migrating instance (error=%d)\n", instance->instanceId, error); goto failed_dest; } // attach any volumes for (int v = 0; v < EUCA_MAX_VOLUMES; v++) { ncVolume *volume = &instance->volumes[v]; if (strcmp(volume->stateName, VOL_STATE_ATTACHED) && strcmp(volume->stateName, VOL_STATE_ATTACHING)) continue; // skip the entry unless attached or attaching LOGDEBUG("[%s] volumes [%d] = '%s'\n", instance->instanceId, v, volume->stateName); // TODO: factor what the following out of here and doAttachVolume() in handlers_default.c int have_remote_device = 0; char *xml = NULL; char *remoteDevStr = NULL; char scUrl[512]; char localDevReal[32], localDevTag[256], remoteDevReal[132]; char *tagBuf = localDevTag; ebs_volume_data *vol_data = NULL; ret = convert_dev_names(volume->localDev, localDevReal, tagBuf); if (ret) goto unroll; //Do the ebs connect. LOGTRACE("[%s][%s] Connecting EBS volume to local host\n", instance->instanceId, volume->volumeId); get_service_url("storage", nc, scUrl); if (strlen(scUrl) == 0) { LOGERROR("[%s][%s] Failed to lookup enabled Storage Controller. Cannot attach volume %s\n", instance->instanceId, volume->volumeId, scUrl); have_remote_device = 0; goto unroll; } else { LOGTRACE("[%s][%s] Using SC URL: %s\n", instance->instanceId, volume->volumeId, scUrl); } //Do the ebs connect. LOGTRACE("[%s][%s] Connecting EBS volume to local host\n", instance->instanceId, volume->volumeId); int rc = connect_ebs_volume(scUrl, volume->attachmentToken, nc->config_use_ws_sec, nc->config_sc_policy_file, nc->ip, nc->iqn, &remoteDevStr, &vol_data); if (rc) { LOGERROR("Error connecting ebs volume %s\n", volume->attachmentToken); have_remote_device = 0; ret = EUCA_ERROR; goto unroll; } // update the volume struct with connection string obtained from SC euca_strncpy(volume->connectionString, vol_data->connect_string, sizeof(volume->connectionString)); if (!remoteDevStr || !strstr(remoteDevStr, "/dev")) { LOGERROR("[%s][%s] failed to connect to iscsi target\n", instance->instanceId, volume->volumeId); remoteDevReal[0] = '\0'; } else { LOGDEBUG("[%s][%s] attached iSCSI target of host device '%s'\n", instance->instanceId, volume->volumeId, remoteDevStr); snprintf(remoteDevReal, sizeof(remoteDevReal), "%s", remoteDevStr); have_remote_device = 1; } EUCA_FREE(remoteDevStr); // something went wrong above, abort if (!have_remote_device) { goto unroll; } // make sure there is a block device if (check_block(remoteDevReal)) { LOGERROR("[%s][%s] cannot verify that host device '%s' is available for hypervisor attach\n", instance->instanceId, volume->volumeId, remoteDevReal); goto unroll; } // generate XML for libvirt attachment request if (gen_volume_xml(volume->volumeId, instance, localDevReal, remoteDevReal) // creates vol-XXX.xml || gen_libvirt_volume_xml(volume->volumeId, instance)) { // creates vol-XXX-libvirt.xml via XSLT transform LOGERROR("[%s][%s] could not produce attach device xml\n", instance->instanceId, volume->volumeId); goto unroll; } // invoke hooks char path[EUCA_MAX_PATH]; char lpath[EUCA_MAX_PATH]; snprintf(path, sizeof(path), EUCALYPTUS_VOLUME_XML_PATH_FORMAT, instance->instancePath, volume->volumeId); // vol-XXX.xml snprintf(lpath, sizeof(lpath), EUCALYPTUS_VOLUME_LIBVIRT_XML_PATH_FORMAT, instance->instancePath, volume->volumeId); // vol-XXX-libvirt.xml if (call_hooks(NC_EVENT_PRE_ATTACH, lpath)) { LOGERROR("[%s][%s] cancelled volume attachment via hooks\n", instance->instanceId, volume->volumeId); goto unroll; } // read in libvirt XML, which may have been modified by the hook above if ((xml = file2str(lpath)) == NULL) { LOGERROR("[%s][%s] failed to read volume XML from %s\n", instance->instanceId, volume->volumeId, lpath); goto unroll; } continue; unroll: ret = EUCA_ERROR; // TODO: unroll all volume attachments goto failed_dest; } sem_p(inst_sem); instance->migration_state = MIGRATION_READY; instance->bootTime = time(NULL); // otherwise nc_state.booting_cleanup_threshold will kick in change_state(instance, BOOTING); // not STAGING, since in that mode we don't poll hypervisor for info LOGINFO("[%s] migration destination ready %s > %s\n", instance->instanceId, instance->migration_src, instance->migration_dst); save_instance_struct(instance); error = add_instance(&global_instances, instance); copy_instances(); sem_v(inst_sem); if (error) { if (error == EUCA_DUPLICATE_ERROR) { LOGINFO("[%s] instance struct already exists (from previous migration?), deleting and re-adding...\n", instance->instanceId); error = remove_instance(&global_instances, instance); if (error) { LOGERROR("[%s] could not replace (remove) instance struct, failing...\n", instance->instanceId); goto failed_dest; } error = add_instance(&global_instances, instance); if (error) { LOGERROR("[%s] could not replace (add) instance struct, failing...\n", instance->instanceId); goto failed_dest; } } else { LOGERROR("[%s] could not add instance struct, failing...\n", instance->instanceId); goto failed_dest; } } continue; failed_dest: sem_p(inst_sem); // Just making sure... if (instance != NULL) { LOGERROR("[%s] setting instance to Teardown(cleaning) after destination failure to prepare for migration\n", instance->instanceId); // Set state to Teardown(cleaning) so source won't wait until timeout to roll back. instance->migration_state = MIGRATION_CLEANING; instance->terminationTime = time(NULL); change_state(instance, TEARDOWN); save_instance_struct(instance); add_instance(&global_instances, instance); // OK if this fails--that should mean it's already been added. copy_instances(); } // If no remaining incoming or pending migrations, deauthorize all clients. // TO-DO: Consolidate with similar sequence in handlers.c into a utility function? if (!incoming_migrations_in_progress) { int incoming_migrations_pending = 0; LOGINFO("[%s] no remaining active incoming migrations -- checking to see if there are any pending migrations\n", instance->instanceId); bunchOfInstances *head = NULL; for (head = global_instances; head; head = head->next) { if ((head->instance->migration_state == MIGRATION_PREPARING) || (head->instance->migration_state == MIGRATION_READY)) { LOGINFO("[%s] is pending migration, state='%s', deferring deauthorization of migration keys\n", head->instance->instanceId, migration_state_names[head->instance->migration_state]); incoming_migrations_pending++; } } // TO-DO: Add belt and suspenders? if (!incoming_migrations_pending) { LOGINFO("[%s] no remaining incoming or pending migrations -- deauthorizing all migration client keys\n", instance->instanceId); authorize_migration_keys("-D -r", NULL, NULL, NULL, FALSE); } } sem_v(inst_sem); // Set to generic EUCA_ERROR unless already set to a more-specific error. if (ret == EUCA_OK) { ret = EUCA_ERROR; } } else { LOGERROR("unexpected migration request (node %s is neither source nor destination)\n", pMeta->nodeName); ret = EUCA_ERROR; } } return ret; }
static int doMigrateInstance(struct nc_state_t *nc, ncMetadata *meta, char *instanceId, char *target) { int ret=OK; char remoteURI[CHAR_BUFFER_SIZE]; virConnectPtr *conn, dst; ncInstance *instance; logprintfl(EUCADEBUG, "doMigrateInstance() in default handler invoked\n"); conn = check_hypervisor_conn(); if (!conn) { logprintfl(EUCAERROR, "doMigrateInstance() cannot connect to hypervisor\n"); } if (target || !strcmp(target, "")) { getRemoteURI(nc, target, remoteURI, CHAR_BUFFER_SIZE); logprintfl(EUCADEBUG, "doMigrateInstance(): connecting to remote hypervisor\n"); dst = virConnectOpen(remoteURI); if (!dst) { logprintfl(EUCAERROR, "doMigrateInstance(): Connection to remote Hypervisor failed (URI: %s)\n", remoteURI); } else { logprintfl(EUCADEBUG, "doMigrateInstance(): Connected to %s\n", remoteURI); } } else { logprintfl(EUCAERROR, "doMigrateInstance(): no migration target\n"); return (ERROR); } sem_p (inst_sem); instance = find_instance(&global_instances, instanceId); sem_v (inst_sem); if (instance == NULL) { logprintfl(EUCAERROR, "doMigrateInstance(): instance not found\n"); return (NOT_FOUND); } if (conn && dst) { sem_p(hyp_sem); virDomainPtr dom = virDomainLookupByName(*conn, instanceId); sem_v(hyp_sem); if (dom) { sem_p (hyp_sem); if (virDomainMigrate (dom, dst, VIR_MIGRATE_LIVE, NULL, NULL, 0)) logprintfl (EUCAINFO, "doMigrateInstance(): migrated instance %s\n", instanceId); else ret = ERROR; sem_v (hyp_sem); } else { logprintfl (EUCAWARN, "warning: domain %s to be migrated not running on hypervisor\n", instanceId); ret = ERROR; } } else { logprintfl(EUCAERROR, "doMigrateInstance(): Migrating %s failed\n", instanceId); ret = ERROR; } if (ret == OK) { sem_p (inst_sem); instance = find_instance(&global_instances, instanceId); logprintfl(EUCADEBUG, "doMigrateInstance(): removing instance from global_instances\n"); if (remove_instance (&global_instances, instance) != OK) { logprintfl(EUCAERROR, "doMigrateInstance(): cannot remove instance from global_instances\n"); ret = ERROR; } sem_v (inst_sem); } return (ret); }