Esempio n. 1
0
def user_deploy(instance_ip,
                username,
                instance_id,
                first_deploy=True,
                **runner_opts):
    """
    Use service.ansible to deploy to an instance.
    #NOTE: This method will _NOT_ work if you do not run instance deployment *FIRST*!
    # In order to add user-ssh keys to root, you will need root access to the VM that is *not* configured in this playbook.
    """
    playbooks_dir = settings.ANSIBLE_PLAYBOOKS_DIR
    playbooks_dir = os.path.join(playbooks_dir, 'user_deploy')

    #TODO: 'User-selectable 'SSH strategy' for instances?
    # Example 'user only' strategy:
    # user_keys = [k.pub_key for k in get_user_ssh_keys(username)]
    instance = Instance.objects.get(provider_alias=instance_id)
    scripts = instance.scripts.all()
    if not first_deploy:
        scripts = scripts.filter(run_every_deploy=True)

    # Example 'all members'  strategy:
    if not instance.project:
        raise Exception(
            "Expected this instance to have a project, found None: %s" %
            instance)
    group = instance.project.owner
    group_ssh_keys = SSHKey.keys_for_group(group)
    user_keys = [k.pub_key for k in group_ssh_keys]

    extra_vars = {
        "USERSSHKEYS":
        user_keys,
        "ASYNC_SCRIPTS": [{
            "name": s.get_title_slug(),
            "text": s.get_text()
        } for s in scripts.filter(wait_for_deploy=False)],
        "DEPLOY_SCRIPTS": [{
            "name": s.get_title_slug(),
            "text": s.get_text()
        } for s in scripts.filter(wait_for_deploy=True)],
    }
    playbook_runner = ansible_deployment(instance_ip,
                                         username,
                                         instance_id,
                                         playbooks_dir,
                                         extra_vars=extra_vars,
                                         raise_exception=False,
                                         **runner_opts)
    hostname = build_host_name(instance_id, instance_ip)
    if hostname not in playbook_runner.stats.failures:
        return playbook_runner
    # An error has occurred during deployment!
    # Handle specific errors from ansible based on the 'register' results.
    playbook_results = playbook_runner.results.get(hostname)
    _check_results_for_script_failure(playbook_results)
    # If the failure was not related to users boot-scripts,
    # handle as a generic ansible failure.
    return raise_playbook_errors(playbook_runner, instance_id, instance_ip,
                                 hostname)
Esempio n. 2
0
def user_deploy(instance_ip, username, instance_id, first_deploy=True):
    """
    Use service.ansible to deploy to an instance.
    #NOTE: This method will _NOT_ work if you do not run instance deployment *FIRST*!
    # In order to add user-ssh keys to root, you will need root access to the VM that is *not* configured in this playbook.
    """
    playbooks_dir = settings.ANSIBLE_PLAYBOOKS_DIR
    playbooks_dir = os.path.join(playbooks_dir, 'user_deploy')

    #TODO: 'User-selectable 'SSH strategy' for instances?
    # Example 'user only' strategy:
    # user_keys = [k.pub_key for k in get_user_ssh_keys(username)]
    instance = Instance.objects.get(provider_alias=instance_id)
    image_scripts = instance.source.providermachine.application_version.boot_scripts.all(
    )
    instance_scripts = instance.scripts.all()
    scripts = image_scripts.union(instance_scripts)

    # Example 'all members'  strategy:
    if not instance.project:
        raise Exception(
            "Expected this instance to have a project, found None: %s" %
            instance)
    group = instance.project.owner
    group_ssh_keys = SSHKey.keys_for_group(group)
    user_keys = [k.pub_key for k in group_ssh_keys]

    async_scripts, deploy_scripts = [], []
    for script in scripts:
        if not (first_deploy or script.run_every_deploy):
            continue
        if script.wait_for_deploy:
            deploy_scripts.append(script)
        else:
            async_scripts.append(script)

    format_script = lambda s: {
        "name": s.get_title_slug(),
        "text": s.get_text()
    }
    extra_vars = {
        "USERSSHKEYS": user_keys,
        "ASYNC_SCRIPTS": map(format_script, async_scripts),
        "DEPLOY_SCRIPTS": map(format_script, deploy_scripts)
    }
    playbook_results = ansible_deployment(instance_ip,
                                          username,
                                          instance_id,
                                          playbooks_dir,
                                          extra_vars=extra_vars,
                                          raise_exception=False)
    hostname = build_host_name(instance_id, instance_ip)
    # An error has occurred during deployment!
    # If the failure was not related to users boot-scripts,
    # handle as a generic ansible failure.
    return raise_playbook_errors(playbook_results, instance_id, instance_ip,
                                 hostname)
