changeset 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 12c32e47724d
files Makefile ie-vagrant.c vagrant_newvm.py
diffstat 3 files changed, 116 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Mon Nov 18 03:50:20 2013 -1000
+++ b/Makefile	Tue Nov 19 00:59:48 2013 -1000
@@ -2,6 +2,8 @@
 CFLAGS = -Wall -O2 -g
 TARGET2 = ie-vagrant
 
+INSTALL_DIR = /usr/local/bin
+
 
 all: $(TARGET) $(TARGET2)
 
@@ -16,14 +18,16 @@
 	sudo chmod 4711 $(TARGET2)
 
 install: $(TARGET) $(TARGET2) newvm.py
-	install ie-virsh /usr/local/bin/
-	install ie-vagrant /usr/local/bin/
-	install newvm.py /usr/local/bin/
-	install change_vagrantfile.py /usr/local/bin/
-	chmod 4711 /usr/local/bin/ie-virsh
-	chmod 4711 /usr/local/bin/ie-vagrant
-	chmod 755 /usr/local/bin/newvm.py
-	chmod 755 /usr/local/bin/change_vagrantfile.py
+	install ie-virsh $(INSTALL_DIR)
+	install ie-vagrant $(INSTALL_DIR)
+	install newvm.py $(INSTALL_DIR)
+	install vagrant_newvm.py $(INSTALL_DIR)
+	install change_vagrantfile.py $(INSTALL_DIR)
+	chmod 4711 $(INSTALL_DIR)/ie-virsh
+	chmod 4711 $(INSTALL_DIR)/ie-vagrant
+	chmod 755 $(INSTALL_DIR)/newvm.py
+	chmod 755 $(INSTALL_DIR)/change_vagrantfile.py
+	chmod 755 $(INSTALL_DIR)/vagrant_newvm.py
 
 clean:
 	rm -f $(TARGET) $(TARGET2) *.o
--- 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:");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vagrant_newvm.py	Tue Nov 19 00:59:48 2013 -1000
@@ -0,0 +1,70 @@
+#!/usr/bin/python
+# -*- encoding: utf-8 -*-
+import shutil
+import sys,os
+from optparse import OptionParser
+
+FILE = "metadata.json"
+PROVIDER_VB = "virtualbox"
+PROVIDER_KVM = "kvm"
+BASE_DIR = "/root/.vagrant.d/boxes/"
+
+# read json file
+# return list of file contents
+
+def change_kvm_metadata():
+    box_base = os.path.join(BASE_DIR, name)
+    dir_name = os.path.join(box_base, PROVIDER_KVM)
+    return os.path.join(dir_name, FILE)
+
+
+def read_metadata_json():
+    metadata = change_kvm_metadata()
+    f = open(metadata, 'r')
+    result = []
+    for l in f:
+        result.append(l)
+    f.close()
+    return result
+
+# write json file
+
+def write_metadata_json(data):
+    metadata = change_kvm_metadata()
+    f = open(metadata, 'w')
+    for l in data:
+        f.write(l)
+    f.close()
+
+def replace_data(data):
+    data1 = []
+    for l in data:
+        if 'provider' in l:
+            data1.append(l.replace(PROVIDER_VB, PROVIDER_KVM))
+        else:
+            data1.append(l)
+    return data1
+
+def fix_provider_name(name):
+    box_base = os.path.join(BASE_DIR, name)
+    before_name = os.path.join(box_base, PROVIDER_VB)
+    if not os.path.exists(before_name):
+        print(before_name + "not found.")
+        return
+    after_name = os.path.join(box_base, PROVIDER_KVM)
+    shutil.move(before_name, after_name)
+
+parser = OptionParser()
+parser.add_option("-n", "--name", dest="name", help="VM name")
+
+(options, args) = parser.parse_args()
+
+name = options.name
+
+fix_provider_name(name)
+
+data = read_metadata_json()
+
+data1 = replace_data(data)
+
+write_metadata_json(data1)