Skip to content

Commit 3701ab5

Browse files
cfergeaupraveenkumar
authored andcommitted
Issue #438: Rework machine/bundle API
The main goal is to separate the "extract bundle file" logic from "load cached bundle info" in the API. This allows us to have more accurate logging during VM startup: at the moment we always print "Extracting bundle" even when it's already extracted. In the subsequent commits, this will allow us to load an already cached bundle when the VM already exists. This is related to #438
1 parent 89245e0 commit 3701ab5

File tree

3 files changed

+53
-32
lines changed

3 files changed

+53
-32
lines changed

pkg/crc/machine/bundle/extract.go

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,22 @@ import (
66
"io"
77
"os"
88
"path/filepath"
9-
"strings"
9+
10+
"github.com/code-ready/crc/pkg/crc/constants"
1011
)
1112

12-
func Extract(sourcepath string, destpath string) (string, error) {
13+
func Extract(sourcepath string) (*CrcBundleInfo, error) {
1314
file, err := os.Open(sourcepath)
1415

1516
if err != nil {
16-
return "", err
17+
return nil, err
1718
}
18-
19-
extractedPath := strings.Split(filepath.Base(file.Name()), ".crcbundle")[0]
20-
extractedPath = filepath.Join(destpath, extractedPath)
21-
22-
_, err = os.Stat(extractedPath)
23-
if err == nil {
24-
return extractedPath, nil
25-
}
26-
2719
defer file.Close()
2820

2921
var fileReader io.Reader = file
3022

3123
if fileReader, err = xz.NewReader(file, 0); err != nil {
32-
return "", err
24+
return nil, err
3325
}
3426

3527
tarBallReader := tar.NewReader(fileReader)
@@ -41,10 +33,10 @@ func Extract(sourcepath string, destpath string) (string, error) {
4133
if err == io.EOF {
4234
break
4335
}
44-
return "", err
36+
return nil, err
4537
}
4638
// get the individual filename and extract to the specified directory
47-
filename := filepath.Join(destpath, header.Name)
39+
filename := filepath.Join(constants.MachineCacheDir, header.Name)
4840

4941
switch header.Typeflag {
5042
case tar.TypeDir:
@@ -53,7 +45,7 @@ func Extract(sourcepath string, destpath string) (string, error) {
5345
err = os.MkdirAll(filename, os.FileMode(header.Mode))
5446

5547
if err != nil {
56-
return "", err
48+
return nil, err
5749
}
5850

5951
case tar.TypeReg, tar.TypeGNUSparse:
@@ -62,15 +54,15 @@ func Extract(sourcepath string, destpath string) (string, error) {
6254
writer, err := os.Create(filename)
6355

6456
if err != nil {
65-
return "", err
57+
return nil, err
6658
}
6759

6860
io.Copy(writer, tarBallReader)
6961

7062
err = os.Chmod(filename, os.FileMode(header.Mode))
7163

7264
if err != nil {
73-
return "", err
65+
return nil, err
7466
}
7567

7668
writer.Close()
@@ -81,5 +73,5 @@ func Extract(sourcepath string, destpath string) (string, error) {
8173

8274
}
8375

84-
return extractedPath, nil
76+
return GetCachedBundleInfo(filepath.Base(sourcepath))
8577
}

pkg/crc/machine/bundle/metadata.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"fmt"
66
"github.com/code-ready/crc/pkg/crc/constants"
77
"io/ioutil"
8+
"os"
89
"path/filepath"
10+
"strings"
911
)
1012

1113
// Metadata structure to unmarshal the crc-bundle-info.json file
@@ -43,23 +45,41 @@ type CrcBundleInfo struct {
4345
cachedPath string
4446
}
4547

46-
func GetCrcBundleInfo(bundlePath string) (*CrcBundleInfo, error) {
47-
var bundleInfo CrcBundleInfo
48-
extractedPath, err := Extract(bundlePath, constants.MachineCacheDir)
48+
func getCachedBundlePath(bundleName string) string {
49+
path := strings.TrimSuffix(bundleName, ".crcbundle")
50+
return filepath.Join(constants.MachineCacheDir, path)
51+
}
52+
53+
func (bundle *CrcBundleInfo) isCached() bool {
54+
_, err := os.Stat(bundle.cachedPath)
55+
return err == nil
56+
}
57+
58+
func (bundle *CrcBundleInfo) readBundleInfo() error {
59+
bundleInfoPath := bundle.resolvePath("crc-bundle-info.json")
60+
f, err := ioutil.ReadFile(bundleInfoPath)
4961
if err != nil {
50-
return nil, fmt.Errorf("Error during extraction : %+v", err)
62+
return fmt.Errorf("Error reading %s file : %+v", bundleInfoPath, err)
5163
}
52-
BundleInfoPath := filepath.Join(extractedPath, "crc-bundle-info.json")
53-
f, err := ioutil.ReadFile(BundleInfoPath)
64+
65+
err = json.Unmarshal(f, bundle)
5466
if err != nil {
55-
return nil, fmt.Errorf("Error reading %s file : %+v", BundleInfoPath, err)
67+
return fmt.Errorf("Error Unmarshal the data: %+v", err)
5668
}
5769

58-
err = json.Unmarshal(f, &bundleInfo)
70+
return nil
71+
}
72+
73+
func GetCachedBundleInfo(bundleName string) (*CrcBundleInfo, error) {
74+
var bundleInfo CrcBundleInfo
75+
bundleInfo.cachedPath = getCachedBundlePath(bundleName)
76+
if !bundleInfo.isCached() {
77+
return nil, fmt.Errorf("Could not find cached bundle info")
78+
}
79+
err := bundleInfo.readBundleInfo()
5980
if err != nil {
60-
return nil, fmt.Errorf("Error Unmarshal the data: %+v", err)
81+
return nil, err
6182
}
62-
bundleInfo.cachedPath = extractedPath
6383
return &bundleInfo, nil
6484
}
6585

pkg/crc/machine/machine.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ func fillClusterConfig(bundleInfo *bundle.CrcBundleInfo, clusterConfig *ClusterC
5050
return nil
5151
}
5252

53+
func getCrcBundleInfo(bundlePath string) (*bundle.CrcBundleInfo, error) {
54+
bundleName := filepath.Base(bundlePath)
55+
logging.Infof("Loading bundle: %s ...", bundleName)
56+
bundleInfo, err := bundle.GetCachedBundleInfo(bundleName)
57+
if err == nil {
58+
return bundleInfo, nil
59+
}
60+
logging.Infof("Extracting bundle: %s ...", bundleName)
61+
return bundle.Extract(bundlePath)
62+
}
63+
5364
func Start(startConfig StartConfig) (StartResult, error) {
5465
defer unsetMachineLogging()
5566

@@ -64,8 +75,7 @@ func Start(startConfig StartConfig) (StartResult, error) {
6475
libMachineAPIClient := libmachine.NewClient(constants.MachineBaseDir, constants.MachineCertsDir)
6576
defer libMachineAPIClient.Close()
6677

67-
logging.Infof("Extracting bundle: %s ...", filepath.Base(startConfig.BundlePath))
68-
crcBundleMetadata, err := bundle.GetCrcBundleInfo(startConfig.BundlePath)
78+
crcBundleMetadata, err := getCrcBundleInfo(startConfig.BundlePath)
6979
if err != nil {
7080
result.Error = err.Error()
7181
return *result, errors.Newf("Error to get bundle Metadata %v", err)
@@ -87,7 +97,6 @@ func Start(startConfig StartConfig) (StartResult, error) {
8797
// Retrieve metadata info
8898
diskPath := crcBundleMetadata.GetDiskImagePath()
8999
machineConfig.DiskPathURL = fmt.Sprintf("file://%s", filepath.ToSlash(diskPath))
90-
91100
machineConfig.SSHKeyPath = crcBundleMetadata.GetSSHKeyPath()
92101
machineConfig.KernelCmdLine = crcBundleMetadata.Nodes[0].KernelCmdLine
93102
machineConfig.Initramfs = crcBundleMetadata.GetInitramfsPath()

0 commit comments

Comments
 (0)