Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commonize utils into one new package #90

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/mosb/mkboot.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/apex/log"
"github.com/pkg/errors"
"github.com/project-machine/mos/pkg/mosconfig"
"github.com/project-machine/mos/pkg/utils"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -58,7 +59,7 @@ func doMkBoot(ctx *cli.Context) error {
defer os.RemoveAll(tmpd)

cachedir := filepath.Join(tmpd, "cache")
mosconfig.EnsureDir(cachedir)
utils.EnsureDir(cachedir)
ociboot := mosconfig.OciBoot{
KeySet: s[0],
Project: s[1],
Expand Down
3 changes: 2 additions & 1 deletion cmd/mosctl/activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/project-machine/mos/pkg/mosconfig"
"github.com/project-machine/mos/pkg/utils"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -32,7 +33,7 @@ var activateCmd = cli.Command{

func doActivate(ctx *cli.Context) error {
rfs := ctx.String("root")
if rfs == "" || !mosconfig.PathExists(rfs) {
if rfs == "" || !utils.PathExists(rfs) {
return fmt.Errorf("A valid root directory must be specified")
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/mosctl/initrdsetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import (
"fmt"

"github.com/project-machine/mos/pkg/mosconfig"
"github.com/project-machine/mos/pkg/trust"
"github.com/project-machine/mos/pkg/utils"
"github.com/urfave/cli"
)

Expand All @@ -15,7 +15,7 @@ var initrdSetupCmd = cli.Command{
}

func doInitrdSetup(ctx *cli.Context) error {
if !mosconfig.PathExists("/dev/tpm0") {
if !utils.PathExists("/dev/tpm0") {
return fmt.Errorf("No TPM. No other subsystems have been implemented")
}

Expand Down
24 changes: 13 additions & 11 deletions cmd/mosctl/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/pkg/errors"
"github.com/project-machine/mos/pkg/mosconfig"
"github.com/project-machine/mos/pkg/trust"
"github.com/project-machine/mos/pkg/utils"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -205,7 +206,7 @@ func makeFileSystemEFI(path string, ssize int) error {
cmd = append(cmd, fmt.Sprintf("-S%d", ssize))
}
cmd = append(cmd, path)
return mosconfig.RunCommand(cmd...)
return utils.RunCommand(cmd...)
}

func makeFileSystemEXT4(path, label string) error {
Expand Down Expand Up @@ -249,7 +250,7 @@ type MkExt4Opts struct {
}

func MkExt4FS(path string, opts MkExt4Opts) error {
conf, err := mosconfig.WriteTempFile("/tmp", "mkfsconf-", mke2fsConf)
conf, err := utils.WriteTempFile("/tmp", "mkfsconf-", mke2fsConf)
if err != nil {
return err
}
Expand All @@ -270,7 +271,8 @@ func MkExt4FS(path string, opts MkExt4Opts) error {
}

cmd = append(cmd, path)
return mosconfig.RunCommandEnv(append(os.Environ(), "MKE2FS_CONFIG="+conf), cmd...)
env := []string{"MKE2FS_CONFIG=" + conf}
return utils.RunCommandEnv(cmd, env)
}

func sortedPartNums(pSet *disko.PartitionSet) []uint {
Expand Down Expand Up @@ -316,7 +318,7 @@ func wipeDiskParts(disk disko.Disk, skipPart func(disko.Disk, uint) bool) error
return errors.Wrap(err, "Failed to close a filehandle?")
}

if err := mosconfig.RunCommand("udevadm", "settle"); err != nil {
if err := utils.RunCommand("udevadm", "settle"); err != nil {
log.Warnf("Failed udevadm settle after wipingParts")
}

Expand Down Expand Up @@ -476,7 +478,7 @@ func findInstallDisk(disks disko.DiskSet) (disko.Disk, error) {
// Obviously this should become a lot more flexible. What's here
// suffices for enabling/testing the core functionality.
func doPartition(opts mosconfig.InstallOpts) error {
luksKey, err := mosconfig.ReadKeyFromUserKeyring("machine:luks")
luksKey, err := utils.ReadKeyFromUserKeyring("machine:luks")
if err != nil {
return err
}
Expand Down Expand Up @@ -554,7 +556,7 @@ func doPartition(opts mosconfig.InstallOpts) error {
return errors.Wrapf(err, "Failed creating EFI partition")
}
dest := filepath.Join(opts.RFS, "boot/efi")
if err := mosconfig.EnsureDir(dest); err != nil {
if err := utils.EnsureDir(dest); err != nil {
return errors.Wrapf(err, "Failed creating boot/EFI")
}
if err := syscall.Mount(efiPath, dest, "vfat", 0, ""); err != nil {
Expand All @@ -566,7 +568,7 @@ func doPartition(opts mosconfig.InstallOpts) error {
if err := makeFileSystemEXT4(configPath, configPart); err != nil {
return errors.Wrapf(err, "Failed creating ext4 on %s", configPath)
}
if err := mosconfig.EnsureDir(opts.ConfigDir); err != nil {
if err := utils.EnsureDir(opts.ConfigDir); err != nil {
return errors.Wrapf(err, "Failed creating mount path %s", opts.ConfigDir)
}
if err := syscall.Mount(configPath, opts.ConfigDir, "ext4", 0, ""); err != nil {
Expand All @@ -579,7 +581,7 @@ func doPartition(opts mosconfig.InstallOpts) error {
if err := makeFileSystemEXT4(storePath, storePart); err != nil {
return errors.Wrapf(err, "Failed creating ext4 on %s", storePath)
}
if err := mosconfig.EnsureDir(opts.StoreDir); err != nil {
if err := utils.EnsureDir(opts.StoreDir); err != nil {
return errors.Wrapf(err, "Failed creating mount path %s", opts.StoreDir)
}
if err := syscall.Mount(storePath, opts.StoreDir, "ext4", 0, ""); err != nil {
Expand Down Expand Up @@ -617,13 +619,13 @@ func doInstall(ctx *cli.Context) error {
return errors.Wrapf(err, "Failed partitioning")
}
} else {
if !mosconfig.PathExists(opts.CaPath) {
if !utils.PathExists(opts.CaPath) {
return errors.Errorf("Install manifest CA (%s) missing", opts.CaPath)
}
if !mosconfig.PathExists(opts.ConfigDir) {
if !utils.PathExists(opts.ConfigDir) {
return errors.Errorf("Configuration directory (%s) missing", opts.ConfigDir)
}
if !mosconfig.PathExists(opts.StoreDir) {
if !utils.PathExists(opts.StoreDir) {
return errors.Errorf("Storage cache dir (%s) missing", opts.StoreDir)
}
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/mosctl/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/apex/log"
"github.com/project-machine/mos/pkg/mosconfig"
"github.com/project-machine/mos/pkg/utils"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -36,7 +37,7 @@ var mountCmd = cli.Command{

func doMount(ctx *cli.Context) error {
rfs := ctx.String("root")
if rfs == "" || !mosconfig.PathExists(rfs) {
if rfs == "" || !utils.PathExists(rfs) {
return fmt.Errorf("A valid root directory must be specified")
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/mosctl/preinstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import (
"fmt"

"github.com/project-machine/mos/pkg/mosconfig"
"github.com/project-machine/mos/pkg/trust"
"github.com/project-machine/mos/pkg/utils"
"github.com/urfave/cli"
)

Expand All @@ -15,7 +15,7 @@ var preInstallCmd = cli.Command{
}

func doPreInstall(ctx *cli.Context) error {
if !mosconfig.PathExists("/dev/tpm0") {
if !utils.PathExists("/dev/tpm0") {
return fmt.Errorf("No TPM. No other subsystems have been implemented")
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/mosctl/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"

"github.com/apex/log"
"github.com/project-machine/mos/pkg/mosconfig"
"github.com/project-machine/mos/pkg/trust"
"github.com/project-machine/mos/pkg/utils"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -33,7 +33,7 @@ func doProvision(ctx *cli.Context) error {
log.Warnf("No disk specified. No disk will be provisioned")
}

if !mosconfig.PathExists("/dev/tpm0") {
if !utils.PathExists("/dev/tpm0") {
return fmt.Errorf("No TPM. No other subsystems have been implemented")
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/mosctl/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"

"github.com/project-machine/mos/pkg/mosconfig"
"github.com/project-machine/mos/pkg/utils"
"github.com/urfave/cli"
)

Expand All @@ -23,7 +24,7 @@ var updateCmd = cli.Command{

func doUpdate(ctx *cli.Context) error {
rfs := ctx.String("root")
if rfs == "" || !mosconfig.PathExists(rfs) {
if rfs == "" || !utils.PathExists(rfs) {
return fmt.Errorf("A valid root directory must be specified")
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/trust/initrdsetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/project-machine/mos/pkg/trust"
"github.com/project-machine/mos/pkg/utils"
"github.com/urfave/cli"
)

Expand All @@ -14,7 +15,7 @@ var initrdSetupCmd = cli.Command{
}

func doInitrdSetup(ctx *cli.Context) error {
if !PathExists("/dev/tpm0") {
if !utils.PathExists("/dev/tpm0") {
return fmt.Errorf("No TPM. No other subsystems have been implemented")
}

Expand Down
35 changes: 18 additions & 17 deletions cmd/trust/keyset.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/project-machine/mos/pkg/mosconfig"
tree "github.com/project-machine/mos/pkg/printdirtree"
"github.com/project-machine/mos/pkg/trust"
"github.com/project-machine/mos/pkg/utils"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -102,12 +103,12 @@ func initkeyset(keysetName string, Org []string) error {
return errors.New("keyset parameter is missing")
}

moskeysetPath, err := getMosKeyPath()
moskeysetPath, err := utils.GetMosKeyPath()
if err != nil {
return err
}
keysetPath := filepath.Join(moskeysetPath, keysetName)
if PathExists(keysetPath) {
if utils.PathExists(keysetPath) {
return fmt.Errorf("%s keyset already exists", keysetName)
}

Expand Down Expand Up @@ -191,11 +192,11 @@ func initkeyset(keysetName string, Org []string) error {

// Generate sample uuid, manifest key and cert
mName := filepath.Join(keysetPath, "manifest", "default")
if err = trust.EnsureDir(mName); err != nil {
if err = utils.EnsureDir(mName); err != nil {
return errors.Wrapf(err, "Failed creating default project directory")
}
sName := filepath.Join(mName, "sudi")
if err = trust.EnsureDir(sName); err != nil {
if err = utils.EnsureDir(sName); err != nil {
return errors.Wrapf(err, "Failed creating default sudi directory")
}

Expand Down Expand Up @@ -337,13 +338,13 @@ func doAddKeyset(ctx *cli.Context) error {
}

// See if keyset exists
mosKeyPath, err := getMosKeyPath()
mosKeyPath, err := utils.GetMosKeyPath()
if err != nil {
return err
}

keysetPath := filepath.Join(mosKeyPath, keysetName)
if PathExists(keysetPath) {
if utils.PathExists(keysetPath) {
return fmt.Errorf("%s keyset already exists", keysetName)
}

Expand Down Expand Up @@ -381,7 +382,7 @@ func doListKeysets(ctx *cli.Context) error {
if len(ctx.Args()) != 0 {
return fmt.Errorf("Wrong number of arguments (please see \"--help\")")
}
moskeysetPath, err := getMosKeyPath()
moskeysetPath, err := utils.GetMosKeyPath()
if err != nil {
return err
}
Expand All @@ -407,13 +408,13 @@ func doShowKeyset(ctx *cli.Context) error {
return fmt.Errorf("Please specify keyset name. Select from 'trust keyset list'")
}

moskeysetPath, err := getMosKeyPath()
moskeysetPath, err := utils.GetMosKeyPath()
if err != nil {
return err
}

keysetPath := filepath.Join(moskeysetPath, keysetName)
if !PathExists(keysetPath) {
if !utils.PathExists(keysetPath) {
return fmt.Errorf("Unknown keyset '%s', cannot find keyset at path: %q", keysetName, keysetPath)
}

Expand Down Expand Up @@ -453,14 +454,14 @@ func doShowKeyset(ctx *cli.Context) error {
}

keyPath := filepath.Join(keysetPath, keyName)
if !PathExists(keyPath) {
if !utils.PathExists(keyPath) {
return fmt.Errorf("Keyset %s key %q does not exist at %q", keysetName, keyName, keyPath)
}

if len(ctx.Args()) > 2 {
item := ctx.Args()[2]
fullPath := filepath.Join(keyPath, item)
if !PathExists(fullPath) {
if !utils.PathExists(fullPath) {
return fmt.Errorf("Failed reading keyset %s key %s item %s at %q: %w", keysetName, keyName, item, fullPath, err)
}

Expand Down Expand Up @@ -524,10 +525,10 @@ func doAddPCR7data(ctx *cli.Context) error {
if limited == "" || tpm == "" || prod == "" {
return errors.New("PCR7 values are missing")
}
if !PathExists(limited) || !PathExists(tpm) || !PathExists(prod) {
if !utils.PathExists(limited) || !utils.PathExists(tpm) || !utils.PathExists(prod) {
return errors.New("Some PCR7 files do not exist")
}
if !PathExists(passwdPolicy) || !PathExists(luksPolicy) {
if !utils.PathExists(passwdPolicy) || !utils.PathExists(luksPolicy) {
return errors.New("Some policy digest files do not exist")
}

Expand Down Expand Up @@ -577,13 +578,13 @@ func buildProvisioner(keysetName string) error {
return nil
}

moskeysetPath, err := getMosKeyPath()
moskeysetPath, err := utils.GetMosKeyPath()
if err != nil {
return err
}
keyPath := filepath.Join(moskeysetPath, keysetName)
outfile := filepath.Join(keyPath, "artifacts", "provision.iso")
trust.EnsureDir(filepath.Dir(outfile))
utils.EnsureDir(filepath.Dir(outfile))

if err := mosconfig.BuildProvisioner(keysetName, "default", outfile); err != nil {
return errors.Wrapf(err, "Failed to create provisioning ISO")
Expand All @@ -599,13 +600,13 @@ func buildInstaller(keysetName string) error {
return nil
}

moskeysetPath, err := getMosKeyPath()
moskeysetPath, err := utils.GetMosKeyPath()
if err != nil {
return err
}
keyPath := filepath.Join(moskeysetPath, keysetName)
outfile := filepath.Join(keyPath, "artifacts", "install.iso")
if err := trust.EnsureDir(filepath.Dir(outfile)); err != nil {
if err := utils.EnsureDir(filepath.Dir(outfile)); err != nil {
return errors.Wrapf(err, "Failed creating %q", outfile)
}

Expand Down
Loading