Given
param "node_version" {
default = "1.0"
}
Case 1
task.query "get-version" {
query = "node version 2>/dev/null | grep '{{param `node_version`}}'"
group = "install"
}
switch "test-version" {
case "eq `` `{{lookup `task.query.get-version.status.stdout`}}`" "version-not-found" {
task "download" {
check = "exit 1"
apply = "curl ..."
group = "install"
}
task "retire" {
check = "exit 1"
apply = "node retire"
group = "install"
}
}
}
Case 2
task.query "get-version" {
query = "node version 2>/dev/null | grep -q '{{param `node_version`}}'"
group = "install"
}
switch "test-version" {
case "eq `1` `{{lookup `task.query.get-version.status.exitstatus`}}`" "version-not-found" {
task "download" {
check = "exit 1"
apply = "curl ..."
group = "install"
}
task "retire" {
check = "exit 1"
apply = "node retire"
group = "install"
}
}
}
The exit 1 on the tasks seems to be needed, as I want it to execute the whole block when the version mismatch.
Result
check
Summary: 0 errors, 3 changes
apply
root/module.main/task.query.get-version:
Error: root/module.main/task.query.get-version still has changes after apply: query apply called but it should never have changes
Messages:
check (returned: 1)
Has Changes: yes
Changes: No changes
Errors:
* root/module.main/task.query.get-version: root/module.main/task.query.get-version still has changes after apply: query apply called but it should never have changes
Failed due to failing dependency:
* root/module.main/macro.switch.get-version/macro.case.version-not-found/task.retire: error in dependency "root/module.main/task.query.check-version"
* root/module.main: error in dependency "root/module.main/macro.switch.get-version"
* root/module.main/macro.switch.get-version/macro.case.version-not-found: error in dependency "root/module.main/task.query.get-version"
* root/module.main/macro.switch.get-version/macro.case.version-not-found/task.download: error in dependency "root/module.main/task.query.get-version"
* root/module.main/macro.switch.get-version: error in dependency "root/module.main/macro.switch.test-version/macro.case.version-not-found"
Summary: 1 errors, 0 changes, 5 dependency errors
UPDATE
Found a workaround.
task.query "get-version" {
query = "node version 2>/dev/null | grep '{{param `node_version`}}'; true"
group = "install"
}
switch "test-version" {
case "eq `` `{{lookup `task.query.get-version.status.stdout`}}`" "version-not-found" {
task "download" {
check = "exit 1"
apply = "curl ..."
group = "install"
}
task "retire" {
check = "exit 1"
apply = "node retire"
group = "install"
}
}
}
It works if I return true on the task.query, although still allows me to check for the value 😄
Only stdout can be used as the exitstatus will always break the apply.
Unfortunately evaluate of eq is also broken. Can only validate if value is empty. If I try to check the version there it fails.
Seems like task.query is evaluating the error code again after the tasks have been applied. That's a surprise.
Given
Case 1
Case 2
The
exit 1on the tasks seems to be needed, as I want it to execute the whole block when the version mismatch.Result
check
apply
UPDATE
Found a workaround.
It works if I return
trueon thetask.query, although still allows me to check for the value 😄Only
stdoutcan be used as theexitstatuswill always break theapply.Unfortunately evaluate of
eqis also broken. Can only validate if value is empty. If I try to check the version there it fails.Seems like
task.queryis evaluating the error code again after the tasks have been applied. That's a surprise.