Skip to content

Commit

Permalink
Move parsing functions to pkg/parsers and the specific kernel handling
Browse files Browse the repository at this point in the history
functions to pkg/parsers/kernel, and parsing filters to
pkg/parsers/filter. Adjust imports and package references.

Docker-DCO-1.1-Signed-off-by: Erik Hollensbe <[email protected]> (github: erikh)
  • Loading branch information
Erik Hollensbe committed Jul 29, 2014
1 parent 335e82f commit 4398108
Show file tree
Hide file tree
Showing 28 changed files with 400 additions and 363 deletions.
17 changes: 9 additions & 8 deletions api/client/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ import (
"github.com/docker/docker/engine"
"github.com/docker/docker/nat"
"github.com/docker/docker/opts"
"github.com/docker/docker/pkg/parsers"
"github.com/docker/docker/pkg/parsers/filters"
"github.com/docker/docker/pkg/signal"
"github.com/docker/docker/pkg/term"
"github.com/docker/docker/pkg/units"
"github.com/docker/docker/registry"
"github.com/docker/docker/runconfig"
"github.com/docker/docker/utils"
"github.com/docker/docker/utils/filters"
)

const (
Expand Down Expand Up @@ -204,7 +205,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error {

//Check if the given image name can be resolved
if *tag != "" {
repository, _ := utils.ParseRepositoryTag(*tag)
repository, _ := parsers.ParseRepositoryTag(*tag)
if _, _, err := registry.ResolveRepositoryName(repository); err != nil {
return err
}
Expand Down Expand Up @@ -1137,7 +1138,7 @@ func (cli *DockerCli) CmdPush(args ...string) error {

cli.LoadConfigFile()

remote, tag := utils.ParseRepositoryTag(name)
remote, tag := parsers.ParseRepositoryTag(name)

// Resolve the Repository name from fqn to hostname + name
hostname, _, err := registry.ResolveRepositoryName(remote)
Expand Down Expand Up @@ -1210,7 +1211,7 @@ func (cli *DockerCli) CmdPull(args ...string) error {
v.Set("tag", *tag)
}

remote, _ = utils.ParseRepositoryTag(remote)
remote, _ = parsers.ParseRepositoryTag(remote)
// Resolve the Repository name from fqn to hostname + name
hostname, _, err := registry.ResolveRepositoryName(remote)
if err != nil {
Expand Down Expand Up @@ -1393,7 +1394,7 @@ func (cli *DockerCli) CmdImages(args ...string) error {
for _, out := range outs.Data {
for _, repotag := range out.GetList("RepoTags") {

repo, tag := utils.ParseRepositoryTag(repotag)
repo, tag := parsers.ParseRepositoryTag(repotag)
outID := out.Get("Id")
if !*noTrunc {
outID = utils.TruncateID(outID)
Expand Down Expand Up @@ -1594,7 +1595,7 @@ func (cli *DockerCli) CmdCommit(args ...string) error {

var (
name = cmd.Arg(0)
repository, tag = utils.ParseRepositoryTag(cmd.Arg(1))
repository, tag = parsers.ParseRepositoryTag(cmd.Arg(1))
)

if name == "" || len(cmd.Args()) > 2 {
Expand Down Expand Up @@ -1918,7 +1919,7 @@ func (cli *DockerCli) CmdTag(args ...string) error {
}

var (
repository, tag = utils.ParseRepositoryTag(cmd.Arg(1))
repository, tag = parsers.ParseRepositoryTag(cmd.Arg(1))
v = url.Values{}
)

Expand All @@ -1941,7 +1942,7 @@ func (cli *DockerCli) CmdTag(args ...string) error {

func (cli *DockerCli) pullImage(image string) error {
v := url.Values{}
repos, tag := utils.ParseRepositoryTag(image)
repos, tag := parsers.ParseRepositoryTag(image)
// pull only the image tagged 'latest' if no tag was specified
if tag == "" {
tag = "latest"
Expand Down
3 changes: 2 additions & 1 deletion api/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/docker/docker/engine"
"github.com/docker/docker/pkg/parsers"
"github.com/docker/docker/pkg/version"
"github.com/docker/docker/utils"
)
Expand All @@ -17,7 +18,7 @@ const (
)

func ValidateHost(val string) (string, error) {
host, err := utils.ParseHost(DEFAULTHTTPHOST, DEFAULTUNIXSOCKET, val)
host, err := parsers.ParseHost(DEFAULTHTTPHOST, DEFAULTUNIXSOCKET, val)
if err != nil {
return val, err
}
Expand Down
5 changes: 3 additions & 2 deletions api/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/docker/docker/api"
"github.com/docker/docker/engine"
"github.com/docker/docker/pkg/listenbuffer"
"github.com/docker/docker/pkg/parsers"
"github.com/docker/docker/pkg/systemd"
"github.com/docker/docker/pkg/user"
"github.com/docker/docker/pkg/version"
Expand Down Expand Up @@ -484,7 +485,7 @@ func postImagesCreate(eng *engine.Engine, version version.Version, w http.Respon
}
if image != "" { //pull
if tag == "" {
image, tag = utils.ParseRepositoryTag(image)
image, tag = parsers.ParseRepositoryTag(image)
}
metaHeaders := map[string][]string{}
for k, v := range r.Header {
Expand All @@ -498,7 +499,7 @@ func postImagesCreate(eng *engine.Engine, version version.Version, w http.Respon
job.SetenvJson("authConfig", authConfig)
} else { //import
if tag == "" {
repo, tag = utils.ParseRepositoryTag(repo)
repo, tag = parsers.ParseRepositoryTag(repo)
}
job = eng.Job("import", r.Form.Get("fromSrc"), repo, tag)
job.Stdin.Add(r.Body)
Expand Down
3 changes: 2 additions & 1 deletion builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/docker/docker/daemon"
"github.com/docker/docker/engine"
"github.com/docker/docker/nat"
"github.com/docker/docker/pkg/parsers"
"github.com/docker/docker/pkg/symlink"
"github.com/docker/docker/pkg/system"
"github.com/docker/docker/registry"
Expand Down Expand Up @@ -86,7 +87,7 @@ func (b *buildFile) CmdFrom(name string) error {
image, err := b.daemon.Repositories().LookupImage(name)
if err != nil {
if b.daemon.Graph().IsNotExist(err) {
remote, tag := utils.ParseRepositoryTag(name)
remote, tag := parsers.ParseRepositoryTag(name)
pullRegistryAuth := b.authConfig
if len(b.configFile.Configs) > 0 {
// The request came with a full auth config file, we prefer to use that
Expand Down
4 changes: 2 additions & 2 deletions builtins/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"github.com/docker/docker/daemon/networkdriver/bridge"
"github.com/docker/docker/dockerversion"
"github.com/docker/docker/engine"
"github.com/docker/docker/pkg/parsers/kernel"
"github.com/docker/docker/registry"
"github.com/docker/docker/server"
"github.com/docker/docker/utils"
)

func Register(eng *engine.Engine) error {
Expand Down Expand Up @@ -68,7 +68,7 @@ func dockerVersion(job *engine.Job) engine.Status {
v.Set("GoVersion", runtime.Version())
v.Set("Os", runtime.GOOS)
v.Set("Arch", runtime.GOARCH)
if kernelVersion, err := utils.GetKernelVersion(); err == nil {
if kernelVersion, err := kernel.GetKernelVersion(); err == nil {
v.Set("KernelVersion", kernelVersion.String())
}
if _, err := v.WriteTo(job.Stdout); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/docker/docker/pkg/graphdb"
"github.com/docker/docker/pkg/namesgenerator"
"github.com/docker/docker/pkg/networkfs/resolvconf"
"github.com/docker/docker/pkg/parsers"
"github.com/docker/docker/pkg/sysinfo"
"github.com/docker/docker/pkg/truncindex"
"github.com/docker/docker/runconfig"
Expand Down Expand Up @@ -724,7 +725,7 @@ func (daemon *Daemon) RegisterLink(parent, child *Container, alias string) error
func (daemon *Daemon) RegisterLinks(container *Container, hostConfig *runconfig.HostConfig) error {
if hostConfig != nil && hostConfig.Links != nil {
for _, l := range hostConfig.Links {
parts, err := utils.PartParser("name:alias", l)
parts, err := parsers.PartParser("name:alias", l)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion daemon/graphdriver/devmapper/deviceset.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"time"

"github.com/docker/docker/daemon/graphdriver"
"github.com/docker/docker/pkg/parsers"
"github.com/docker/docker/pkg/units"
"github.com/docker/docker/utils"
"github.com/docker/libcontainer/label"
Expand Down Expand Up @@ -1166,7 +1167,7 @@ func NewDeviceSet(root string, doInit bool, options []string) (*DeviceSet, error

foundBlkDiscard := false
for _, option := range options {
key, val, err := utils.ParseKeyValueOpt(option)
key, val, err := parsers.ParseKeyValueOpt(option)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion daemon/networkdriver/bridge/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/docker/docker/engine"
"github.com/docker/docker/pkg/iptables"
"github.com/docker/docker/pkg/networkfs/resolvconf"
"github.com/docker/docker/pkg/parsers/kernel"
"github.com/docker/docker/utils"
"github.com/docker/libcontainer/netlink"
)
Expand Down Expand Up @@ -306,7 +307,7 @@ func createBridge(bridgeIP string) error {
}

func createBridgeIface(name string) error {
kv, err := utils.GetKernelVersion()
kv, err := kernel.GetKernelVersion()
// only set the bridge's mac address if the kernel version is > 3.3
// before that it was not supported
setBridgeMacAddr := err == nil && (kv.Kernel >= 3 && kv.Major >= 3)
Expand Down
5 changes: 3 additions & 2 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/docker/docker/engine"
"github.com/docker/docker/opts"
flag "github.com/docker/docker/pkg/mflag"
"github.com/docker/docker/pkg/parsers/kernel"
"github.com/docker/docker/sysinit"
"github.com/docker/docker/utils"
)
Expand Down Expand Up @@ -292,10 +293,10 @@ func checkKernelAndArch() error {
// without actually causing a kernel panic, so we need this workaround until
// the circumstances of pre-3.8 crashes are clearer.
// For details see http://github.com/docker/docker/issues/407
if k, err := utils.GetKernelVersion(); err != nil {
if k, err := kernel.GetKernelVersion(); err != nil {
log.Printf("WARNING: %s\n", err)
} else {
if utils.CompareKernelVersion(k, &utils.KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0}) < 0 {
if kernel.CompareKernelVersion(k, &kernel.KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0}) < 0 {
if os.Getenv("DOCKER_NOWARN_KERNEL_VERSION") == "" {
log.Printf("WARNING: You are running linux kernel version %s, which might be unstable running docker. Please upgrade your kernel to 3.8.0.", k.String())
}
Expand Down
3 changes: 2 additions & 1 deletion graph/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/docker/docker/engine"
"github.com/docker/docker/image"
"github.com/docker/docker/pkg/parsers"
"github.com/docker/docker/utils"
)

Expand Down Expand Up @@ -78,7 +79,7 @@ func (s *TagStore) CmdTag(job *engine.Job) engine.Status {
newName = job.Args[0]
oldName = job.Args[1]
)
newRepo, newTag := utils.ParseRepositoryTag(newName)
newRepo, newTag := parsers.ParseRepositoryTag(newName)
// FIXME: Set should either parse both old and new name, or neither.
// the current prototype is inconsistent.
if err := s.Set(newRepo, newTag, oldName, true); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion graph/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sync"

"github.com/docker/docker/image"
"github.com/docker/docker/pkg/parsers"
"github.com/docker/docker/utils"
)

Expand Down Expand Up @@ -72,7 +73,7 @@ func (store *TagStore) reload() error {
func (store *TagStore) LookupImage(name string) (*image.Image, error) {
// FIXME: standardize on returning nil when the image doesn't exist, and err for everything else
// (so we can pass all errors here)
repos, tag := utils.ParseRepositoryTag(name)
repos, tag := parsers.ParseRepositoryTag(name)
if tag == "" {
tag = DEFAULTTAG
}
Expand Down
4 changes: 2 additions & 2 deletions nat/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strconv"
"strings"

"github.com/docker/docker/utils"
"github.com/docker/docker/pkg/parsers"
)

const (
Expand Down Expand Up @@ -103,7 +103,7 @@ func ParsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding,
rawPort = fmt.Sprintf(":%s", rawPort)
}

parts, err := utils.PartParser(PortSpecTemplate, rawPort)
parts, err := parsers.PartParser(PortSpecTemplate, rawPort)
if err != nil {
return nil, nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions opts/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"regexp"
"strings"

"github.com/docker/docker/utils"
"github.com/docker/docker/pkg/parsers"
)

// ListOpts type
Expand Down Expand Up @@ -94,7 +94,7 @@ func ValidateAttach(val string) (string, error) {
}

func ValidateLink(val string) (string, error) {
if _, err := utils.PartParser("name:alias", val); err != nil {
if _, err := parsers.PartParser("name:alias", val); err != nil {
return val, err
}
return val, nil
Expand Down
1 change: 1 addition & 0 deletions pkg/parsers/MAINTAINERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Erik Hollensbe <[email protected]> (@erikh)
File renamed without changes.
File renamed without changes.
93 changes: 93 additions & 0 deletions pkg/parsers/kernel/kernel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package kernel

import (
"bytes"
"errors"
"fmt"
)

type KernelVersionInfo struct {
Kernel int
Major int
Minor int
Flavor string
}

func (k *KernelVersionInfo) String() string {
return fmt.Sprintf("%d.%d.%d%s", k.Kernel, k.Major, k.Minor, k.Flavor)
}

// Compare two KernelVersionInfo struct.
// Returns -1 if a < b, 0 if a == b, 1 it a > b
func CompareKernelVersion(a, b *KernelVersionInfo) int {
if a.Kernel < b.Kernel {
return -1
} else if a.Kernel > b.Kernel {
return 1
}

if a.Major < b.Major {
return -1
} else if a.Major > b.Major {
return 1
}

if a.Minor < b.Minor {
return -1
} else if a.Minor > b.Minor {
return 1
}

return 0
}

func GetKernelVersion() (*KernelVersionInfo, error) {
var (
err error
)

uts, err := uname()
if err != nil {
return nil, err
}

release := make([]byte, len(uts.Release))

i := 0
for _, c := range uts.Release {
release[i] = byte(c)
i++
}

// Remove the \x00 from the release for Atoi to parse correctly
release = release[:bytes.IndexByte(release, 0)]

return ParseRelease(string(release))
}

func ParseRelease(release string) (*KernelVersionInfo, error) {
var (
kernel, major, minor, parsed int
flavor, partial string
)

// Ignore error from Sscanf to allow an empty flavor. Instead, just
// make sure we got all the version numbers.
parsed, _ = fmt.Sscanf(release, "%d.%d%s", &kernel, &major, &partial)
if parsed < 2 {
return nil, errors.New("Can't parse kernel version " + release)
}

// sometimes we have 3.12.25-gentoo, but sometimes we just have 3.12-1-amd64
parsed, _ = fmt.Sscanf(partial, ".%d%s", &minor, &flavor)
if parsed < 1 {
flavor = partial
}

return &KernelVersionInfo{
Kernel: kernel,
Major: major,
Minor: minor,
Flavor: flavor,
}, nil
}
Loading

0 comments on commit 4398108

Please sign in to comment.