Skip to content

Commit

Permalink
Merge pull request #50 from AntonioMeireles/individualNFS
Browse files Browse the repository at this point in the history
multi-user support
  • Loading branch information
AntonioMeireles committed Jan 8, 2016
2 parents 800a09a + e7e2441 commit cbd7ed0
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 50 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ export GOOS=darwin
export CGO_ENABLED=1

VERSION := $(shell git describe --abbrev=6 --dirty=-unreleased --always --tags)
BUILDDATE = $(shell /bin/date "+%FT%T%Z")

ifeq ($(DEBUG),true)
GO_GCFLAGS := -gcflags "-N -l"
else
GO_LDFLAGS := $(GO_LDFLAGS) -w -s
endif

GO_LDFLAGS := $(GO_LDFLAGS) -X main.Version=$(VERSION)
GO_LDFLAGS := $(GO_LDFLAGS) -X main.Version=$(VERSION) \
-X main.BuildDate=$(BUILDDATE)

all: corectl docs
@git status
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Update Strategy: No Reboots
Last login: Tue Aug 25 13:23:20 +0000 2015 on /dev/tty1.
core@localhost ~ $
```
you 'll find out that `/Users` is available (via NFS) already inside your VM.
you 'll find out that your `${HOME}` become available (via NFS) inside your VM.
that will come handy when you come to play with `docker` volumes later...
### usage
Expand Down
18 changes: 12 additions & 6 deletions globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ import (
"github.com/spf13/viper"
)

const LatestImageBreackage = "2016-01-08T16:00:00WET"

type (
vmContext struct{ vm *VMInfo }
sessionContext struct {
configDir, imageDir, runDir, pwd, uid, gid, username string
hasPowers, debug, json bool
rawArgs *viper.Viper
VMs []vmContext
configDir, imageDir, runDir, pwd, uid, gid, homedir string
hasPowers, debug, json bool
rawArgs *viper.Viper
VMs []vmContext
}
// VMInfo - per VM settings
VMInfo struct {
Expand Down Expand Up @@ -272,16 +274,20 @@ COREOS_PRIVATE_IPV4=${COREOS_PUBLIC_IPV4}
block-until-url "${endpoint}"
HOSTNAME="$(curl -Ls ${endpoint}/hostname)"
HOMEDIR="$(curl -Ls ${endpoint}/homedir)"
( echo endpoint=${endpoint}
echo UUID=${UUID}
echo HOSTNAME="${HOSTNAME}"
echo HOMEDIR="${HOMEDIR}"
echo COREOS_PUBLIC_IPV4=${COREOS_PUBLIC_IPV4}
echo COREOS_PRIVATE_IPV4=${COREOS_PRIVATE_IPV4}
) > /etc/environment
sed -i "s,@@hostname@@,${HOSTNAME},g" /usr/share/oem/xhyve.yml
sed -i "s,@@homedir@@,${HOMEDIR},g" /usr/share/oem/xhyve.yml
sed -i "s,Users\.mount,$(systemd-escape -p ${HOMEDIR})\.mount,g" /usr/share/oem/xhyve.yml
echo "$(curl -Ls ${endpoint}/sshKey)" | update-ssh-keys -a proc-cmdline-ssh_internal
Expand Down Expand Up @@ -320,8 +326,8 @@ coreos:
command: start
content: |
[Mount]
What=192.168.64.1:/Users
Where=/Users
What=192.168.64.1:@@homedir@@
Where=@@homedir@@
Options=rw,async,nolock,noatime,rsize=32768,wsize=32768
Type=nfs
`
Expand Down
8 changes: 7 additions & 1 deletion helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func (session *sessionContext) init() (err error) {
session.runDir = filepath.Join(session.configDir, "/running/")

session.uid, session.gid = caller.Uid, caller.Gid
session.username = caller.Username
session.homedir = caller.HomeDir

if session.pwd, err = os.Getwd(); err != nil {
return
Expand Down Expand Up @@ -387,6 +387,12 @@ func (vm *VMInfo) metadataService() (endpoint string, err error) {
w.Write([]byte(vm.Name))
}
})
mux.HandleFunc(root+"/homedir",
func(w http.ResponseWriter, r *http.Request) {
if isAllowed(rIP(r.RemoteAddr), w) {
w.Write([]byte(engine.homedir))
}
})

