<commit_before># this program simply creates the configuration file needed by the agent
# it is assumed that all of the directories have already been created with
# the proper permissions

import argparse
import os
import shutil
import subprocess
import sys
import platform
import textwrap
import urlparse
from dcm.agent import config, cloudmetadata
import ConfigParser

import dcm.agent


# below are the variables with no defaults that must be determined
cloud_choice = None
platform_cloice = None


platform_choices = ["ubuntu", "el", "debian", "suse", "centos", "rhel",
                    "fedora_core"]
g_user_env_str = "DCM_USER"
g_basedir_env_str = "DCM_BASEDIR"

cloud_choices = {
    1: cloudmetadata.CLOUD_TYPES.Amazon,
    2: cloudmetadata.CLOUD_TYPES.Atmos,
    3: cloudmetadata.CLOUD_TYPES.ATT,
    4: cloudmetadata.CLOUD_TYPES.Azure,
    5: cloudmetadata.CLOUD_TYPES.Bluelock,
    6: cloudmetadata.CLOUD_TYPES.CloudCentral,
    7: cloudmetadata.CLOUD_TYPES.CloudSigma,
    8: cloudmetadata.CLOUD_TYPES.CloudStack,
    9: cloudmetadata.CLOUD_TYPES.CloudStack3,
    10: cloudmetadata.CLOUD_TYPES.Eucalyptus,
    11: cloudmetadata.CLOUD_TYPES.GoGrid,
    12: cloudmetadata.CLOUD_TYPES.Google,
    13: cloudmetadata.CLOUD_TYPES.IBM,
    14: cloudmetadata.CLOUD_TYPES.Joyent,
    15: cloudmetadata.CLOUD_TYPES.OpenStack,
    16: cloudmetadata.CLOUD_TYPES.Rackspace,
    17: cloudmetadata.CLOUD_TYPES.ServerExpress,
    18: cloudmetadata.CLOUD_TYPES.Terremark,
    19: cloudmetadata.CLOUD_TYPES.VMware,
    20: cloudmetadata.CLOUD_TYPES.Other
}


def setup_command_line_parser():
    parser = argparse.ArgumentParser(
        description='DCM Agent Installer for linux.')

    parser.add_argument("--cloud", "-c", metavar="{Amazon, etc...}",
                        dest="cloud",
                        help="The cloud where this virtual machine will be "
                             "run.  Options: %s" % ", ".join(
                                 cloud_choices.values()))
                        # choices=cloud_choices.values())
    parser.add_argument("--url", "-u", dest="url",
                        help="The location of the dcm web socket listener")

    parser.add_argument("--verbose", "-v", dest="verbose",
                        action='store_true',
                        default=False,
                        help="Increase the amount of output produced by the "
                             "script."),

    parser.add_argument("--initial", "-I", dest="initial",
                        action='store_true',
                        default=False,
                        help=argparse.SUPPRESS),

    parser.add_argument("--interactive", "-i", dest="interactive",
                        action='store_true',
                        default=False,
                        help="Run an interactive session where questions "
                             "will be asked and answered vi stdio."),

    parser.add_argument("--base-path", "-p",
                        dest="base_path",
                        help="The path to enstratius")

    parser.add_argument("--mount-point", "-m",
                        dest="mount_path",
                        help="The path to mount point")

    parser.add_argument("--on-boot", "-B",
                        dest="on_boot",
                        action='store_true',
                        default=False,
                        help="Setup the agent to start when the VM boots")

    parser.add_argument("--services-path", "-s",
                        dest="services_path",
                        help="The services path")

    parser.add_argument("--reload-conf", "-r",
                        dest="reload",
                        help="The previous config file that will be used "
                             "to populate defaults.")

    parser.add_argument("--temp-path", "-t",
                        dest="temp_path",
                        help="The temp path")

    parser.add_argument("--platform", "-P",
                        dest="platform",
                        help="The platform where this is being installed. "
                             "It is recommended that you only set this option "
                             "if this script fails to determine it for you.",
                        choices=platform_choices)

    parser.add_argument("--user", "-U",
                        dest="user",
                        help="The system user that will run the agent.")

    parser.add_argument("--connection-type", "-C",
                        dest="con_type",
                        help="The type of connection that will be formed "
                             "with the agent manager.")

    parser.add_argument("--logfile", "-l",
                        dest="logfile")
    return parser


