Mercurial > hg > Applications > virsh-wrapper
view ie-vagrant.c @ 30:2888c7ec9013
can't define vm name is not number.
author | taiki <taiki@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 02 Sep 2014 08:10:27 -1000 |
parents | 66a88f51993f |
children |
line wrap: on
line source
#include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <signal.h> #include <string.h> #include <sys/types.h> #include <regex.h> #include <time.h> /* Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License */ #define command "/usr/bin/vagrant" #define init_command "init" #define up_command "up" #define destroy_command "destroy" #define ssh_command "ssh" #define box_command "box" #define box_add_command "add" #define box_list_command "list" #define box_default_url "http://ie.u-ryukyu.ac.jp/vagrant/fedora-19.box" #define provider_arg "--provider=kvm" #define NEW(type) ((type*)malloc(sizeof(type))) #define VAGRANT_FILE "Vagrantfile" /* Define global variables */ void usage() { printf("Usage: COMMAND [init|up|destroy|ssh]\n"); } void exec_init(int uid, int gid) { pid_t pid = fork(); if (pid < 0) { perror("fork"); exit(-1); } else if (pid == 0) { if (execl(command, command, init_command, NULL) < 0) { perror("Execl:"); } } else { // grant to edit vagrantfile to user sleep(2); if (chown(VAGRANT_FILE, uid, gid) != 0) { printf("chown error.\n"); exit(1); } char exec[1024]; strncpy(exec, "/usr/local/bin/change_vagrantfile.py", 1024); fprintf(stdout, "executing %s\n", exec); system(exec); } } void exec_box_add(char *box_name) { pid_t pid = fork(); if (pid < 0) { perror("fork"); exit(-1); } else if (pid == 0) { if (execl(command, command, box_command, box_add_command, box_name, box_default_url, NULL) < 0) { perror("Execl:"); } } else { int status = 0; printf("wait...\n"); if (wait(&status) == -1) { perror("wait"); } if (!WIFEXITED(status) == -1) { perror("wait"); } char exec[1024]; strncpy(exec, "/usr/local/bin/vagrant_newvm.py -n ", 1024); strncat(exec, box_name, 1024); fprintf(stdout, "executing %s\n", exec); system(exec); } } /* main(int argc, char **argv) - main process loop */ int main(int argc, char **argv) { int gid; int uid; /* Set euid and egid to actual user */ char *name = getlogin(); uid = getuid(); gid = getgid(); printf("uid %d gid %d name %s\n", uid,gid,name); setegid(getgid()); seteuid(getuid()); regex_t *pattern = NEW(regex_t); if (regcomp(pattern, name, 0) != 0) { exit(0); } /* Confirm user is in GROUP(999) group */ /* if ( gid != 999 ) { printf("User Not Authorized! Exiting...\n"); exit(1); } */ /* Set env valiable */ putenv("VAGRANT_HOME=/root/.vagrant.d/"); putenv("VAGRANT_DEFAULT_PROVIDER=kvm"); /* Set uid, gid, euid and egid to root */ setegid(0); seteuid(0); setgid(0); setuid(0); /* Check argv for proper arguments and run * the corresponding script, if invoked. */ if ( strncmp(argv[1], "init", 4) == 0 ) { exec_init(uid, gid); } else if ( strncmp(argv[1], "destroy", 4) == 0 ) { if (execl(command, command, destroy_command, NULL) < 0) { perror("Execl:"); } } else if ( strncmp(argv[1], "box", 3) == 0 ) { if ( strncmp(argv[2], "add", 3) == 0 ) { char box_name[1024] = "default_box"; strncpy(box_name, argv[3], 1024); exec_box_add(box_name); } else if (strncmp(argv[2], "list", 4) == 0 ) { if (execl(command, command, box_command, box_list_command, NULL) < 0) { perror("Execl:"); } } } else if ( strncmp(argv[1], "up", 2) == 0 ) { if (execl(command, command, up_command, provider_arg, NULL) < 0) { perror("Execl:"); } } else if ( strncmp(argv[1], "ssh", 3) == 0 ) { if (execl(command, command, ssh_command, NULL) < 0) { perror("Execl:"); } } else { usage(); exit(1); } exit(0); } /* end */