-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change VM customization to use a YAML output file instead of XML.
As part of the virt-v2v migration, we inspect the OS type and disk paths. Until now, this has been done with the XML output file. Due to the change to YAML format, this process needs to be adapted as well. Since we cannot use external libraries in the virt-v2v directory, it was implemented using bufio. Signed-off-by: Bella Khizgiyaev <[email protected]>
- Loading branch information
Showing
3 changed files
with
80 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"strings" | ||
) | ||
|
||
type VmConfig struct { | ||
OSInfo string | ||
DiskPaths []string | ||
} | ||
|
||
// ReadYAMLFile reads the YAML file and extracts the HostDisk paths and osinfo label. | ||
func GetVmConfigYaml(filePath string) (*VmConfig, error) { | ||
file, err := os.Open(filePath) | ||
if err != nil { | ||
return nil, fmt.Errorf("error opening YAML file: %w", err) | ||
} | ||
defer file.Close() | ||
|
||
config := &VmConfig{} | ||
scanner := bufio.NewScanner(file) | ||
var inLabels, inVolumes bool | ||
for scanner.Scan() { | ||
line := strings.TrimSpace(scanner.Text()) | ||
|
||
if line == "" || strings.HasPrefix(line, "#") { | ||
continue | ||
} | ||
|
||
if strings.HasPrefix(line, "labels:") { | ||
inLabels = true | ||
continue | ||
} | ||
|
||
if inLabels { | ||
if strings.HasPrefix(line, "libguestfs.org/osinfo:") { | ||
config.OSInfo = strings.TrimSpace(strings.TrimPrefix(line, "libguestfs.org/osinfo:")) | ||
} | ||
if !strings.HasPrefix(line, "libguestfs.org/") { | ||
inLabels = false | ||
} | ||
} | ||
|
||
if strings.Contains(line, "volumes:") { | ||
inVolumes = true | ||
continue | ||
} | ||
if inVolumes { | ||
if strings.Contains(line, "hostDisk:") { | ||
scanner.Scan() | ||
pathLine := strings.TrimSpace(scanner.Text()) | ||
if strings.HasPrefix(pathLine, "path:") { | ||
pathValue := strings.TrimSpace(strings.TrimPrefix(pathLine, "path:")) | ||
if pathValue != "" { | ||
config.DiskPaths = append(config.DiskPaths, pathValue) | ||
} | ||
} | ||
} | ||
if strings.Contains(line, "- name:") { | ||
inVolumes = false | ||
} | ||
} | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
return nil, fmt.Errorf("error reading YAML file: %w", err) | ||
} | ||
|
||
return config, nil | ||
} |