basvandervlies / scl_modules

SCL modules
GNU General Public License v2.0
2 stars 3 forks source link

promise-type-docker-compose needs some error handling improvements #11

Open craigcomstock opened 20 hours ago

craigcomstock commented 20 hours ago

Thanks for this module. I am working through getting it working on a Debian-12 system and ran into a few snags.

While working through the snags I noticed that the textual description of errors from the module calling commands wasn't included in the log messages for notkept promises. I will make a PR here with some adjustments I came up with.


First I tried installing docker.io and docker-compose packages from Debian and those seem to be of such old version that they are incompatible with your promise type. e.g. docker.io provides the docker command but docker-compose doesn't provide the "plugin" version to enable "docker compose" but rather the stand-alone docker-compose command. Also, this version of docker-compose doesn't support the --format option.

Secondly, when I made a sample policy like in the README I get an error:

policy:

bundle agent composetest
{
  docker_compose:
    "${this.promise_dirname}/compose.yaml"
      state => "start";
}

result:

failed to solve: failed to read dockerfile: open Dockerfile: no such file or directory
   error: docker_compose.sh:'docker compose --file=/var/cfengine/inputs/services/cfbs/composetest/compose.yaml  up --detach' failed with:'#0 building with "default" instance using docker driver
   error: Promise module did not return a result for promise evaluation (docker_compose promise, promiser: '/var/cfengine/inputs/services/cfbs/composetest/compose.yaml' module: '/var/cfengine/modules/promises/docker_compose.sh')
   error: Promise module sent invalid line: '#1 [web internal] load build definition from Dockerfile'
   error: Promise module sent invalid line: '#1 transferring dockerfile: 2B 0.0s done'
   error: Promise module sent invalid line: '#1 DONE 0.0s''

Notice that the line

failed to solve: failed to read dockerfile: open Dockerfile: no such file or directory

is coming out on stderr and if I look in Mission Portal, Policy Analyzer or Reports I can't see that detail in the log messages. Also note that the error caused no promise result to be returned from the module:

   error: Promise module did not return a result for promise evaluation (docker_compose promise, promiser: '/var/cfengine/inputs/services/cfbs/composetest/compose.yaml' module: '/var/cfengine/modules/promises/docker_compose.sh')

And then I see that Dockerfile is not where I expect it to be!

root@bookworm:~# cp /var/cfengine/inputs/services/cfbs/composetest/
app.py            compose.yaml      main.cf           requirements.txt  

Egads, so cfbs add ./composetest which I expected to include all the files is not including Dockerfile. This is not really so much a bug but a "feature" of the limits to what the update policy in MPF will copy but it might be worth mentioning in your README.

I added a def.json to my composetest directory which will get slurped up by cfbs build and added to the main def.json

{
  "variables": {
    "default:update_def.input_name_patterns_extra": {
      "value": [ ".*\\Dockerfile" ],
      "comment": "Dockerfile or *.Dockerfile should be copied so that promise-type-docker-compose works when referencing this.promise_dirname/*.yaml files."
    }
  }
}

This causes my Dockerfile to be copied to inputs next to my compose.yaml file and all is well.

craigcomstock commented 18 hours ago

I have a work in progress extending what I did for the first command executed in the bash script as well as dynamically finding either docker-compose or "docker compose".

https://github.com/craigcomstock/scl_modules/tree/issue-11

I will make a PR after I have tested and fixed up this draft.

Thanks!

craigcomstock commented 14 hours ago

https://github.com/basvandervlies/scl_modules/pull/12 could probably use a touch of cleanup and more error handling could be added. Commits need to be squashed. :) Thanks for the useful module! It will be featured in a monthly module monday blog post on Dec. 2.