//! //! Destroy the backing store for a given instance //! //! @param[in] instance the instance identifier for which we will destroy the backing store //! @param[in] do_destroy_files set to TRUE if we need to destroy the content otherwise set to FALSE. //! //! @return EUCA_OK on success //! //! @pre The instance parameter must not be NULL and do_destroy_files must be a valid boolean. //! //! @note //! int destroy_instance_backing(ncInstance * instance, boolean do_destroy_files) { int i = 0; int ret = EUCA_OK; char path[EUCA_MAX_PATH] = ""; char work_regex[1024] = ""; // {userId}/{instanceId}/.* char scURL[512] = ""; if (get_localhost_sc_url(scURL) != EUCA_OK || strlen(scURL) == 0) { LOGWARN("[%s] could not obtain SC URL (is SC enabled?)\n", instance->instanceId); scURL[0] = '\0'; } // find and detach iSCSI targets, if any for (i = 0; i < EUCA_MAX_VOLUMES; ++i) { ncVolume *volume = &instance->volumes[i]; if (!is_volume_used(volume)) continue; if (disconnect_ebs_volume (scURL, localhost_config.use_ws_sec, localhost_config.ws_sec_policy_file, volume->attachmentToken, volume->connectionString, localhost_config.ip, localhost_config.iqn) != 0) { LOGERROR("[%s][%s] failed to disconnect volume\n", instance->instanceId, volume->volumeId); } } // see if instance directory is there (sometimes startup fails before it is created) set_path(path, sizeof(path), instance, NULL); if (check_path(path)) return (ret); if (do_destroy_files) { set_id2(instance, "/.*", work_regex, sizeof(work_regex)); if (blobstore_delete_regex(work_bs, work_regex) == -1) { LOGERROR("[%s] failed to remove some artifacts in %s\n", instance->instanceId, path); } for (i = 0; i < EUCA_MAX_VOLUMES; ++i) { ncVolume *volume = &instance->volumes[i]; snprintf(path, sizeof(path), EUCALYPTUS_VOLUME_XML_PATH_FORMAT, instance->instancePath, volume->volumeId); unlink(path); } // delete all non-blob files in the directory if (blobstore_delete_nonblobs(work_bs, instance->instancePath) < 0) { LOGERROR("[%s] failed to delete some non-blob files under %s\n", instance->instanceId, instance->instancePath); } } // Finally try to remove the directory. If either the user or our code introduced // any new files, this last step will fail. set_path(path, sizeof(path), instance, NULL); if (rmdir(path) && do_destroy_files) { LOGERROR("[%s] failed to remove backing directory %s\n", instance->instanceId, path); } return (ret); }
//! //! Finds a matching volume OR returns a pointer to the next empty/avail volume slot //! OR if full, returns NULL. //! //! @param[in] pInstance a pointer to the instance structure the volume should be under //! @param[in] sVolumeId the volume identifier string (vol-XXXXXXXX) //! //! @return a pointer to the matching volume OR returns a pointer to the next empty/avail //! volume slot OR if full, returns NULL. //! //! @pre Both \p pInstance and \p sVolumeId fields must not be NULL //! //! @todo There's gotta be a way to improve and not scan the whole list all the time //! static ncVolume *find_volume(ncInstance * pInstance, const char *sVolumeId) { ncVolume *pVol = NULL; ncVolume *pMatch = NULL; ncVolume *pAvail = NULL; ncVolume *pEmpty = NULL; register u32 i = 0; // Make sure our given parameters aren't NULL if ((pInstance != NULL) && (sVolumeId != NULL)) { for (i = 0, pVol = pInstance->volumes; i < EUCA_MAX_VOLUMES; i++, pVol++) { // look for matches if (!strncmp(pVol->volumeId, sVolumeId, CHAR_BUFFER_SIZE)) { assert(pMatch == NULL); pMatch = pVol; } // look for the first empty and available slot if (!strnlen(pVol->volumeId, CHAR_BUFFER_SIZE)) { if (pEmpty == NULL) pEmpty = pVol; } else if (!is_volume_used(pVol)) { if (pAvail == NULL) pAvail = pVol; } } // Return match first if any are found if (pMatch) return (pMatch); // then return the empty slot if (pEmpty) return (pEmpty); // If nothing else, return the first available slot. return (pAvail); } return (NULL); }
//! //! Destroy the backing store for a given instance //! //! @param[in] instance the instance identifier for which we will destroy the backing store //! @param[in] do_destroy_files set to TRUE if we need to destroy the content otherwise set to FALSE. //! //! @return EUCA_OK on success //! //! @pre The instance parameter must not be NULL and do_destroy_files must be a valid boolean. //! //! @note //! int destroy_instance_backing(ncInstance * instance, boolean do_destroy_files) { int i = 0; int ret = EUCA_OK; char path[EUCA_MAX_PATH] = ""; char work_regex[1024] = ""; // {userId}/{instanceId}/.* char scURL[512] = ""; ncVolume *volume = NULL; virtualMachine *vm = &(instance->params); virtualBootRecord *vbr = NULL; if (get_localhost_sc_url(scURL) != EUCA_OK || strlen(scURL) == 0) { LOGWARN("[%s] could not obtain SC URL (is SC enabled?)\n", instance->instanceId); scURL[0] = '\0'; } // find and detach iSCSI targets, if any for (i = 0; ((i < EUCA_MAX_VBRS) && (i < vm->virtualBootRecordLen)); i++) { vbr = &(vm->virtualBootRecord[i]); if (vbr->locationType == NC_LOCATION_SC) { if (disconnect_ebs_volume (scURL, localhost_config.use_ws_sec, localhost_config.ws_sec_policy_file, vbr->resourceLocation, vbr->preparedResourceLocation, localhost_config.ip, localhost_config.iqn) != 0) { LOGERROR("[%s] failed to disconnect volume attached to '%s'\n", instance->instanceId, vbr->backingPath); } } } // there may be iSCSI targets for volumes if instance disappeared or was migrated for (i = 0; i < EUCA_MAX_VOLUMES; ++i) { ncVolume *volume = &instance->volumes[i]; if (!is_volume_used(volume)) continue; if (disconnect_ebs_volume (scURL, localhost_config.use_ws_sec, localhost_config.ws_sec_policy_file, volume->attachmentToken, volume->connectionString, localhost_config.ip, localhost_config.iqn) != 0) { LOGERROR("[%s][%s] failed to disconnect volume\n", instance->instanceId, volume->volumeId); } } // see if instance directory is there (sometimes startup fails before it is created) set_path(path, sizeof(path), instance, NULL); if (check_path(path)) return (ret); // to ensure that we are able to delete all blobs, we chown files back to 'eucalyptus' // (e.g., libvirt on KVM on Maverick chowns them to libvirt-qemu while // VM is running and then chowns them to root after termination) { DIR *dir = NULL; if ((dir = opendir(path)) == NULL) { return (-1); } struct dirent *dir_entry; while ((dir_entry = readdir(dir)) != NULL) { char *entry_name = dir_entry->d_name; if (!strcmp(".", entry_name) || !strcmp("..", entry_name)) continue; // get the path of the directory item char entry_path[BLOBSTORE_MAX_PATH]; snprintf(entry_path, sizeof(entry_path), "%s/%s", path, entry_name); if (diskutil_ch(entry_path, EUCALYPTUS_ADMIN, NULL, BACKING_FILE_PERM)) { LOGWARN("[%s] failed to chown files before cleanup\n", instance->instanceId); } } closedir(dir); } if (do_destroy_files) { set_id2(instance, "/.*", work_regex, sizeof(work_regex)); if (blobstore_delete_regex(work_bs, work_regex) == -1) { LOGERROR("[%s] failed to remove some artifacts in %s\n", instance->instanceId, path); } for (i = 0; i < EUCA_MAX_VOLUMES; ++i) { volume = &instance->volumes[i]; snprintf(path, sizeof(path), EUCALYPTUS_VOLUME_XML_PATH_FORMAT, instance->instancePath, volume->volumeId); unlink(path); } // delete all non-blob files in the directory if (blobstore_delete_nonblobs(work_bs, instance->instancePath) < 0) { LOGWARN("[%s] failed to delete some non-blob files under %s\n", instance->instanceId, instance->instancePath); } } // Finally try to remove the directory. If either the user or our code introduced // any new files, this last step will fail. set_path(path, sizeof(path), instance, NULL); if (rmdir(path) && do_destroy_files) { LOGWARN("[%s] failed to remove backing directory %s\n", instance->instanceId, path); } 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; }
//! //! 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/WHIdXS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VXCaMySVB/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; }