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

Modify generate API #169

Merged
merged 3 commits into from
Aug 1, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
121 changes: 112 additions & 9 deletions cmd/ocitools/generate.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package main

import (
"fmt"
"os"
"runtime"
"strconv"
"strings"

"github.com/opencontainers/ocitools/generate"
rspec "github.com/opencontainers/runtime-spec/specs-go"
Expand Down Expand Up @@ -117,7 +120,11 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
if context.IsSet("label") {
annotations := context.StringSlice("label")
for _, s := range annotations {
if err := g.AddAnnotation(s); err != nil {
pair := strings.Split(s, "=")
if len(pair) != 2 {
return fmt.Errorf("incorrectly specified annotation: %s", s)
}
if err := g.AddAnnotation(pair[0], pair[1]); err != nil {
return err
}
}
Expand Down Expand Up @@ -184,7 +191,11 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
if context.IsSet("sysctl") {
sysctls := context.StringSlice("sysctl")
for _, s := range sysctls {
g.AddLinuxSysctl(s)
pair := strings.Split(s, "=")
if len(pair) != 2 {
return fmt.Errorf("incorrectly specified sysctl: %s", s)
}
g.AddLinuxSysctl(pair[0], pair[1])
}
}

Expand Down Expand Up @@ -239,7 +250,11 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
if context.IsSet("tmpfs") {
tmpfsSlice := context.StringSlice("tmpfs")
for _, s := range tmpfsSlice {
if err := g.AddTmpfsMount(s); err != nil {
dest, options, err := parseTmpfsMount(s)
if err != nil {
return err
}
if err := g.AddTmpfsMount(dest, options); err != nil {
return err
}
}
Expand All @@ -253,7 +268,12 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
if context.IsSet("bind") {
binds := context.StringSlice("bind")
for _, bind := range binds {
if err := g.AddBindMount(bind); err != nil {
source, dest, options, err := parseBindMount(bind)
if err != nil {
return err
}

if err := g.AddBindMount(source, dest, options); err != nil {
return err
}
}
Expand All @@ -262,7 +282,8 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
if context.IsSet("prestart") {
preStartHooks := context.StringSlice("prestart")
for _, hook := range preStartHooks {
if err := g.AddPreStartHook(hook); err != nil {
path, args := parseHook(hook)
if err := g.AddPreStartHook(path, args); err != nil {
return err
}
}
Expand All @@ -271,7 +292,8 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
if context.IsSet("poststop") {
postStopHooks := context.StringSlice("poststop")
for _, hook := range postStopHooks {
if err := g.AddPostStopHook(hook); err != nil {
path, args := parseHook(hook)
if err := g.AddPostStopHook(path, args); err != nil {
return err
}
}
Expand All @@ -280,7 +302,8 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
if context.IsSet("poststart") {
postStartHooks := context.StringSlice("poststart")
for _, hook := range postStartHooks {
if err := g.AddPostStartHook(hook); err != nil {
path, args := parseHook(hook)
if err := g.AddPostStartHook(path, args); err != nil {
return err
}
}
Expand All @@ -294,13 +317,23 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
}

for _, uidMap := range uidMaps {
if err := g.AddLinuxUIDMapping(uidMap); err != nil {
hid, cid, size, err := parseIDMapping(uidMap)
if err != nil {
return err
}

if err := g.AddLinuxUIDMapping(hid, cid, size); err != nil {
return err
}
}

for _, gidMap := range gidMaps {
if err := g.AddLinuxGIDMapping(gidMap); err != nil {
hid, cid, size, err := parseIDMapping(gidMap)
if err != nil {
return err
}

if err := g.AddLinuxGIDMapping(hid, cid, size); err != nil {
return err
}
}
Expand Down Expand Up @@ -386,3 +419,73 @@ func setupLinuxNamespaces(g *generate.Generator, needsNewUser bool, nsMaps map[s
g.AddOrReplaceLinuxNamespace(nsName, nsPath)
}
}

func parseIDMapping(idms string) (uint32, uint32, uint32, error) {
idm := strings.Split(idms, ":")
if len(idm) != 3 {
return 0, 0, 0, fmt.Errorf("idmappings error: %s", idms)
}

hid, err := strconv.Atoi(idm[0])
if err != nil {
return 0, 0, 0, err
}

cid, err := strconv.Atoi(idm[1])
if err != nil {
return 0, 0, 0, err
}

size, err := strconv.Atoi(idm[2])
if err != nil {
return 0, 0, 0, err
}

return uint32(hid), uint32(cid), uint32(size), nil
}

func parseHook(s string) (string, []string) {
parts := strings.Split(s, ":")
args := []string{}
path := parts[0]
if len(parts) > 1 {
args = parts[1:]
}
return path, args
}

func parseTmpfsMount(s string) (string, []string, error) {
var dest string
var options []string
var err error

parts := strings.Split(s, ":")
if len(parts) == 2 {
dest = parts[0]
options = strings.Split(parts[1], ",")
} else if len(parts) == 1 {
dest = parts[0]
options = []string{"rw", "noexec", "nosuid", "nodev", "size=65536k"}
} else {
err = fmt.Errorf("invalid value for --tmpfs")
}

return dest, options, err
}

func parseBindMount(s string) (string, string, string, error) {
var source, dest string
options := "ro"

bparts := strings.SplitN(s, ":", 3)
switch len(bparts) {
case 2:
source, dest = bparts[0], bparts[1]
case 3:
source, dest, options = bparts[0], bparts[1], bparts[2]
default:
return source, dest, options, fmt.Errorf("--bind should have format src:dest:[options]")
}

return source, dest, options, nil
}
Loading