From 66a2868aabbce6eb2d33b17b4bf285ddbfa6b82f Mon Sep 17 00:00:00 2001 From: Renan DelValle Date: Tue, 1 Nov 2016 21:24:15 -0400 Subject: [PATCH] Adding support for configuring tasks with Docker Containers --- container.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++ examples/client.go | 25 +++++++++++++++++++++-- job.go | 13 ++++++++++-- 3 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 container.go diff --git a/container.go b/container.go new file mode 100644 index 0000000..77c7c39 --- /dev/null +++ b/container.go @@ -0,0 +1,51 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package realis + +import ( + "github.com/rdelval/gorealis/gen-go/apache/aurora" +) + +type Container interface { + Build() *aurora.Container +} + +// TODO(rdelvalle): Implement Mesos container builder +type MesosContainer struct { + container *aurora.MesosContainer +} + +type DockerContainer struct { + container *aurora.DockerContainer +} + +func NewDockerContainer() DockerContainer { + return DockerContainer{container: aurora.NewDockerContainer()} +} + +func (c DockerContainer) Build() *aurora.Container { + return &aurora.Container{Docker: c.container} +} + +func (c DockerContainer) Image(image string) DockerContainer { + c.container.Image = image + return c +} + +func (c DockerContainer) AddParameter(name, value string) DockerContainer { + c.container.Parameters = append(c.container.Parameters, &aurora.DockerParameter{name, value}) + return c +} + diff --git a/examples/client.go b/examples/client.go index 252f3b0..e5651c3 100644 --- a/examples/client.go +++ b/examples/client.go @@ -29,7 +29,7 @@ func main() { executor := flag.String("executor", "thermos", "Executor to use") url := flag.String("url", "", "URL at which the Aurora Scheduler exists as [url]:[port]") clustersConfig := flag.String("clusters", "", "Location of the clusters.json file used by aurora.") - clusterName := flag.String("cluster", "devcluster", "Name of cluster to run job on") + clusterName := flag.String("cluster", "devcluster", "Name of cluster to run job on (only necessary if clusters is set)") updateId := flag.String("updateId", "", "Update ID to operate on") username := flag.String("username", "aurora", "Username to use for authorization") password := flag.String("password", "secret", "Password to use for authorization") @@ -82,7 +82,7 @@ func main() { job = realis.NewJob(). Environment("prod"). Role("vagrant"). - Name("hello_world_from_gorealis"). + Name("hello_world_from_gorealis_docker"). ExecutorName(aurora.AURORA_EXECUTOR_NAME). ExecutorData(string(payload)). CPU(1). @@ -133,6 +133,27 @@ func main() { } } break + case "createDocker": + fmt.Println("Creating a docker based job") + container := realis.NewDockerContainer().Image("python:2.7").AddParameter("network", "host") + job.Container(container) + resp, err := r.CreateJob(job) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + fmt.Println(resp.String()) + + if resp.ResponseCode == aurora.ResponseCode_OK { + if ok, err := monitor.Instances(job.JobKey(), job.GetInstanceCount(), 10, 300); !ok || err != nil { + _, err := r.KillJob(job.JobKey()) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + } + } + break case "scheduleCron": fmt.Println("Scheduling a Cron job") // Cron config diff --git a/job.go b/job.go index dbc43f6..b9d220d 100644 --- a/job.go +++ b/job.go @@ -25,8 +25,8 @@ type Job interface { Role(role string) Job Name(name string) Job CPU(cpus float64) Job - CronSchedule(cron string) Job - CronCollisionPolicy(policy aurora.CronCollisionPolicy) Job + CronSchedule(cron string) Job + CronCollisionPolicy(policy aurora.CronCollisionPolicy) Job Disk(disk int64) Job RAM(ram int64) Job ExecutorName(name string) Job @@ -44,6 +44,7 @@ type Job interface { InstanceCount(instCount int32) Job GetInstanceCount() int32 MaxFailure(maxFail int32) Job + Container(container Container) Job } // Structure to collect all information pertaining to an Aurora job. @@ -262,3 +263,11 @@ func (j AuroraJob) AddLimitConstraint(name string, limit int32) Job { return j } + +// Set a container to run for the job configuration to run. +// TODO (rdelvalle): Add no thermos mode where container is launched as a task and not an executor. +func (j AuroraJob) Container(container Container) Job { + j.jobConfig.TaskConfig.Container = container.Build() + + return j +}