Andrew Rynhard d430a37e46 refactor: use go 1.13 error wrapping
This removes the github.com/pkg/errors package in favor of the official
error wrapping in go 1.13.

Signed-off-by: Andrew Rynhard <andrew@andrewrynhard.com>
2019-10-15 22:20:50 -07:00

87 lines
2.2 KiB
Go

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package metal
import (
"fmt"
"io/ioutil"
"net"
"path/filepath"
"golang.org/x/sys/unix"
"github.com/talos-systems/talos/internal/pkg/kernel"
"github.com/talos-systems/talos/internal/pkg/runtime"
"github.com/talos-systems/talos/pkg/blockdevice/probe"
"github.com/talos-systems/talos/pkg/constants"
"github.com/talos-systems/talos/pkg/download"
)
const (
mnt = "/mnt"
)
// Metal is a discoverer for non-cloud environments.
type Metal struct{}
// Name implements the platform.Platform interface.
func (b *Metal) Name() string {
return "Metal"
}
// Configuration implements the platform.Platform interface.
func (b *Metal) Configuration() ([]byte, error) {
var option *string
if option = kernel.ProcCmdline().Get(constants.KernelParamConfig).First(); option == nil {
return nil, fmt.Errorf("no config option was found")
}
switch *option {
case constants.MetalConfigISOLabel:
return readConfigFromISO()
default:
return download.Download(*option)
}
}
// Hostname implements the platform.Platform interface.
func (b *Metal) Hostname() (hostname []byte, err error) {
return nil, nil
}
// Mode implements the platform.Platform interface.
func (b *Metal) Mode() runtime.Mode {
return runtime.Metal
}
// ExternalIPs provides any external addresses assigned to the instance
func (b *Metal) ExternalIPs() (addrs []net.IP, err error) {
return addrs, err
}
func readConfigFromISO() (b []byte, err error) {
var dev *probe.ProbedBlockDevice
dev, err = probe.GetDevWithFileSystemLabel(constants.MetalConfigISOLabel)
if err != nil {
return nil, fmt.Errorf("failed to find %s iso: %w", constants.MetalConfigISOLabel, err)
}
if err = unix.Mount(dev.Path, mnt, dev.SuperBlock.Type(), unix.MS_RDONLY, ""); err != nil {
return nil, fmt.Errorf("failed to mount iso: %w", err)
}
b, err = ioutil.ReadFile(filepath.Join(mnt, filepath.Base(constants.ConfigPath)))
if err != nil {
return nil, fmt.Errorf("read config: %s", err.Error())
}
if err = unix.Unmount(mnt, 0); err != nil {
return nil, fmt.Errorf("failed to unmount: %w", err)
}
return b, nil
}