gorealis-v2/job.go

253 lines
7.4 KiB
Go
Raw Normal View History

2016-08-02 11:42:00 -07:00
/**
* 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.
*/
2016-08-02 11:42:00 -07:00
package realis
import (
"github.com/rdelval/gorealis/gen-go/apache/aurora"
2016-08-02 11:42:00 -07:00
"strconv"
)
2016-08-24 11:59:01 -07:00
type Job interface {
// Set Job Key environment.
Environment(env string) Job
Role(role string) Job
Name(name string) Job
CPU(cpus float64) Job
Disk(disk int64) Job
RAM(ram int64) Job
ExecutorName(name string) Job
ExecutorData(data string) Job
AddPorts(num int) Job
AddLabel(key string, value string) Job
AddNamedPorts(names ...string) Job
AddLimitConstraint(name string, limit int32) Job
AddValueConstraint(name string, negated bool, values ...string) Job
AddURIs(extract bool, cache bool, values ...string) Job
JobKey() *aurora.JobKey
JobConfig() *aurora.JobConfiguration
TaskConfig() *aurora.TaskConfig
IsService(isService bool) Job
InstanceCount(instCount int32) Job
GetInstanceCount() int32
2016-08-24 11:59:01 -07:00
MaxFailure(maxFail int32) Job
}
// Structure to collect all information pertaining to an Aurora job.
2016-08-24 11:59:01 -07:00
type AuroraJob struct {
2016-08-02 11:42:00 -07:00
jobConfig *aurora.JobConfiguration
2016-08-24 11:59:01 -07:00
resources map[string]*aurora.Resource
2016-08-02 11:42:00 -07:00
portCount int
}
// Create a Job object with everything initialized.
2016-08-24 11:59:01 -07:00
func NewJob() Job {
2016-08-02 11:42:00 -07:00
jobConfig := aurora.NewJobConfiguration()
taskConfig := aurora.NewTaskConfig()
jobKey := aurora.NewJobKey()
//Job Config
jobConfig.Key = jobKey
jobConfig.TaskConfig = taskConfig
//Task Config
taskConfig.Job = jobKey
taskConfig.Container = aurora.NewContainer()
taskConfig.Container.Mesos = aurora.NewMesosContainer()
taskConfig.ExecutorConfig = aurora.NewExecutorConfig()
taskConfig.MesosFetcherUris = make(map[*aurora.MesosFetcherURI]bool)
taskConfig.Metadata = make(map[*aurora.Metadata]bool)
taskConfig.Constraints = make(map[*aurora.Constraint]bool)
//Resources
numCpus := aurora.NewResource()
ramMb := aurora.NewResource()
diskMb := aurora.NewResource()
2016-08-24 11:59:01 -07:00
resources := make(map[string]*aurora.Resource)
resources["cpu"] = numCpus
resources["ram"] = ramMb
resources["disk"] = diskMb
2016-08-02 11:42:00 -07:00
taskConfig.Resources = make(map[*aurora.Resource]bool)
taskConfig.Resources[numCpus] = true
taskConfig.Resources[ramMb] = true
taskConfig.Resources[diskMb] = true
2016-08-24 11:59:01 -07:00
return AuroraJob{jobConfig, resources, 0}
2016-08-02 11:42:00 -07:00
}
// Set Job Key environment.
2016-08-24 11:59:01 -07:00
func (j AuroraJob) Environment(env string) Job {
j.jobConfig.Key.Environment = env
return j
2016-08-02 11:42:00 -07:00
}
// Set Job Key Role.
2016-08-24 11:59:01 -07:00
func (j AuroraJob) Role(role string) Job {
j.jobConfig.Key.Role = role
2016-08-02 11:42:00 -07:00
//Will be deprecated
identity := &aurora.Identity{role}
2016-08-24 11:59:01 -07:00
j.jobConfig.Owner = identity
j.jobConfig.TaskConfig.Owner = identity
return j
2016-08-02 11:42:00 -07:00
}
// Set Job Key Name.
2016-08-24 11:59:01 -07:00
func (j AuroraJob) Name(name string) Job {
j.jobConfig.Key.Name = name
return j
2016-08-02 11:42:00 -07:00
}
// Set name of the executor that will the task will be configured to.
2016-08-24 11:59:01 -07:00
func (j AuroraJob) ExecutorName(name string) Job {
j.jobConfig.TaskConfig.ExecutorConfig.Name = name
return j
2016-08-02 11:42:00 -07:00
}
// Will be included as part of entire task inside the scheduler that will be serialized.
2016-08-24 11:59:01 -07:00
func (j AuroraJob) ExecutorData(data string) Job {
j.jobConfig.TaskConfig.ExecutorConfig.Data = data
return j
2016-08-02 11:42:00 -07:00
}
2016-08-24 11:59:01 -07:00
func (j AuroraJob) CPU(cpus float64) Job {
j.resources["cpu"].NumCpus = &cpus
j.jobConfig.TaskConfig.NumCpus = cpus //Will be deprecated soon
2016-08-02 11:42:00 -07:00
2016-08-24 11:59:01 -07:00
return j
2016-08-02 11:42:00 -07:00
}
2016-08-24 11:59:01 -07:00
func (j AuroraJob) RAM(ram int64) Job {
j.resources["ram"].RamMb = &ram
j.jobConfig.TaskConfig.RamMb = ram //Will be deprecated soon
2016-08-02 11:42:00 -07:00
2016-08-24 11:59:01 -07:00
return j
2016-08-02 11:42:00 -07:00
}
2016-08-24 11:59:01 -07:00
func (j AuroraJob) Disk(disk int64) Job {
j.resources["disk"].DiskMb = &disk
j.jobConfig.TaskConfig.DiskMb = disk //Will be deprecated
2016-08-02 11:42:00 -07:00
2016-08-24 11:59:01 -07:00
return j
2016-08-02 11:42:00 -07:00
}
// How many failures to tolerate before giving up.
2016-08-24 11:59:01 -07:00
func (j AuroraJob) MaxFailure(maxFail int32) Job {
j.jobConfig.TaskConfig.MaxTaskFailures = maxFail
return j
2016-08-02 11:42:00 -07:00
}
// How many instances of the job to run
2016-08-24 11:59:01 -07:00
func (j AuroraJob) InstanceCount(instCount int32) Job {
j.jobConfig.InstanceCount = instCount
return j
2016-08-02 11:42:00 -07:00
}
// How many instances of the job to run
func (j AuroraJob) GetInstanceCount() int32 {
return j.jobConfig.InstanceCount
}
// Restart the job's tasks if they fail
2016-08-24 11:59:01 -07:00
func (j AuroraJob) IsService(isService bool) Job {
j.jobConfig.TaskConfig.IsService = isService
return j
}
// Get the current job configurations key to use for some realis calls.
func (j AuroraJob) JobKey() *aurora.JobKey {
return j.jobConfig.Key
2016-08-02 11:42:00 -07:00
}
// Get the current job configurations key to use for some realis calls.
2016-08-24 11:59:01 -07:00
func (j AuroraJob) JobConfig() *aurora.JobConfiguration {
return j.jobConfig
}
func (j AuroraJob) TaskConfig() *aurora.TaskConfig {
return j.jobConfig.TaskConfig
2016-08-02 11:42:00 -07:00
}
// Add a list of URIs with the same extract and cache configuration. Scheduler must have
// --enable_mesos_fetcher flag enabled. Currently there is no duplicate detection.
2016-08-24 11:59:01 -07:00
func (j AuroraJob) AddURIs(extract bool, cache bool, values ...string) Job {
2016-08-02 11:42:00 -07:00
for _, value := range values {
2016-08-24 11:59:01 -07:00
j.jobConfig.
TaskConfig.
MesosFetcherUris[&aurora.MesosFetcherURI{value, &extract, &cache}] = true
2016-08-02 11:42:00 -07:00
}
2016-08-24 11:59:01 -07:00
return j
2016-08-02 11:42:00 -07:00
}
// Adds a Mesos label to the job. Note that Aurora will add the
// prefix "org.apache.aurora.metadata." to the beginning of each key.
2016-08-24 11:59:01 -07:00
func (j AuroraJob) AddLabel(key string, value string) Job {
j.jobConfig.TaskConfig.Metadata[&aurora.Metadata{key, value}] = true
return j
2016-08-02 11:42:00 -07:00
}
// Add a named port to the job configuration These are random ports as it's
// not currently possible to request specific ports using Aurora.
2016-08-24 11:59:01 -07:00
func (j AuroraJob) AddNamedPorts(names ...string) Job {
j.portCount += len(names)
for _, name := range names {
2016-08-24 11:59:01 -07:00
j.jobConfig.TaskConfig.Resources[&aurora.Resource{NamedPort: &name}] = true
}
2016-08-24 11:59:01 -07:00
return j
}
// Adds a request for a number of ports to the job configuration. The names chosen for these ports
2016-08-15 12:33:17 -07:00
// will be org.apache.aurora.port.X, where X is the current port count for the job configuration
// starting at 0. These are random ports as it's not currently possible to request
// specific ports using Aurora.
2016-08-24 11:59:01 -07:00
func (j AuroraJob) AddPorts(num int) Job {
start := j.portCount
j.portCount += num
for i := start; i < j.portCount; i++ {
2016-08-15 12:33:17 -07:00
portName := "org.apache.aurora.port." + strconv.Itoa(i)
2016-08-24 11:59:01 -07:00
j.jobConfig.TaskConfig.Resources[&aurora.Resource{NamedPort: &portName}] = true
2016-08-02 11:42:00 -07:00
}
2016-08-24 11:59:01 -07:00
return j
2016-08-02 11:42:00 -07:00
}
// From Aurora Docs:
// Add a Value constraint
2016-08-02 11:42:00 -07:00
// name - Mesos slave attribute that the constraint is matched against.
// If negated = true , treat this as a 'not' - to avoid specific values.
// Values - list of values we look for in attribute name
2016-08-24 11:59:01 -07:00
func (j AuroraJob) AddValueConstraint(name string, negated bool, values ...string) Job {
2016-08-02 11:42:00 -07:00
constraintValues := make(map[string]bool)
for _, value := range values {
constraintValues[value] = true
}
2016-08-24 11:59:01 -07:00
j.jobConfig.TaskConfig.Constraints[&aurora.Constraint{name,
2016-08-02 11:42:00 -07:00
&aurora.TaskConstraint{&aurora.ValueConstraint{negated, constraintValues}, nil}}] = true
2016-08-24 11:59:01 -07:00
return j
2016-08-02 11:42:00 -07:00
}
// From Aurora Docs:
// A constraint that specifies the maximum number of active tasks on a host with
2016-08-02 11:42:00 -07:00
// a matching attribute that may be scheduled simultaneously.
2016-08-24 11:59:01 -07:00
func (j AuroraJob) AddLimitConstraint(name string, limit int32) Job {
j.jobConfig.TaskConfig.Constraints[&aurora.Constraint{name,
2016-08-02 11:42:00 -07:00
&aurora.TaskConstraint{nil, &aurora.LimitConstraint{limit}}}] = true
2016-08-24 11:59:01 -07:00
return j
2016-08-02 11:42:00 -07:00
}