From a34d33863a593d63ca3a0a58ba0650bfc968297e Mon Sep 17 00:00:00 2001 From: Kushal Das Date: Nov 03 2015 14:34:30 +0000 Subject: we have working start_instance. --- diff --git a/src/controller/controller.go b/src/controller/controller.go index ae611a1..99877ce 100644 --- a/src/controller/controller.go +++ b/src/controller/controller.go @@ -3,6 +3,7 @@ package controller import ( "computer" "encoding/json" + "fmt" "logging" "os" "redis_op" @@ -28,6 +29,20 @@ func Safe_schedule(cpu, ram int) bool { } } +/* +Checks for path. +*/ +func exists(path string) bool { + _, err := os.Stat(path) + if err == nil { + return true + } + if os.IsNotExist(err) { + return false + } + return false +} + /* Saves the system db to the disk */ func Save_sdb(path string) { f, _ := os.Create(path) @@ -63,16 +78,30 @@ func Start_service() { } // Means we have safe amount of CPU and RAM image_name := job["diskname"].(string) + instance_name := job["name"].(string) image_path := redis_op.Hget("image-list", image_name) - Start_instance(cpu, ram, string(image_path)) + save_instance_details(job, instance_name) + Start_instance(cpu, ram, string(image_path), instance_name) } } } -/* To go through all steps and start an instance.*/ -func Start_instance(cpu, ram int, image_path string) { +func save_instance_details(job redis_op.Job, instance_name string) { + filename := fmt.Sprintf("%s.json", instance_name) + if !exists(filename) { + f, _ := os.Create(filename) + enc := json.NewEncoder(f) + enc.Encode(job) + f.Close() + + } - computer.Setup_storage_instance(image_path, "i-foo", "/var/lib/libvirt/images/", "10G", "/dev/sda1") - computer.Setup_instance("i-foo", "/var/lib/libvirt/images/i-foo.qcow2", strconv.Itoa(ram), strconv.Itoa(cpu)) +} + +/* To go through all steps and start an instance.*/ +func Start_instance(cpu, ram int, image_path, instance_name string) { + instance_path := fmt.Sprintf("/var/lib/libvirt/images/%s.qcow2", instance_name) + computer.Setup_storage_instance(image_path, instance_name, "/var/lib/libvirt/images/", "10G", "/dev/sda1") + computer.Setup_instance(instance_name, instance_path, strconv.Itoa(ram), strconv.Itoa(cpu)) }