def run_command(cmd):
    try:
        process = subprocess.Popen(
            cmd,
            shell=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            bufsize=0,
            env={})
        stdout, stderr = process.communicate()
        rc = process.returncode
    except Exception as ex:
        rc = 1
        stdout = None
        stderr = ex.message
    return rc, stdout, stderr


def identify_platform(opts):

    if not opts.initial and platform.system().lower() != "linux":
        raise Exception("This agent can only be used on Linux platform.")

    lsb = "/usr/bin/lsb_release"
    if os.path.exists(lsb) and os.access(lsb, os.X_OK):
        rc, stdout, stderr = run_command(" ".join([lsb, "-i"]))
        if rc == 0 and stdout:
            parts = stdout.split()
            if len(parts) == 3:
                cand = parts[2].strip()
                if cand == "Ubuntu":
                    return "ubuntu"
                elif cand == "CentOS":
                    return "centos"
                elif cand == "RedHatEnterpriseServer":
                    return "rhel"
                elif cand == "SUSE LINUX":
                    return "suse"
                elif cand == "n/a":
                    return "el"

    if os.path.exists("/etc/redhat-release"):
        with open("/etc/redhat-release") as fptr:
            redhat_info = fptr.readline().split()[0]
            if redhat_info == "CentOS":
                return "centos"
            elif redhat_info == "Red":
                return "rhel"
    if os.path.exists("/etc/debian_version"):
        return "debian"
    if os.path.exists("/etc/SuSE-release"):
        return "suse"
    if os.path.exists("/etc/system-release"):
        with open("/etc/system-release") as fptr:
            line = fptr.readline().strip().lower()
            if line.find("amazon linux ami") >= 0:
                return "el"

    if opts.initial:
        return None
    raise Exception("The platform could not be determined")


def _get_input(prompt):
    return raw_input(prompt)


def select_cloud(default=cloudmetadata.CLOUD_TYPES.Amazon):
    """
    :param default:
    :return:
    """
    for c in sorted(cloud_choices.keys()):
        col = "%2d) %-13s" % (c, cloud_choices[c])
        print col

    cloud = None
    while cloud is None:
        input_str = _get_input("Select your cloud (%s): " % default)
        input_str = input_str.strip()
        if not input_str:
            input_str = default
        if input_str.lower() in [i.lower() for i in cloud_choices.values()]:
            return input_str
        try:
            ndx = int(input_str)
            cloud = cloud_choices[ndx]
        except:
            print "%s is not a valid choice." % input_str
    return cloud


def normalize_cloud_name(conf_d):
    (h, cloud) = conf_d["cloud"]["type"]
    name = cloudmetadata.normalize_cloud_name(cloud)
    if name is None:
        raise Exception("Cloud %s is not a known type." % cloud)
    conf_d["cloud"]["type"] = (h, name)


def pick_meta_data(conf_d):
    (_, cloud) = conf_d["cloud"]["type"]
    if cloud == cloudmetadata.CLOUD_TYPES.Amazon:
        mu = "http://169.254.169.254/latest/meta-data/"
    elif cloud == cloudmetadata.CLOUD_TYPES.Eucalyptus:
        mu = "http://169.254.169.254/1.0/meta-data/"
    elif cloud == cloudmetadata.CLOUD_TYPES.OpenStack:
        mu = "http://169.254.169.254/openstack/2012-08-10/meta_data.json"
    elif cloud == cloudmetadata.CLOUD_TYPES.Google:
        mu = "http://metadata.google.internal/computeMetadata/v1"
    elif cloud == cloudmetadata.CLOUD_TYPES.CloudStack:
        mu = "lastest/instance-id"
    elif cloud == cloudmetadata.CLOUD_TYPES.CloudStack3:
        mu = "latest/local-hostname"
    else:
        return None

    (h, _) = conf_d["cloud"]["metadata_url"]
    conf_d["cloud"]["metadata_url"] = (h, mu)


def get_default_conf_dict():
    conf_dict = {}
    option_list = config.build_options_list()

    for c in option_list:<commit_msg>CloudStack has null metadata in conf.<commit_after># this program simply creates the configuration file needed by the agent
# it is assumed that all of the directories have already been created with
# the proper permissions

