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

View file

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

View file

@ -16,7 +16,7 @@ type Opts struct {
Privileged bool
}
var defaultOpts = &Opts{
var DefaultOpts = &Opts{
Volumes: false,
Network: false,
Privileged: false,
@ -26,42 +26,13 @@ var defaultOpts = &Opts{
// a list of build configurations for each axis
// using the default parsing options.
func Parse(raw string) ([]*common.Config, error) {
return ParseOpts(raw, defaultOpts)
return ParseOpts(raw, DefaultOpts)
}
// ParseOpts parses a build matrix and returns
// a list of build configurations for each axis
// using the provided parsing options.
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)
if err != nil {
return nil, err
@ -71,7 +42,7 @@ func parse(raw string) ([]*common.Config, error) {
// when no matrix values exist we should return
// a single config value with an empty label.
if len(axis) == 0 {
conf, err := parseYaml(raw)
conf, err := ParseSingle(raw, opts)
if err != nil {
return nil, err
}
@ -81,19 +52,43 @@ func parse(raw string) ([]*common.Config, error) {
for _, ax := range axis {
// inject the matrix values into the raw script
injected := inject.Inject(raw, ax)
conf, err := parseYaml(injected)
conf, err := ParseSingle(injected, opts)
if err != nil {
return nil, err
}
conf.Axis = common.Axis(ax)
confs = append(confs, conf)
}
return confs, nil
}
// 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{}
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
}

View file

@ -12,16 +12,13 @@ type Remote interface {
Login(token, secret string) (*common.User, error)
// 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)
// Repo fetches the named repository from the remote system.
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)
// Script fetches the build script (.drone.yml) from the remote

View file

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