srv := &http.Server{
Addr: fmt.Sprintf(":%v", free.Addr().(*net.TCPAddr).Port),
Expand Down
25 changes: 18 additions & 7 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"time"

"github.com/blang/semver"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -87,9 +88,10 @@ func init() {

func localImages() (local map[string]semver.Versions, err error) {
var (
files []os.FileInfo
f os.FileInfo
channel string
files []os.FileInfo
f os.FileInfo
channel string
stamp, _ = time.Parse("2006-01-02T15:04:05MST", LatestImageBreackage)
)
local = make(map[string]semver.Versions, 0)
for _, channel = range DefaultChannels {
Expand All @@ -100,11 +102,20 @@ func localImages() (local map[string]semver.Versions, err error) {
var v semver.Versions
for _, f = range files {
if f.IsDir() {
var s semver.Version
if s, err = semver.Make(f.Name()); err != nil {
return
if f.ModTime().After(stamp) {
var s semver.Version
if s, err = semver.Make(f.Name()); err != nil {
return
}
v = append(v, s)
} else {
// force rebuild if local image assembled before last time
// we changed its expcted format
if err = os.RemoveAll(filepath.Join(engine.imageDir,
channel, f.Name())); err != nil {
return
}
}
v = append(v, s)
}
}
semver.Sort(v)
Expand Down
11 changes: 7 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"log"
"os"
"strings"
"time"

"github.com/google/go-github/github"
"github.com/spf13/cobra"
Expand All @@ -45,7 +46,7 @@ var (
engine sessionContext
// -ldflags "-X main.Version=
// `git describe --abbrev=6 --dirty=-unreleased --always --tags`"
Version string
Version, BuildDate string
)

func main() {
Expand All @@ -72,13 +73,15 @@ func init() {

func versionCommand(cmd *cobra.Command, args []string) {
var (
err error
latest *github.RepositoryRelease
err error
latest *github.RepositoryRelease
stamp, _ = time.Parse("2006-01-02T15:04:05MST", BuildDate)
)
fmt.Printf("%s\n%s\n\n", "CoreOS over OSX made simple.",
"❯❯❯ http://github.com/TheNewNormal/corectl")
fmt.Printf("Installed version: %s (built at %v)\n",
strings.TrimPrefix(Version, "v"), stamp)

fmt.Println("Installed version:", strings.TrimPrefix(Version, "v"))
if latest, _, err =
github.NewClient(nil).Repositories.GetLatestRelease("TheNewNormal",
"corectl"); err != nil {
Expand Down
55 changes: 25 additions & 30 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,27 +343,23 @@ func init() {
func nfsSetup() (err error) {
const exportsF = "/etc/exports"
var (
buf []byte
shared bool
right string
shortSig = "/Users -network 192.168.64.0 " +
buf, bufN []byte
shared bool
oldSig = "/Users -network 192.168.64.0 " +
"-mask 255.255.255.0 -alldirs -mapall="
suffix = engine.uid + ":" + engine.gid
signature = shortSig + suffix
exportSet = func() (ok bool, err error) {
signature = engine.homedir + " -network 192.168.64.0 " +
"-mask 255.255.255.0 -alldirs -mapall=" + suffix
exportSet = func() (ok bool) {
for _, line := range strings.Split(string(buf), "\n") {
if strings.HasPrefix(line, shortSig) {
right = strings.Split(line, "=")[1]
if right != suffix {
err = fmt.Errorf("'/Users' is already being shared " +
"via NFS by another user other than the " +
"currently running one. Either keep invoking " +
"'corectl' as that user or manually fix your " +
"'/etc/exports' file")
break
}
if strings.HasPrefix(line, signature) {
ok = true
}
if !strings.HasPrefix(line, oldSig) {
bufN = append(bufN, []byte(line+"\n")...)
} else {
bufN = append(bufN, []byte("\n")...)
}
}
return
}
Expand All @@ -376,17 +372,17 @@ func nfsSetup() (err error) {
}
return false
}()
exportsCheck = func() (err error) {
exportsCheck = func(previous []byte) (err error) {
cmd := exec.Command("nfsd", "-F", exportsF, "checkexports")
cmd.Stdin, cmd.Stdout, cmd.Stderr = nil, nil, os.Stderr

if err = cmd.Run(); err != nil {
err = fmt.Errorf("unable to validate %s (see above)", exportsF)
// getting back to where we were
ioutil.WriteFile(exportsF, buf, os.ModeAppend)
ioutil.WriteFile(exportsF, previous, os.ModeAppend)
}
return
}()
}
)
// check if /etc/exports exists, and if not create an empty one
if _, err = os.Stat(exportsF); os.IsNotExist(err) {
Expand All @@ -399,15 +395,13 @@ func nfsSetup() (err error) {
return
}

if shared, err = exportSet(); err != nil {
return
if shared = exportSet(); !shared {
ioutil.WriteFile(exportsF, append(bufN, []byte(signature)...),
os.ModeAppend)
}

if !shared {
ioutil.WriteFile(exportsF,
append(buf, append([]byte("\n"),
append([]byte(signature), []byte("\n")...)...)...),
os.ModeAppend)
if err = exportsCheck(buf); err != nil {
return
}

if nfsIsRunning {
Expand All @@ -416,16 +410,17 @@ func nfsSetup() (err error) {
return fmt.Errorf("unable to update NFS "+
"service definitions... (%v)", err)
}
log.Println("'/Users' was made available to VMs via NFS")
log.Printf("'%s' was made available to VMs via NFS\n",
engine.homedir)
}
} else {
if err = exec.Command("nfsd", "start").Run(); err != nil {
return fmt.Errorf("unable to start NFS service... (%v)", err)
}
log.Println("NFS started in order for '/Users' to be " +
"made available to the VMs")
log.Printf("NFS started in order for '%s' to be "+
"made available to the VMs\n", engine.homedir)
}
return exportsCheck
return
}

func (vm *VMInfo) storeConfig() (err error) {
Expand Down

0 comments on commit cbd7ed0

Please sign in to comment.