Gio
08/09/2024, 2:33 AMMarvin
08/09/2024, 2:34 AMWorkPoolUpdate
, you can use the prefect work-pool update
command. Here are the steps and some details to guide you:
1. Create a JSON file with the new base job template:
- This file should define both the job_configuration
and variables
sections.
- Ensure that all variables used in the job_configuration
section are declared within the variables
section using double curly braces for placeholders (e.g., {{ variables.image_pull_policy }}
).
2. Use the update command in the CLI:
- Execute the following command in your CI/CD pipeline or command line:
sh
prefect work-pool update --base-job-template path/to/your/base-job-template.json my-work-pool
### Example of JSON Template
Here’s an example template:
json
{
"variables": {
"image_pull_policy": "Never",
"auto_remove": true
},
"job_configuration": {
"image_pull_policy": "{{ variables.image_pull_policy }}",
"auto_remove": "{{ variables.auto_remove }}"
}
}
### Relevant Links:
1. Prefect Documentation: Work Pools
2. GitHub Issue Example: Better documentation for work pool 'base-job-template'
This process allows version control of your job templates and ensures that any updates are consistently applied across your work pools.Gio
08/09/2024, 2:38 AMfrom prefect.client.schemas.actions import WorkPoolUpdate
Marvin
08/09/2024, 2:38 AMWorkPoolUpdate
class in Python, you can follow these steps:
1. Import the necessary module:
python
from prefect.client.schemas.actions import WorkPoolUpdate
2. Create an instance of WorkPoolUpdate
with the new base job template:
python
# Define your new base job template
base_job_template = {
"variables": {
"image_pull_policy": "Never",
"auto_remove": True
},
"job_configuration": {
"image_pull_policy": "{{ variables.image_pull_policy }}",
"auto_remove": "{{ variables.auto_remove }}"
}
}
# Create an instance of WorkPoolUpdate
work_pool_update = WorkPoolUpdate(base_job_template=base_job_template)
3. Update the work pool using the Prefect client:
python
from prefect.client import get_client
import asyncio
async def update_work_pool():
async with get_client() as client:
# Update the work pool with the new base job template
await client.update_work_pool(work_pool_name="my-work-pool", work_pool_update=work_pool_update)
# Run the coroutine to perform the update
asyncio.run(update_work_pool())
### Documentation Reference
You can also refer to the Work Pools Prefect documentation for additional context.
This example demonstrates how to use the WorkPoolUpdate
class along with the Prefect client to programmatically update the base job template for a specified work pool.Gio
08/09/2024, 3:09 AMMarvin
08/09/2024, 3:09 AMsecret.json
.
- Use the following Prefect CLI command to store this JSON as a secret:
sh
prefect block register -n MySecretName -c secret.json
2. Store the JSON via Python code:
- Use the Prefect library to save the JSON directly from your Python script.
python
from prefect.blocks.system import Secret
# Define your JSON
my_json = {
"variables": {
"image_pull_policy": "Never",
"auto_remove": True
},
"job_configuration": {
"image_pull_policy": "{{ variables.image_pull_policy }}",
"auto_remove": "{{ variables.auto_remove }}"
}
}
# Create a Secret block
secret_block = Secret(value=my_json)
# Save the Secret block to the Prefect storage with a given name
secret_block.save(name="my-json-secret")
3. Retrieve and use the JSON Secret in a Flow:
- When you need to retrieve and use the stored JSON secret, you can do so in your Prefect flow or task.
python
from prefect import flow, task
from prefect.blocks.system import Secret
@task
def get_secret():
# Load the secret
secret_block = Secret.load("my-json-secret")
return secret_block.value
@flow
def main_flow():
# Retrieve the secret
json_secret = get_secret()
print(json_secret)
if __name__ == "__main__":
main_flow()
### Documentation Reference
For more details, you can refer to the Prefect secrets documentation.
These steps will help you securely store and retrieve JSON secrets using Prefect blocks, enhancing the security of your credentials and configuration data.