internal/boot: Make some function public

More specifically only those that are needed in
/cmd/osbuild-image/tests.

This patch can be merged with the previous one if we want to make sure
every commit can be built, but I'm going to keep it like this for now so
that we can easily see the changes.
This commit is contained in:
Martin Sehnoutka 2020-09-01 10:37:29 +02:00 committed by Tom Gundersen
parent ec6ce8387d
commit 125fce92db
6 changed files with 62 additions and 61 deletions

View file

@ -23,10 +23,10 @@ type awsCredentials struct {
Bucket string
}
// getAWSCredentialsFromEnv gets the credentials from environment variables
// GetAWSCredentialsFromEnv gets the credentials from environment variables
// If none of the environment variables is set, it returns nil.
// If some but not all environment variables are set, it returns an error.
func getAWSCredentialsFromEnv() (*awsCredentials, error) {
func GetAWSCredentialsFromEnv() (*awsCredentials, error) {
accessKeyId, akExists := os.LookupEnv("AWS_ACCESS_KEY_ID")
secretAccessKey, sakExists := os.LookupEnv("AWS_SECRET_ACCESS_KEY")
region, regionExists := os.LookupEnv("AWS_REGION")
@ -54,9 +54,9 @@ func encodeBase64(input string) string {
return base64.StdEncoding.EncodeToString([]byte(input))
}
// createUserData creates cloud-init's user-data that contains user redhat with
// CreateUserData creates cloud-init's user-data that contains user redhat with
// the specified public key
func createUserData(publicKeyFile string) (string, error) {
func CreateUserData(publicKeyFile string) (string, error) {
publicKey, err := ioutil.ReadFile(publicKeyFile)
if err != nil {
return "", fmt.Errorf("cannot read the public key: %#v", err)
@ -83,11 +83,11 @@ func wrapErrorf(innerError error, format string, a ...interface{}) error {
return fmt.Errorf(format, a...)
}
// uploadImageToAWS mimics the upload feature of osbuild-composer.
// UploadImageToAWS mimics the upload feature of osbuild-composer.
// It takes an image and an image name and creates an ec2 instance from them.
// The s3 key is never returned - the same thing is done in osbuild-composer,
// the user has no way of getting the s3 key.
func uploadImageToAWS(c *awsCredentials, imagePath string, imageName string) error {
func UploadImageToAWS(c *awsCredentials, imagePath string, imageName string) error {
uploader, err := awsupload.New(c.Region, c.AccessKeyId, c.SecretAccessKey)
if err != nil {
return fmt.Errorf("cannot create aws uploader: %#v", err)
@ -105,8 +105,8 @@ func uploadImageToAWS(c *awsCredentials, imagePath string, imageName string) err
return nil
}
// newEC2 creates EC2 struct from given credentials
func newEC2(c *awsCredentials) (*ec2.EC2, error) {
// NewEC2 creates EC2 struct from given credentials
func NewEC2(c *awsCredentials) (*ec2.EC2, error) {
creds := credentials.NewStaticCredentials(c.AccessKeyId, c.SecretAccessKey, "")
sess, err := session.NewSession(&aws.Config{
Credentials: creds,
@ -126,9 +126,9 @@ type imageDescription struct {
// because this feature is not supported in composer
}
// describeEC2Image searches for EC2 image by its name and returns
// DescribeEC2Image searches for EC2 image by its name and returns
// its id and snapshot id
func describeEC2Image(e *ec2.EC2, imageName string) (*imageDescription, error) {
func DescribeEC2Image(e *ec2.EC2, imageName string) (*imageDescription, error) {
imageDescriptions, err := e.DescribeImages(&ec2.DescribeImagesInput{
Filters: []*ec2.Filter{
{
@ -151,8 +151,8 @@ func describeEC2Image(e *ec2.EC2, imageName string) (*imageDescription, error) {
}, nil
}
// deleteEC2Image deletes the specified image and its associated snapshot
func deleteEC2Image(e *ec2.EC2, imageDesc *imageDescription) error {
// DeleteEC2Image deletes the specified image and its associated snapshot
func DeleteEC2Image(e *ec2.EC2, imageDesc *imageDescription) error {
var retErr error
// firstly, deregister the image
@ -176,11 +176,11 @@ func deleteEC2Image(e *ec2.EC2, imageDesc *imageDescription) error {
return retErr
}
// withBootedImageInEC2 runs the function f in the context of booted
// WithBootedImageInEC2 runs the function f in the context of booted
// image in AWS EC2
func withBootedImageInEC2(e *ec2.EC2, imageDesc *imageDescription, publicKey string, f func(address string) error) (retErr error) {
func WithBootedImageInEC2(e *ec2.EC2, imageDesc *imageDescription, publicKey string, f func(address string) error) (retErr error) {
// generate user data with given public key
userData, err := createUserData(publicKey)
userData, err := CreateUserData(publicKey)
if err != nil {
return err
}
@ -188,7 +188,7 @@ func withBootedImageInEC2(e *ec2.EC2, imageDesc *imageDescription, publicKey str
// Security group must be now generated, because by default
// all traffic to EC2 instance is filtered.
securityGroupName, err := generateRandomString("osbuild-image-tests-security-group-")
securityGroupName, err := GenerateRandomString("osbuild-image-tests-security-group-")
if err != nil {
return fmt.Errorf("cannot generate a random name for the image: %#v", err)
}

View file

@ -156,7 +156,7 @@ func deleteResource(client resources.Client, id string, apiVersion string) error
return nil
}
// withBootedImageInAzure runs the function f in the context of booted
// WithBootedImageInAzure runs the function f in the context of booted
// image in Azure
func WithBootedImageInAzure(creds *azureCredentials, imageName, testId, publicKeyFile string, f func(address string) error) (retErr error) {
publicKey, err := readPublicKey(publicKeyFile)

View file

@ -19,9 +19,9 @@ import (
"github.com/osbuild/osbuild-composer/internal/distro"
)
// withNetworkNamespace provides the function f with a new network namespace
// WithNetworkNamespace provides the function f with a new network namespace
// which is deleted immediately after f returns
func withNetworkNamespace(f func(ns netNS) error) error {
func WithNetworkNamespace(f func(ns NetNS) error) error {
ns, err := newNetworkNamespace()
if err != nil {
return err
@ -95,9 +95,9 @@ func writeCloudInitISO(writer io.Writer, userData, metaData string) error {
return nil
}
// withBootedQemuImage boots the specified image in the specified namespace
// WithBootedQemuImage boots the specified image in the specified namespace
// using qemu. The VM is killed immediately after function returns.
func withBootedQemuImage(image string, ns netNS, f func() error) error {
func WithBootedQemuImage(image string, ns NetNS, f func() error) error {
return withTempFile("", "osbuild-image-tests-cloudinit", func(cloudInitFile *os.File) error {
err := writeCloudInitISO(
cloudInitFile,
@ -177,9 +177,9 @@ func withBootedQemuImage(image string, ns netNS, f func() error) error {
})
}
// withBootedNspawnImage boots the specified image in the specified namespace
// WithBootedNspawnImage boots the specified image in the specified namespace
// using nspawn. The VM is killed immediately after function returns.
func withBootedNspawnImage(image string, ns netNS, f func() error) error {
func WithBootedNspawnImage(image string, ns NetNS, f func() error) error {
cmd := exec.Command(
"systemd-nspawn",
"--boot", "--register=no",
@ -202,9 +202,9 @@ func withBootedNspawnImage(image string, ns netNS, f func() error) error {
return f()
}
// withBootedNspawnImage boots the specified directory in the specified namespace
// WithBootedNspawnImage boots the specified directory in the specified namespace
// using nspawn. The VM is killed immediately after function returns.
func withBootedNspawnDirectory(dir string, ns netNS, f func() error) error {
func WithBootedNspawnDirectory(dir string, ns NetNS, f func() error) error {
cmd := exec.Command(
"systemd-nspawn",
"--boot", "--register=no",
@ -227,10 +227,10 @@ func withBootedNspawnDirectory(dir string, ns netNS, f func() error) error {
return f()
}
// withExtractedTarArchive extracts the provided archive and passes
// WithExtractedTarArchive extracts the provided archive and passes
// a path to the result to the function f. The result is deleted
// immediately after the function returns.
func withExtractedTarArchive(archive string, f func(dir string) error) error {
func WithExtractedTarArchive(archive string, f func(dir string) error) error {
return withTempDir("", "tar-archive", func(dir string) error {
cmd := exec.Command(
"tar",
@ -249,10 +249,10 @@ func withExtractedTarArchive(archive string, f func(dir string) error) error {
})
}
// withSSHKeyPair runs the function f with a newly generated
// WithSSHKeyPair runs the function f with a newly generated
// ssh key-pair, they key-pair is deleted immediately after
// the function f returns
func withSSHKeyPair(f func(privateKey, publicKey string) error) error {
func WithSSHKeyPair(f func(privateKey, publicKey string) error) error {
return withTempDir("", "keys", func(dir string) error {
privateKey := dir + "/id_rsa"
publicKey := dir + "/id_rsa.pub"

View file

@ -53,9 +53,9 @@ func killProcessCleanly(process *os.Process, timeout time.Duration) error {
return process.Kill()
}
// generateRandomString generates a new random string with specified prefix.
// GenerateRandomString generates a new random string with specified prefix.
// The random part is based on UUID.
func generateRandomString(prefix string) (string, error) {
func GenerateRandomString(prefix string) (string, error) {
id, err := uuid.NewRandom()
if err != nil {
return "", err

View file

@ -19,12 +19,12 @@ import (
const netnsDir = "/var/run/netns"
// Network namespace abstraction
type netNS string
type NetNS string
// newNetworkNamespace returns a new network namespace with a random
// name. The calling goroutine remains in the same namespace
// as before the call.
func newNetworkNamespace() (netNS, error) {
func newNetworkNamespace() (NetNS, error) {
// This method needs to unshare the current thread. Go runtime can switch
// the goroutine to run on a different thread at any point, so we need
// to ensure that this method runs in the same thread for its whole
@ -99,7 +99,7 @@ func newNetworkNamespace() (netNS, error) {
return "", fmt.Errorf("cannot bind mount the new namespace: %#v", err)
}
ns := netNS(path.Base(f.Name()))
ns := NetNS(path.Base(f.Name()))
// Initialization OK, do not delete the namespace file.
initOK = true
@ -109,7 +109,7 @@ func newNetworkNamespace() (netNS, error) {
// NamespaceCommand returns an *exec.Cmd struct with the difference
// that it's prepended by "ip netns exec NAMESPACE_NAME" command, which
// runs the command in a namespaced environment.
func (n netNS) NamespacedCommand(name string, arg ...string) *exec.Cmd {
func (n NetNS) NamespacedCommand(name string, arg ...string) *exec.Cmd {
args := []string{"netns", "exec", string(n), name}
args = append(args, arg...)
return exec.Command("ip", args...)
@ -118,19 +118,19 @@ func (n netNS) NamespacedCommand(name string, arg ...string) *exec.Cmd {
// NamespaceCommand returns an *exec.Cmd struct with the difference
// that it's prepended by "ip netns exec NAMESPACE_NAME" command, which
// runs the command in a namespaced environment.
func (n netNS) NamespacedCommandContext(ctx context.Context, name string, arg ...string) *exec.Cmd {
func (n NetNS) NamespacedCommandContext(ctx context.Context, name string, arg ...string) *exec.Cmd {
args := []string{"netns", "exec", string(n), name}
args = append(args, arg...)
return exec.CommandContext(ctx, "ip", args...)
}
// Path returns the path to the namespace file
func (n netNS) Path() string {
func (n NetNS) Path() string {
return path.Join(netnsDir, string(n))
}
// Delete deletes the namespaces
func (n netNS) Delete() error {
func (n NetNS) Delete() error {
cmd := exec.Command("umount", n.Path())
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout