added cluster in place of pool, some other minor changes

This commit is contained in:
Brad Rydzewski 2015-04-21 15:48:06 -07:00
parent 0cd5dc1a5b
commit 79b83025a8
5 changed files with 64 additions and 67 deletions

View file

@ -1,4 +1,4 @@
package pool package cluster
import ( import (
"sync" "sync"
@ -9,14 +9,14 @@ import (
// TODO (bradrydzewski) ability to cancel work. // TODO (bradrydzewski) ability to cancel work.
// TODO (bradrydzewski) ability to remove a worker. // TODO (bradrydzewski) ability to remove a worker.
type Pool struct { type Cluster struct {
sync.Mutex sync.Mutex
clients map[dockerclient.Client]bool clients map[dockerclient.Client]bool
clientc chan dockerclient.Client clientc chan dockerclient.Client
} }
func New() *Pool { func New() *Cluster {
return &Pool{ return &Cluster{
clients: make(map[dockerclient.Client]bool), clients: make(map[dockerclient.Client]bool),
clientc: make(chan dockerclient.Client, 999), clientc: make(chan dockerclient.Client, 999),
} }
@ -24,26 +24,26 @@ func New() *Pool {
// Allocate allocates a client to the pool to // Allocate allocates a client to the pool to
// be available to accept work. // be available to accept work.
func (p *Pool) Allocate(c dockerclient.Client) bool { func (c *Cluster) Allocate(cli dockerclient.Client) bool {
if p.IsAllocated(c) { if c.IsAllocated(cli) {
return false return false
} }
p.Lock() c.Lock()
p.clients[c] = true c.clients[cli] = true
p.Unlock() c.Unlock()
p.clientc <- c c.clientc <- cli
return true return true
} }
// IsAllocated is a helper function that returns // IsAllocated is a helper function that returns
// true if the client is currently allocated to // true if the client is currently allocated to
// the Pool. // the Pool.
func (p *Pool) IsAllocated(c dockerclient.Client) bool { func (c *Cluster) IsAllocated(cli dockerclient.Client) bool {
p.Lock() c.Lock()
defer p.Unlock() defer c.Unlock()
_, ok := p.clients[c] _, ok := c.clients[cli]
return ok return ok
} }
@ -51,21 +51,21 @@ func (p *Pool) IsAllocated(c dockerclient.Client) bool {
// available clients. If the client is currently // available clients. If the client is currently
// reserved and performing work it will finish, // reserved and performing work it will finish,
// but no longer be given new work. // but no longer be given new work.
func (p *Pool) Deallocate(c dockerclient.Client) { func (c *Cluster) Deallocate(cli dockerclient.Client) {
p.Lock() c.Lock()
defer p.Unlock() defer c.Unlock()
delete(p.clients, c) delete(c.clients, cli)
} }
// List returns a list of all Workers currently // List returns a list of all Workers currently
// allocated to the Pool. // allocated to the Pool.
func (p *Pool) List() []dockerclient.Client { func (c *Cluster) List() []dockerclient.Client {
p.Lock() c.Lock()
defer p.Unlock() defer c.Unlock()
var clients []dockerclient.Client var clients []dockerclient.Client
for c := range p.clients { for cli := range c.clients {
clients = append(clients, c) clients = append(clients, cli)
} }
return clients return clients
} }
@ -73,17 +73,17 @@ func (p *Pool) List() []dockerclient.Client {
// Reserve reserves the next available worker to // Reserve reserves the next available worker to
// start doing work. Once work is complete, the // start doing work. Once work is complete, the
// worker should be released back to the pool. // worker should be released back to the pool.
func (p *Pool) Reserve() <-chan dockerclient.Client { func (p *Cluster) Reserve() <-chan dockerclient.Client {
return p.clientc return p.clientc
} }
// Release releases the worker back to the pool // Release releases the worker back to the pool
// of available workers. // of available workers.
func (p *Pool) Release(c dockerclient.Client) bool { func (c *Cluster) Release(cli dockerclient.Client) bool {
if !p.IsAllocated(c) { if !c.IsAllocated(cli) {
return false return false
} }
p.clientc <- c c.clientc <- cli
return true return true
} }

View file

@ -6,7 +6,7 @@ const (
) )
type Token struct { type Token struct {
Kind string `json:"-"` Kind string `json:"kind"`
Login string `json:"-"` Login string `json:"-"`
Label string `json:"label"` Label string `json:"label"`
Repos []string `json:"repos,omitempty"` Repos []string `json:"repos,omitempty"`

View file

@ -16,7 +16,7 @@ type Opts struct {
Privileged bool Privileged bool
} }
var defaultOpts = &Opts{ var DefaultOpts = &Opts{
Volumes: false, Volumes: false,
Network: false, Network: false,
Privileged: false, Privileged: false,
@ -26,42 +26,13 @@ var defaultOpts = &Opts{
// a list of build configurations for each axis // a list of build configurations for each axis
// using the default parsing options. // using the default parsing options.
func Parse(raw string) ([]*common.Config, error) { func Parse(raw string) ([]*common.Config, error) {
return ParseOpts(raw, defaultOpts) return ParseOpts(raw, DefaultOpts)
} }
// ParseOpts parses a build matrix and returns // ParseOpts parses a build matrix and returns
// a list of build configurations for each axis // a list of build configurations for each axis
// using the provided parsing options. // using the provided parsing options.
func ParseOpts(raw string, opts *Opts) ([]*common.Config, error) { func ParseOpts(raw string, opts *Opts) ([]*common.Config, error) {
confs, err := parse(raw)
if err != nil {
return nil, err
}
for _, conf := range confs {
err := Lint(conf)
if err != nil {
return nil, err
}
transformSetup(conf)
transformClone(conf)
transformBuild(conf)
transformImages(conf)
transformDockerPlugin(conf)
if !opts.Network {
rmNetwork(conf)
}
if !opts.Volumes {
rmVolumes(conf)
}
if !opts.Privileged {
rmPrivileged(conf)
}
}
return confs, nil
}
// helper function to parse a matrix configuraiton file.
func parse(raw string) ([]*common.Config, error) {
axis, err := matrix.Parse(raw) axis, err := matrix.Parse(raw)
if err != nil { if err != nil {
return nil, err return nil, err
@ -71,7 +42,7 @@ func parse(raw string) ([]*common.Config, error) {
// when no matrix values exist we should return // when no matrix values exist we should return
// a single config value with an empty label. // a single config value with an empty label.
if len(axis) == 0 { if len(axis) == 0 {
conf, err := parseYaml(raw) conf, err := ParseSingle(raw, opts)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -81,19 +52,43 @@ func parse(raw string) ([]*common.Config, error) {
for _, ax := range axis { for _, ax := range axis {
// inject the matrix values into the raw script // inject the matrix values into the raw script
injected := inject.Inject(raw, ax) injected := inject.Inject(raw, ax)
conf, err := parseYaml(injected) conf, err := ParseSingle(injected, opts)
if err != nil { if err != nil {
return nil, err return nil, err
} }
conf.Axis = common.Axis(ax) conf.Axis = common.Axis(ax)
confs = append(confs, conf) confs = append(confs, conf)
} }
return confs, nil return confs, nil
} }
// helper funtion to parse a yaml configuration file. // helper funtion to parse a yaml configuration file.
func parseYaml(raw string) (*common.Config, error) { func ParseSingle(raw string, opts *Opts) (*common.Config, error) {
conf := &common.Config{} conf := &common.Config{}
err := yaml.Unmarshal([]byte(raw), conf) err := yaml.Unmarshal([]byte(raw), conf)
if err != nil {
return nil, err
}
// lint the yaml file
err = Lint(conf)
if err != nil {
return nil, err
}
// apply rules / transofms
transformSetup(conf)
transformClone(conf)
transformBuild(conf)
transformImages(conf)
transformDockerPlugin(conf)
if !opts.Network {
rmNetwork(conf)
}
if !opts.Volumes {
rmVolumes(conf)
}
if !opts.Privileged {
rmPrivileged(conf)
}
return conf, err return conf, err
} }

View file

@ -12,16 +12,13 @@ type Remote interface {
Login(token, secret string) (*common.User, error) Login(token, secret string) (*common.User, error)
// Orgs fetches the organizations for the given user. // Orgs fetches the organizations for the given user.
//
// TODO(bradrydzewski) consider consolidating this to return
// the list of organizations along with
// the user Login info.
Orgs(u *common.User) ([]string, error) Orgs(u *common.User) ([]string, error)
// Repo fetches the named repository from the remote system. // Repo fetches the named repository from the remote system.
Repo(u *common.User, owner, repo string) (*common.Repo, error) Repo(u *common.User, owner, repo string) (*common.Repo, error)
// Perm fetches the named repository from the remote system. // Perm fetches the named repository permissions from
// the remote system for the specified user.
Perm(u *common.User, owner, repo string) (*common.Perm, error) Perm(u *common.User, owner, repo string) (*common.Perm, error)
// Script fetches the build script (.drone.yml) from the remote // Script fetches the build script (.drone.yml) from the remote

View file

@ -138,6 +138,11 @@ func RunBuild(c *gin.Context) {
return return
} }
// params, _ := store.RepoParams(repo.FullName)
// if params != nil && len(params) != 0 {
// raw = []byte(inject.InjectSafe(string(raw), params))
// }
// TODO push build to queue // TODO push build to queue
c.JSON(202, build) c.JSON(202, build)