github.com/kolanos/fargate@v0.2.3/cmd/service_update.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/jpignata/fargate/console"
     7  	ECS "github.com/jpignata/fargate/ecs"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  type ServiceUpdateOperation struct {
    12  	ServiceName string
    13  	Cpu         string
    14  	Memory      string
    15  	Service     ECS.Service
    16  }
    17  
    18  func (o *ServiceUpdateOperation) Validate() {
    19  	ecs := ECS.New(sess, clusterName)
    20  
    21  	if o.Cpu == "" && o.Memory == "" {
    22  		console.ErrorExit(fmt.Errorf("--cpu and/or --memory must be supplied"), "Invalid command line arguments")
    23  	}
    24  
    25  	o.Service = ecs.DescribeService(o.ServiceName)
    26  	cpu, memory := ecs.GetCpuAndMemoryFromTaskDefinition(o.Service.TaskDefinitionArn)
    27  
    28  	if o.Cpu == "" {
    29  		o.Cpu = cpu
    30  	}
    31  
    32  	if o.Memory == "" {
    33  		o.Memory = memory
    34  	}
    35  
    36  	err := validateCpuAndMemory(o.Cpu, o.Memory)
    37  
    38  	if err != nil {
    39  		console.ErrorExit(err, "Invalid settings: %s CPU units / %s MiB", o.Cpu, o.Memory)
    40  	}
    41  }
    42  
    43  var (
    44  	flagServiceUpdateCpu    string
    45  	flagServiceUpdateMemory string
    46  )
    47  
    48  var serviceUpdateCmd = &cobra.Command{
    49  	Use:   "update <service-name> --cpu <cpu-units> | --memory <MiB>",
    50  	Short: "Update service configuration",
    51  	Long: `Update service configuration
    52  
    53  CPU and memory settings are specified as CPU units and mebibytes respectively
    54  using the --cpu and --memory flags. Every 1024 CPU units is equivilent to a
    55  single vCPU. AWS Fargate only supports certain combinations of CPU and memory
    56  configurations:
    57  
    58  | CPU (CPU Units) | Memory (MiB)                          |
    59  | --------------- | ------------------------------------- |
    60  | 256             | 512, 1024, or 2048                    |
    61  | 512             | 1024 through 4096 in 1GiB increments  |
    62  | 1024            | 2048 through 8192 in 1GiB increments  |
    63  | 2048            | 4096 through 16384 in 1GiB increments |
    64  | 4096            | 8192 through 30720 in 1GiB increments |
    65  
    66  At least one of --cpu or --memory must be specified.`,
    67  	Args: cobra.ExactArgs(1),
    68  	Run: func(cmd *cobra.Command, args []string) {
    69  		operation := &ServiceUpdateOperation{
    70  			ServiceName: args[0],
    71  			Cpu:         flagServiceUpdateCpu,
    72  			Memory:      flagServiceUpdateMemory,
    73  		}
    74  
    75  		operation.Validate()
    76  
    77  		updateService(operation)
    78  	},
    79  }
    80  
    81  func init() {
    82  	serviceCmd.AddCommand(serviceUpdateCmd)
    83  
    84  	serviceUpdateCmd.Flags().StringVarP(&flagServiceUpdateCpu, "cpu", "c", "", "Amount of cpu units to allocate for each task")
    85  	serviceUpdateCmd.Flags().StringVarP(&flagServiceUpdateMemory, "memory", "m", "", "Amount of MiB to allocate for each task")
    86  }
    87  
    88  func updateService(operation *ServiceUpdateOperation) {
    89  	ecs := ECS.New(sess, clusterName)
    90  
    91  	newTaskDefinitionArn := ecs.UpdateTaskDefinitionCpuAndMemory(
    92  		operation.Service.TaskDefinitionArn,
    93  		operation.Cpu,
    94  		operation.Memory,
    95  	)
    96  
    97  	ecs.UpdateServiceTaskDefinition(operation.ServiceName, newTaskDefinitionArn)
    98  	console.Info("Updated service %s to %s CPU units / %s MiB", operation.ServiceName, operation.Cpu, operation.Memory)
    99  }