client: init the alloc dir

This commit is contained in:
Ryan Uber
2015-09-12 11:47:44 -07:00
parent 9dc59deadf
commit 5b7073b294
2 changed files with 41 additions and 1 deletions

View File

@@ -98,6 +98,11 @@ func NewClient(cfg *config.Config) (*Client, error) {
shutdownCh: make(chan struct{}),
}
// Initialize the client
if err := c.init(); err != nil {
return nil, fmt.Errorf("failed intializing client: %v", err)
}
// Restore the state
if err := c.restoreState(); err != nil {
return nil, fmt.Errorf("failed to restore state: %v", err)
@@ -123,6 +128,16 @@ func NewClient(cfg *config.Config) (*Client, error) {
return c, nil
}
// init is used to initialize the client and perform any setup
// needed before we begin starting its various components.
func (c *Client) init() error {
// Ensure the alloc dir exists
if err := os.MkdirAll(c.config.AllocDir, 0700); err != nil {
return fmt.Errorf("failed creating alloc dir: %s", err)
}
return nil
}
// Leave is used to prepare the client to leave the cluster
func (c *Client) Leave() error {
// TODO
@@ -243,7 +258,7 @@ func (c *Client) restoreState() error {
// Scan the directory
list, err := ioutil.ReadDir(filepath.Join(c.config.StateDir, "alloc"))
if err != nil && !os.IsNotExist(err) {
if err != nil {
return fmt.Errorf("failed to list alloc state: %v", err)
}

View File

@@ -2,7 +2,10 @@ package client
import (
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
"sync/atomic"
"testing"
"time"
@@ -365,3 +368,25 @@ func TestClient_SaveRestoreState(t *testing.T) {
t.Fatalf("bad: %#v", ar.Alloc())
}
}
func TestClient_Init(t *testing.T) {
dir, err := ioutil.TempDir("", "nomad")
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.RemoveAll(dir)
allocDir := filepath.Join(dir, "alloc")
client := &Client{
config: &config.Config{
AllocDir: allocDir,
},
}
if err := client.init(); err != nil {
t.Fatalf("err: %s", err)
}
if _, err := os.Stat(allocDir); err != nil {
t.Fatalf("err: %s", err)
}
}