import argparse
import os
import shutil
import subprocess
import sys
import platform
import textwrap
import urlparse
from dcm.agent import config, cloudmetadata
import ConfigParser

import dcm.agent


# below are the variables with no defaults that must be determined
cloud_choice = None
platform_cloice = None


platform_choices = ["ubuntu", "el", "debian", "suse", "centos", "rhel",
                    "fedora_core"]
g_user_env_str = "DCM_USER"
g_basedir_env_str = "DCM_BASEDIR"

cloud_choices = {
    1: cloudmetadata.CLOUD_TYPES.Amazon,
    2: cloudmetadata.CLOUD_TYPES.Atmos,
    3: cloudmetadata.CLOUD_TYPES.ATT,
    4: cloudmetadata.CLOUD_TYPES.Azure,
    5: cloudmetadata.CLOUD_TYPES.Bluelock,
    6: cloudmetadata.CLOUD_TYPES.CloudCentral,
    7: cloudmetadata.CLOUD_TYPES.CloudSigma,
    8: cloudmetadata.CLOUD_TYPES.CloudStack,
    9: cloudmetadata.CLOUD_TYPES.CloudStack3,
    10: cloudmetadata.CLOUD_TYPES.Eucalyptus,
    11: cloudmetadata.CLOUD_TYPES.GoGrid,
    12: cloudmetadata.CLOUD_TYPES.Google,
    13: cloudmetadata.CLOUD_TYPES.IBM,
    14: cloudmetadata.CLOUD_TYPES.Joyent,
    15: cloudmetadata.CLOUD_TYPES.OpenStack,
    16: cloudmetadata.CLOUD_TYPES.Rackspace,
    17: cloudmetadata.CLOUD_TYPES.ServerExpress,
    18: cloudmetadata.CLOUD_TYPES.Terremark,
    19: cloudmetadata.CLOUD_TYPES.VMware,
    20: cloudmetadata.CLOUD_TYPES.Other
}


def setup_command_line_parser():
    parser = argparse.ArgumentParser(
        description='DCM Agent Installer for linux.')

    parser.add_argument("--cloud", "-c", metavar="{Amazon, etc...}",
                        dest="cloud",
                        help="The cloud where this virtual machine will be "
                             "run.  Options: %s" % ", ".join(
                                 cloud_choices.values()))
                        # choices=cloud_choices.values())
    parser.add_argument("--url", "-u", dest="url",
                        help="The location of the dcm web socket listener")

    parser.add_argument("--verbose", "-v", dest="verbose",
                        action='store_true',
                        default=False,
                        help="Increase the amount of output produced by the "
                             "script."),

    parser.add_argument("--initial", "-I", dest="initial",
                        action='store_true',
                        default=False,
                        help=argparse.SUPPRESS),

    parser.add_argument("--interactive", "-i", dest="interactive",
                        action='store_true',
                        default=False,
                        help="Run an interactive session where questions "
                             "will be asked and answered vi stdio."),

    parser.add_argument("--base-path", "-p",
                        dest="base_path",
                        help="The path to enstratius")

    parser.add_argument("--mount-point", "-m",
                        dest="mount_path",
                        help="The path to mount point")

    parser.add_argument("--on-boot", "-B",
                        dest="on_boot",
                        action='store_true',
                        default=False,
                        help="Setup the agent to start when the VM boots")

    parser.add_argument("--services-path", "-s",
                        dest="services_path",
                        help="The services path")

    parser.add_argument("--reload-conf", "-r",
                        dest="reload",
                        help="The previous config file that will be used "
                             "to populate defaults.")

    parser.add_argument("--temp-path", "-t",
                        dest="temp_path",
                        help="The temp path")

    parser.add_argument("--platform", "-P",
                        dest="platform",
                        help="The platform where this is being installed. "
                             "It is recommended that you only set this option "
                             "if this script fails to determine it for you.",
                        choices=platform_choices)

    parser.add_argument("--user", "-U",
                        dest="user",
                        help="The system user that will run the agent.")

    parser.add_argument("--connection-type", "-C",
                        dest="con_type",
                        help="The type of connection that will be formed "
                             "with the agent manager.")

    parser.add_argument("--logfile", "-l",
                        dest="logfile")
    return parser