Esempio n. 3
0
def user_deploy(instance_ip, username, instance_id, first_deploy=True, **runner_opts):
    """
    Use service.ansible to deploy to an instance.
    #NOTE: This method will _NOT_ work if you do not run instance deployment *FIRST*!
    # In order to add user-ssh keys to root, you will need root access to the VM that is *not* configured in this playbook.
    """
    playbooks_dir = settings.ANSIBLE_PLAYBOOKS_DIR
    playbooks_dir = os.path.join(playbooks_dir, 'user_deploy')

    #TODO: 'User-selectable 'SSH strategy' for instances?
    # Example 'user only' strategy:
    # user_keys = [k.pub_key for k in get_user_ssh_keys(username)]
    instance = Instance.objects.get(provider_alias=instance_id)
    image_scripts = instance.source.providermachine.application_version.boot_scripts.all()
    instance_scripts = instance.scripts.all()
    scripts = image_scripts.union(instance_scripts)

    # Example 'all members'  strategy:
    if not instance.project:
        raise Exception("Expected this instance to have a project, found None: %s" % instance)
    group = instance.project.owner
    group_ssh_keys = SSHKey.keys_for_group(group)
    user_keys = [k.pub_key for k in group_ssh_keys]

    async_scripts, deploy_scripts = [], []
    for script in scripts:
        if not (first_deploy or script.run_every_deploy):
            continue
        if script.wait_for_deploy:
            deploy_scripts.append(script)
        else:
            async_scripts.append(script)

    format_script = lambda s: {"name": s.get_title_slug(), "text": s.get_text()}
    extra_vars = {
        "USERSSHKEYS": user_keys,
        "ASYNC_SCRIPTS":  map(format_script, async_scripts),
        "DEPLOY_SCRIPTS":  map(format_script, deploy_scripts)
    }
    playbook_runner = ansible_deployment(
        instance_ip, username, instance_id, playbooks_dir,
        extra_vars=extra_vars, raise_exception=False, **runner_opts)
    hostname = build_host_name(instance_id, instance_ip)
    if hostname not in playbook_runner.stats.failures:
        return playbook_runner
    # An error has occurred during deployment!
    # Handle specific errors from ansible based on the 'register' results.
    playbook_results = playbook_runner.results.get(hostname)
    _check_results_for_script_failure(playbook_results)
    # If the failure was not related to users boot-scripts,
    # handle as a generic ansible failure.
    return raise_playbook_errors(playbook_runner, instance_id, instance_ip, hostname)
Esempio n. 4
0
def user_deploy(instance_ip, username, instance_id, **runner_opts):
    """
    Use service.ansible to deploy to an instance.
    #NOTE: This method will _NOT_ work if you do not run instance deployment *FIRST*!
    # In order to add user-ssh keys to root, you will need root access to the VM that is *not* configured in this playbook.
    """
    playbooks_dir = settings.ANSIBLE_PLAYBOOKS_DIR
    playbooks_dir = os.path.join(playbooks_dir, 'user_deploy')

    #TODO: 'User-selectable 'SSH strategy' for instances?
    # Example 'user only' strategy:
    # user_keys = [k.pub_key for k in get_user_ssh_keys(username)]
    instance = Instance.objects.get(provider_alias=instance_id)
    scripts = instance.scripts.all(
    )  # TODO: determine if script should be run by passing in a 'first_deploy=True/False'

    # Example 'all members'  strategy:
    if not instance.project:
        raise Exception(
            "Expected this instance to have a project, found None: %s" %
            instance)
    group = instance.project.owner
    group_ssh_keys = SSHKey.keys_for_group(group)
    user_keys = [k.pub_key for k in group_ssh_keys]

    extra_vars = {
        "USERSSHKEYS":
        user_keys,
        "SCRIPTS": [{
            "name": s.get_title_slug(),
            "text": s.get_text()
        } for s in scripts]
    }
    return ansible_deployment(instance_ip,
                              username,
                              instance_id,
                              playbooks_dir,
                              extra_vars=extra_vars,
                              **runner_opts)