Skip to content

Commit

Permalink
change pipe
Browse files Browse the repository at this point in the history
Signed-off-by: Arik Hadas <[email protected]>
  • Loading branch information
ahadas committed May 20, 2024
1 parent 69857b4 commit 76fd600
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
20 changes: 12 additions & 8 deletions cmd/virt-v2v-monitor/virt-v2v-monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

var COPY_DISK_RE = regexp.MustCompile(`^.*Copying disk (\d+)/(\d+)`)
var DISK_PROGRESS_RE = regexp.MustCompile(`\s+\((\d+).*|.+ (\d+)% \[[*-]+\]`)
var DISK_PROGRESS_RE = regexp.MustCompile(`.+ (\d+)% \[[*-]+\]`)
var FINISHED_RE = regexp.MustCompile(`^\[[ .0-9]*\] Finishing off`)

// Here is a scan function that imposes limit on returned line length. virt-v2v
Expand All @@ -29,7 +29,7 @@ func LimitedScanLines(data []byte, atEOF bool) (advance int, token []byte, err e
if token != nil || err != nil {
return
}
if len(data) == 1*1024*1024 {
if len(data) == bufio.MaxScanTokenSize {
// Line is too long for the buffer. Trim it.
advance = len(data)
token = data
Expand Down Expand Up @@ -99,7 +99,7 @@ func main() {

reader := bufio.NewReader(os.Stdin)
scanner := NewBufferedScanner(reader, 1*1024*1024)

fmt.Println("Arik")
for scanner.Scan() {
line := scanner.Bytes()
os.Stdout.Write(line)
Expand All @@ -109,23 +109,27 @@ func main() {
klog.Fatal("Output monitoring failed! ", err)
}

fmt.Println("this is the line we scanning now ", string(line))
//fmt.Println("this is the line we scanning now ", string(line))

if match := COPY_DISK_RE.FindSubmatch(line); match != nil {
diskNumber, _ = strconv.ParseUint(string(match[1]), 10, 0)
disks, _ = strconv.ParseUint(string(match[2]), 10, 0)
klog.Infof("Copying disk %d out of %d", diskNumber, disks)
//klog.Infof("Copying disk %d out of %d", diskNumber, disks)
fmt.Printf("Copying disk %d out of %d", diskNumber, disks)
progress = 0
err = updateProgress(progressCounter, diskNumber, progress)
} else if match := DISK_PROGRESS_RE.FindSubmatch(line); match != nil {
klog.Info("we are here at progress ", line)
//klog.Info("we are here at progress ", line)
fmt.Printf("we are here at progress line=%s match-0=%s match-1=%s\n", string(line), string(match[0]), string(match[1]))
progress, _ = strconv.ParseUint(string(match[1]), 10, 0)
klog.Infof("Progress update, completed %d %%", progress)
//klog.Infof("Progress update, completed %d %%", progress)
fmt.Printf("Progress update, completed %d %%\n", progress)
err = updateProgress(progressCounter, diskNumber, progress)
} else if match := FINISHED_RE.Find(line); match != nil {
// Make sure we flag conversion as finished. This is
// just in case we miss the last progress update for some reason.
klog.Infof("Finished")
//klog.Infof("Finished")
fmt.Printf("Finished\n")
for disk := uint64(0); disk < disks; disk++ {
err = updateProgress(progressCounter, disk, 100)
}
Expand Down
24 changes: 8 additions & 16 deletions virt-v2v/cold/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func main() {
}

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

if !isValidSource(source) {
Expand Down Expand Up @@ -194,36 +194,28 @@ func LinkDisks(diskKind string, num int) (err error) {

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

stderrPipe, err := virtV2vCmd.StderrPipe()
if err != nil {
fmt.Printf("Error setting up stderr pipe: %v\n", err)
return
}
r, w := io.Pipe()
virtV2vCmd.Stdout = w
virtV2vCmd.Stderr = w

if err = virtV2vCmd.Start(); err != nil {
fmt.Printf("Error executing command: %v\n", err)
return
}

combinedReader := io.MultiReader(stdoutPipe, stderrPipe)
virtV2vMonitorCmd := exec.Command("/usr/local/bin/virt-v2v-monitor")
virtV2vMonitorCmd.Stdin = combinedReader
virtV2vMonitorCmd.Stdin = r
//k, _ := virtV2vMonitorCmd.StderrPipe()
//r2 := io.TeeReader(k, os.Stderr)
virtV2vMonitorCmd.Stdout = os.Stdout
virtV2vMonitorCmd.Stderr = os.Stderr

if err = virtV2vMonitorCmd.Start(); err != nil {
fmt.Printf("Error executing monitor command: %v\n", err)
return
}

if source == OVA {
scanner := bufio.NewScanner(stderrPipe)
scanner := bufio.NewScanner(nil)
const maxCapacity = 1024 * 1024
buf := make([]byte, 0, 64*1024)
scanner.Buffer(buf, maxCapacity)
Expand Down

0 comments on commit 76fd600

Please sign in to comment.