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 main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"github.com/rdelval/gorealis"
|
2016-09-19 15:34:56 -04:00
|
|
|
"github.com/rdelval/gorealis/gen-go/apache/aurora"
|
|
|
|
"github.com/rdelval/gorealis/response"
|
2016-08-02 11:42:00 -07:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2016-08-26 16:35:31 -07:00
|
|
|
func main() {
|
2016-08-11 11:39:44 -07:00
|
|
|
cmd := flag.String("cmd", "", "Job request type to send to Aurora Scheduler")
|
|
|
|
executor := flag.String("executor", "thermos", "Executor to use")
|
|
|
|
url := flag.String("url", "", "URL at which the Aurora Scheduler exists as [url]:[port]")
|
2016-08-16 18:40:02 -07:00
|
|
|
clustersConfig := flag.String("clusters", "", "Location of the clusters.json file used by aurora.")
|
2016-08-24 15:38:44 -07:00
|
|
|
clusterName := flag.String("cluster", "devcluster", "Name of cluster to run job on")
|
2016-08-02 11:42:00 -07:00
|
|
|
updateId := flag.String("updateId", "", "Update ID to operate on")
|
2016-08-11 11:39:44 -07:00
|
|
|
username := flag.String("username", "aurora", "Username to use for authorization")
|
|
|
|
password := flag.String("password", "secret", "Password to use for authorization")
|
2016-08-02 11:42:00 -07:00
|
|
|
flag.Parse()
|
|
|
|
|
2016-08-16 18:40:02 -07:00
|
|
|
// Attempt to load leader from zookeeper
|
|
|
|
if *clustersConfig != "" {
|
|
|
|
clusters, err := realis.LoadClusters(*clustersConfig)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2016-08-24 17:21:59 -07:00
|
|
|
cluster, ok := clusters[*clusterName]
|
|
|
|
if !ok {
|
2016-08-24 15:38:44 -07:00
|
|
|
fmt.Printf("Cluster %s chosen doesn't exist\n", *clusterName)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2016-08-16 18:40:02 -07:00
|
|
|
|
|
|
|
*url, err = realis.LeaderFromZK(cluster)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-02 11:42:00 -07:00
|
|
|
//Create new configuration with default transport layer
|
|
|
|
config, err := realis.NewDefaultConfig(*url)
|
|
|
|
if err != nil {
|
2016-08-16 18:40:02 -07:00
|
|
|
fmt.Println(err)
|
2016-08-02 11:42:00 -07:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2016-08-11 11:39:44 -07:00
|
|
|
// Configured for vagrant
|
|
|
|
realis.AddBasicAuth(&config, *username, *password)
|
2016-08-02 11:42:00 -07:00
|
|
|
r := realis.NewClient(config)
|
|
|
|
defer r.Close()
|
|
|
|
|
2016-08-26 16:35:31 -07:00
|
|
|
monitor := &realis.Monitor{r}
|
2016-08-24 15:38:44 -07:00
|
|
|
var job realis.Job
|
2016-08-02 11:42:00 -07:00
|
|
|
|
|
|
|
switch *executor {
|
|
|
|
case "thermos":
|
2016-09-29 20:45:24 -04:00
|
|
|
payload, err := ioutil.ReadFile("thermos_payload.json")
|
2016-08-02 11:42:00 -07:00
|
|
|
if err != nil {
|
2016-08-16 18:40:02 -07:00
|
|
|
fmt.Println("Error reading json config file: ", err)
|
2016-08-02 11:42:00 -07:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2016-08-09 13:30:26 -07:00
|
|
|
|
2016-08-02 13:06:36 -07:00
|
|
|
job = realis.NewJob().
|
|
|
|
Environment("prod").
|
2016-08-02 11:53:44 -07:00
|
|
|
Role("vagrant").
|
|
|
|
Name("hello_world_from_gorealis").
|
|
|
|
ExecutorName(aurora.AURORA_EXECUTOR_NAME).
|
|
|
|
ExecutorData(string(payload)).
|
2016-08-02 13:06:36 -07:00
|
|
|
CPU(1).
|
2016-08-09 13:30:26 -07:00
|
|
|
RAM(64).
|
2016-08-02 11:53:44 -07:00
|
|
|
Disk(100).
|
|
|
|
IsService(true).
|
|
|
|
InstanceCount(1).
|
2016-08-02 11:42:00 -07:00
|
|
|
AddPorts(1)
|
|
|
|
break
|
|
|
|
case "compose":
|
2016-08-02 13:06:36 -07:00
|
|
|
job = realis.NewJob().
|
|
|
|
Environment("prod").
|
2016-08-02 11:53:44 -07:00
|
|
|
Role("vagrant").
|
|
|
|
Name("docker-compose").
|
|
|
|
ExecutorName("docker-compose-executor").
|
|
|
|
ExecutorData("{}").
|
2016-08-02 13:06:36 -07:00
|
|
|
CPU(1).
|
2016-08-09 13:30:26 -07:00
|
|
|
RAM(64).
|
2016-08-02 11:53:44 -07:00
|
|
|
Disk(100).
|
|
|
|
IsService(false).
|
|
|
|
InstanceCount(1).
|
2016-08-02 11:42:00 -07:00
|
|
|
AddPorts(1).
|
2016-08-10 11:45:54 -07:00
|
|
|
AddLabel("fileName", "sample-app/docker-compose.yml").
|
2016-08-12 12:48:42 -07:00
|
|
|
AddURIs(true, true, "https://github.com/mesos/docker-compose-executor/releases/download/0.1.0/sample-app.tar.gz")
|
2016-08-02 11:42:00 -07:00
|
|
|
break
|
|
|
|
default:
|
|
|
|
fmt.Println("Only thermos and compose are supported for now")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch *cmd {
|
|
|
|
case "create":
|
|
|
|
fmt.Println("Creating job")
|
2016-08-25 18:56:55 -07:00
|
|
|
resp, err := r.CreateJob(job)
|
2016-08-02 11:42:00 -07:00
|
|
|
if err != nil {
|
2016-08-16 18:40:02 -07:00
|
|
|
fmt.Println(err)
|
2016-08-15 14:35:51 -07:00
|
|
|
os.Exit(1)
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
2016-08-25 18:56:55 -07:00
|
|
|
fmt.Println(resp.String())
|
2016-08-26 16:35:31 -07:00
|
|
|
|
2016-09-19 15:34:56 -04:00
|
|
|
if resp.ResponseCode == aurora.ResponseCode_OK {
|
|
|
|
if !monitor.Instances(job.JobKey(), job.GetInstanceCount(), 5, 50) {
|
2016-08-26 16:35:31 -07:00
|
|
|
_, err := r.KillJob(job.JobKey())
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-02 11:42:00 -07:00
|
|
|
break
|
|
|
|
case "kill":
|
|
|
|
fmt.Println("Killing job")
|
|
|
|
|
2016-08-25 18:56:55 -07:00
|
|
|
resp, err := r.KillJob(job.JobKey())
|
2016-08-02 11:42:00 -07:00
|
|
|
if err != nil {
|
2016-08-16 18:40:02 -07:00
|
|
|
fmt.Println(err)
|
2016-08-15 14:35:51 -07:00
|
|
|
os.Exit(1)
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
|
|
|
|
2016-09-19 15:34:56 -04:00
|
|
|
if resp.ResponseCode == aurora.ResponseCode_OK {
|
|
|
|
if !monitor.Instances(job.JobKey(), 0, 5, 50) {
|
2016-08-26 16:35:31 -07:00
|
|
|
fmt.Println("Unable to kill all instances of job")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2016-08-25 18:56:55 -07:00
|
|
|
fmt.Println(resp.String())
|
2016-08-02 11:42:00 -07:00
|
|
|
break
|
|
|
|
case "restart":
|
|
|
|
fmt.Println("Restarting job")
|
2016-08-25 18:56:55 -07:00
|
|
|
resp, err := r.RestartJob(job.JobKey())
|
2016-08-02 11:42:00 -07:00
|
|
|
if err != nil {
|
2016-08-16 18:40:02 -07:00
|
|
|
fmt.Println(err)
|
2016-08-15 14:35:51 -07:00
|
|
|
os.Exit(1)
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
|
|
|
|
2016-08-25 18:56:55 -07:00
|
|
|
fmt.Println(resp.String())
|
2016-08-02 11:42:00 -07:00
|
|
|
break
|
2016-08-26 16:35:31 -07:00
|
|
|
case "liveCount":
|
|
|
|
fmt.Println("Getting instance count")
|
|
|
|
|
|
|
|
live, err := r.GetInstanceIds(job.JobKey(), aurora.LIVE_STATES)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Number of live instances: ", len(live))
|
|
|
|
break
|
|
|
|
case "activeCount":
|
|
|
|
fmt.Println("Getting instance count")
|
|
|
|
|
|
|
|
live, err := r.GetInstanceIds(job.JobKey(), aurora.ACTIVE_STATES)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2016-08-26 16:39:56 -07:00
|
|
|
fmt.Println("Number of live instances: ", len(live))
|
2016-08-26 16:35:31 -07:00
|
|
|
break
|
2016-08-02 11:42:00 -07:00
|
|
|
case "flexUp":
|
|
|
|
fmt.Println("Flexing up job")
|
2016-08-26 16:35:31 -07:00
|
|
|
|
|
|
|
numOfInstances := int32(5)
|
|
|
|
resp, err := r.AddInstances(aurora.InstanceKey{job.JobKey(), 0}, numOfInstances)
|
2016-08-02 11:42:00 -07:00
|
|
|
if err != nil {
|
2016-08-16 18:40:02 -07:00
|
|
|
fmt.Println(err)
|
2016-08-15 14:35:51 -07:00
|
|
|
os.Exit(1)
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
2016-08-26 16:35:31 -07:00
|
|
|
|
2016-09-19 15:34:56 -04:00
|
|
|
if resp.ResponseCode == aurora.ResponseCode_OK {
|
|
|
|
if !monitor.Instances(job.JobKey(), job.GetInstanceCount()+numOfInstances, 5, 50) {
|
2016-08-26 16:35:31 -07:00
|
|
|
fmt.Println("Flexing up failed")
|
|
|
|
}
|
|
|
|
}
|
2016-08-25 18:56:55 -07:00
|
|
|
fmt.Println(resp.String())
|
2016-08-02 11:42:00 -07:00
|
|
|
break
|
|
|
|
case "update":
|
2016-08-25 18:56:55 -07:00
|
|
|
fmt.Println("Updating a job with with more RAM and to 3 instances")
|
2016-08-24 15:38:44 -07:00
|
|
|
taskConfig, err := r.FetchTaskConfig(aurora.InstanceKey{job.JobKey(), 0})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
updateJob := realis.NewUpdateJob(taskConfig)
|
2016-08-26 16:35:31 -07:00
|
|
|
updateJob.InstanceCount(5).RAM(128)
|
2016-08-02 11:42:00 -07:00
|
|
|
|
2016-08-25 18:56:55 -07:00
|
|
|
resp, err := r.StartJobUpdate(updateJob, "")
|
2016-08-02 11:42:00 -07:00
|
|
|
if err != nil {
|
2016-08-16 18:40:02 -07:00
|
|
|
fmt.Println(err)
|
2016-08-15 14:35:51 -07:00
|
|
|
os.Exit(1)
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
2016-08-25 18:56:55 -07:00
|
|
|
|
|
|
|
jobUpdateKey := response.JobUpdateKey(resp)
|
2016-08-26 16:35:31 -07:00
|
|
|
monitor.JobUpdate(*jobUpdateKey, 5, 100)
|
2016-08-25 18:56:55 -07:00
|
|
|
|
|
|
|
break
|
|
|
|
case "updateDetails":
|
2016-09-29 20:45:24 -04:00
|
|
|
resp, err := r.JobUpdateDetails(aurora.JobUpdateQuery{
|
|
|
|
Key: &aurora.JobUpdateKey{job.JobKey(), *updateId}, Limit: 1})
|
|
|
|
|
2016-08-25 18:56:55 -07:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2016-09-29 20:45:24 -04:00
|
|
|
fmt.Println(response.JobUpdateDetails(resp))
|
2016-08-02 11:42:00 -07:00
|
|
|
break
|
|
|
|
case "abortUpdate":
|
|
|
|
fmt.Println("Abort update")
|
2016-08-25 18:56:55 -07:00
|
|
|
resp, err := r.AbortJobUpdate(aurora.JobUpdateKey{job.JobKey(), *updateId}, "")
|
2016-08-02 11:42:00 -07:00
|
|
|
if err != nil {
|
2016-08-16 18:40:02 -07:00
|
|
|
fmt.Println(err)
|
2016-08-15 14:35:51 -07:00
|
|
|
os.Exit(1)
|
2016-08-02 11:42:00 -07:00
|
|
|
}
|
2016-08-25 18:56:55 -07:00
|
|
|
fmt.Println(resp.String())
|
2016-08-02 11:42:00 -07:00
|
|
|
break
|
2016-09-30 00:44:38 -04:00
|
|
|
case "rollbackUpdate":
|
|
|
|
fmt.Println("Abort update")
|
|
|
|
resp, err := r.RollbackJobUpdate(aurora.JobUpdateKey{job.JobKey(), *updateId}, "")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fmt.Println(resp.String())
|
|
|
|
break
|
2016-08-24 15:38:44 -07:00
|
|
|
case "taskConfig":
|
|
|
|
fmt.Println("Getting job info")
|
|
|
|
config, err := r.FetchTaskConfig(aurora.InstanceKey{job.JobKey(), 0})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
print(config.String())
|
|
|
|
break
|
2016-08-02 11:42:00 -07:00
|
|
|
default:
|
2016-08-25 18:56:55 -07:00
|
|
|
fmt.Println("Command not supported")
|
2016-08-02 11:42:00 -07:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|