def run_command(cmd):
    try:
        process = subprocess.Popen(
            cmd,
            shell=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            bufsize=0,
            env={})
        stdout, stderr = process.communicate()
        rc = process.returncode
    except Exception as ex:
        rc = 1
        stdout = None
        stderr = ex.message
    return rc, stdout, stderr


def identify_platform(opts):

    if not opts.initial and platform.system().lower() != "linux":
        raise Exception("This agent can only be used on Linux platform.")

    lsb = "/usr/bin/lsb_release"
    if os.path.exists(lsb) and os.access(lsb, os.X_OK):
        rc, stdout, stderr = run_command(" ".join([lsb, "-i"]))
        if rc == 0 and stdout:
            parts = stdout.split()
            if len(parts) == 3:
                cand = parts[2].strip()
                if cand == "Ubuntu":
                    return "ubuntu"
                elif cand == "CentOS":
                    return "centos"
                elif cand == "RedHatEnterpriseServer":
                    return "rhel"
                elif cand == "SUSE LINUX":
                    return "suse"
                elif cand == "n/a":
                    return "el"

    if os.path.exists("/etc/redhat-release"):
        with open("/etc/redhat-release") as fptr:
            redhat_info = fptr.readline().split()[0]
            if redhat_info == "CentOS":
                return "centos"
            elif redhat_info == "Red":
                return "rhel"
    if os.path.exists("/etc/debian_version"):
        return "debian"
    if os.path.exists("/etc/SuSE-release"):
        return "suse"
    if os.path.exists("/etc/system-release"):
        with open("/etc/system-release") as fptr:
            line = fptr.readline().strip().lower()
            if line.find("amazon linux ami") >= 0:
                return "el"

    if opts.initial:
        return None
    raise Exception("The platform could not be determined")


def _get_input(prompt):
    return raw_input(prompt)


def select_cloud(default=cloudmetadata.CLOUD_TYPES.Amazon):
    """
    :param default:
    :return:
    """
    for c in sorted(cloud_choices.keys()):
        col = "%2d) %-13s" % (c, cloud_choices[c])
        print col

    cloud = None
    while cloud is None:
        input_str = _get_input("Select your cloud (%s): " % default)
        input_str = input_str.strip()
        if not input_str:
            input_str = default
        if input_str.lower() in [i.lower() for i in cloud_choices.values()]:
            return input_str
        try:
            ndx = int(input_str)
            cloud = cloud_choices[ndx]
        except:
            print "%s is not a valid choice." % input_str
    return cloud


def normalize_cloud_name(conf_d):
    (h, cloud) = conf_d["cloud"]["type"]
    name = cloudmetadata.normalize_cloud_name(cloud)
    if name is None:
        raise Exception("Cloud %s is not a known type." % cloud)
    conf_d["cloud"]["type"] = (h, name)


def pick_meta_data(conf_d):
    (_, cloud) = conf_d["cloud"]["type"]
    if cloud == cloudmetadata.CLOUD_TYPES.Amazon:
        mu = "http://169.254.169.254/latest/meta-data/"
    elif cloud == cloudmetadata.CLOUD_TYPES.Eucalyptus:
        mu = "http://169.254.169.254/1.0/meta-data/"
    elif cloud == cloudmetadata.CLOUD_TYPES.OpenStack:
        mu = "http://169.254.169.254/openstack/2012-08-10/meta_data.json"
    elif cloud == cloudmetadata.CLOUD_TYPES.Google:
        mu = "http://metadata.google.internal/computeMetadata/v1"
    elif cloud == cloudmetadata.CLOUD_TYPES.CloudStack:
        mu = None
    elif cloud == cloudmetadata.CLOUD_TYPES.CloudStack3:
        mu = None
    else:
        return None

    (h, _) = conf_d["cloud"]["metadata_url"]
    conf_d["cloud"]["metadata_url"] = (h, mu)


def get_default_conf_dict():
    conf_dict = {}
    option_list = config.build_options_list()

    for c in option_list:
{'fraction_non_alphanumeric': 0.11468253968253968, 'fraction_numerical': 0.01020408163265306, 'mean_word_length': 1.8651940880298847, 'pattern_counts': {'":': 16, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6487628', 'n_tokens_mistral': 5210, 'n_tokens_neox': 5032, 'n_words': 1210}