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

@ -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
}