Skip to content

Commit

Permalink
add diskpart success log verification
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Alvarez <[email protected]>
  • Loading branch information
pendo324 committed Oct 4, 2023
1 parent f28cdef commit df7c8d8
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 40 deletions.
28 changes: 0 additions & 28 deletions pkg/disk/createDiskAdmin.TEMPLATE.ps1

This file was deleted.

58 changes: 49 additions & 9 deletions pkg/disk/disk_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ package disk

import (
"bytes"
_ "embed"
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"

"github.com/spf13/afero"

"github.com/runfinch/finch/pkg/flog"
"github.com/runfinch/finch/pkg/winutil"
)

Expand Down Expand Up @@ -77,9 +79,6 @@ func (m *userDataDiskManager) diskExists(diskPath string) (bool, error) {
return true, nil
}

//go:embed createDiskAdmin.TEMPLATE.ps1
var createDiskTmpl string

type createDiskOpts struct {
Path string
Size int64
Expand Down Expand Up @@ -126,16 +125,57 @@ func (m *userDataDiskManager) createDisk(diskPath string) error {
"2>&1",
},
); err != nil {
return fmt.Errorf("failed to run command: %s", err)
return fmt.Errorf("failed to run dpgo command: %w", err)
}

tempOutContents, _ := afero.ReadFile(m.fs, tempOut.Name())
m.logger.Infof("createDisk out: %s", string(tempOutContents))

dpGoOutStr := strings.TrimSpace(string(tempOutContents))
m.logger.Debugf("create disk cmd stdout: %s", dpGoOutStr)
_ = m.fs.Remove(tempOut.Name())

if err != nil {
return fmt.Errorf("failed to create disk: %w, command output: %s", err, tempOutContents)
lines := strings.Split(dpGoOutStr, "\n")
var logs []flog.Log
for _, l := range lines {
// Fix new lines
nl := strings.ReplaceAll(l, `\r\n`, `\n`)
nl = strings.ReplaceAll(nl, `\r`, `\n`)
var logParsed flog.Log
if err = json.Unmarshal([]byte(l), &logParsed); err != nil {
return fmt.Errorf("error parsing create disk log: %w, log string: %s", err, nl)
}
logs = append(logs, logParsed)
}
m.logger.Debugf("create disk cmd stdout parsed: %v", logs)

// Make sure all of the DiskPart success logs are present
diskPartSuccessMessages := []string{
"DiskPart successfully created the virtual disk file.",
"DiskPart successfully selected the virtual disk file.",
"DiskPart successfully attached the virtual disk file.",
"DiskPart succeeded in creating the specified partition.",
"DiskPart successfully formatted the volume.",
"DiskPart successfully detached the virtual disk file.",
}
foundLog := false
for _, l := range logs {
if strings.Contains(l.Message, "create disk cmd stdout: ") {
foundLog = true
for _, m := range diskPartSuccessMessages {
if !strings.Contains(l.Message, m) {
return fmt.Errorf(
"diskpart failed to create working disk because missing message %s, check output: %s",
m,
l.Message,
)
}
}
}
if foundLog {
break
}
}
if !foundLog {
return fmt.Errorf("failed to find diskpart logs, check output: %v", logs)
}

return nil
Expand Down
9 changes: 6 additions & 3 deletions pkg/disk/dpgo/disk_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

//go:build windows
// +build windows

Expand Down Expand Up @@ -106,13 +109,13 @@ func (cd *createDiskAction) createDisk(path string, size int64) error {

cmd.SetStdout(&dpStdout)

cd.logger.Debugln("starting diskpart to exit")
cd.logger.Debugln("starting diskpart")
if err := cmd.Start(); err != nil {
return err
}

go func() {
cd.logger.Debugf("writing %s to diskpart stdin", tmpl.String())
cd.logger.Debugf("writing to diskpart stdin: %s", tmpl.String())
fmt.Fprintf(dpStdin, "%s\r\n", tmpl.String())
dpStdin.Close()
}()
Expand All @@ -122,7 +125,7 @@ func (cd *createDiskAction) createDisk(path string, size int64) error {
return err
}

cd.logger.Infof("stdout: %s", dpStdout.String())
cd.logger.Debugf("create disk cmd stdout: %s\n", dpStdout.String())

return nil
}
3 changes: 3 additions & 0 deletions pkg/disk/dpgo/diskpart_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

//go:build windows
// +build windows

Expand Down
7 changes: 7 additions & 0 deletions pkg/flog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ type Logger interface {
SetFormatter(formatter Formatter)
}

// Log defines the properties of every log message.
type Log struct {
Level string `json:"level,omitempty"`
Message string `json:"msg,omitempty"`
Time string `json:"time,omitempty"`
}

// Level denotes a log level. Check the constants below for more information.
type Level int

Expand Down

0 comments on commit df7c8d8

Please sign in to comment.