uselagoon / build-deploy-tool

Tool to generate build resources
2 stars 5 forks source link

Fail earlier with malformed `.lagoon.yml` #297

Open rocketeerbkw opened 4 months ago

rocketeerbkw commented 4 months ago

There is currently a validation step for .lagoon.yml but it doesn't happen before the build tool needs to parse information from the file. In the case where a user has committed a malformed file (like using tabs instead of spaces) the build will fail with a cryptic error message:

++ cat .lagoon.yml
++ shyaml get-value environment_variables.git_sha false
Traceback (most recent call last):
  File "/usr/bin/shyaml", line 8, in <module>
    sys.exit(entrypoint())
             ^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/shyaml.py", line 831, in entrypoint
    sys.exit(main(sys.argv[1:]))
             ^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/shyaml.py", line 781, in main
    for output in do(stream=sys.stdin, **opts):
  File "/usr/lib/python3.11/site-packages/shyaml.py", line 661, in do
    for content in yaml.load_all(stream, Loader=loader):
  File "/usr/lib/python3.11/site-packages/yaml/__init__.py", line 93, in load_all
    yield loader.get_data()
          ^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/yaml/constructor.py", line 45, in get_data
    return self.construct_document(self.get_node())
                                   ^^^^^^^^^^^^^^^
  File "yaml/_yaml.pyx", line 665, in yaml._yaml.CParser.get_node
  File "yaml/_yaml.pyx", line 687, in yaml._yaml.CParser._compose_document
  File "yaml/_yaml.pyx", line 731, in yaml._yaml.CParser._compose_node
  File "yaml/_yaml.pyx", line 845, in yaml._yaml.CParser._compose_mapping_node
  File "yaml/_yaml.pyx", line 731, in yaml._yaml.CParser._compose_node
  File "yaml/_yaml.pyx", line 845, in yaml._yaml.CParser._compose_mapping_node
  File "yaml/_yaml.pyx", line 731, in yaml._yaml.CParser._compose_node
  File "yaml/_yaml.pyx", line 847, in yaml._yaml.CParser._compose_mapping_node
  File "yaml/_yaml.pyx", line 860, in yaml._yaml.CParser._parse_next_event
yaml.parser.ParserError: while parsing a block mapping
  in "<stdin>", line 110, column 5
did not find expected key
  in "<stdin>", line 371, column 6

I think we should be able to do the validation step as the very first thing, before there are any calls to parse the file. This would be right after the git checkout and before the first shyaml call https://github.com/uselagoon/build-deploy-tool/blob/dbd45d20e6939de67d298a364d1cf10ee8ec87d1/legacy/build-deploy.sh#L43-L45

Can our validator give a more useful error message about where in the yaml file the error is? Even if it can't, at least having the error put into the section that says There are issues with your .lagoon.yml file that must be fixed. would be an improvement.

Additionally, we might also move the storing of the .lagoon.yml pre-deploy configmap before the validaton step? Then lagoon administrators would be able to look at the file in the case of an error.

shreddedbacon commented 4 months ago

The entire structure of these files needs to be tweaked, and this is part of the work for moving more and more into go

shreddedbacon commented 4 months ago

PR #289 does some work to remove some of the shyaml. That PR still leaves the shyaml check for the git_sha there, but I think it can actually be removed if you read the comment I've left in the file.