diff --git a/config/config.go b/config/config.go index a06b572e11f069c9165cbd8ff012138e956e2a32..b90b28b614df39e36207125ec09ab57844fb44ff 100644 --- a/config/config.go +++ b/config/config.go @@ -8,18 +8,13 @@ import ( // Config describes all configurable keys type Config struct { - // DiskStoragePool is the name of the storage pool to use - DiskStoragePool string - // ImageOwner is the UID that should own volume images - ImageOwner string - // ImageGroup is the GID that should own volume images - ImageGroup string - // BridgeInterface is the bridge that all network interfaces are added to - BridgeInterface string - // ImageHost is the base URL for the disk image server - ImageHost string - // ImageDir is the path to the local image pool - ImageDir string + Bind string // Bind is the address (and port) to bind the REST server to + DiskStoragePool string // DiskStoragePool is the name of the storage pool to use + ImageOwner string // ImageOwner is the UID that should own volume images + ImageGroup string // ImageGroup is the GID that should own volume images + BridgeInterface string // BridgeInterface is the bridge that all network interfaces are added to + ImageHost string // ImageHost is the base URL for the disk image server + ImageDir string // ImageDir is the path to the local image pool } var ( @@ -28,6 +23,7 @@ var ( // Defaults are the default values for each config option, set in the Load() function Defaults = Config{ + Bind: "8080", DiskStoragePool: "default", ImageOwner: "64055", ImageGroup: "64055", diff --git a/instances/downloader.go b/instances/downloader.go index 209fd6d35d5fbc9693065572b6b82002885d6779..f78a7fef864e430604c8681a2b010a0a3deee8f2 100644 --- a/instances/downloader.go +++ b/instances/downloader.go @@ -1,7 +1,6 @@ package instances import ( - "bytes" "crypto/sha256" "encoding/hex" "fmt" @@ -18,17 +17,17 @@ import ( "git.callpipe.com/entanglement.garden/rhyzome/config" ) -func (i *Instance) downloadImage() error { - hashes, err := getHashes(i.BaseImage) +func downloadImage(image string, dest string) error { + hashes, err := getHashes(image) if err != nil { return err } - filename := fmt.Sprintf("%s.qcow2.xz", i.BaseImage) + filename := fmt.Sprintf("%s.qcow2.xz", image) - url := fmt.Sprintf("%s/%s/%s.qcow2.xz", config.C.ImageHost, i.BaseImage, i.BaseImage) - destination := filepath.Join(config.C.ImageDir, filename) - log.Printf("Downloading %s to %s\n", url, destination) + url := fmt.Sprintf("%s/%s/%s.qcow2.xz", config.C.ImageHost, image, image) + destination := filepath.Join(config.C.ImageDir, dest) + log.Println("Downloading", url, "to", destination) resp, err := http.Get(url) if err != nil { @@ -59,17 +58,18 @@ func (i *Instance) downloadImage() error { } -func decompressImage(image string) error { - destination := fmt.Sprintf("%s.qcow2", image) - filename := fmt.Sprintf("%s.xz", destination) - source := filepath.Join(config.C.ImageDir, filename) +func decompressImage(filename string) error { + destination := filepath.Join(config.C.ImageDir, filename) + source := fmt.Sprintf("%s.xz", destination) + + log.Println("Uncompressing", source, "to", destination) - data, err := ioutil.ReadFile(source) + file, err := os.Open(source) if err != nil { return err } - r, err := xz.NewReader(bytes.NewReader(data), 0) + r, err := xz.NewReader(file, 0) if err != nil { return err } diff --git a/instances/instances.go b/instances/instances.go index c18d7e3cad66c8309483e48a915e51198ce2fc96..a5bbfebf59b63ea971795614319a1685b72bd83b 100644 --- a/instances/instances.go +++ b/instances/instances.go @@ -142,16 +142,14 @@ func (i *Instance) CreateAsJob(j *jobs.Job) error { j.SetStatus("Building base disk") // Create the base disk - volumeXML, err := i.GetVolumeXML() - if err != nil { + j.SetStatus("Downloading base image") + volumeName := fmt.Sprintf("%s.qcow2", i.Name) + if err = downloadImage(i.BaseImage, fmt.Sprintf("%s.xz", volumeName)); err != nil { return err } - if err = i.downloadImage(); err != nil { - return err - } - - if err = decompressImage(i.BaseImage); err != nil { + j.SetStatus("Decompressing base image") + if err = decompressImage(volumeName); err != nil { return err } @@ -161,22 +159,11 @@ func (i *Instance) CreateAsJob(j *jobs.Job) error { return err } - baseVolume, err := storagePool.LookupStorageVolByName(fmt.Sprintf("%s.qcow2", i.BaseImage)) - if err != nil { - return err - } - if baseVolume == nil { - return fmt.Errorf("Base image \"%s\" does not exist", i.BaseImage) - } - - j.SetStatus("Cloning base image") - volume, err := storagePool.StorageVolCreateXMLFrom(volumeXML, baseVolume, 0) - if err != nil { + if err := storagePool.Refresh(0); err != nil { return err } j.SetStatus("Creating domain") - volumeName, err := volume.GetName() if err != nil { return err } diff --git a/rest/router.go b/rest/router.go index 450625eae10db24c488ce1fdc980032d1f05a239..20eb457eb95436bbdc6ba9e685521b7b242abb33 100644 --- a/rest/router.go +++ b/rest/router.go @@ -6,6 +6,8 @@ import ( "net/http" "github.com/gorilla/mux" + + "git.callpipe.com/entanglement.garden/rhyzome/config" ) func ListenAndServe() { @@ -19,7 +21,7 @@ func ListenAndServe() { r.HandleFunc("/api/v1alpha1/jobs/{id}", GetJob) r.Use(loggingMiddleware) - log.Fatal(http.ListenAndServe(":8080", r)) + log.Fatal(http.ListenAndServe(config.C.Bind, r)) } func renderRoot(w http.ResponseWriter, r *http.Request) {