Skip to content

Commit

Permalink
Fix virt-v2v monitor consumer for go wrapper + minor changes.
Browse files Browse the repository at this point in the history
Signed-off-by: Bella Khizgiyaev <[email protected]>
  • Loading branch information
bkhizgiy authored and ahadas committed Feb 18, 2024
1 parent 693a75f commit 2f30544
Showing 1 changed file with 23 additions and 72 deletions.
95 changes: 23 additions & 72 deletions virt-v2v/cold/entrypoint.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package main

import (
"context"
"fmt"
"net/http"
"io"
"os"
"os/exec"
"path/filepath"
Expand All @@ -19,15 +18,15 @@ const (
VDDK = "/opt/vmware-vix-disklib-distrib"
)

var (
xmlFilePath string
server *http.Server
)

func main() {
virtV2vArgs := []string{"virt-v2v", "-v", "-x"}
source := os.Getenv("V2V_source")

if !isValidSource(source) {
fmt.Printf("virt-v2v supports the following providers: {OVA, vSphere}. Provided: %s\n", source)
os.Exit(1)
}

requiredEnvVars := map[string][]string{
vSphere: {"V2V_libvirtURL", "V2V_secretKey", "V2V_vmName"},
OVA: {"V2V_diskPath", "V2V_vmName"},
Expand Down Expand Up @@ -65,12 +64,14 @@ func main() {

if source == vSphere {
if _, err := os.Stat("/etc/secret/cacert"); err == nil {
// use the specified certificate
err = os.Symlink("/etc/secret/cacert", "/opt/ca-bundle.crt")
if err != nil {
fmt.Println("Error creating ca cert link ", err)
os.Exit(1)
}
} else {
// otherwise, keep system pool certificates
err := os.Symlink("/etc/pki/tls/certs/ca-bundle.crt.bak", "/opt/ca-bundle.crt")
if err != nil {
fmt.Println("Error creating ca cert link ", err)
Expand All @@ -93,25 +94,6 @@ func main() {
fmt.Println("Error executing virt-v2v command ", err)
os.Exit(1)
}

if source == OVA {
var err error
xmlFilePath, err = getXMLFile(DIR, "xml")
if err != nil {
fmt.Println("Error gettin XML file:", err)
os.Exit(1)
}

http.HandleFunc("/xml", xmlHandler)
http.HandleFunc("/shutdown", shutdownHandler)
server = &http.Server{Addr: ":8080"}

fmt.Println("Starting server on :8080")
if err := server.ListenAndServe(); err != http.ErrServerClosed {
fmt.Printf("Error starting server: %v\n", err)
os.Exit(1)
}
}
}

func checkEnvVariablesSet(envVars ...string) bool {
Expand Down Expand Up @@ -163,7 +145,13 @@ func LinkDisks(diskKind string, num int) (err error) {

func executeVirtV2v(args []string) (err error) {
virtV2vCmd := exec.Command(args[0], args[1:]...)
virtV2vCmd.Stdout = os.Stdout
virtV2vStdoutPipe, err := virtV2vCmd.StdoutPipe()
if err != nil {
fmt.Printf("Error setting up stdout pipe: %v\n", err)
return
}

tee := io.TeeReader(virtV2vStdoutPipe, os.Stdout)
virtV2vCmd.Stderr = os.Stderr

fmt.Println("exec ", virtV2vCmd)
Expand All @@ -173,7 +161,7 @@ func executeVirtV2v(args []string) (err error) {
}

virtV2vMonitorCmd := exec.Command("/usr/local/bin/virt-v2v-monitor")
virtV2vMonitorCmd.Stdin, _ = virtV2vCmd.StdoutPipe()
virtV2vMonitorCmd.Stdin = tee
virtV2vMonitorCmd.Stdout = os.Stdout
virtV2vMonitorCmd.Stderr = os.Stderr

Expand All @@ -183,54 +171,17 @@ func executeVirtV2v(args []string) (err error) {
}

if err = virtV2vCmd.Wait(); err != nil {
fmt.Printf("Error waiting for virt-v2v to finish : %v\n", err)
fmt.Printf("Error waiting for virt-v2v to finish: %v\n", err)
return
}
return
}

func getXMLFile(dir, fileExtension string) (string, error) {
files, err := filepath.Glob(filepath.Join(dir, "*."+fileExtension))
if err != nil {
return "", err
}
if len(files) > 0 {
return files[0], nil
}
return "", fmt.Errorf("XML file was not found.")
}

func xmlHandler(w http.ResponseWriter, r *http.Request) {
if xmlFilePath == "" {
fmt.Println("Error: XML file path is empty.")
http.Error(w, "XML file path is empty", http.StatusInternalServerError)
return
}

xmlData, err := os.ReadFile(xmlFilePath)
if err != nil {
fmt.Printf("Error reading XML file: %v\n", err)
http.Error(w, "Error reading XML file", http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/xml")
_, err = w.Write(xmlData)
if err == nil {
w.WriteHeader(http.StatusOK)
} else {
fmt.Printf("Error writing response: %v\n", err)
http.Error(w, "Error writing response", http.StatusInternalServerError)
}

}

func shutdownHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("Shutdown request received. Shutting down server.")
err := server.Shutdown(context.Background())
if err != nil {
fmt.Printf("Error shutting down server: %v\n", err)
} else {
fmt.Println("Server shut down successfully.")
func isValidSource(source string) bool {
switch source {
case OVA, vSphere:
return true
default:
return false
}
}

0 comments on commit 2f30544

Please sign in to comment.