This repository was archived by the owner on Mar 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvldt
executable file
·105 lines (90 loc) · 2.32 KB
/
vldt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/perl
#
# vldt validates puppet files. It supports the following file types:
# - Puppet code
# - Embedded ruby code
# - YAML files (Hiera)
#
use strict;
use warnings;
use Capture::Tiny ':all';
use Term::ANSIColor;
my $puppetcmd = "/opt/puppetlabs/bin/puppet";
if ( scalar @ARGV == 0 )
{ print "Error: vldt should have at least one argument: the file(s) you want to validate.\n";
exit 1;
}
foreach my $file ( @ARGV )
{ if ( scalar @ARGV > 1 )
{ print "Results for file $file: \n\n";
}
if ( ! -e $file )
{ print "Error: skipping \"$file\". It does not exist.\n\n";
next;
}
if ( ! -f $file )
{ print "Error: skipping \"$file\". It is not a regular file.\n\n";
next;
}
if ( $file =~ /\.pp$/ )
{ validate_puppet($file);
}
elsif ( $file =~ /\.yaml$/ )
{ validate_yaml($file);
}
elsif ( $file =~ /\.erb$/ )
{ validate_erb($file);
}
else
{ print "Skipping file because of unknown extension.\n";
}
print "\n";
}
sub validate_puppet
{ my ($filename) = @_;
my ($output,@result) = capture_merged
{ system("$puppetcmd parser validate $filename");
};
if ( $result[0] == 0 )
{ print color('green');
print "Validation by puppet parser OK.\n";
print color('reset');
}
else
{ print color('red');
print "Validation by puppet parser FAILED. Output:\n$output";
print color('reset');
}
}
sub validate_yaml
{ my ($filename) = @_;
my ($output,@result) = capture_merged
{ system("ruby -ryaml -e \"YAML.load_file \'$filename\'\"");
};
if ( not $output )
{ print color('green');
print "Validation by YAML module OK.\n";
print color('reset');
}
else
{ print color('red');
print "Validation by YAML module FAILED. Output:\n$output";
print color('reset');
}
}
sub validate_erb
{ my ($filename) = @_;
my ($output,@result) = capture_merged
{ system("erb -P -x -T \'-\' $filename | ruby -c");
};
if ( $output =~ /Syntax OK/ )
{ print color('green');
print "Validation by Ruby OK.\n";
print color('reset');
}
else
{ print color('red');
print "Validation by Ruby FAILED. Output:\n$output";
print color('reset');
}
}