<commit_before>/*
Copyright 2015 All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
	"fmt"
	"os"
	"time"

	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/golang/glog"
)

var (
	// the aws client
	awsCli *awsClient
)

//
// Steps:
//  - grab the command line configuration
//  - retrieve the instance identity document from metadata service
//  - find the instances in the auto-scaling group
//  - create a etcd client from the instance and see if we can connect the cluster
//  - write out the environment file
//  - if in proxy mode we can exit here
//  - check if the instance id exists in the cluster and if not, try to add us
//  - list the members in the cluster and find the instance status, if terminated, try and remove
//

func main() {
	if err := getConfig(); err != nil {
		printUsage(fmt.Sprintf("invalid configuration, error: %s", err))
	}
	glog.Infof("starting %s version: %s, author: %s <%s>", program, version, author, email)

	// step: retrieve this instances identity
	identity, err := getInstanceIdentity()
	if err != nil {
		glog.Errorf("failed to get the instance identity, error: %s", err)
		os.Exit(1)
	}

	// step: create a aws client
	awsCli, err = newAwsClient(identity.Region)
	if err != nil {
		glog.Errorf("failed to create a aws client, error: %s", err)
		os.Exit(1)
	}

	// step: get a list of instances in the scaling group
	instances, err := getAutoScalingMembers(identity)
	if err != nil {
		glog.Errorf("failed to retrieve a list of instance from auto-scaling group, error: %s", err)
		os.Exit(1)
	}

	cluster_state := "new"

	// step: create an etcd client for us
	client, err := newEtcdClient(getEtcdEndpoints(instances))
	if err != nil {
		glog.Warningf("failed to create an etcd client, error: %s", err)
	} else {
		found, err := client.hasMember(identity.InstanceID)
		if err != nil {
			glog.Warningf("failed to check if we're in the cluster, error: %s", err)
		}
		if found {
			cluster_state = "existing"
		}
	}

	if config.proxyMode {
		cluster_state = "existing"<commit_msg>- fixing the logic for preexisting clusters<commit_after>/*
Copyright 2015 All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
	"fmt"
	"os"
	"time"

	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/golang/glog"
)

var (
	// the aws client
	awsCli *awsClient
)

//
// Steps:
//  - grab the command line configuration
//  - retrieve the instance identity document from metadata service
//  - find the instances in the auto-scaling group
//  - create a etcd client from the instance and see if we can connect the cluster
//  - write out the environment file
//  - if in proxy mode we can exit here
//  - check if the instance id exists in the cluster and if not, try to add us
//  - list the members in the cluster and find the instance status, if terminated, try and remove
//

func main() {
	if err := getConfig(); err != nil {
		printUsage(fmt.Sprintf("invalid configuration, error: %s", err))
	}
	glog.Infof("starting %s version: %s, author: %s <%s>", program, version, author, email)

	// step: retrieve this instances identity
	identity, err := getInstanceIdentity()
	if err != nil {
		glog.Errorf("failed to get the instance identity, error: %s", err)
		os.Exit(1)
	}

	// step: create a aws client
	awsCli, err = newAwsClient(identity.Region)
	if err != nil {
		glog.Errorf("failed to create a aws client, error: %s", err)
		os.Exit(1)
	}

	// step: get a list of instances in the scaling group
	instances, err := getAutoScalingMembers(identity)
	if err != nil {
		glog.Errorf("failed to retrieve a list of instance from auto-scaling group, error: %s", err)
		os.Exit(1)
	}

	cluster_state := "new"

	// step: create an etcd client for us and check if we can connect to cluster
	if _, err := newEtcdClient(getEtcdEndpoints(instances)); err == nil {
		cluster_state = "existing"
	}

	// step: if in proxy mode, its always existing
	if config.proxyMode {
		cluster_state = "existing"
{'fraction_non_alphanumeric': 0.10248133951987089, 'fraction_numerical': 0.004841638087552955, 'mean_word_length': 3.5949953660797034, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '427315', 'n_tokens_mistral': 1571, 'n_tokens_neox': 1489, 'n_words': 639}