diff ie-vagrant.c @ 17:fb9f3738a8e6

add "box add" and "box list" add python script
author taiki
date Tue, 19 Nov 2013 00:59:48 -1000
parents d8ea207162ca
children 66a88f51993f
line wrap: on
line diff
--- a/ie-vagrant.c	Mon Nov 18 03:50:20 2013 -1000
+++ b/ie-vagrant.c	Tue Nov 19 00:59:48 2013 -1000
@@ -5,6 +5,7 @@
 
 #include <stdio.h>
 #include <sys/types.h>
+#include <sys/wait.h>
 #include <unistd.h>
 #include <signal.h>
 #include <string.h>
@@ -35,6 +36,7 @@
 
 #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"
 
@@ -61,7 +63,7 @@
         if (execl(command, command, init_command, NULL) < 0) {
             perror("Execl:");
         }
-    } else {
+    } else { // grant to edit vagrantfile to user
         sleep(2);
         if (chown(VAGRANT_FILE, uid, gid) != 0) {
             printf("chown error.\n");
@@ -74,6 +76,34 @@
     }
 }
 
+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)
@@ -128,9 +158,9 @@
   }
  } else if ( strncmp(argv[1], "box", 3) == 0 ) {
     if ( strncmp(argv[2], "add", 3) == 0 ) {
-        if (execl(command, command, box_add_command, NULL) < 0) {
-            perror("Execl:");
-        }
+        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:");