<commit_before>package ec2

//-----------------------------------------------------------------------------
// Package factored import statement:
//-----------------------------------------------------------------------------

import (

	// Stdlib:
	"os/exec"
	"strconv"
	"strings"

	// Community:
	log "github.com/Sirupsen/logrus"
	"github.com/katosys/kato/pkg/tools"
)

//-----------------------------------------------------------------------------
// func: Add
//-----------------------------------------------------------------------------

// Add a new instance to the cluster.
func (d *Data) Add() {

	// Set current command:
	d.command = "add"

	// Load state from state file:
	if err := d.loadState(); err != nil {
		log.WithField("cmd", "ec2:"+d.command).Fatal(err)
	}

	// Discover CoreOS AMI (for standalone runs):
	if d.AmiID == "" {
		d.retrieveCoreosAmiID(nil)
	}

	// Security group IDs:
	var securityGroupIDs []string
	for _, role := range strings.Split(d.Roles, ",") {
		switch role {
		case "quorum":
			securityGroupIDs = append(securityGroupIDs, d.QuorumSecGrp)
		case "master":
			securityGroupIDs = append(securityGroupIDs, d.MasterSecGrp)
		case "worker":
			securityGroupIDs = append(securityGroupIDs, d.WorkerSecGrp)
		case "border":
			securityGroupIDs = append(securityGroupIDs, d.BorderSecGrp)
		}
	}

	// Udata arguments bundle:
	argsUdata := []string{"udata",
		"--roles", d.Roles,
		"--cluster-id", d.ClusterID,
		"--cluster-state", d.ClusterState,
		"--quorum-count", strconv.Itoa(d.QuorumCount),
		"--master-count", strconv.Itoa(d.MasterCount),
		"--host-name", d.HostName,
		"--host-id", d.HostID,
		"--domain", d.Domain,
		"--ec2-region", d.Region,
		"--ns1-api-key", d.Ns1ApiKey,
		"--etcd-token", d.EtcdToken,
		"--calico-ip-pool", d.CalicoIPPool,
		"--rexray-storage-driver", "ebs",
		"--iaas-provider", "ec2",
		"--prometheus",
		"--gzip-udata",
	}

	// Append the --sysdig-access-key if present:
	if d.SysdigAccessKey != "" {
		argsUdata = append(argsUdata, "--sysdig-access-key", d.SysdigAccessKey)
	}

	// Append the --datadog-api-key if present:
	if d.DatadogAPIKey != "" {
		argsUdata = append(argsUdata, "--datadog-api-key", d.DatadogAPIKey)
	}

	// Append the --slack-webhook if present:
	if d.SlackWebhook != "" {
		argsUdata = append(argsUdata, "--slack-webhook", d.SlackWebhook)
	}

	// Append the --ca-cert-path flag if present:
	if d.CaCertPath != "" {
		argsUdata = append(argsUdata, "--ca-cert-path", d.CaCertPath)
	}

	// Append --stub-zone flags if present:
	for _, z := range d.StubZones {
		argsUdata = append(argsUdata, "--stub-zone", z)
	}

	// Append --smtp-url flags if present:
	if d.SMTPURL != "" {
		argsUdata = append(argsUdata, "--smtp-url", d.SMTPURL)
	}

	// Append --admin-email flags if present:
	if d.AdminEmail != "" {
		argsUdata = append(argsUdata, "--admin-email", d.AdminEmail)
	}

	// Ec2 run arguments bundle:
	argsRun := []string{"ec2", "run",
		"--tag-name", d.HostName + "-" + d.HostID + "." + d.Domain,
		"--region", d.Region,
		"--zone", d.Zone,
		"--ami-id", d.AmiID,
		"--instance-type", d.InstanceType,
		"--key-pair", d.KeyPair,
		"--subnet-id", d.ExtSubnetID,
		"--security-group-ids", strings.Join(securityGroupIDs, ","),
		"--iam-role", "kato",
		"--source-dest-check", "false",
		"--public-ip", "true",
	}<commit_msg>New function securityGroupIDs()<commit_after>package ec2

//-----------------------------------------------------------------------------
// Package factored import statement:
//-----------------------------------------------------------------------------

import (

	// Stdlib:
	"os/exec"
	"strconv"
	"strings"

	// Community:
	log "github.com/Sirupsen/logrus"
	"github.com/katosys/kato/pkg/tools"
)

//-----------------------------------------------------------------------------
// func: securityGroupIDs
//-----------------------------------------------------------------------------

func (d *Data) securityGroupIDs(roles string) (list []string) {

	for _, role := range strings.Split(roles, ",") {
		switch role {
		case "quorum":
			list = append(list, d.QuorumSecGrp)
		case "master":
			list = append(list, d.MasterSecGrp)
		case "worker":
			list = append(list, d.WorkerSecGrp)
		case "border":
			list = append(list, d.BorderSecGrp)
		}
	}

	return
}

//-----------------------------------------------------------------------------
// func: Add
//-----------------------------------------------------------------------------

// Add a new instance to the cluster.
func (d *Data) Add() {

	// Set current command:
	d.command = "add"

	// Load state from state file:
	if err := d.loadState(); err != nil {
		log.WithField("cmd", "ec2:"+d.command).Fatal(err)
	}

	// Discover CoreOS AMI (for standalone runs):
	if d.AmiID == "" {
		d.retrieveCoreosAmiID(nil)
	}

	// Udata arguments bundle:
	argsUdata := []string{"udata",
		"--roles", d.Roles,
		"--cluster-id", d.ClusterID,
		"--cluster-state", d.ClusterState,
		"--quorum-count", strconv.Itoa(d.QuorumCount),
		"--master-count", strconv.Itoa(d.MasterCount),
		"--host-name", d.HostName,
		"--host-id", d.HostID,
		"--domain", d.Domain,
		"--ec2-region", d.Region,
		"--ns1-api-key", d.Ns1ApiKey,
		"--etcd-token", d.EtcdToken,
		"--calico-ip-pool", d.CalicoIPPool,
		"--rexray-storage-driver", "ebs",
		"--iaas-provider", "ec2",
		"--prometheus",
		"--gzip-udata",
	}

	// Append the --sysdig-access-key if present:
	if d.SysdigAccessKey != "" {
		argsUdata = append(argsUdata, "--sysdig-access-key", d.SysdigAccessKey)
	}

	// Append the --datadog-api-key if present:
	if d.DatadogAPIKey != "" {
		argsUdata = append(argsUdata, "--datadog-api-key", d.DatadogAPIKey)
	}

	// Append the --slack-webhook if present:
	if d.SlackWebhook != "" {
		argsUdata = append(argsUdata, "--slack-webhook", d.SlackWebhook)
	}

	// Append the --ca-cert-path flag if present:
	if d.CaCertPath != "" {
		argsUdata = append(argsUdata, "--ca-cert-path", d.CaCertPath)
	}

	// Append --stub-zone flags if present:
	for _, z := range d.StubZones {
		argsUdata = append(argsUdata, "--stub-zone", z)
	}

	// Append --smtp-url flags if present:
	if d.SMTPURL != "" {
		argsUdata = append(argsUdata, "--smtp-url", d.SMTPURL)
	}

	// Append --admin-email flags if present:
	if d.AdminEmail != "" {
		argsUdata = append(argsUdata, "--admin-email", d.AdminEmail)
	}

	// Ec2 run arguments bundle:
	argsRun := []string{"ec2", "run",
		"--tag-name", d.HostName + "-" + d.HostID + "." + d.Domain,
		"--region", d.Region,
		"--zone", d.Zone,
		"--ami-id", d.AmiID,
		"--instance-type", d.InstanceType,
		"--key-pair", d.KeyPair,
		"--subnet-id", d.ExtSubnetID,
		"--security-group-ids", strings.Join(d.securityGroupIDs(d.Roles), ","),
		"--iam-role", "kato",
		"--source-dest-check", "false",
		"--public-ip", "true",
	}
{'fraction_non_alphanumeric': 0.292437472250999, 'fraction_numerical': 0.0023679147550688174, 'mean_word_length': 2.0386690647482015, 'pattern_counts': {'":': 8, '<': 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': '4695257', 'n_tokens_mistral': 2545, 'n_tokens_neox': 2355, 'n_words': 470}