def main():
    """
    The Fun Starts Here.
    """
    detect_debug_mode()

    # Shell out to the oddball storage script from external deps.
    # See the external_dependencies.yml for details.

    # By default, put the storage script in "Big Hammer" mode.
    os.environ["VERBOSE"] = "1"
    os.environ["FORCE"] = "1"
    os.environ["DRY"] = "0"

    # The new storage script does not consistently honor all of the names
    # dumped in the environment by previous iterations of the RightScripts
    # so we're going to have to do a dirty little translation.

    translated = {"STORAGE_MOUNTPOINT": "MOUNT_POINT"}
    for key, val in translated.items():
        if key in environ:
            log_and_stdout(
                "   *** Translating {}={} into {}={} for mount"
                " script ***   ".format(key, environ.get(key), val, environ.get(key))
            )
            os.environ[val] = os.environ.get(key)

    assert_command(
        "chmod +x ./setup.sh ./setup_aws.sh ./common.sh" " ./addons/bcache.sh",
        "Failed to mark storage scripts executable!",
        cwd="./lib/shell/storage-scripts",
    )

    assert_command("./setup.sh", "Storage/Volume setup failed!", cwd="./lib/shell/storage-scripts")
def set_hostname_w_fqdn():
    """
    Set the system's hostname from env vars.
    """
    for key, regex in {
            'SERVER_NAME': '^.+$',
            'INSTANCE_ID': '^.+$',
            'DEFAULT_DOMAIN': '^.+\.nextdoor\.com'
    }.items():
        utils.validate_env(key, regex)

    myhostname = normalize_hostname(
        os.environ['SERVER_NAME'],
        os.environ['INSTANCE_ID']
    )

    mydomain = normalize_domain(os.environ['DEFAULT_DOMAIN'])

    # Because RightScale appears to eat stdout. :\
    #    utils.assert_command("puppecho {}.{} > /etc/hostname".format(myhostname, mydomain), "Could not modify /etc/hostname!")
    utils.assert_command("augtool set /files/etc/hostname/hostname {}.{}".format(
        myhostname, mydomain), "Could not modify /etc/hostname!")
    utils.assert_command("hostname -F /etc/hostname",
                         "Failed when executing 'hostname' command!")
def install_dependencies():
    """
    Install some useful utilities.
    """
    utils.assert_command('apt-get install -y augeas-tools',
                         'Failed to install Augeas!')
def elb_connect():
    """
    Wire the node into ELB using RightInputs as parameters.
    """

    dmc = '^.+$'  # don't much care

    if 'ELB_NAME' in environ:
        # FIXME: What are the validations for these things?
        for key, validation in {
                'ELB_NAME': dmc,
                'AWS_ACCESS_KEY_ID': dmc,
                'AWS_SECRET_ACCESS_KEY': dmc,
                'EC2_PLACEMENT_AVAILABILITY_ZONE': dmc,
                'EC2_INSTANCE_ID': dmc,
                'HOME': dmc,
        }.items():
            utils.validate_env(key, validation)

        # Extract a temp copy of Kingpin into /tmp before doing the ELB reg.
        # This *should* be possible to do without extracting Kingpin from the
        # zip *EXCEPT* boto has problems referencing some templates within
        # a zip file which it does not have if the files live in the
        # filesystem.
        apt_get_update()
        assert_command('apt-get install -y unzip',
                       'Failed to install unzip utility!')
        assert_command('mkdir -p /tmp/kingpin',
                       'Failed to create temporary directorty for Kingping!')
        assert_command('unzip -o -u ./lib/python/kingpin.zip -d /tmp/kingpin',
                       'Failed to unzip temporary Kingpin instance!')

        # Note: this dereferences envvars and plugs them into the JSON.
        # Kingpin can handle this dereferencing as well butI do it here for \
        # transparency in that I also validate and print the resulting template
        # when in DEBUG mode.
        try:
            template_file = './lib/templates/elb-connect.json.template'
            with NamedTemporaryFile() as kp_script:

                log_and_stdout(
                    "Kingpin script temporary file is {}".format(
                        kp_script.name))

                kp_template = Template(
                    open(template_file, 'r').read()).safe_substitute(environ)

                log_and_stdout(
                    "Content of Kingpin script is:\n{}".format(kp_template))

                kp_script.write(kp_template.encode('utf-8'))
                kp_script.flush()
                kp_script.seek(0)

                log_and_stdout("Kingpin script created.".format(kp_template))

                environ['SKIP_DRY'] = "1"
                cmd = "python2.7 /tmp/kingpin --debug -j {}".format(
                    kp_script.name)
                assert_command(cmd, "Failed during Kingpin run!")

        except KeyError as e:
            log_and_stdout(str(e))
            sys.exit(-1)

        except IOError as e:
            log_and_stdout("Output: {}".format(e.strerror))
            log_and_stdout("Exit code: {}".format(e.errno))
            log_and_stdout("Failed when creating Kingpin script!")
            sys.exit(str(e.errno))

        assert_command('rm -rf /tmp/kingpin',
                       'Failed to remove temporary Kingpin instance!')

    else:
        log_and_stdout(
            '   *** No ELB_NAME specified and thus no ELB membership. This is not an error! ***   ')