def test_non_existing_group_name(): config = \ """ webservers: databases: jerome: """ yaml_dict = yaml.safe_load(config) with pytest.raises(ValueError): read_roles(yaml_dict, gen_groups())
def test_bad_definition_of_role(): config = \ """ webservers: databases: windows: 3 """ yaml_dict = yaml.safe_load(config) with pytest.raises(ValueError): read_roles(yaml_dict, gen_groups())
def test_bad_definition_of_role_using_list_level_1(): config = \ """ webservers: databases: windows: - docker - derp """ yaml_dict = yaml.safe_load(config) with pytest.raises(ValueError): read_roles(yaml_dict, gen_groups())
def test_list_role_inheritance(): config = \ """ databases: docker: username: JoaoPinto all: docker: username: JorgeJesus webservers: docker: username: LuisFigo """ yaml_dict = yaml.safe_load(config) new_groups = read_roles(yaml_dict, gen_groups()) assert new_groups["webservers"].get_role( "docker").variables["username"] == "LuisFigo" assert new_groups["shiny"].get_role( "docker").variables["username"] == "LuisFigo" assert new_groups["nginx"].get_role( "docker").variables["username"] == "LuisFigo" assert new_groups["webservers-other"].get_role( "docker").variables["username"] == "LuisFigo" assert new_groups["databases"].get_role( "docker").variables["username"] == "JoaoPinto" assert new_groups["sql"].get_role( "docker").variables["username"] == "JoaoPinto" assert new_groups["databases-other"].get_role( "docker").variables["username"] == "JoaoPinto" assert new_groups["generic"].get_role( "docker").variables["username"] == "JorgeJesus" assert new_groups["windows"].get_role( "docker").variables["username"] == "JorgeJesus" assert new_groups["seven"].get_role( "docker").variables["username"] == "JorgeJesus" assert new_groups["computing"].get_role( "docker").variables["username"] == "JorgeJesus" assert new_groups["computing-other"].get_role( "docker").variables["username"] == "JorgeJesus"
def test_use_of_all(): config = \ """ webservers: databases: all: docker: username: JorgeJesus """ yaml_dict = yaml.safe_load(config) new_groups = read_roles(yaml_dict, gen_groups()) for groupname in new_groups.keys(): assert len(new_groups[groupname].roles) == 1 assert new_groups[groupname].has_role("docker") assert new_groups[groupname].get_role("docker").variables == { "username": "******" }
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
def test_list_of_roles(): config = \ """ webservers: databases: docker: var1: 1 var2: "2" mysql: var1: "A" var2: "B" mongo: windows: docker: var1: 1 var2: "2" office: var1: "A" var2: "B" samba: computing: spark: sperk: spirk: spork: spurk: """ yaml_dict = yaml.safe_load(config) boosted_groups = read_roles(yaml_dict, gen_groups()) assert len(boosted_groups["generic"].roles) == 0 assert len(boosted_groups["webservers"].roles) == 0 assert len(boosted_groups["databases"].roles) == 3 assert len(boosted_groups["windows"].roles) == 3 assert len(boosted_groups["computing"].roles) == 5 assert (sorted(map(lambda x: x.name, boosted_groups["computing"].roles)) == [ "spark", "sperk", "spirk", "spork", "spurk" ]) assert (sorted(map( lambda x: x.name, boosted_groups["databases"].roles)) == ["docker", "mongo", "mysql"]) varnames = ['var1', 'var2'] assert boosted_groups["databases"].get_role( "docker").variables.keys() == varnames assert boosted_groups["databases"].get_role( "mysql").variables.keys() == varnames assert boosted_groups["databases"].get_role("mongo").variables == None assert boosted_groups["windows"].get_role( "docker").variables.keys() == varnames assert boosted_groups["windows"].get_role( "office").variables.keys() == varnames assert boosted_groups["windows"].get_role("samba").variables == None
external_network: public flavor: m1.nano sql: create_server: image: cirros flavor: m1.medium username: l337 docker: computing: docker: username: JorgeJesus """ groups = read_roles(yaml.safe_load(instruments), groups) expected_databases_playbook = \ """- import_playbook: mongo.yml - import_playbook: sql.yml """ expected_sql_playbook = \ """- hosts: sql gather_facts: yes remote_user: l337 tasks: - name: Execute role \'docker\'