Project

General

Profile

Actions

action #175848

open

coordination #161414: [epic] Improved salt based infrastructure management

Validate sls files in salt-{states,pillars}-openqa with a best effort approach

Added by tinita 17 days ago. Updated 6 days ago.

Status:
New
Priority:
Low
Assignee:
-
Category:
Feature requests
Target version:
Start date:
2025-01-20
Due date:
% Done:

0%

Estimated time:

Description

Motivation

We regularly have invalid YAML in our salt repos because we can't easily validate .sls files with jinja tags.

A quite simple script replacing {{ }} and {% %} constructs with some dummy values can help us, although it can't catch everything.

Suggestions

Actions #1

Updated by tinita 17 days ago ยท Edited

Here's a script that works for our salt repos:

#!/usr/bin/perl
use strict;
use warnings;
use v5.10;
use YAML::PP;
use Encode;
use Getopt::Long;
GetOptions("replace" => \my $replace);

my $yp = YAML::PP->new( duplicate_keys => 1 );
for my $file (@ARGV) {
    open my $fh, '<', $file or die $!;
    my $sls = decode_utf8 do { local $/; <$fh> };
    close $fh;
    if ($replace) {
        my $yaml = replace($sls);
        print encode_utf8 $yaml;
    }
    else {
        say "================ Validating $file";
        validate($file, $sls);
    }
}

sub replace {
    my ($sls) = @_;
    # Remove anything between {% else %} and {% endif %}. Might need
    # to include {% elsif %} as well, but works without it so far
    $sls =~ s/\{%.? else .?%\}.*?(\{%.? endif .?%\})/$1/gs;
    # Now just remove any {% ... %}
    $sls =~ s/\{%.*?%\}//g;
    # Replace {{ something }} with nothing when it's on its own line
    $sls =~ s/^ *\{\{.*?\}\} *$//mg;
    # Replace {{ something }} with REPLACEMENT
    $sls =~ s/\{\{.*?\}\}/REPLACEMENT/g;
    return $sls;
}

sub validate {
    my ($file, $sls) = @_;
    my $yaml = replace($sls);
    my $data = eval { $yp->load_string($yaml) };
    if ($@) {
        #say $sls;
        say "Error ($file):\n$@";
        say "Call 'perl $0 --replace $file | yamlpp-highlight' to check the YAML yourself";
    }
}

Usage:

find . -name "*.sls" | xargs perl jinja-yaml.pl

Checking a certain file with an error can be done with

perl jinja-yaml.pl openqa/openvswitch.sls --replace | yamlpp-highlight
Actions #2

Updated by tinita 17 days ago

  • Description updated (diff)
Actions #3

Updated by jbaier_cz 17 days ago

  • Description updated (diff)
Actions #4

Updated by tinita 17 days ago

  • Description updated (diff)
Actions #5

Updated by okurz 16 days ago

  • Tags set to infra, salt, salt-states-openqa
  • Project changed from openQA Project (public) to openQA Infrastructure (public)
  • Category changed from Feature requests to Feature requests
  • Priority changed from Normal to Low
  • Target version set to Tools - Next
  • Parent task set to #161414
Actions #6

Updated by okurz 6 days ago

  • Target version changed from Tools - Next to future
Actions

Also available in: Atom PDF