@@ -63,41 +63,67 @@ def sync_apps_command(
6363
6464
6565def __sync_apps (apps_git , root_git ):
66+ logging .info ("Apps repository: %s" , apps_git .get_clone_url ())
67+ logging .info ("Root repository: %s" , root_git .get_clone_url ())
68+
6669 repo_apps = __get_repo_apps (apps_git )
67- logging .info ("Found %s app(s) in %s: %s" , len (repo_apps ), apps_git .get_clone_url (), ", " .join (repo_apps ))
70+ logging .info ("Found %s app(s) in apps repository: %s" , len (repo_apps ), ", " .join (repo_apps ))
71+
72+ repo_apps .add ("test-app" )
73+
74+ logging .info ("Searching apps repository in root repository's 'apps/' directory..." )
75+ apps_config_file , apps_config_file_name , current_repo_apps , apps_from_other_repos = __find_apps_config_from_repo (
76+ apps_git , root_git
77+ )
78+ if apps_config_file is None :
79+ raise GitOpsException (f"Could't find config file for apps repository in root repository's 'apps/' directory" )
80+
81+ if current_repo_apps == repo_apps :
82+ logging .info ("Root repository already up-to-date. I'm done here." )
83+ return
6884
69- apps_config_file , app_file_name , apps_from_other_repos = __find_apps_config_from_repo (apps_git , root_git )
7085 __check_if_app_already_exists (repo_apps , apps_from_other_repos )
86+
87+ logging .info ("Sync applications in root repository's %s." , apps_config_file_name )
7188 merge_yaml_element (apps_config_file , "applications" , dict .fromkeys (repo_apps , {}), True )
72- __commit_and_push (apps_git , root_git , app_file_name )
89+ __commit_and_push (apps_git , root_git , apps_config_file_name )
7390
7491
7592def __find_apps_config_from_repo (apps_git , root_git ):
76- logging .info ("Searching for %s in apps/" , apps_git .get_clone_url ())
7793 yaml = YAML ()
78- # List for all entries in .applications from each config repository
79- apps_from_other_repos = []
94+ apps_from_other_repos = [] # List for all entries in .applications from each config repository
8095 found_app_config_file = None
8196 found_app_config_file_name = None
82- app_file_entries = __get_bootstrap_entries (root_git )
83- for app_file in app_file_entries :
84- app_file_name = "apps/" + app_file ["name" ] + ".yaml"
85- logging .info ("Analyzing %s" , app_file_name )
97+ found_app_config_apps = set ()
98+ bootstrap_entries = __get_bootstrap_entries (root_git )
99+ for bootstrap_entry in bootstrap_entries :
100+ if "name" not in bootstrap_entry :
101+ raise GitOpsException ("Every bootstrap entry must have a 'name' property." )
102+ app_file_name = "apps/" + bootstrap_entry ["name" ] + ".yaml"
103+ logging .info ("Analyzing %s in root repository" , app_file_name )
86104 app_config_file = root_git .get_full_file_path (app_file_name )
87- with open (app_config_file , "r" ) as stream :
88- app_config_content = yaml .load (stream )
105+ try :
106+ with open (app_config_file , "r" ) as stream :
107+ app_config_content = yaml .load (stream )
108+ except FileNotFoundError as ex :
109+ raise GitOpsException (f"File '{ app_file_name } ' not found in root repository." ) from ex
110+ if "repository" not in app_config_content :
111+ raise GitOpsException (f"Cannot find key 'repository' in '{ app_file_name } '" )
89112 if app_config_content ["repository" ] == apps_git .get_clone_url ():
90- logging .info ("Found repository in %s" , app_file_name )
113+ logging .info ("Found apps repository in %s" , app_file_name )
91114 found_app_config_file = app_config_file
92115 found_app_config_file_name = app_file_name
116+ found_app_config_apps = __get_applications_from_app_config (app_config_content )
93117 else :
94- if "applications" in app_config_content and app_config_content ["applications" ] is not None :
95- apps_from_other_repos += app_config_content ["applications" ].keys ()
96- if found_app_config_file is None :
97- raise GitOpsException (
98- f"Could't find config file with .repository={ apps_git .get_clone_url ()} in apps/ directory"
99- )
100- return found_app_config_file , found_app_config_file_name , apps_from_other_repos
118+ apps_from_other_repos += __get_applications_from_app_config (app_config_content )
119+ return found_app_config_file , found_app_config_file_name , found_app_config_apps , apps_from_other_repos
120+
121+
122+ def __get_applications_from_app_config (app_config ):
123+ apps = []
124+ if "applications" in app_config and app_config ["applications" ] is not None :
125+ apps += app_config ["applications" ].keys ()
126+ return set (apps )
101127
102128
103129def __commit_and_push (apps_git , root_git , app_file_name ):
@@ -109,9 +135,14 @@ def __commit_and_push(apps_git, root_git, app_file_name):
109135def __get_bootstrap_entries (root_git ):
110136 root_git .checkout ("master" )
111137 bootstrap_values_file = root_git .get_full_file_path ("bootstrap/values.yaml" )
112- with open (bootstrap_values_file , "r" ) as stream :
113- bootstrap = YAML ().load (stream )
114- return bootstrap ["bootstrap" ]
138+ try :
139+ with open (bootstrap_values_file , "r" ) as stream :
140+ bootstrap_yaml = YAML ().load (stream )
141+ except FileNotFoundError as ex :
142+ raise GitOpsException ("File 'bootstrap/values.yaml' not found in root repository." ) from ex
143+ if "bootstrap" not in bootstrap_yaml :
144+ raise GitOpsException ("Cannot find key 'bootstrap' in 'bootstrap/values.yaml'" )
145+ return bootstrap_yaml ["bootstrap" ]
115146
116147
117148def __get_repo_apps (apps_git ):
@@ -127,4 +158,4 @@ def __get_repo_apps(apps_git):
127158def __check_if_app_already_exists (apps_dirs , apps_from_other_repos ):
128159 for app_key in apps_dirs :
129160 if app_key in apps_from_other_repos :
130- raise GitOpsException (f"application: { app_key } already exists in a different repository" )
161+ raise GitOpsException (f"application ' { app_key } ' already exists in a different repository" )
0 commit comments