Skip to content
This repository has been archived by the owner on Feb 24, 2020. It is now read-only.

Commit

Permalink
stage1: dedupe shared volume path creation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
euank committed Dec 12, 2016
1 parent 25284c0 commit a64181f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
19 changes: 19 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package common

import (
"bufio"
"errors"
"fmt"
"net"
"os"
Expand All @@ -37,6 +38,7 @@ import (

const (
sharedVolumesDir = "/sharedVolumes"
SharedVolumePerm = os.FileMode(0755)
stage1Dir = "/stage1"
stage2Dir = "/opt/stage2"
AppsInfoDir = "/appsinfo"
Expand Down Expand Up @@ -199,6 +201,23 @@ func SharedVolumesPath(root string) string {
return filepath.Join(root, sharedVolumesDir)
}

// CreateSharedVolumesPath ensures the sharedVolumePath for the pod root passed
// in exists. It returns the shared volume path or an error.
func CreateSharedVolumesPath(root string) (string, error) {
sharedVolPath := SharedVolumesPath(root)

if err := os.MkdirAll(sharedVolPath, SharedVolumePerm); err != nil {
return "", errwrap.Wrap(errors.New("could not create shared volumes directory"), err)
}
// In case it already existed and we didn't make it, ensure permissions are
// what the caller expects them to be.
if err := os.Chmod(sharedVolPath, SharedVolumePerm); err != nil {
return "", errwrap.Wrap(fmt.Errorf("could not change permissions of %q", sharedVolPath), err)
}

return sharedVolPath, nil
}

// MetadataServicePublicURL returns the public URL used to host the metadata service
func MetadataServicePublicURL(ip net.IP, token string) string {
return fmt.Sprintf("http://%v:%v/%v", ip, MetadataServicePort, token)
Expand Down
17 changes: 7 additions & 10 deletions stage1/init/common/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,24 +274,24 @@ func BindMount(mnt fs.MountUnmounter, source, destination string, readOnly bool)
}

// ensureDestinationExists will recursively create a given mountpoint. If directories
// are created, their permissions are initialized to stage1/init/common.SharedVolPerm
// are created, their permissions are initialized to common.SharedVolumePerm
func ensureDestinationExists(source, destination string) error {
fileInfo, err := os.Stat(source)
if err != nil {
return errwrap.Wrap(fmt.Errorf("could not stat source location: %v", source), err)
}

targetPathParent, _ := filepath.Split(destination)
if err := os.MkdirAll(targetPathParent, SharedVolPerm); err != nil {
if err := os.MkdirAll(targetPathParent, common.SharedVolumePerm); err != nil {
return errwrap.Wrap(fmt.Errorf("could not create parent directory: %v", targetPathParent), err)
}

if fileInfo.IsDir() {
if err := os.Mkdir(destination, SharedVolPerm); err != nil && !os.IsExist(err) {
if err := os.Mkdir(destination, common.SharedVolumePerm); err != nil && !os.IsExist(err) {
return errwrap.Wrap(errors.New("could not create destination directory "+destination), err)
}
} else {
if file, err := os.OpenFile(destination, os.O_CREATE, SharedVolPerm); err != nil {
if file, err := os.OpenFile(destination, os.O_CREATE, common.SharedVolumePerm); err != nil {
return errwrap.Wrap(errors.New("could not create destination file"), err)
} else {
file.Close()
Expand All @@ -301,12 +301,9 @@ func ensureDestinationExists(source, destination string) error {
}

func AppAddMounts(p *stage1commontypes.Pod, ra *schema.RuntimeApp, enterCmd []string) error {
sharedVolPath := common.SharedVolumesPath(p.Root)
if err := os.MkdirAll(sharedVolPath, SharedVolPerm); err != nil {
return errwrap.Wrap(errors.New("could not create shared volumes directory"), err)
}
if err := os.Chmod(sharedVolPath, SharedVolPerm); err != nil {
return errwrap.Wrap(fmt.Errorf("could not change permissions of %q", sharedVolPath), err)
sharedVolPath, err := common.CreateSharedVolumesPath(p.Root)
if err != nil {
return err
}

vols := make(map[types.ACName]types.Volume)
Expand Down
12 changes: 4 additions & 8 deletions stage1/init/common/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ import (

const (
// FlavorFile names the file storing the pod's flavor
FlavorFile = "flavor"
SharedVolPerm = os.FileMode(0755)
FlavorFile = "flavor"
)

// execEscape uses Golang's string quoting for ", \, \n, and regex for special cases
Expand Down Expand Up @@ -464,12 +463,9 @@ func appToNspawnArgs(p *stage1commontypes.Pod, ra *schema.RuntimeApp) ([]string,
appName := ra.Name
app := ra.App

sharedVolPath := common.SharedVolumesPath(p.Root)
if err := os.MkdirAll(sharedVolPath, SharedVolPerm); err != nil {
return nil, errwrap.Wrap(errors.New("could not create shared volumes directory"), err)
}
if err := os.Chmod(sharedVolPath, SharedVolPerm); err != nil {
return nil, errwrap.Wrap(fmt.Errorf("could not change permissions of %q", sharedVolPath), err)
sharedVolPath, err := common.CreateSharedVolumesPath(p.Root)
if err != nil {
return nil, err
}

vols := make(map[types.ACName]types.Volume)
Expand Down
15 changes: 6 additions & 9 deletions stage1/init/kvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,9 @@ func KvmNetworkingToSystemd(p *stage1commontypes.Pod, n *networking.Networking)
func mountSharedVolumes(p *stage1commontypes.Pod, ra *schema.RuntimeApp) error {
appName := ra.Name

sharedVolPath := common.SharedVolumesPath(p.Root)
if err := os.MkdirAll(sharedVolPath, stage1initcommon.SharedVolPerm); err != nil {
return errwrap.Wrap(errors.New("could not create shared volumes directory"), err)
}
if err := os.Chmod(sharedVolPath, stage1initcommon.SharedVolPerm); err != nil {
return errwrap.Wrap(fmt.Errorf("could not change permissions of %q", sharedVolPath), err)
sharedVolPath, err := common.CreateSharedVolumesPath(p.Root)
if err != nil {
return err
}

imageManifest := p.Images[appName.String()]
Expand Down Expand Up @@ -149,16 +146,16 @@ func ensureDestinationExists(source, destination string) error {
}

targetPathParent, _ := filepath.Split(destination)
if err := os.MkdirAll(targetPathParent, stage1initcommon.SharedVolPerm); err != nil {
if err := os.MkdirAll(targetPathParent, common.SharedVolumePerm); err != nil {
return errwrap.Wrap(fmt.Errorf("could not create parent directory: %v", targetPathParent), err)
}

if fileInfo.IsDir() {
if err := os.Mkdir(destination, stage1initcommon.SharedVolPerm); !os.IsExist(err) {
if err := os.Mkdir(destination, common.SharedVolumePerm); !os.IsExist(err) {
return err
}
} else {
if file, err := os.OpenFile(destination, os.O_CREATE, stage1initcommon.SharedVolPerm); err != nil {
if file, err := os.OpenFile(destination, os.O_CREATE, common.SharedVolumePerm); err != nil {
return err
} else {
file.Close()
Expand Down

0 comments on commit a64181f

Please sign in to comment.