commands: add implementations for create, use, rm, stop

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi
2019-04-12 16:39:06 -07:00
parent 0e72bf0049
commit bd3d5cd19e
20 changed files with 1695 additions and 101 deletions

View File

@ -8,27 +8,13 @@ import (
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/gofrs/flock"
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"
)
type NodeGroup struct {
Name string
Driver string
Nodes []Node
Endpoint string
}
type Node struct {
Name string
Endpoint string
Platforms []string
}
func NewStore(root string) (*Store, error) {
func New(root string) (*Store, error) {
root = filepath.Join(root, "buildx")
if err := os.MkdirAll(filepath.Join(root, "instances"), 0700); err != nil {
return nil, err
@ -86,7 +72,7 @@ func (t *Txn) List() ([]*NodeGroup, error) {
}
func (t *Txn) NodeGroupByName(name string) (*NodeGroup, error) {
name, err := validateName(name)
name, err := ValidateName(name)
if err != nil {
return nil, err
}
@ -102,7 +88,7 @@ func (t *Txn) NodeGroupByName(name string) (*NodeGroup, error) {
}
func (t *Txn) Save(ng *NodeGroup) error {
name, err := validateName(ng.Name)
name, err := ValidateName(ng.Name)
if err != nil {
return err
}
@ -114,7 +100,7 @@ func (t *Txn) Save(ng *NodeGroup) error {
}
func (t *Txn) Remove(name string) error {
name, err := validateName(name)
name, err := ValidateName(name)
if err != nil {
return err
}
@ -215,14 +201,7 @@ type current struct {
Global bool
}
var namePattern = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9\.\-_\+]*$`)
func validateName(s string) (string, error) {
if !namePattern.MatchString(s) {
return "", errors.Errorf("invalid name %s, name needs to start with a letter and may not contain symbols, except ._-", s)
}
return strings.ToLower(s), nil
}
var namePattern = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9\.\-_]*$`)
func atomicWriteFile(filename string, data []byte, perm os.FileMode) error {
f, err := ioutil.TempFile(filepath.Dir(filename), ".tmp-"+filepath.Base(filename))