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-09 16:18:30 -07:00
|
|
|
|
2016-08-02 11:42:00 -07:00
|
|
|
// Package realis provides the ability to use Thrift API to communicate with Apache Aurora.
|
|
|
|
package realis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
2017-03-09 11:37:03 -08:00
|
|
|
"net/http"
|
|
|
|
"net/http/cookiejar"
|
|
|
|
"time"
|
|
|
|
|
2017-03-10 15:50:26 -08:00
|
|
|
"fmt"
|
|
|
|
|
2016-08-02 11:42:00 -07:00
|
|
|
"git.apache.org/thrift.git/lib/go/thrift"
|
|
|
|
"github.com/pkg/errors"
|
2016-09-19 15:34:56 -04:00
|
|
|
"github.com/rdelval/gorealis/gen-go/apache/aurora"
|
2016-11-02 20:41:43 -04:00
|
|
|
"github.com/rdelval/gorealis/response"
|
2016-08-02 11:42:00 -07:00
|
|
|
)
|
|
|
|
|
2016-08-12 12:48:42 -07:00
|
|
|
type Realis interface {
|
2016-08-25 18:56:55 -07:00
|
|
|
AbortJobUpdate(updateKey aurora.JobUpdateKey, message string) (*aurora.Response, error)
|
2016-08-24 15:38:44 -07:00
|
|
|
AddInstances(instKey aurora.InstanceKey, count int32) (*aurora.Response, error)
|
2017-03-10 01:14:09 -08:00
|
|
|
RemoveInstances(key *aurora.JobKey, count int32) (*aurora.Response, error)
|
2016-08-24 15:38:44 -07:00
|
|
|
CreateJob(auroraJob Job) (*aurora.Response, error)
|
2016-09-30 01:24:49 -04:00
|
|
|
DescheduleCronJob(key *aurora.JobKey) (*aurora.Response, error)
|
2017-02-13 19:31:41 -05:00
|
|
|
GetTaskStatus(query *aurora.TaskQuery) ([]*aurora.ScheduledTask, error)
|
2016-08-24 15:38:44 -07:00
|
|
|
FetchTaskConfig(instKey aurora.InstanceKey) (*aurora.TaskConfig, error)
|
2016-09-19 15:34:56 -04:00
|
|
|
GetInstanceIds(key *aurora.JobKey, states map[aurora.ScheduleStatus]bool) (map[int32]bool, error)
|
2016-09-29 20:45:24 -04:00
|
|
|
JobUpdateDetails(updateQuery aurora.JobUpdateQuery) (*aurora.Response, error)
|
2016-08-12 12:48:42 -07:00
|
|
|
KillJob(key *aurora.JobKey) (*aurora.Response, error)
|
2016-08-25 18:56:55 -07:00
|
|
|
KillInstances(key *aurora.JobKey, instances ...int32) (*aurora.Response, error)
|
|
|
|
RestartInstances(key *aurora.JobKey, instances ...int32) (*aurora.Response, error)
|
2016-08-12 12:48:42 -07:00
|
|
|
RestartJob(key *aurora.JobKey) (*aurora.Response, error)
|
2016-09-30 00:44:38 -04:00
|
|
|
RollbackJobUpdate(key aurora.JobUpdateKey, message string) (*aurora.Response, error)
|
2016-09-30 01:24:49 -04:00
|
|
|
ScheduleCronJob(auroraJob Job) (*aurora.Response, error)
|
2016-08-12 12:48:42 -07:00
|
|
|
StartJobUpdate(updateJob *UpdateJob, message string) (*aurora.Response, error)
|
2016-09-30 01:24:49 -04:00
|
|
|
StartCronJob(key *aurora.JobKey) (*aurora.Response, error)
|
2017-03-09 11:37:03 -08:00
|
|
|
GetJobUpdateSummaries(jobUpdateQuery *aurora.JobUpdateQuery) (*aurora.Response, error)
|
2016-08-12 12:48:42 -07:00
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
type realisClient struct {
|
2017-03-09 11:37:03 -08:00
|
|
|
client *aurora.AuroraSchedulerManagerClient
|
|
|
|
readonlyClient *aurora.ReadOnlySchedulerClient
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
|
|
|
|
2017-02-13 19:31:41 -05:00
|
|
|
// Wrapper object to provide future flexibility
|
2016-08-02 11:42:00 -07:00
|
|
|
type RealisConfig struct {
|
2017-02-13 19:31:41 -05:00
|
|
|
transport thrift.TTransport
|
2017-02-10 19:23:20 -05:00
|
|
|
protoFactory thrift.TProtocolFactory
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
|
|
|
|
2016-08-09 16:18:30 -07:00
|
|
|
// Create a new Client with a default transport layer
|
2016-08-12 12:48:42 -07:00
|
|
|
func NewClient(config RealisConfig) Realis {
|
2017-03-10 15:50:26 -08:00
|
|
|
return &realisClient{
|
2017-03-09 11:37:03 -08:00
|
|
|
client: aurora.NewAuroraSchedulerManagerClientFactory(config.transport, config.protoFactory),
|
|
|
|
readonlyClient: aurora.NewReadOnlySchedulerClientFactory(config.transport, config.protoFactory)}
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
|
|
|
|
2017-02-13 19:31:41 -05:00
|
|
|
// Creates a default Thrift Transport object for communications in gorealis using an HTTP Post Client
|
|
|
|
func defaultTTransport(urlstr string, timeoutms int) (thrift.TTransport, error) {
|
2016-08-02 11:42:00 -07:00
|
|
|
jar, err := cookiejar.New(nil)
|
|
|
|
if err != nil {
|
2017-02-13 19:31:41 -05:00
|
|
|
return &thrift.THttpClient{}, errors.Wrap(err, "Error creating Cookie Jar")
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
|
|
|
|
2017-02-13 19:31:41 -05:00
|
|
|
trans, err := thrift.NewTHttpPostClientWithOptions(urlstr+"/api",
|
2016-11-14 13:27:25 -08:00
|
|
|
thrift.THttpClientOptions{Client: &http.Client{Timeout: time.Millisecond * time.Duration(timeoutms), Jar: jar}})
|
2016-08-02 11:42:00 -07:00
|
|
|
|
|
|
|
if err != nil {
|
2017-02-13 19:31:41 -05:00
|
|
|
return &thrift.THttpClient{}, errors.Wrap(err, "Error creating transport")
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := trans.Open(); err != nil {
|
2017-02-13 19:31:41 -05:00
|
|
|
return &thrift.THttpClient{}, errors.Wrapf(err, "Error opening connection to %s", urlstr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return trans, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a default configuration of the transport layer, requires a URL to test connection with.
|
|
|
|
// Uses HTTP Post as transport layer and Thrift JSON as the wire protocol by default.
|
|
|
|
func NewDefaultConfig(url string, timeoutms int) (RealisConfig, error) {
|
|
|
|
return NewTJSONConfig(url, timeoutms)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a realis config object using HTTP Post and Thrift JSON protocol to communicate with Aurora.
|
|
|
|
func NewTJSONConfig(url string, timeoutms int) (RealisConfig, error) {
|
|
|
|
trans, err := defaultTTransport(url, timeoutms)
|
|
|
|
if err != nil {
|
|
|
|
return RealisConfig{}, errors.Wrap(err, "Error creating realis config")
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
2017-02-13 19:31:41 -05:00
|
|
|
|
|
|
|
httpTrans := (trans).(*thrift.THttpClient)
|
|
|
|
httpTrans.SetHeader("Content-Type", "application/x-thrift")
|
|
|
|
|
|
|
|
return RealisConfig{transport: trans, protoFactory: thrift.NewTJSONProtocolFactory()}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a realis config config using HTTP Post and Thrift Binary protocol to communicate with Aurora.
|
|
|
|
func NewTBinaryConfig(url string, timeoutms int) (RealisConfig, error) {
|
|
|
|
trans, err := defaultTTransport(url, timeoutms)
|
|
|
|
if err != nil {
|
|
|
|
return RealisConfig{}, errors.Wrap(err, "Error creating realis config")
|
|
|
|
}
|
|
|
|
|
2017-02-10 19:23:20 -05:00
|
|
|
httpTrans := (trans).(*thrift.THttpClient)
|
|
|
|
httpTrans.SetHeader("Accept", "application/vnd.apache.thrift.binary")
|
|
|
|
httpTrans.SetHeader("Content-Type", "application/vnd.apache.thrift.binary")
|
|
|
|
httpTrans.SetHeader("User-Agent", "GoRealis v1.0.4")
|
2016-08-02 11:42:00 -07:00
|
|
|
|
2017-02-10 19:23:20 -05:00
|
|
|
return RealisConfig{transport: trans, protoFactory: thrift.NewTBinaryProtocolFactoryDefault()}, nil
|
2016-08-02 11:42:00 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-09 16:18:30 -07:00
|
|
|
// Helper function to add basic authorization needed to communicate with Apache Aurora.
|
2016-08-02 11:42:00 -07:00
|
|
|
func AddBasicAuth(config *RealisConfig, username string, password string) {
|
|
|
|
httpTrans := (config.transport).(*thrift.THttpClient)
|
|
|
|
httpTrans.SetHeader("Authorization", "Basic "+basicAuth(username, password))
|
|
|
|
}
|
|
|
|
|
|
|
|
func basicAuth(username, password string) string {
|
|
|
|
auth := username + ":" + password
|
|
|
|
return base64.StdEncoding.EncodeToString([]byte(auth))
|
|
|
|
}
|
|
|
|
|
2016-08-09 16:18:30 -07:00
|
|
|
// Releases resources associated with the realis client.
|
2017-03-10 15:50:26 -08:00
|
|
|
func (r *realisClient) Close() {
|
2016-08-02 11:42:00 -07:00
|
|
|
r.client.Transport.Close()
|
2017-03-09 11:37:03 -08:00
|
|
|
r.readonlyClient.Transport.Close()
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
|
|
|
|
2016-08-09 16:18:30 -07:00
|
|
|
// Uses predefined set of states to retrieve a set of active jobs in Apache Aurora.
|
2017-03-10 15:50:26 -08:00
|
|
|
func (r *realisClient) GetInstanceIds(key *aurora.JobKey, states map[aurora.ScheduleStatus]bool) (map[int32]bool, error) {
|
2016-08-02 11:42:00 -07:00
|
|
|
taskQ := &aurora.TaskQuery{Role: key.Role,
|
|
|
|
Environment: key.Environment,
|
|
|
|
JobName: key.Name,
|
2016-08-26 16:35:31 -07:00
|
|
|
Statuses: states}
|
2016-08-02 11:42:00 -07:00
|
|
|
|
2016-09-30 01:24:49 -04:00
|
|
|
resp, err := r.client.GetTasksWithoutConfigs(taskQ)
|
2016-08-02 11:42:00 -07:00
|
|
|
if err != nil {
|
2016-08-16 18:40:02 -07:00
|
|
|
return nil, errors.Wrap(err, "Error querying Aurora Scheduler for active IDs")
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
|
|
|
|
2016-09-30 01:24:49 -04:00
|
|
|
tasks := response.ScheduleStatusResult(resp).GetTasks()
|
2016-08-02 11:42:00 -07:00
|
|
|
|
2016-08-09 14:21:43 -07:00
|
|
|
jobInstanceIds := make(map[int32]bool)
|
2016-08-02 11:42:00 -07:00
|
|
|
for _, task := range tasks {
|
2016-08-09 14:21:43 -07:00
|
|
|
jobInstanceIds[task.GetAssignedTask().GetInstanceId()] = true
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
|
|
|
|
2016-08-09 14:21:43 -07:00
|
|
|
return jobInstanceIds, nil
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
|
|
|
|
2017-03-10 15:50:26 -08:00
|
|
|
func (r *realisClient) GetJobUpdateSummaries(jobUpdateQuery *aurora.JobUpdateQuery) (*aurora.Response, error) {
|
|
|
|
resp, err := r.readonlyClient.GetJobUpdateSummaries(jobUpdateQuery)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Error getting job update summaries from Aurora Scheduler")
|
|
|
|
}
|
|
|
|
return response.ResponseCodeCheck(resp)
|
2017-03-09 11:37:03 -08:00
|
|
|
}
|
|
|
|
|
2016-08-25 18:56:55 -07:00
|
|
|
// Kill specific instances of a job.
|
2017-03-10 15:50:26 -08:00
|
|
|
func (r *realisClient) KillInstances(key *aurora.JobKey, instances ...int32) (*aurora.Response, error) {
|
2016-08-09 13:44:54 -07:00
|
|
|
|
2016-08-09 14:21:43 -07:00
|
|
|
instanceIds := make(map[int32]bool)
|
2016-08-09 13:44:54 -07:00
|
|
|
|
2016-08-25 18:56:55 -07:00
|
|
|
for _, instId := range instances {
|
|
|
|
instanceIds[instId] = true
|
|
|
|
}
|
2016-08-09 13:44:54 -07:00
|
|
|
|
2016-11-11 17:58:01 -05:00
|
|
|
resp, err := r.client.KillTasks(key, instanceIds)
|
2016-08-09 13:44:54 -07:00
|
|
|
if err != nil {
|
2016-08-16 18:40:02 -07:00
|
|
|
return nil, errors.Wrap(err, "Error sending Kill command to Aurora Scheduler")
|
2016-08-09 13:44:54 -07:00
|
|
|
}
|
|
|
|
|
2016-11-11 17:58:01 -05:00
|
|
|
return response.ResponseCodeCheck(resp)
|
2016-08-09 13:44:54 -07:00
|
|
|
}
|
|
|
|
|
2016-08-09 16:18:30 -07:00
|
|
|
// Sends a kill message to the scheduler for all active tasks under a job.
|
2017-03-10 15:50:26 -08:00
|
|
|
func (r *realisClient) KillJob(key *aurora.JobKey) (*aurora.Response, error) {
|
2016-08-02 11:42:00 -07:00
|
|
|
|
2016-08-26 16:35:31 -07:00
|
|
|
instanceIds, err := r.GetInstanceIds(key, aurora.ACTIVE_STATES)
|
2016-08-02 11:42:00 -07:00
|
|
|
if err != nil {
|
2016-08-16 18:40:02 -07:00
|
|
|
return nil, errors.Wrap(err, "Could not retrieve relevant task instance IDs")
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
|
|
|
|
2016-08-09 14:21:43 -07:00
|
|
|
if len(instanceIds) > 0 {
|
2016-11-11 17:58:01 -05:00
|
|
|
resp, err := r.client.KillTasks(key, instanceIds)
|
2016-08-02 11:42:00 -07:00
|
|
|
|
|
|
|
if err != nil {
|
2016-08-16 18:40:02 -07:00
|
|
|
return nil, errors.Wrap(err, "Error sending Kill command to Aurora Scheduler")
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
2016-08-09 13:30:26 -07:00
|
|
|
|
2016-11-11 17:58:01 -05:00
|
|
|
return response.ResponseCodeCheck(resp)
|
2016-08-02 11:42:00 -07:00
|
|
|
} else {
|
2016-08-16 18:40:02 -07:00
|
|
|
return nil, errors.New("No tasks in the Active state")
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-09 16:18:30 -07:00
|
|
|
// Sends a create job message to the scheduler with a specific job configuration.
|
2017-03-10 15:50:26 -08:00
|
|
|
func (r *realisClient) CreateJob(auroraJob Job) (*aurora.Response, error) {
|
2016-11-11 17:58:01 -05:00
|
|
|
resp, err := r.client.CreateJob(auroraJob.JobConfig())
|
2016-08-02 11:42:00 -07:00
|
|
|
|
|
|
|
if err != nil {
|
2016-08-16 18:40:02 -07:00
|
|
|
return nil, errors.Wrap(err, "Error sending Create command to Aurora Scheduler")
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
|
|
|
|
2016-11-11 17:58:01 -05:00
|
|
|
return response.ResponseCodeCheck(resp)
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
|
|
|
|
2017-03-10 15:50:26 -08:00
|
|
|
func (r *realisClient) ScheduleCronJob(auroraJob Job) (*aurora.Response, error) {
|
2016-11-11 17:58:01 -05:00
|
|
|
resp, err := r.client.ScheduleCronJob(auroraJob.JobConfig())
|
2016-09-30 01:24:49 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Error sending Cron Job Schedule message to Aurora Scheduler")
|
|
|
|
}
|
|
|
|
|
2016-11-11 17:58:01 -05:00
|
|
|
return response.ResponseCodeCheck(resp)
|
2016-09-30 01:24:49 -04:00
|
|
|
}
|
|
|
|
|
2017-03-10 15:50:26 -08:00
|
|
|
func (r *realisClient) DescheduleCronJob(key *aurora.JobKey) (*aurora.Response, error) {
|
2016-11-11 17:58:01 -05:00
|
|
|
resp, err := r.client.DescheduleCronJob(key)
|
2016-09-30 01:24:49 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Error sending Cron Job De-schedule message to Aurora Scheduler")
|
|
|
|
}
|
|
|
|
|
2016-11-11 17:58:01 -05:00
|
|
|
return response.ResponseCodeCheck(resp)
|
2016-09-30 01:24:49 -04:00
|
|
|
}
|
|
|
|
|
2017-03-10 15:50:26 -08:00
|
|
|
func (r *realisClient) StartCronJob(key *aurora.JobKey) (*aurora.Response, error) {
|
2016-11-11 17:58:01 -05:00
|
|
|
resp, err := r.client.StartCronJob(key)
|
2016-09-30 01:24:49 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Error sending Start Cron Job message to Aurora Scheduler")
|
|
|
|
}
|
|
|
|
|
2016-11-11 17:58:01 -05:00
|
|
|
return response.ResponseCodeCheck(resp)
|
2016-09-30 01:24:49 -04:00
|
|
|
}
|
|
|
|
|
2016-08-25 18:56:55 -07:00
|
|
|
// Restarts specific instances specified
|
2017-03-10 15:50:26 -08:00
|
|
|
func (r *realisClient) RestartInstances(key *aurora.JobKey, instances ...int32) (*aurora.Response, error) {
|
2016-08-25 18:56:55 -07:00
|
|
|
instanceIds := make(map[int32]bool)
|
|
|
|
|
|
|
|
for _, instId := range instances {
|
|
|
|
instanceIds[instId] = true
|
|
|
|
}
|
|
|
|
|
2016-11-11 17:58:01 -05:00
|
|
|
resp, err := r.client.RestartShards(key, instanceIds)
|
2016-08-25 18:56:55 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Error sending Restart command to Aurora Scheduler")
|
|
|
|
}
|
|
|
|
|
2016-11-11 17:58:01 -05:00
|
|
|
return response.ResponseCodeCheck(resp)
|
2016-08-25 18:56:55 -07:00
|
|
|
}
|
|
|
|
|
2016-08-09 16:18:30 -07:00
|
|
|
// Restarts all active tasks under a job configuration.
|
2017-03-10 15:50:26 -08:00
|
|
|
func (r *realisClient) RestartJob(key *aurora.JobKey) (*aurora.Response, error) {
|
2016-08-02 11:42:00 -07:00
|
|
|
|
2016-08-26 16:35:31 -07:00
|
|
|
instanceIds, err := r.GetInstanceIds(key, aurora.ACTIVE_STATES)
|
2016-08-02 11:42:00 -07:00
|
|
|
if err != nil {
|
2016-08-16 18:40:02 -07:00
|
|
|
return nil, errors.Wrap(err, "Could not retrieve relevant task instance IDs")
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
|
|
|
|
2016-08-09 14:21:43 -07:00
|
|
|
if len(instanceIds) > 0 {
|
2016-11-11 17:58:01 -05:00
|
|
|
resp, err := r.client.RestartShards(key, instanceIds)
|
2016-08-02 11:42:00 -07:00
|
|
|
|
|
|
|
if err != nil {
|
2016-08-16 18:40:02 -07:00
|
|
|
return nil, errors.Wrap(err, "Error sending Restart command to Aurora Scheduler")
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
|
|
|
|
2016-11-11 17:58:01 -05:00
|
|
|
return response.ResponseCodeCheck(resp)
|
2016-08-02 11:42:00 -07:00
|
|
|
} else {
|
2016-08-16 18:40:02 -07:00
|
|
|
return nil, errors.New("No tasks in the Active state")
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 18:40:02 -07:00
|
|
|
// Update all tasks under a job configuration. Currently gorealis doesn't support for canary deployments.
|
2017-03-10 15:50:26 -08:00
|
|
|
func (r *realisClient) StartJobUpdate(updateJob *UpdateJob, message string) (*aurora.Response, error) {
|
2016-08-02 11:42:00 -07:00
|
|
|
|
2016-11-11 17:58:01 -05:00
|
|
|
resp, err := r.client.StartJobUpdate(updateJob.req, message)
|
2016-08-02 11:42:00 -07:00
|
|
|
|
|
|
|
if err != nil {
|
2016-08-16 18:40:02 -07:00
|
|
|
return nil, errors.Wrap(err, "Error sending StartJobUpdate command to Aurora Scheduler")
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
|
|
|
|
2016-11-11 17:58:01 -05:00
|
|
|
return response.ResponseCodeCheck(resp)
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
|
|
|
|
2016-08-09 16:18:30 -07:00
|
|
|
// Abort Job Update on Aurora. Requires the updateId which can be obtained on the Aurora web UI.
|
2017-03-10 15:50:26 -08:00
|
|
|
func (r *realisClient) AbortJobUpdate(
|
2016-08-25 18:56:55 -07:00
|
|
|
updateKey aurora.JobUpdateKey,
|
2016-08-09 13:30:26 -07:00
|
|
|
message string) (*aurora.Response, error) {
|
2016-08-02 11:42:00 -07:00
|
|
|
|
2016-11-11 17:58:01 -05:00
|
|
|
resp, err := r.client.AbortJobUpdate(&updateKey, message)
|
2016-08-02 11:42:00 -07:00
|
|
|
|
|
|
|
if err != nil {
|
2016-08-16 18:40:02 -07:00
|
|
|
return nil, errors.Wrap(err, "Error sending AbortJobUpdate command to Aurora Scheduler")
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
|
|
|
|
2016-11-11 17:58:01 -05:00
|
|
|
return response.ResponseCodeCheck(resp)
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
|
|
|
|
2016-08-09 16:18:30 -07:00
|
|
|
// Scale up the number of instances under a job configuration using the configuration for specific
|
|
|
|
// instance to scale up.
|
2017-03-10 15:50:26 -08:00
|
|
|
func (r *realisClient) AddInstances(instKey aurora.InstanceKey, count int32) (*aurora.Response, error) {
|
2016-08-02 11:42:00 -07:00
|
|
|
|
2016-11-11 17:58:01 -05:00
|
|
|
resp, err := r.client.AddInstances(&instKey, count)
|
2016-08-02 11:42:00 -07:00
|
|
|
|
|
|
|
if err != nil {
|
2016-08-16 18:40:02 -07:00
|
|
|
return nil, errors.Wrap(err, "Error sending AddInstances command to Aurora Scheduler")
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
|
|
|
|
2016-11-11 17:58:01 -05:00
|
|
|
return response.ResponseCodeCheck(resp)
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
2016-08-24 15:38:44 -07:00
|
|
|
|
2017-03-10 01:14:09 -08:00
|
|
|
//Scale down the number of instances under a job configuration using the configuratipn of a specific instance
|
2017-03-10 15:50:26 -08:00
|
|
|
func (r *realisClient) RemoveInstances(key *aurora.JobKey, count int32) (*aurora.Response, error) {
|
2017-03-10 01:14:09 -08:00
|
|
|
instanceIds, err := r.GetInstanceIds(key, aurora.ACTIVE_STATES)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "RemoveInstances: Could not retrieve relevant instance IDs")
|
|
|
|
}
|
|
|
|
if len(instanceIds) < int(count) {
|
2017-03-10 15:50:26 -08:00
|
|
|
return nil, errors.New(fmt.Sprintf("RemoveInstances: No sufficient instances to Kill - "+
|
2017-03-10 01:14:09 -08:00
|
|
|
"Instances to kill %d Total Instances %d", count, len(instanceIds)))
|
|
|
|
}
|
|
|
|
instanceList := make([]int32, count)
|
|
|
|
i := 0
|
|
|
|
for k := range instanceIds {
|
|
|
|
instanceList[i] = k
|
|
|
|
i += 1
|
|
|
|
if i == int(count) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r.KillInstances(key, instanceList...)
|
|
|
|
}
|
|
|
|
|
2017-03-10 15:50:26 -08:00
|
|
|
func (r *realisClient) GetTaskStatus(query *aurora.TaskQuery) (tasks []*aurora.ScheduledTask, e error) {
|
2016-11-15 22:24:07 -08:00
|
|
|
|
|
|
|
resp, err := r.client.GetTasksStatus(query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Error querying Aurora Scheduler for task status")
|
|
|
|
}
|
|
|
|
//Check for response code..
|
|
|
|
if resp.GetResponseCode() != aurora.ResponseCode_OK {
|
|
|
|
return nil, errors.New(resp.ResponseCode.String() + "--" + response.CombineMessage(resp))
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.ScheduleStatusResult(resp).GetTasks(), nil
|
|
|
|
}
|
|
|
|
|
2017-03-10 15:50:26 -08:00
|
|
|
func (r *realisClient) FetchTaskConfig(instKey aurora.InstanceKey) (*aurora.TaskConfig, error) {
|
2016-08-24 15:38:44 -07:00
|
|
|
|
|
|
|
ids := make(map[int32]bool)
|
|
|
|
|
|
|
|
ids[instKey.InstanceId] = true
|
|
|
|
taskQ := &aurora.TaskQuery{Role: instKey.JobKey.Role,
|
|
|
|
Environment: instKey.JobKey.Environment,
|
|
|
|
JobName: instKey.JobKey.Name,
|
2016-08-24 17:21:59 -07:00
|
|
|
InstanceIds: ids,
|
|
|
|
Statuses: aurora.ACTIVE_STATES}
|
2016-08-24 15:38:44 -07:00
|
|
|
|
2016-09-30 01:24:49 -04:00
|
|
|
resp, err := r.client.GetTasksStatus(taskQ)
|
2016-08-24 15:38:44 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Error querying Aurora Scheduler for task configuration")
|
|
|
|
}
|
|
|
|
|
2016-11-15 22:24:07 -08:00
|
|
|
//Check for response code..
|
|
|
|
if resp.GetResponseCode() != aurora.ResponseCode_OK {
|
2017-02-13 19:31:41 -05:00
|
|
|
return nil, errors.New(resp.ResponseCode.String() + "--" + response.CombineMessage(resp))
|
2016-11-15 22:24:07 -08:00
|
|
|
}
|
|
|
|
|
2016-09-30 01:24:49 -04:00
|
|
|
tasks := response.ScheduleStatusResult(resp).GetTasks()
|
2016-08-24 15:38:44 -07:00
|
|
|
|
2016-08-24 17:21:59 -07:00
|
|
|
if len(tasks) == 0 {
|
2016-08-24 15:38:44 -07:00
|
|
|
return nil, errors.Errorf("Instance %d for jobkey %s/%s/%s doesn't exist",
|
|
|
|
instKey.InstanceId,
|
|
|
|
instKey.JobKey.Environment,
|
|
|
|
instKey.JobKey.Role,
|
|
|
|
instKey.JobKey.Name)
|
|
|
|
}
|
|
|
|
|
2016-11-11 17:58:01 -05:00
|
|
|
// Currently, instance 0 is always picked
|
2016-08-24 15:38:44 -07:00
|
|
|
return tasks[0].AssignedTask.Task, nil
|
|
|
|
}
|
2016-08-25 18:56:55 -07:00
|
|
|
|
2017-03-10 15:50:26 -08:00
|
|
|
func (r *realisClient) JobUpdateDetails(updateQuery aurora.JobUpdateQuery) (*aurora.Response, error) {
|
2016-08-25 18:56:55 -07:00
|
|
|
|
2016-09-29 20:45:24 -04:00
|
|
|
resp, err := r.client.GetJobUpdateDetails(&updateQuery)
|
2016-08-25 18:56:55 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Unable to get job update details")
|
|
|
|
}
|
|
|
|
|
2016-11-11 17:58:01 -05:00
|
|
|
return response.ResponseCodeCheck(resp)
|
2016-08-25 18:56:55 -07:00
|
|
|
}
|
2017-03-10 15:50:26 -08:00
|
|
|
func (r *realisClient) RollbackJobUpdate(key aurora.JobUpdateKey, message string) (*aurora.Response, error) {
|
2016-09-30 00:44:38 -04:00
|
|
|
|
|
|
|
resp, err := r.client.RollbackJobUpdate(&key, message)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Unable to roll back job update")
|
|
|
|
}
|
|
|
|
|
2016-11-11 17:58:01 -05:00
|
|
|
return response.ResponseCodeCheck(resp)
|
2016-09-30 00:44:38 -04:00
|
|
|
}
|