1212from datacrunch .containers .containers import (
1313 Container ,
1414 ComputeResource ,
15+ EnvVar ,
16+ EnvVarType ,
1517 ScalingOptions ,
1618 ScalingPolicy ,
1719 ScalingTriggers ,
2931DEPLOYMENT_NAME = "my-deployment"
3032IMAGE_NAME = "your-image-name:version"
3133
32- # Environment variables
34+ # Get client secret and id from environment variables
3335DATACRUNCH_CLIENT_ID = os .environ .get ('DATACRUNCH_CLIENT_ID' )
3436DATACRUNCH_CLIENT_SECRET = os .environ .get ('DATACRUNCH_CLIENT_SECRET' )
3537
3638# DataCrunch client instance
37- datacrunch_client = None
39+ datacrunch = None
3840
3941
4042def wait_for_deployment_health (client : DataCrunchClient , deployment_name : str , max_attempts : int = 10 , delay : int = 30 ) -> bool :
@@ -79,15 +81,9 @@ def cleanup_resources(client: DataCrunchClient) -> None:
7981def main () -> None :
8082 """Main function demonstrating deployment lifecycle management."""
8183 try :
82- # Check required environment variables
83- if not DATACRUNCH_CLIENT_ID or not DATACRUNCH_CLIENT_SECRET :
84- print (
85- "Please set DATACRUNCH_CLIENT_ID and DATACRUNCH_CLIENT_SECRET environment variables" )
86- return
87-
8884 # Initialize client
89- global datacrunch_client
90- datacrunch_client = DataCrunchClient (
85+ global datacrunch
86+ datacrunch = DataCrunchClient (
9187 DATACRUNCH_CLIENT_ID , DATACRUNCH_CLIENT_SECRET )
9288
9389 # Create container configuration
@@ -104,6 +100,21 @@ def main() -> None:
104100 type = VolumeMountType .SCRATCH ,
105101 mount_path = "/data"
106102 )
103+ ],
104+ env = [
105+ # Secret environment variables needed to be added beforehand
106+ EnvVar (
107+ name = "HF_TOKEN" ,
108+ # This is a reference to a secret already created
109+ value_or_reference_to_secret = "hf_token" ,
110+ type = EnvVarType .SECRET
111+ ),
112+ # Plain environment variables can be added directly
113+ EnvVar (
114+ name = "VERSION" ,
115+ value_or_reference_to_secret = "1.5.2" ,
116+ type = EnvVarType .PLAIN
117+ )
107118 ]
108119 )
109120
@@ -143,19 +154,19 @@ def main() -> None:
143154 )
144155
145156 # Create the deployment
146- created_deployment = datacrunch_client .containers .create_deployment (
157+ created_deployment = datacrunch .containers .create_deployment (
147158 deployment )
148159 print (f"Created deployment: { created_deployment .name } " )
149160
150161 # Wait for deployment to be healthy
151- if not wait_for_deployment_health (datacrunch_client , DEPLOYMENT_NAME ):
162+ if not wait_for_deployment_health (datacrunch , DEPLOYMENT_NAME ):
152163 print ("Deployment health check failed" )
153- cleanup_resources (datacrunch_client )
164+ cleanup_resources (datacrunch )
154165 return
155166
156167 # Update scaling configuration
157168 try :
158- deployment = datacrunch_client .containers .get_deployment_by_name (
169+ deployment = datacrunch .containers .get_deployment_by_name (
159170 DEPLOYMENT_NAME )
160171 # Create new scaling options with increased replica counts
161172 deployment .scaling = ScalingOptions (
@@ -177,7 +188,7 @@ def main() -> None:
177188 )
178189 )
179190 )
180- updated_deployment = datacrunch_client .containers .update_deployment (
191+ updated_deployment = datacrunch .containers .update_deployment (
181192 DEPLOYMENT_NAME , deployment )
182193 print (f"Updated deployment scaling: { updated_deployment .name } " )
183194 except APIException as e :
@@ -186,33 +197,33 @@ def main() -> None:
186197 # Demonstrate deployment operations
187198 try :
188199 # Pause deployment
189- datacrunch_client .containers .pause_deployment (DEPLOYMENT_NAME )
200+ datacrunch .containers .pause_deployment (DEPLOYMENT_NAME )
190201 print ("Deployment paused" )
191202 time .sleep (60 )
192203
193204 # Resume deployment
194- datacrunch_client .containers .resume_deployment (DEPLOYMENT_NAME )
205+ datacrunch .containers .resume_deployment (DEPLOYMENT_NAME )
195206 print ("Deployment resumed" )
196207
197208 # Restart deployment
198- datacrunch_client .containers .restart_deployment (DEPLOYMENT_NAME )
209+ datacrunch .containers .restart_deployment (DEPLOYMENT_NAME )
199210 print ("Deployment restarted" )
200211
201212 # Purge queue
202- datacrunch_client .containers .purge_deployment_queue (
213+ datacrunch .containers .purge_deployment_queue (
203214 DEPLOYMENT_NAME )
204215 print ("Queue purged" )
205216 except APIException as e :
206217 print (f"Error in deployment operations: { e } " )
207218
208219 # Clean up
209- cleanup_resources (datacrunch_client )
220+ cleanup_resources (datacrunch )
210221
211222 except Exception as e :
212223 print (f"Unexpected error: { e } " )
213224 # Attempt cleanup even if there was an error
214225 try :
215- cleanup_resources (datacrunch_client )
226+ cleanup_resources (datacrunch )
216227 except Exception as cleanup_error :
217228 print (f"Error during cleanup after failure: { cleanup_error } " )
218229
0 commit comments