Example #1
0
def gen_groups():

    groups = \
        """
        webservers:
          shiny: 1
          nginx: 1
          other: 2

        databases:
          sql: 1
          other: 5

        generic:
            other: 5
            windows:
                xp: 5
                seven: 5
                other: 5

        computing:
          other: 7
        """

    return read_groups(yaml.safe_load(groups), dict(), None)
Example #2
0
def test_gen_inventory():
    yaml_dict = yaml.safe_load(config)
    groups = read_groups(yaml_dict, dict(), None)

    inventory = gen_inventory(groups)

    assert inventory  == \
"""[databases-neo4j-mongo-001]
Example #3
0
def test_duplicate_group_name():
    config = \
    """
    webservers:
      shiny: 1
      nginx: 1
      apache: 1

    databases:
        apache: 1
    """

    yaml_dict = yaml.safe_load(config)

    with pytest.raises(ValueError):
        groups = read_groups(yaml_dict, dict(), None)
Example #4
0
def test_use_of_other_simple():
    config = \
    """
    webservers:
      shiny: 1
      nginx: 1
      other: 1
    """

    yaml_dict = yaml.safe_load(config)

    groups = read_groups(yaml_dict, dict(), None)

    assert groups["webservers"].servers == 0
    assert groups["webservers-other"].servers == 1
    assert groups["shiny"].servers == 1
    assert groups["nginx"].servers == 1
    assert len(groups["webservers"].children) == 3
Example #5
0
def test_use_of_other_complex():
    config = \
        """
        webservers:
          shiny: 1
          nginx: 1
          other: 2

        databases:
          sql: 1
          other: 5

        generic:
            other: 5
            windows:
                xp: 5
                seven: 5
                other: 5

        computing:
          other: 7
        """

    yaml_dict = yaml.safe_load(config)

    groups = read_groups(yaml_dict, dict(), None)

    assert groups["webservers"].servers == 0
    assert groups["webservers-other"].servers == 2
    assert groups["databases"].servers == 0
    assert groups["databases-other"].servers == 5
    assert groups["computing"].servers == 0
    assert groups["computing-other"].servers == 7
    assert groups["windows"].servers == 0
    assert groups["windows-other"].servers == 5
    assert groups["generic"].servers == 0
    assert groups["generic-other"].servers == 5
Example #6
0
def test_valid_number_of_servers_in_child_groups():
    config = \
    """
    webservers:
      shiny: 1
      nginx: 1
      apache: 1
    """

    yaml_dict = yaml.safe_load(config)

    groups = read_groups(yaml_dict, dict(), None)

    assert "webservers" in groups
    assert "shiny" in groups
    assert "nginx" in groups
    assert "apache" in groups
    assert groups["shiny"].parent == groups["webservers"]
    assert groups["nginx"].parent == groups["webservers"]
    assert groups["apache"].parent == groups["webservers"]
    assert groups["webservers"].isRoot()
    assert not groups["shiny"].isRoot()
    assert not groups["nginx"].isRoot()
    assert not groups["apache"].isRoot()
    assert not groups["webservers"].isLeaf()
    assert groups["shiny"].isLeaf()
    assert groups["nginx"].isLeaf()
    assert groups["apache"].isLeaf()
    assert groups["shiny"].servers == 1
    assert groups["nginx"].servers == 1
    assert groups["apache"].servers == 1
    assert groups["webservers"].servers == 0
    assert len(groups["shiny"].children) == 0
    assert len(groups["nginx"].children) == 0
    assert len(groups["apache"].children) == 0
    assert len(groups["webservers"].children) == 3
Example #7
0
def genesis(orchestra, instruments, stage, username):
    """
    Transform a bare set of requirements into an
    orchestra of servers ready to perform for you.

    This is a complementary tool to ansible for cloud
    orchestration. It generates ansible scripts that
    help you automate the deployment of servers and
    software on the cloud.

    Specifically, this program creates:

    (1) - a static inventory file to be used
    together with a dynamic inventory file. This allows
    you to run playbooks for groups of servers within
    the fast-changing cloud environment. This is not
    possible using a dynamic inventory file by itself,
    like, for instance, 'openstack.py'.

    (2) - playbooks for each group, populated with roles
    optionally specified in '--instruments'. If no roles
    are provided,

    ORCHESTRA is a yaml file which lists the names
    and number of servers of each group and their children.

    INSTRUMENTS is a yaml file which lists the roles and
    variables of each group of servers.

    STAGE is the name of the cloud provider, one of:
    {'openstack'}

    More details can be found in README.md.
    """

    # Yaml parse error is thrown for us if not formatted correctly
    yaml_groups_dict = yaml.safe_load(orchestra)

    # Read and spit out inventory
    groups = read_groups(yaml_groups_dict)

    # Generate inventory
    with open('inventory/hosts', 'w') as inventory_file:
        inventory = gen_inventory(groups)
        inventory_file.write(inventory)

    # Parse roles contents
    if instruments:
        yaml_roles_dict = yaml.safe_load(instruments)
        groups = read_roles(yaml_roles_dict, groups)

    # Write variables and generate individual playbooks
    for group in groups.values():
        # Create folder in group_vars/GROUP_NAME
        directory = "group_vars/{}".format(group.name)
        if not os.path.exists(directory):
            os.makedirs(directory)
        # It's alright to generate variables for parent groups even though these are not used directly
        write_variables(group, username)
        # Non-leaf groups import playbooks of children
        with open('playbooks/group/{}.yml'.format(group.name),
                  'w') as playbook_file:
            playbook = gen_individual_playbook(group, username)
            playbook_file.write(playbook)

    # Playbook for running all individual playbooks
    with open('playbooks/intermezzo.yml', 'w') as intermezzo_file:
        intermezzo_file.write(gen_all_groups_playbook(groups))

    # Create all instances
    with open('playbooks/concerto.yml', 'w') as concerto_file:
        concerto_file.write(gen_concerto(groups, stage, username))

    # PRINT
    # Success! The following files were generated:
    # Run ... to do this and do that

    return groups
import yaml
import pytest
from maestro.input import read_roles, read_groups
from maestro.playbooks import gen_concerto, gen_individual_playbook, gen_all_groups_playbook


orchestra = \
"""
databases:
  sql: 1
  mongo: 1

computing: 7
"""

groups = read_groups(yaml.safe_load(orchestra))

instruments = \
"""
databases:
  create_server:
    image: cirros
    external_network: public
    flavor: m1.nano

sql:
  create_server:
    image: cirros
    flavor: m1.medium
    username: l337
  docker: