Ejemplo n.º 1
0
func doCluster(c *cli.Context) {

	ecsManager, err := buildECSManager()

	if err != nil {
		fmt.Fprintf(os.Stderr, "[ERROR]%s\n", color.Red(err.Error()))
		os.Exit(1)
	}

	operation, errSubCommand := createOperation(c.Args())

	if errSubCommand != nil {
		fmt.Fprintf(os.Stderr, "[ERROR]%s\n", color.Red(errSubCommand.Error()))
		os.Exit(1)
	}

	projectDir, err := os.Getwd()
	if err != nil {
		panic(err)
	}

	clusterController := cluster.ClusterControler{
		Ecs:            ecsManager,
		TargetResource: operation.TargetResource,
	}

	plans := createClusterPlans(&clusterController, projectDir)

	if operation.SubCommand == "apply" {
		clusterController.ApplyClusterPlans(plans)
	}
}
Ejemplo n.º 2
0
func createClusterPlans(controller *cluster.ClusterControler, projectDir string) []*plan.ClusterUpdatePlan {

	clusters := controller.SearchClusters(projectDir)
	plans := controller.CreateClusterUpdatePlans(clusters)

	for _, plan := range plans {
		fmt.Printf("Cluster '%s'\n", plan.Name)

		fmt.Println(color.Cyan(fmt.Sprintf("\t[Add] num = %d", len(plan.NewServices))))
		for _, add := range plan.NewServices {
			fmt.Println(color.Cyan(fmt.Sprintf("\t\t (+) %s", add.Name)))
		}

		fmt.Println(color.Green(fmt.Sprintf("\t[Update] num = %d", len(plan.UpdateServices))))
		for _, update := range plan.UpdateServices {
			fmt.Println(color.Green(fmt.Sprintf("\t\t (+) %s(%s)", *update.Before.ServiceName, *update.Before.ClusterARN)))
		}

		fmt.Println(color.Red(fmt.Sprintf("\t[Remove] num = %d", len(plan.DeleteServices))))
		for _, delete := range plan.DeleteServices {
			fmt.Println(color.Red(fmt.Sprintf("\t\t (-) %s(%s)", *delete.ServiceName, *delete.ClusterARN)))
		}
		fmt.Println()
	}

	return plans
}
Ejemplo n.º 3
0
func doTask(c *cli.Context) {

	ecsManager, err := buildECSManager()

	if err != nil {
		logger.Main.Error(color.Red(err.Error()))
		os.Exit(1)
	}

	operation, errSubCommand := createOperation(c.Args())

	if errSubCommand != nil {
		logger.Main.Error(color.Red(errSubCommand.Error()))
		os.Exit(1)
	}

	projectDir, err := os.Getwd()
	if err != nil {
		logger.Main.Error(color.Red(err.Error()))
		os.Exit(1)
	}

	taskController, err := task.NewTaskDefinitionController(ecsManager, projectDir, operation.TargetResource)
	if err != nil {
		logger.Main.Error(color.Red(err.Error()))
		os.Exit(1)
	}

	plans := createTaskPlans(taskController, projectDir)

	if operation.SubCommand == "apply" {
		results, errapp := taskController.ApplyTaskDefinitionPlans(plans)

		if errapp != nil {
			logger.Main.Error(color.Red(errapp.Error()))
			os.Exit(1)
		}

		for _, output := range results {
			logger.Main.Infof("Registered Task Definition '%s'", *output.TaskDefinition.Family)
			logger.Main.Info(color.Cyan(util.StringValueWithIndent(output.TaskDefinition, 1)))
		}
	}
}
Ejemplo n.º 4
0
func (self *ServiceController) ApplyServicePlans(plans []*plan.ServiceUpdatePlan) {

	logger.Main.Info("Start apply serivces...")

	for _, plan := range plans {
		if err := self.ApplyServicePlan(plan); err != nil {
			logger.Main.Error(color.Red(err.Error()))
			os.Exit(1)
		}
	}
}
Ejemplo n.º 5
0
func (self *ServiceController) RoundColorStatus(status string) *color.Escape {

	if status == "RUNNING" {
		return color.Green(status)
	} else if status == "PENDING" {
		return color.Yellow(status)
	} else if status == "STOPPED" {
		return color.Red(status)
	} else {
		return color.Magenta(status)
	}
}
Ejemplo n.º 6
0
func doBluegreen(c *cli.Context) {

	ecsManager, err := buildECSManager()

	if err != nil {
		logger.Main.Error(color.Red(err.Error()))
		os.Exit(1)
	}

	operation, errSubCommand := createOperation(c.Args())

	if errSubCommand != nil {
		logger.Main.Error(color.Red(errSubCommand.Error()))
		os.Exit(1)
	}

	projectDir, err := os.Getwd()
	if err != nil {
		logger.Main.Error(color.Red(err.Error()))
		os.Exit(1)
	}

	bgController, errbgc := bluegreen.NewBlueGreenController(ecsManager, projectDir, operation.TargetResource)
	if errbgc != nil {
		logger.Main.Error(color.Red(errbgc.Error()))
		os.Exit(1)
	}

	jsonOutput := c.Bool("json-output")
	bgPlans, err := createBlueGreenPlans(bgController, jsonOutput)

	if err != nil {
		logger.Main.Error(color.Red(err.Error()))
		os.Exit(1)
	}

	// cluster check

	if operation.SubCommand == "apply" {

		nodeploy := c.Bool("nodeploy")

		if len(bgPlans) > 0 {
			errbg := bgController.ApplyBlueGreenDeploys(bgPlans, nodeploy)
			if errbg != nil {
				logger.Main.Error(color.Red(errbg.Error()))
				os.Exit(1)
			}
		} else {
			logger.Main.Infof("Not found Blue Green Definition")

			if len(operation.TargetResource) > 0 && !nodeploy {
				logger.Main.Infof("Try to update service '%s'", operation.TargetResource)
				doService(c)
			}

		}
	}
}
Ejemplo n.º 7
0
func doTask(c *cli.Context) {

	ecsManager, err := buildECSManager()

	if err != nil {
		fmt.Fprintf(os.Stderr, "[ERROR]%s\n", color.Red(err.Error()))
		os.Exit(1)
	}

	operation, errSubCommand := createOperation(c.Args())

	if errSubCommand != nil {
		fmt.Fprintf(os.Stderr, "[ERROR]%s\n", color.Red(errSubCommand.Error()))
		os.Exit(1)
	}

	projectDir, err := os.Getwd()
	if err != nil {
		panic(err)
	}

	// plan
	taskController := &task.TaskDefinitionController{
		Ecs:            ecsManager,
		TargetResource: operation.TargetResource,
	}

	plans := createTaskPlans(taskController, projectDir)

	if operation.SubCommand == "apply" {
		results := taskController.ApplyTaskDefinitionPlans(plans)

		for _, output := range results {
			fmt.Printf("Registered Task Definition '%s'", *output.TaskDefinition.Family)
			fmt.Print(color.Cyan(util.StringValueWithIndent(output.TaskDefinition, 1)))
		}
	}
}
Ejemplo n.º 8
0
func doService(c *cli.Context) {

	ecsManager, err := buildECSManager()

	if err != nil {
		logger.Main.Error(color.Red(err.Error()))
		os.Exit(1)
	}

	operation, errSubCommand := createOperation(c.Args())

	if errSubCommand != nil {
		logger.Main.Error(color.Red(errSubCommand.Error()))
		os.Exit(1)
	}

	projectDir, err := os.Getwd()
	if err != nil {
		logger.Main.Error(color.Red(err.Error()))
		os.Exit(1)
	}

	jsonOutput := c.Bool("json-output")
	clusterController, err := service.NewServiceController(ecsManager, projectDir, operation.TargetResource)

	plans, err := createClusterPlans(clusterController, projectDir, jsonOutput)

	if err != nil {
		logger.Main.Error(color.Red(err.Error()))
		os.Exit(1)
	}

	if operation.SubCommand == "apply" {
		clusterController.ApplyServicePlans(plans)
	}
}
Ejemplo n.º 9
0
func (self *ClusterControler) CreateClusterUpdatePlan(cluster schema.Cluster) *plan.ClusterUpdatePlan {
	output, err := self.Ecs.DescribeClusters([]*string{&cluster.Name})

	if err != nil {
		fmt.Fprintln(os.Stderr, color.Red("[ERROR] discribe_cluster"))
		os.Exit(1)
	}

	if len(output.Failures) > 0 {
		fmt.Fprintln(os.Stderr, color.Red(fmt.Sprintf("[ERROR] Cluster '%s' not found", cluster.Name)))
		os.Exit(1)
	}

	target := output.Clusters[0]

	if *target.Status != "ACTIVE" {
		fmt.Fprintln(os.Stderr, color.Red(fmt.Sprintf("[ERROR] Cluster '%s' is not ACTIVE.", cluster.Name)))
		os.Exit(1)
	}

	resListServices, _ := self.Ecs.ListServices(cluster.Name)

	resDescribeService, _ := self.Ecs.DescribeService(cluster.Name, resListServices.ServiceARNs)

	currentServices := map[string]*ecs.Service{}
	for _, service := range resDescribeService.Services {
		currentServices[*service.ServiceName] = service
	}

	deleteServices := map[string]*ecs.Service{}
	updateServices := map[string]*plan.UpdateService{}
	for name, currentService := range currentServices {

		if newService, ok := cluster.Services[name]; ok {
			// update
			updateServices[name] = &plan.UpdateService{
				Before: currentService,
				After:  &newService,
			}
		} else {
			// delete
			deleteServices[name] = currentService
		}
	}

	newServices := map[string]*schema.Service{}
	for name, newService := range cluster.Services {

		if _, ok := currentServices[name]; !ok {
			// create
			newServices[name] = &newService
		}
	}

	return &plan.ClusterUpdatePlan{
		Name:            cluster.Name,
		CurrentServices: currentServices,
		DeleteServices:  deleteServices,
		UpdateServices:  updateServices,
		NewServices:     newServices,
	}
}