forked from kohey18/kangol
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_task.go
73 lines (61 loc) · 1.86 KB
/
run_task.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws"
"github.com/recruit-mp/kangol/awscloudwatchlogs"
"github.com/recruit-mp/kangol/awsecs"
"github.com/recruit-mp/kangol/task"
)
func runTask(conf, tag string, command string, cpu int64, memory int64) {
deployment, taskDefinition, err := task.ReadConfig(conf, appendTags(tag))
if err != nil {
log.Fatal(err.Error())
}
service := deployment.Service
cluster := deployment.Cluster
oldRevision, err := awsecs.GetOldRevision(service, cluster)
if err != nil {
log.Fatal(err.Error())
}
log.Info("Now Revision is ... ", oldRevision)
revision := ""
if conf != "" {
newRevision, err := awsecs.RegisterTaskDefinition(taskDefinition)
if err != nil {
log.Fatal(err.Error())
}
revision = newRevision
} else {
revision = oldRevision
}
log.Info("Running Revision is ... ", revision)
commands := []*string{}
for _, v := range strings.Split(command, " ") {
commands = append(commands, aws.String(v))
}
taskArn, runTaskError := awsecs.RunOneShotTask(cluster, revision, commands, cpu, memory)
if taskArn != "" {
log.Info("Show CloudWatch Logs")
for _, v := range taskDefinition.ContainerDefinitions {
logGroup := v.LogConfiguration.Options["awslogs-group"]
logPrefix := v.LogConfiguration.Options["awslogs-stream-prefix"]
taskID := strings.Split(taskArn, "/")[1]
logStream := fmt.Sprintf("%s/%s/%s", *logPrefix, service, taskID)
log.Info("logGroup ... ", *logGroup)
log.Info("logStream ... ", logStream)
events, err := awscloudwatchlogs.GetLogEvents(*logGroup, logStream)
if err != nil {
log.Warn("Failed Get Log Events -> ", err.Error())
}
for _, v := range events {
log.Info(v)
}
}
}
if runTaskError != nil {
log.Fatal("RunTask Error -> ", runTaskError.Error())
}
log.Info("Task Exit Successfully -> ", taskArn)
}