165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536 | @app.command()
async def deploy(
entrypoint: str = typer.Argument(
None,
help=(
"The path to a flow entrypoint within a project, in the form of"
" `./path/to/file.py:flow_func_name`"
),
),
flow_name: str = typer.Option(
None,
"--flow",
"-f",
help="The name of a registered flow to create a deployment for.",
),
name: str = typer.Option(
None, "--name", "-n", help="The name to give the deployment."
),
description: str = typer.Option(
None,
"--description",
"-d",
help=(
"The description to give the deployment. If not provided, the description"
" will be populated from the flow's description."
),
),
version: str = typer.Option(
None, "--version", help="A version to give the deployment."
),
tags: List[str] = typer.Option(
None,
"-t",
"--tag",
help=(
"One or more optional tags to apply to the deployment. Note: tags are used"
" only for organizational purposes. For delegating work to agents, use the"
" --work-queue flag."
),
),
work_pool_name: str = typer.Option(
None,
"-p",
"--pool",
help="The work pool that will handle this deployment's runs.",
),
work_queue_name: str = typer.Option(
None,
"-q",
"--work-queue",
help=(
"The work queue that will handle this deployment's runs. "
"It will be created if it doesn't already exist. Defaults to `None`."
),
),
variables: List[str] = typer.Option(
None,
"-v",
"--variable",
help=(
"One or more job variable overrides for the work pool provided in the"
" format of key=value"
),
),
cron: str = typer.Option(
None,
"--cron",
help="A cron string that will be used to set a CronSchedule on the deployment.",
),
interval: int = typer.Option(
None,
"--interval",
help=(
"An integer specifying an interval (in seconds) that will be used to set an"
" IntervalSchedule on the deployment."
),
),
interval_anchor: Optional[str] = typer.Option(
None, "--anchor-date", help="The anchor date for an interval schedule"
),
rrule: str = typer.Option(
None,
"--rrule",
help="An RRule that will be used to set an RRuleSchedule on the deployment.",
),
timezone: str = typer.Option(
None,
"--timezone",
help="Deployment schedule timezone string e.g. 'America/New_York'",
),
param: List[str] = typer.Option(
None,
"--param",
help=(
"An optional parameter override, values are parsed as JSON strings e.g."
" --param question=ultimate --param answer=42"
),
),
params: str = typer.Option(
None,
"--params",
help=(
"An optional parameter override in a JSON string format e.g."
' --params=\'{"question": "ultimate", "answer": 42}\''
),
),
):
"""
Deploy a flow from this project by creating a deployment.
Should be run from a project root directory.
"""
if len([value for value in (cron, rrule, interval) if value is not None]) > 1:
exit_with_error("Only one schedule type can be provided.")
if interval_anchor and not interval:
exit_with_error("An anchor date can only be provided with an interval schedule")
try:
with open("deployment.yaml", "r") as f:
base_deploy = yaml.safe_load(f)
except FileNotFoundError:
app.console.print(
"No deployment.yaml file found, only provided CLI options will be used.",
style="orange",
)
base_deploy = {}
with open("prefect.yaml", "r") as f:
project = yaml.safe_load(f)
# sanitize
if project.get("build") is None:
project["build"] = []
if project.get("push") is None:
project["push"] = []
if project.get("pull") is None:
project["pull"] = []
if (
not flow_name
and not base_deploy.get("flow_name")
and not entrypoint
and not base_deploy.get("entrypoint")
):
exit_with_error("An entrypoint or flow name must be provided.")
if not name and not base_deploy.get("name"):
exit_with_error("A deployment name must be provided.")
if (flow_name or base_deploy.get("flow_name")) and (
entrypoint or base_deploy.get("entrypoint")
):
exit_with_error("Can only pass an entrypoint or a flow name but not both.")
# Pull from deployment config if not passed
if entrypoint is None:
entrypoint = base_deploy.get("entrypoint")
if flow_name is None:
flow_name = base_deploy.get("flow_name")
# flow-name and entrypoint logic
flow = None
if entrypoint:
flow = await register_flow(entrypoint)
flow_name = flow.name
elif flow_name:
prefect_dir = find_prefect_directory()
if not prefect_dir:
exit_with_error(
"No .prefect directory could be found - run [yellow]`prefect project"
" init`[/yellow] to create one."
)
if not (prefect_dir / "flows.json").exists():
exit_with_error(
f"Flow {flow_name!r} cannot be found; run\n [yellow]prefect project"
" register-flow ./path/to/file.py:flow_fn_name[/yellow]\nto register"
" its location."
)
with open(prefect_dir / "flows.json", "r") as f:
flows = json.load(f)
if flow_name not in flows:
exit_with_error(
f"Flow {flow_name!r} cannot be found; run\n [yellow]prefect project"
" register-flow ./path/to/file.py:flow_fn_name[/yellow]\nto register"
" its location."
)
# set entrypoint from prior registration
entrypoint = flows[flow_name]
base_deploy["flow_name"] = flow_name
base_deploy["entrypoint"] = entrypoint
## parse parameters
# minor optimization in case we already loaded the flow
if not flow:
try:
flow = await run_sync_in_worker_thread(
load_flow_from_entrypoint, base_deploy["entrypoint"]
)
except Exception as exc:
exit_with_error(exc)
base_deploy["parameter_openapi_schema"] = parameter_schema(flow)
if param and (params is not None):
exit_with_error("Can only pass one of `param` or `params` options")
parameters = dict()
if param:
for p in param or []:
k, unparsed_value = p.split("=", 1)
try:
v = json.loads(unparsed_value)
app.console.print(
f"The parameter value {unparsed_value} is parsed as a JSON string"
)
except json.JSONDecodeError:
v = unparsed_value
parameters[k] = v
if params is not None:
parameters = json.loads(params)
base_deploy["parameters"] = parameters
# update schedule
schedule = None
if cron:
cron_kwargs = {"cron": cron, "timezone": timezone}
schedule = CronSchedule(
**{k: v for k, v in cron_kwargs.items() if v is not None}
)
elif interval:
interval_kwargs = {
"interval": timedelta(seconds=interval),
"anchor_date": interval_anchor,
"timezone": timezone,
}
schedule = IntervalSchedule(
**{k: v for k, v in interval_kwargs.items() if v is not None}
)
elif rrule:
try:
schedule = RRuleSchedule(**json.loads(rrule))
if timezone:
# override timezone if specified via CLI argument
schedule.timezone = timezone
except json.JSONDecodeError:
schedule = RRuleSchedule(rrule=rrule, timezone=timezone)
## RUN BUILD AND PUSH STEPS
step_outputs = {}
for step in project["build"] + project["push"]:
step_outputs.update(await run_step(step))
variable_overrides = {}
for variable in variables or []:
key, value = variable.split("=", 1)
variable_overrides[key] = value
step_outputs.update(variable_overrides)
# set other CLI flags
if name:
base_deploy["name"] = name
if version:
base_deploy["version"] = version
if tags:
base_deploy["tags"] = tags
if description:
base_deploy["description"] = description
elif not base_deploy["description"]:
base_deploy["description"] = flow.description
if work_pool_name:
base_deploy["work_pool"]["name"] = work_pool_name
if work_queue_name:
base_deploy["work_pool"]["work_queue_name"] = work_queue_name
base_deploy["work_pool"]["job_variables"].update(variable_overrides)
## apply templating from build and push steps to the final deployment spec
_parameter_schema = base_deploy.pop("parameter_openapi_schema")
base_deploy = apply_values(base_deploy, step_outputs)
base_deploy["parameter_openapi_schema"] = _parameter_schema
# set schedule afterwards to avoid templating errors
if schedule:
base_deploy["schedule"] = schedule
# prepare the pull step
project["pull"] = apply_values(project["pull"], step_outputs)
async with prefect.get_client() as client:
flow_id = await client.create_flow_from_name(base_deploy["flow_name"])
if base_deploy["work_pool"]:
try:
work_pool = await client.read_work_pool(
base_deploy["work_pool"]["name"]
)
# dont allow submitting to prefect-agent typed work pools
if work_pool.type == "prefect-agent":
exit_with_error(
"Cannot deploy project with work pool of type 'prefect-agent'."
)
except ObjectNotFound:
app.console.print(
(
"\nThis deployment references a work pool that does not exist."
" This means no worker will be able to pick up its runs. You"
" can create a work pool in the Prefect UI."
),
style="red",
)
deployment_id = await client.create_deployment(
flow_id=flow_id,
name=base_deploy["name"],
work_queue_name=base_deploy["work_pool"]["work_queue_name"],
work_pool_name=base_deploy["work_pool"]["name"],
version=base_deploy["version"],
schedule=base_deploy["schedule"],
parameters=base_deploy["parameters"],
description=base_deploy["description"],
tags=base_deploy["tags"],
path=base_deploy.get("path"),
entrypoint=base_deploy["entrypoint"],
parameter_openapi_schema=base_deploy["parameter_openapi_schema"].dict(),
pull_steps=project["pull"],
infra_overrides=base_deploy["work_pool"]["job_variables"],
)
app.console.print(
(
f"Deployment '{base_deploy['flow_name']}/{base_deploy['name']}'"
f" successfully created with id '{deployment_id}'."
),
style="green",
)
if PREFECT_UI_URL:
app.console.print(
"View Deployment in UI:"
f" {PREFECT_UI_URL.value()}/deployments/deployment/{deployment_id}"
)
if base_deploy["work_pool"]["name"] is not None:
app.console.print(
"\nTo execute flow runs from this deployment, start a worker that"
f" pulls work from the {base_deploy['work_pool']['name']!r} work pool"
)
elif base_deploy["work_pool"]["work_queue_name"] is not None:
app.console.print(
"\nTo execute flow runs from this deployment, start a worker that"
" pulls work from the"
f" {base_deploy['work_pool']['work_queue_name']!r} work queue"
)
else:
app.console.print(
(
"\nThis deployment does not specify a work pool or queue, which"
" means no worker will be able to pick up its runs. To add a"
" work pool, edit the deployment spec and re-run this command,"
" or visit the deployment in the UI."
),
style="red",
)
|