Commit 8ffe85ab by MichaelRoytman Committed by GitHub

Merge pull request #3309 from edx/michael/fix-script

Michael/fix script
parents 598db995 74c439b1
...@@ -81,8 +81,8 @@ def _map_roles_to_roles(graph, dirs, git_dir, key, type_1, type_2): ...@@ -81,8 +81,8 @@ def _map_roles_to_roles(graph, dirs, git_dir, key, type_1, type_2):
# add node for type_2, typically dependent role # add node for type_2, typically dependent role
node_2 = Node(name, type_2) node_2 = Node(name, type_2)
# add edge, typically role - dependent role # add edge, typically dependent role - role
graph.add_edge(node_1, node_2) graph.add_edge(node_2, node_1)
def _map_plays_to_roles(graph, dirs, git_dir, key, type_1, type_2): def _map_plays_to_roles(graph, dirs, git_dir, key, type_1, type_2):
""" """
...@@ -130,8 +130,8 @@ def _map_plays_to_roles(graph, dirs, git_dir, key, type_1, type_2): ...@@ -130,8 +130,8 @@ def _map_plays_to_roles(graph, dirs, git_dir, key, type_1, type_2):
# add node for type_2, typically for role # add node for type_2, typically for role
node_2 = Node(name, type_2) node_2 = Node(name, type_2)
# add edge, typically playbook - role it uses # add edge, typically role - playbook that uses it
graph.add_edge(node_1, node_2) graph.add_edge(node_2, node_1)
def _open_yaml_file(file_str): def _open_yaml_file(file_str):
""" """
...@@ -178,7 +178,20 @@ def change_set_to_roles(files, git_dir, roles_dirs, playbooks_dirs, graph): ...@@ -178,7 +178,20 @@ def change_set_to_roles(files, git_dir, roles_dirs, playbooks_dirs, graph):
# if the change set file is in the set of role files # if the change set file is in the set of role files
if file_path in candidate_files: if file_path in candidate_files:
# get name of role and add it to set of roles of the change set # get name of role and add it to set of roles of the change set
items.add(_get_resource_name(file_path, "roles")) items.add(_get_role_name_from_file(file_path))
return items
def get_plays(files, git_dir, playbooks_dirs):
"""
Determines which files in the change set are aws playbooks
files: A list of files modified by a commit range.
git_dir: A path to the top-most directory in the local git repository tool is to be run in.
playbook_dirs: A list of relative paths to directories in which Ansible playbooks reside.
"""
plays = set()
# for all directories containing playbooks # for all directories containing playbooks
for play_dir in playbooks_dirs: for play_dir in playbooks_dirs:
...@@ -192,33 +205,35 @@ def change_set_to_roles(files, git_dir, roles_dirs, playbooks_dirs, graph): ...@@ -192,33 +205,35 @@ def change_set_to_roles(files, git_dir, roles_dirs, playbooks_dirs, graph):
for f in files: for f in files:
file_path = pathlib2.Path(git_dir, f) file_path = pathlib2.Path(git_dir, f)
# if the change set file is in teh set of playbook files # if the change set file is in the set of playbook files
if file_path in candidate_files: if file_path in candidate_files:
plays.add(_get_playbok_name_from_file(file_path))
# gets first level of children of playbook in graph, which represents return plays
# all roles the playbook uses
descendants = nx.all_neighbors(graph, (file_path.stem, "aws_playbook")) def _get_playbook_name_from_file(path):
"""
Gets name of playbook from the filepath, which is the last part of the filepath.
# adds all the roles that a playbook uses to set of roles of the change set Input:
items |= {desc.name for desc in descendants} path: A path to the playbook
return items """
# get last part of filepath
return path.stem
def _get_resource_name(path, kind): def _get_role_name_from_file(path):
""" """
Gets name of resource from the filepath, which is the directory following occurence of kind. Gets name of role from the filepath, which is the directory following occurence of the word "roles".
Input: Input:
path: A path to the resource (e.g. a role or a playbook) path: A path to the role
kind: A description of the type of resource; this keyword precedes the name of a role or a playbook
in a file path and allows for the separation of its name;
e.g. for "configuration/playbooks/roles/discovery/...", kind = "roles" returns
"discovery" as the role name
""" """
# get individual parts of a file path # get individual parts of a file path
dirs = path.parts dirs = path.parts
# type of resource is the next part of the file path after kind (e.g. after "roles" or "playbooks") # name of role is the next part of the file path after "roles"
return dirs[dirs.index(kind)+1] return dirs[dirs.index("roles")+1]
def get_dependencies(roles, graph): def get_dependencies(roles, graph):
""" """
...@@ -355,6 +370,9 @@ if __name__ == '__main__': ...@@ -355,6 +370,9 @@ if __name__ == '__main__':
# build graph # build graph
graph = build_graph(TRAVIS_BUILD_DIR, config["roles_paths"], config["aws_plays_paths"], config["docker_plays_paths"]) graph = build_graph(TRAVIS_BUILD_DIR, config["roles_paths"], config["aws_plays_paths"], config["docker_plays_paths"])
# gets any playbooks in the commit range
plays = get_plays(change_set, TRAVIS_BUILD_DIR, config["aws_plays_paths"])
# transforms list of roles and plays into list of original roles and the roles contained in the plays # transforms list of roles and plays into list of original roles and the roles contained in the plays
roles = change_set_to_roles(change_set, TRAVIS_BUILD_DIR, config["roles_paths"], config["aws_plays_paths"], graph) roles = change_set_to_roles(change_set, TRAVIS_BUILD_DIR, config["roles_paths"], config["aws_plays_paths"], graph)
...@@ -364,6 +382,8 @@ if __name__ == '__main__': ...@@ -364,6 +382,8 @@ if __name__ == '__main__':
# determine which docker plays cover at least one role # determine which docker plays cover at least one role
docker_plays = get_docker_plays(dependent_roles, graph) docker_plays = get_docker_plays(dependent_roles, graph)
docker_plays = docker_plays | plays
# filter out docker plays without a Dockerfile # filter out docker plays without a Dockerfile
docker_plays = filter_docker_plays(docker_plays, TRAVIS_BUILD_DIR) docker_plays = filter_docker_plays(docker_plays, TRAVIS_BUILD_DIR)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment