feat: Logfile output
This commit is contained in:
		
							parent
							
								
									672281f5c6
								
							
						
					
					
						commit
						a5861cc016
					
				
							
								
								
									
										42
									
								
								od-cli.py
									
									
									
									
									
								
							
							
						
						
									
										42
									
								
								od-cli.py
									
									
									
									
									
								
							| @ -2,9 +2,11 @@ | |||||||
| # TODOs: | # TODOs: | ||||||
| # - Log for past runs (id/link to pipeline) | # - Log for past runs (id/link to pipeline) | ||||||
| # - Stop / Restart Pipeline | # - Stop / Restart Pipeline | ||||||
|  | # - Use toml config instead of StrEnums for easy extensibility | ||||||
| import gitlab | import gitlab | ||||||
| import typer | import typer | ||||||
| import os | import os | ||||||
|  | import tomllib | ||||||
| 
 | 
 | ||||||
| from enum import StrEnum | from enum import StrEnum | ||||||
| from typing_extensions import Annotated | from typing_extensions import Annotated | ||||||
| @ -18,12 +20,19 @@ MASTER_PASSWORD = os.environ.get("OD_MASTER_PASSWORD", "sovereign-workplace") | |||||||
| USER = os.environ.get("OD_USER", "od-user") | USER = os.environ.get("OD_USER", "od-user") | ||||||
| GL_USER = os.environ.get("OD_GL_USER", "od-gl-user") | GL_USER = os.environ.get("OD_GL_USER", "od-gl-user") | ||||||
| GL_PROJECT = os.environ.get("OD_GL_PROJECT", "1317") | GL_PROJECT = os.environ.get("OD_GL_PROJECT", "1317") | ||||||
|  | LOG_FILE = os.environ.get("OD_PIPELINE_LOG", "./__pipelines.log") | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | class ExternalProviders(StrEnum): | ||||||
|  |     kubernetes = "kubernetes" | ||||||
|  |     stackit = "stackit" | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class Clusters(StrEnum): | class Clusters(StrEnum): | ||||||
|     qa = "qa" |     qa = "qa" | ||||||
|     run = "run" |     run = "run" | ||||||
|     b1_stackit_butterfly = "b1-stackit-butterfly" |     b1_stackit_butterfly = "b1-stackit-butterfly" | ||||||
|  |     one = "one" | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class Apps(StrEnum): | class Apps(StrEnum): | ||||||
| @ -41,6 +50,8 @@ class Apps(StrEnum): | |||||||
|     openproject = "openproject" |     openproject = "openproject" | ||||||
|     jitsi = "jitsi" |     jitsi = "jitsi" | ||||||
|     notes = "notes" |     notes = "notes" | ||||||
|  |     dev_nextcloud = "dev_nextcloud" | ||||||
|  |     dev_bawu = "dev_bawu" | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @app.command() | @app.command() | ||||||
| @ -76,10 +87,12 @@ def pipeline(pid: str): | |||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def _new_pipeline(ref: str, variables: str): | def _new_pipeline(ref: str, variables: str): | ||||||
|     variables = _parse_variables(variables) |     parsed_variables = _parse_variables(variables) | ||||||
|     opendesk = gl.projects.get(1317) |     opendesk = gl.projects.get(1317) | ||||||
|     new_pipeline = opendesk.pipelines.create({"ref": ref, "variables": variables}) |     np = opendesk.pipelines.create({"ref": ref, "variables": parsed_variables}) | ||||||
|     print(new_pipeline) |     log = f"[{np.created_at[:-5].replace('T', ' ')}] {np.web_url} ({ref}): {variables}\n" | ||||||
|  |     print(log) | ||||||
|  |     _write_to_pipeline_log(log) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @app.command() | @app.command() | ||||||
| @ -92,6 +105,9 @@ def new_pipeline( | |||||||
|     ee: bool = False, |     ee: bool = False, | ||||||
|     env_stop: bool = False, |     env_stop: bool = False, | ||||||
|     flush_external: bool = False, |     flush_external: bool = False, | ||||||
|  |     external_provider: Annotated[ | ||||||
|  |         ExternalProviders, typer.Option(case_sensitive=False) | ||||||
|  |     ] = ExternalProviders.stackit, | ||||||
|     debug: bool = True, |     debug: bool = True, | ||||||
|     default_accounts: bool = True, |     default_accounts: bool = True, | ||||||
|     deploy: Annotated[List[Apps], typer.Option(case_sensitive=False)] = [Apps.none], |     deploy: Annotated[List[Apps], typer.Option(case_sensitive=False)] = [Apps.none], | ||||||
| @ -105,6 +121,7 @@ def new_pipeline( | |||||||
|         f"MASTER_PASSWORD_WEB_VAR:{MASTER_PASSWORD}", |         f"MASTER_PASSWORD_WEB_VAR:{MASTER_PASSWORD}", | ||||||
|         f"ENV_STOP_BEFORE:{_tf_to_yn(env_stop)}", |         f"ENV_STOP_BEFORE:{_tf_to_yn(env_stop)}", | ||||||
|         f"FLUSH_EXTERNAL_SERVICES_BEFORE:{_tf_to_yn(flush_external)}", |         f"FLUSH_EXTERNAL_SERVICES_BEFORE:{_tf_to_yn(flush_external)}", | ||||||
|  |         f"FLUSH_EXTERNAL_SERVICES_TYPE:{external_provider.upper()}", | ||||||
|         f"RUN_TESTS:{_tf_to_yn(test)}", |         f"RUN_TESTS:{_tf_to_yn(test)}", | ||||||
|         f"TESTS_BRANCH:{test_branch}", |         f"TESTS_BRANCH:{test_branch}", | ||||||
|         f"DEBUG_ENABLED:{_tf_to_yn(debug)}", |         f"DEBUG_ENABLED:{_tf_to_yn(debug)}", | ||||||
| @ -125,11 +142,21 @@ def new_pipeline( | |||||||
|     elif Apps.all in deploy and len(deploy) > 1: |     elif Apps.all in deploy and len(deploy) > 1: | ||||||
|         print("You cannot deploy 'all' but also specify specific apps at the same time") |         print("You cannot deploy 'all' but also specify specific apps at the same time") | ||||||
|         exit(1) |         exit(1) | ||||||
|  |     elif Apps.dev_nextcloud in deploy and len(deploy) == 1: | ||||||
|  |         STACK = ["nextcloud", "collabora", "cryptpad", "ums", "services", "migrations"] | ||||||
|  |         variables += [f"DEPLOY_{app.upper()}:yes" for app in STACK] | ||||||
|  |     elif Apps.dev_nextcloud in deploy and len(deploy) > 1: | ||||||
|  |         print("You cannot deploy 'dev_nextcloud' but also specify specific apps at the same time") | ||||||
|  |         exit(1) | ||||||
|  |     elif Apps.dev_bawu in deploy and len(deploy) == 1: | ||||||
|  |         STACK = ["nextcloud", "collabora", "ums", "ox", "services", "migrations"] | ||||||
|  |         variables += [f"DEPLOY_{app.upper()}:yes" for app in STACK] | ||||||
|  |     elif Apps.dev_bawu in deploy and len(deploy) > 1: | ||||||
|  |         print("You cannot deploy 'dev_bawu' but also specify specific apps at the same time") | ||||||
|  |         exit(1) | ||||||
|     else: |     else: | ||||||
|         variables += [f"DEPLOY_{app.value.upper()}:yes" for app in deploy] |         variables += [f"DEPLOY_{app.value.upper()}:yes" for app in deploy] | ||||||
| 
 | 
 | ||||||
|     print(variables) |  | ||||||
| 
 |  | ||||||
|     _new_pipeline(ref, ",".join(variables)) |     _new_pipeline(ref, ",".join(variables)) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @ -170,6 +197,11 @@ def _test_tf(): | |||||||
|         assert _tf_to_yn(tf) == yn, f"{tf} != {yn} but is {_tf_to_yn(tf)}" |         assert _tf_to_yn(tf) == yn, f"{tf} != {yn} but is {_tf_to_yn(tf)}" | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | def _write_to_pipeline_log(log: str): | ||||||
|  |     with open(LOG_FILE, "a") as pipeline_log: | ||||||
|  |         pipeline_log.write(log) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| if __name__ == "__main__": | if __name__ == "__main__": | ||||||
|     gl.auth() |     gl.auth() | ||||||
|     app() |     app() | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Philip Gaber
						Philip Gaber