Refactor to add oci and util packages

Signed-off-by: Mrunal Patel <mrunalp@gmail.com>

Change the sandbox directory path

Signed-off-by: Mrunal Patel <mrunalp@gmail.com>
This commit is contained in:
Mrunal Patel 2016-07-29 15:35:10 -07:00
parent 839463d837
commit ac1340488d
7 changed files with 101 additions and 76 deletions

View file

@ -23,9 +23,14 @@ func main() {
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "sandboxdir",
Value: "/var/lib/ocid/sandbox",
Value: "/var/lib/ocid/sandboxes",
Usage: "ocid pod sandbox dir",
},
cli.StringFlag{
Name: "runtime",
Value: "/usr/bin/runc",
Usage: "OCI runtime path",
},
}
app.Action = func(c *cli.Context) error {
@ -43,7 +48,7 @@ func main() {
s := grpc.NewServer()
sandboxDir := c.String("sandboxdir")
service, err := server.New("", sandboxDir)
service, err := server.New(c.String("runtime"), sandboxDir)
if err != nil {
log.Fatal(err)
}

60
oci/oci.go Normal file
View file

@ -0,0 +1,60 @@
package oci
import (
"path/filepath"
"strings"
"github.com/mrunalp/ocid/utils"
)
// New creates a new Runtime with options provided
func New(runtimePath string, sandboxDir string) (*Runtime, error) {
r := &Runtime{
name: filepath.Base(runtimePath),
path: runtimePath,
sandboxDir: sandboxDir,
}
return r, nil
}
type Runtime struct {
name string
path string
sandboxDir string
containerDir string
}
// Name returns the name of the OCI Runtime
func (r *Runtime) Name() string {
return r.name
}
// Path returns the full path the OCI Runtime executable
func (r *Runtime) Path() string {
return r.path
}
// SandboxDir returns the path to the base directory for storing sandbox configurations
func (r *Runtime) SandboxDir() string {
return r.sandboxDir
}
// Version returns the version of the OCI Runtime
func (r *Runtime) Version() (string, error) {
runtimeVersion, err := getOCIVersion(r.path, "-v")
if err != nil {
return "", err
}
return runtimeVersion, nil
}
func getOCIVersion(name string, args ...string) (string, error) {
out, err := utils.ExecCmd(name, args...)
if err != nil {
return "", err
}
firstLine := out[:strings.Index(out, "\n")]
v := firstLine[strings.LastIndex(firstLine, " ")+1:]
return v, nil
}

View file

@ -1,31 +0,0 @@
package server
import (
"bytes"
"os/exec"
"strings"
)
func execCmd(name string, args ...string) (string, error) {
cmd := exec.Command(name, args...)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return "", err
}
return out.String(), nil
}
func execRuncVersion(name string, args ...string) (string, error) {
out, err := execCmd(name, args...)
if err != nil {
return "", err
}
firstLine := out[:strings.Index(out, "\n")]
v := firstLine[strings.LastIndex(firstLine, " ")+1:]
return v, nil
}

View file

@ -39,7 +39,7 @@ func (s *Server) Version(ctx context.Context, req *pb.VersionRequest) (*pb.Versi
func (s *Server) CreatePodSandbox(ctx context.Context, req *pb.CreatePodSandboxRequest) (*pb.CreatePodSandboxResponse, error) {
var err error
if err := os.MkdirAll(s.sandboxDir, 0755); err != nil {
if err := os.MkdirAll(s.runtime.SandboxDir(), 0755); err != nil {
return nil, err
}
@ -49,7 +49,7 @@ func (s *Server) CreatePodSandbox(ctx context.Context, req *pb.CreatePodSandboxR
return nil, fmt.Errorf("PodSandboxConfig.Name should not be empty")
}
podSandboxDir := filepath.Join(s.sandboxDir, name)
podSandboxDir := filepath.Join(s.runtime.SandboxDir(), name)
if _, err := os.Stat(podSandboxDir); err == nil {
return nil, fmt.Errorf("pod sandbox (%s) already exists", podSandboxDir)
}

View file

@ -1,59 +1,30 @@
package server
import (
"github.com/mrunalp/ocid/oci"
)
const (
runtimeAPIVersion = "v1alpha1"
)
// Server implements the RuntimeService and ImageService
type Server struct {
runtime ociRuntime
sandboxDir string
sandboxes []*sandbox
runtime *oci.Runtime
sandboxes []*sandbox
}
// New creates a new Server with options provided
func New(runtimePath, sandboxDir string) (*Server, error) {
// TODO(runcom): runtimePath arg is unused but it might be useful
// if we're willing to open the doors to other runtimes in the future.
r := &runcRuntime{}
r, err := oci.New(runtimePath, sandboxDir)
if err != nil {
return nil, err
}
return &Server{
runtime: r,
sandboxDir: sandboxDir,
runtime: r,
}, nil
}
// TODO(runcom): export? this is being done just because runc shows a 3 line version :/
// but it might actually be a useful abstraction (?)
type ociRuntime interface {
Name() string
Path() (string, error)
Version() (string, error)
}
type runcRuntime struct {
}
func (r *runcRuntime) Name() string {
return "runc"
}
func (r *runcRuntime) Path() (string, error) {
// TODO(runcom): we're saying runc is always in $PATH here for now
return "runc", nil
}
func (r *runcRuntime) Version() (string, error) {
path, err := r.Path()
if err != nil {
return "", err
}
runtimeVersion, err := execRuncVersion(path, "-v")
if err != nil {
return "", err
}
return runtimeVersion, nil
}
type sandbox struct {
name string
logDir string

View file

@ -9,6 +9,7 @@ import (
"runtime"
"strings"
"github.com/mrunalp/ocid/utils"
"github.com/opencontainers/ocitools/generate"
)
@ -23,7 +24,7 @@ func getGPRCVersion() (string, error) {
grepCmd := fmt.Sprintf(`grep -r "\"google.golang.org/grpc\"" %s -A 1 | grep "\"Rev\"" | cut -d: -f2 | tr -d ' "\n'`, p)
out, err := execCmd("bash", "-c", grepCmd)
out, err := utils.ExecCmd("bash", "-c", grepCmd)
if err != nil {
return "", err
}

19
utils/utils.go Normal file
View file

@ -0,0 +1,19 @@
package utils
import (
"bytes"
"os/exec"
)
func ExecCmd(name string, args ...string) (string, error) {
cmd := exec.Command(name, args...)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return "", err
}
return out.String(), nil
}