@@ -5,9 +5,11 @@ import (
55 "fmt"
66 "math"
77
8+ "github.com/coldbrewcloud/coldbrew-cli/aws"
89 "github.com/coldbrewcloud/coldbrew-cli/aws/ecs"
910 "github.com/coldbrewcloud/coldbrew-cli/console"
1011 "github.com/coldbrewcloud/coldbrew-cli/core"
12+ "github.com/coldbrewcloud/coldbrew-cli/utils"
1113 "github.com/coldbrewcloud/coldbrew-cli/utils/conv"
1214)
1315
@@ -31,7 +33,30 @@ func (c *Command) updateECSTaskDefinition(dockerImageFullURI string) (string, er
3133 return "" , err
3234 }
3335 memory /= 1000 * 1000
34- useCloudWatchLogs := false
36+
37+ // logging
38+ loggingDriver := conv .S (c .conf .Logging .Driver )
39+ if c .conf .Logging .Options == nil {
40+ c .conf .Logging .Options = make (map [string ]string )
41+ }
42+ switch loggingDriver {
43+ case aws .ECSTaskDefinitionLogDriverAWSLogs :
44+ // test if group needs to be created
45+ awsLogsGroupName , ok := c .conf .Logging .Options ["awslogs-group" ]
46+ if ! ok || utils .IsBlank (awsLogsGroupName ) {
47+ awsLogsGroupName = core .DefaultCloudWatchLogsGroupName (conv .S (c .conf .Name ), conv .S (c .conf .ClusterName ))
48+ c .conf .Logging .Options ["awslogs-group" ] = awsLogsGroupName
49+ }
50+ if err := c .PrepareCloudWatchLogsGroup (awsLogsGroupName ); err != nil {
51+ return "" , err
52+ }
53+
54+ // assign region if not provided
55+ awsLogsRegionName , ok := c .conf .Logging .Options ["awslogs-region" ]
56+ if ! ok || utils .IsBlank (awsLogsRegionName ) {
57+ c .conf .Logging .Options ["awslogs-region" ] = conv .S (c .globalFlags .AWSRegion )
58+ }
59+ }
3560
3661 console .UpdatingResource ("Updating ECS Task Definition" , ecsTaskDefinitionName , false )
3762 ecsTaskDef , err := c .awsClient .ECS ().UpdateTaskDefinition (
@@ -42,7 +67,7 @@ func (c *Command) updateECSTaskDefinition(dockerImageFullURI string) (string, er
4267 memory ,
4368 c .conf .Env ,
4469 portMappings ,
45- useCloudWatchLogs )
70+ loggingDriver , c . conf . Logging . Options )
4671 if err != nil {
4772 return "" , fmt .Errorf ("Failed to update ECS Task Definition [%s]: %s" , ecsTaskDefinitionName , err .Error ())
4873 }
@@ -136,3 +161,25 @@ func (c *Command) updateECSService(ecsClusterName, ecsServiceName, ecsTaskDefini
136161
137162 return nil
138163}
164+
165+ func (c * Command ) PrepareCloudWatchLogsGroup (groupName string ) error {
166+ groups , err := c .awsClient .CloudWatchLogs ().ListGroups (groupName )
167+ if err != nil {
168+ return fmt .Errorf ("Failed to list CloudWatch Logs Group [%s]: %s" , groupName , err .Error ())
169+ }
170+
171+ for _ , group := range groups {
172+ if conv .S (group .LogGroupName ) == groupName {
173+ // log group exists; return with no error
174+ return nil
175+ }
176+ }
177+
178+ // log group does not exist; create a new group
179+ console .AddingResource ("Creating CloudWatch Logs Group" , groupName , false )
180+ if err := c .awsClient .CloudWatchLogs ().CreateGroup (groupName ); err != nil {
181+ return fmt .Errorf ("Failed to create CloudWatch Logs Group [%s]: %s" , groupName , err .Error ())
182+ }
183+
184+ return nil
185+ }
0 commit comments