From 79b83025a88016629c3643dca4a5b33f48ef3f6c Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Tue, 21 Apr 2015 15:48:06 -0700 Subject: [PATCH] added cluster in place of pool, some other minor changes --- builder/pool/pool.go => cluster/cluster.go | 54 +++++++++---------- common/token.go | 2 +- parser/parse.go | 63 ++++++++++------------ remote/remote.go | 7 +-- server/builds.go | 5 ++ 5 files changed, 64 insertions(+), 67 deletions(-) rename builder/pool/pool.go => cluster/cluster.go (61%) diff --git a/builder/pool/pool.go b/cluster/cluster.go similarity index 61% rename from builder/pool/pool.go rename to cluster/cluster.go index 31061029b..ff2d740e6 100644 --- a/builder/pool/pool.go +++ b/cluster/cluster.go @@ -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 } diff --git a/common/token.go b/common/token.go index e183806f8..85b0967be 100644 --- a/common/token.go +++ b/common/token.go @@ -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"` diff --git a/parser/parse.go b/parser/parse.go index f10664746..7543efc26 100644 --- a/parser/parse.go +++ b/parser/parse.go @@ -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 } diff --git a/remote/remote.go b/remote/remote.go index 64e5609f5..4f110e677 100644 --- a/remote/remote.go +++ b/remote/remote.go @@ -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 diff --git a/server/builds.go b/server/builds.go index 513cf70cf..8535737a1 100644 --- a/server/builds.go +++ b/server/builds.go @@ -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)