<commit_before>package aws

import (
	logger "log"
	"strings"

	"github.com/pchico83/i2kit/cli/providers/aws/cf"
	"github.com/pchico83/i2kit/cli/providers/aws/elb"
	"github.com/pchico83/i2kit/cli/schemas/environment"
	"github.com/pchico83/i2kit/cli/schemas/service"
)

//Deploy deploys a AWS Cloud Formation stack
func Deploy(s *service.Service, e *environment.Environment, log *logger.Logger) error {
	consumed := 0
	stackName := s.GetFullName(e, "-")
	config := e.Provider.GetConfig()
	stack, err := cf.Get(stackName, config)
	if err != nil {
		return err
	}
	if stack != nil && (*stack.StackStatus == "ROLLBACK_COMPLETE" || strings.HasSuffix(*stack.StackStatus, "_FAILED")) {
		if err = Destroy(s, e, log); err != nil {
			return err
		}
		log.Printf("Destroying previous stack '%s' in '%s' state...", stackName, *stack.StackStatus)
		stack = nil
	}

	var stackID string
	var template string
	if stack == nil {
		template, err = Translate(s, e, config)
		if err != nil {
			return err
		}<commit_msg>Better handling CF states (#86)<commit_after>package aws

import (
	"fmt"
	logger "log"

	"github.com/pchico83/i2kit/cli/providers/aws/cf"
	"github.com/pchico83/i2kit/cli/providers/aws/elb"
	"github.com/pchico83/i2kit/cli/schemas/environment"
	"github.com/pchico83/i2kit/cli/schemas/service"
)

//Deploy deploys a AWS Cloud Formation stack
func Deploy(s *service.Service, e *environment.Environment, log *logger.Logger) error {
	consumed := 0
	stackName := s.GetFullName(e, "-")
	config := e.Provider.GetConfig()
	stack, err := cf.Get(stackName, config)
	if err != nil {
		return err
	}
	if stack != nil && (*stack.StackStatus == "CREATE_FAILED" || *stack.StackStatus == "DELETE_FAILED" || *stack.StackStatus == "ROLLBACK_COMPLETE" || *stack.StackStatus == "ROLLBACK_FAILED") {
		if err = Destroy(s, e, log); err != nil {
			return err
		}
		log.Printf("Destroying previous stack '%s' in '%s' state...", stackName, *stack.StackStatus)
		stack = nil
	}
	if stack != nil && *stack.StackStatus == "UPDATE_ROLLBACK_FAILED" {
		log.Printf("Stack %s is in 'UPDATE_ROLLBACK_FAILED' state. Fix the stack in the AWS console or destroy this service to unlock it.", stackName)
		return fmt.Errorf("'UPDATE_ROLLBACK_FAILED' state")
	}

	var stackID string
	var template string
	if stack == nil {
		template, err = Translate(s, e, config)
		if err != nil {
			return err
		}
{'fraction_non_alphanumeric': 0.14678510998307953, 'fraction_numerical': 0.011844331641285956, 'mean_word_length': 4.141304347826087, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3486713', 'n_tokens_mistral': 899, 'n_tokens_neox': 859, 'n_words': 209}