3131DEPLOYMENT_NAME = "my-deployment"
3232IMAGE_NAME = "your-image-name:version"
3333
34- # Environment variables
34+ # Get client secret and id from environment variables
3535DATACRUNCH_CLIENT_ID = os .environ .get ('DATACRUNCH_CLIENT_ID' )
3636DATACRUNCH_CLIENT_SECRET = os .environ .get ('DATACRUNCH_CLIENT_SECRET' )
3737
3838# DataCrunch client instance
39- datacrunch_client = None
39+ datacrunch = None
4040
4141
4242def wait_for_deployment_health (client : DataCrunchClient , deployment_name : str , max_attempts : int = 10 , delay : int = 30 ) -> bool :
@@ -81,15 +81,9 @@ def cleanup_resources(client: DataCrunchClient) -> None:
8181def main () -> None :
8282 """Main function demonstrating deployment lifecycle management."""
8383 try :
84- # Check required environment variables
85- if not DATACRUNCH_CLIENT_ID or not DATACRUNCH_CLIENT_SECRET :
86- print (
87- "Please set DATACRUNCH_CLIENT_ID and DATACRUNCH_CLIENT_SECRET environment variables" )
88- return
89-
9084 # Initialize client
91- global datacrunch_client
92- datacrunch_client = DataCrunchClient (
85+ global datacrunch
86+ datacrunch = DataCrunchClient (
9387 DATACRUNCH_CLIENT_ID , DATACRUNCH_CLIENT_SECRET )
9488
9589 # Create container configuration
@@ -160,19 +154,19 @@ def main() -> None:
160154 )
161155
162156 # Create the deployment
163- created_deployment = datacrunch_client .containers .create_deployment (
157+ created_deployment = datacrunch .containers .create_deployment (
164158 deployment )
165159 print (f"Created deployment: { created_deployment .name } " )
166160
167161 # Wait for deployment to be healthy
168- if not wait_for_deployment_health (datacrunch_client , DEPLOYMENT_NAME ):
162+ if not wait_for_deployment_health (datacrunch , DEPLOYMENT_NAME ):
169163 print ("Deployment health check failed" )
170- cleanup_resources (datacrunch_client )
164+ cleanup_resources (datacrunch )
171165 return
172166
173167 # Update scaling configuration
174168 try :
175- deployment = datacrunch_client .containers .get_deployment_by_name (
169+ deployment = datacrunch .containers .get_deployment_by_name (
176170 DEPLOYMENT_NAME )
177171 # Create new scaling options with increased replica counts
178172 deployment .scaling = ScalingOptions (
@@ -194,7 +188,7 @@ def main() -> None:
194188 )
195189 )
196190 )
197- updated_deployment = datacrunch_client .containers .update_deployment (
191+ updated_deployment = datacrunch .containers .update_deployment (
198192 DEPLOYMENT_NAME , deployment )
199193 print (f"Updated deployment scaling: { updated_deployment .name } " )
200194 except APIException as e :
@@ -203,33 +197,33 @@ def main() -> None:
203197 # Demonstrate deployment operations
204198 try :
205199 # Pause deployment
206- datacrunch_client .containers .pause_deployment (DEPLOYMENT_NAME )
200+ datacrunch .containers .pause_deployment (DEPLOYMENT_NAME )
207201 print ("Deployment paused" )
208202 time .sleep (60 )
209203
210204 # Resume deployment
211- datacrunch_client .containers .resume_deployment (DEPLOYMENT_NAME )
205+ datacrunch .containers .resume_deployment (DEPLOYMENT_NAME )
212206 print ("Deployment resumed" )
213207
214208 # Restart deployment
215- datacrunch_client .containers .restart_deployment (DEPLOYMENT_NAME )
209+ datacrunch .containers .restart_deployment (DEPLOYMENT_NAME )
216210 print ("Deployment restarted" )
217211
218212 # Purge queue
219- datacrunch_client .containers .purge_deployment_queue (
213+ datacrunch .containers .purge_deployment_queue (
220214 DEPLOYMENT_NAME )
221215 print ("Queue purged" )
222216 except APIException as e :
223217 print (f"Error in deployment operations: { e } " )
224218
225219 # Clean up
226- cleanup_resources (datacrunch_client )
220+ cleanup_resources (datacrunch )
227221
228222 except Exception as e :
229223 print (f"Unexpected error: { e } " )
230224 # Attempt cleanup even if there was an error
231225 try :
232- cleanup_resources (datacrunch_client )
226+ cleanup_resources (datacrunch )
233227 except Exception as cleanup_error :
234228 print (f"Error during cleanup after failure: { cleanup_error } " )
235229
0 commit comments