Skip to content
This repository was archived by the owner on Jul 19, 2022. It is now read-only.

Commit 14033d4

Browse files
learned to run-task
1 parent 25ad36b commit 14033d4

File tree

5 files changed

+173
-0
lines changed

5 files changed

+173
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1111
- `delete-domain` command - Delete a domain
1212
- `map-route` command - Add a url route to an app
1313
- `unmap-route` command - Remove a url route from an app
14+
- `run-task` command - Run a one-off task on an app
1415

1516
### Changed
1617
- Updated to cf cli v6.35.2

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,30 @@ Delete an app
611611
delete_mapped_routes: true
612612
```
613613

614+
#### run-task
615+
616+
Run a one-off task on an app
617+
618+
* `org`: *Optional.* The organization to target (required if not set in the source config)
619+
* `space`: *Optional.* The space to target (required if not set in the source config)
620+
* `app_name`: *Required.* The name of the application
621+
* `task_command`: *Required.* The command to run for the task
622+
* `task_name`: *Optional.* Name to give the task (generated if omitted)
623+
* `memory`: *Optional.* Memory limit (e.g. 256M, 1024M, 1G)
624+
* `disk_quota`: *Optional.* Disk limit (e.g. 256M, 1024M, 1G)
625+
626+
```yml
627+
- put: cf-run-task
628+
resource: cf-env
629+
params:
630+
command: run-task
631+
app_name: myapp-ui
632+
task_command: "bundle exec rake db:migrate"
633+
task_name: migrate
634+
memory: 256M
635+
disk_quota: 1G
636+
```
637+
614638
#### scale
615639

616640
Change or view the instance count, disk space limit, and memory limit for an app

assets/cf-functions.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,28 @@ function cf_delete() {
435435
fi
436436
}
437437

438+
function cf_run_task() {
439+
local app_name=${1:?app_name null or not set}
440+
local task_command=${2:?task_command null or not set}
441+
local task_name=${3:-}
442+
local memory=${4:-}
443+
local disk_quota=${5:-}
444+
445+
local args=("$app_name" "$task_command")
446+
[ -n "$task_name" ] && args+=(--name "$task_name")
447+
[ -n "$memory" ] && args+=(-m "$memory")
448+
[ -n "$disk_quota" ] && args+=(-k "$disk_quota")
449+
450+
cf run-task "${args[@]}"
451+
}
452+
453+
# very loose match on some "task name" (or command...) in the cf tasks output
454+
function cf_was_task_run() {
455+
local app_name=${1:?app_name null or not set}
456+
local task_name=${2:?task_name null or not set}
457+
CF_TRACE=false cf tasks "$app_name" | grep "$task_name" >/dev/null
458+
}
459+
438460
function cf_is_app_started() {
439461
local app_name=${1:?app_name null or not set}
440462
local guid=$(cf_get_app_guid "$app_name")

assets/out

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,15 @@ echo "$params" | jq -c '.commands[]' | while read -r options; do
266266
printf '\e[92m[INFO]\e[0m Executing \e[33m%s\e[0m: %s\n' "$command" "$app_name"
267267
cf_target "$org" "$space"
268268
cf_start "$app_name" "$staging_timeout" "$startup_timeout"
269+
elif [ "run-task" = "$command" ]; then
270+
app_name=$(echo $options | jq -r '.app_name //empty')
271+
task_command=$(echo $options | jq -r '.task_command //empty')
272+
task_name=$(echo $options | jq -r '.task_name //empty')
273+
memory=$(echo $options | jq -r '.memory //empty')
274+
disk_quota=$(echo $options | jq -r '.disk_quota //empty')
275+
printf '\e[92m[INFO]\e[0m Executing \e[33m%s\e[0m: %s\n' "$command" "$app_name"
276+
cf_target "$org" "$space"
277+
cf_run_task "$app_name" "$task_command" "$task_name" "$memory" "$disk_quota"
269278
elif [ "scale" = "$command" ]; then
270279
app_name=$(echo $options | jq -r '.app_name //empty')
271280
instances=$(echo $options | jq -r '.instances //empty')

itest/put.sh

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,118 @@ it_can_zero_downtime_push() {
488488
cf_is_app_started "$app_name"
489489
}
490490

491+
it_can_run_a_task_with_disk_quota() {
492+
local working_dir=$(mktemp -d $TMPDIR/put-src.XXXXXX)
493+
494+
local params=$(jq -n \
495+
--arg org "$org" \
496+
--arg space "$space" \
497+
--arg app_name "$app_name" \
498+
--arg task_command "echo run-task-with-disk_quota-test" \
499+
--arg disk_quota "756M" \
500+
'{
501+
command: "run-task",
502+
org: $org,
503+
space: $space,
504+
app_name: $app_name,
505+
task_command: $task_command,
506+
disk_quota: $disk_quota
507+
}')
508+
509+
local config=$(echo $source | jq --argjson params "$params" '.params = $params')
510+
511+
put_with_params "$config" "$working_dir" | jq -e '
512+
.version | keys == ["timestamp"]
513+
'
514+
515+
cf_was_task_run "$app_name" "run-task-with-disk_quota-test"
516+
}
517+
518+
it_can_run_a_task_with_memory() {
519+
local working_dir=$(mktemp -d $TMPDIR/put-src.XXXXXX)
520+
521+
local params=$(jq -n \
522+
--arg org "$org" \
523+
--arg space "$space" \
524+
--arg app_name "$app_name" \
525+
--arg task_command "echo run-task-with-memory-test" \
526+
--arg memory "512M" \
527+
'{
528+
command: "run-task",
529+
org: $org,
530+
space: $space,
531+
app_name: $app_name,
532+
task_command: $task_command,
533+
memory: $memory
534+
}')
535+
536+
local config=$(echo $source | jq --argjson params "$params" '.params = $params')
537+
538+
put_with_params "$config" "$working_dir" | jq -e '
539+
.version | keys == ["timestamp"]
540+
'
541+
542+
cf_was_task_run "$app_name" "run-task-with-memory-test"
543+
}
544+
545+
it_can_run_a_task_with_name() {
546+
local working_dir=$(mktemp -d $TMPDIR/put-src.XXXXXX)
547+
548+
local params=$(jq -n \
549+
--arg org "$org" \
550+
--arg space "$space" \
551+
--arg app_name "$app_name" \
552+
--arg task_command "echo run-task-with-name-test" \
553+
--arg task_name "run-task-with-name-test" \
554+
'{
555+
command: "run-task",
556+
org: $org,
557+
space: $space,
558+
app_name: $app_name,
559+
task_command: $task_command,
560+
task_name: $task_name
561+
}')
562+
563+
local config=$(echo $source | jq --argjson params "$params" '.params = $params')
564+
565+
put_with_params "$config" "$working_dir" | jq -e '
566+
.version | keys == ["timestamp"]
567+
'
568+
569+
cf_was_task_run "$app_name" "run-task-with-name-test"
570+
}
571+
572+
it_can_run_a_task() {
573+
local working_dir=$(mktemp -d $TMPDIR/put-src.XXXXXX)
574+
575+
local params=$(jq -n \
576+
--arg org "$org" \
577+
--arg space "$space" \
578+
--arg app_name "$app_name" \
579+
--arg task_command "echo run-task-test-all" \
580+
--arg task_name "run-task-test-all" \
581+
--arg memory "512M" \
582+
--arg disk_quota "756M" \
583+
'{
584+
command: "run-task",
585+
org: $org,
586+
space: $space,
587+
app_name: $app_name,
588+
task_command: $task_command,
589+
task_name: $task_name,
590+
memory: $memory,
591+
disk_quota: $disk_quota
592+
}')
593+
594+
local config=$(echo $source | jq --argjson params "$params" '.params = $params')
595+
596+
put_with_params "$config" "$working_dir" | jq -e '
597+
.version | keys == ["timestamp"]
598+
'
599+
600+
cf_was_task_run "$app_name" "run-task-test-all"
601+
}
602+
491603
it_can_scale_an_app_instances() {
492604
local working_dir=$(mktemp -d $TMPDIR/put-src.XXXXXX)
493605

@@ -1327,6 +1439,11 @@ run it_can_bind_an_asynchronous_service
13271439
run it_can_start_an_app
13281440
run it_can_zero_downtime_push
13291441

1442+
run it_can_run_a_task_with_disk_quota
1443+
run it_can_run_a_task_with_memory
1444+
run it_can_run_a_task_with_name
1445+
run it_can_run_a_task
1446+
13301447
run it_can_scale_an_app_instances
13311448
run it_can_scale_an_app_disk_quota
13321449
run it_can_scale_an_app_memory

0 commit comments

Comments
 (0)