Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

hierarchical dirs from sfg and lib; EXTENDING doc

  • Loading branch information...
commit f153d48d38354bc42ab62e7e4e310febf49ffcad 1 parent 89dbbc7
Dmytro Kovalov authored
1  .gitignore
... ... @@ -1,3 +1,4 @@
1 1 *.yml
2 2 .*.swp
3 3 .#*
  4 +scanner/lib/Uliska/do
1  parser/lib/uliska_parser/darwin/9.rb
... ... @@ -0,0 +1 @@
  1 +
144 scanner/EXTENDING.rdoc
Source Rendered
... ... @@ -0,0 +1,144 @@
  1 +
  2 += Extending Uliska Scanner
  3 +
  4 +Main uliska.pl script only initializes and executes commands at the
  5 +top level, based on OS kernel name, kernel major and minor versions.
  6 +
  7 +Rest is delegated to modules deeper in the +cfg+ and +lib/Uliska+
  8 +sub-trees.
  9 +
  10 +Uliska can be extended to cover more OS'es -- making the name-space
  11 +tree 'wider', or going 'deeper' in the name-space tree covering more
  12 +specific details of the distribution or environment.
  13 +
  14 +== Adding new OS (going wider)
  15 +
  16 +At the top level of the scanner tree add files accordingly to kernel
  17 +name and versions of UNIX system. For HP-UX 11i additional files
  18 +should be something like:
  19 +
  20 + lib/Uliska/HP-UX.pm
  21 + lib/Uliska/HP-UX/11.pm
  22 + cfg/HP-UX.cfg
  23 + cfg/HP-UX/11.cfg
  24 +
  25 +Top level files are similar to the ones describe in the following
  26 +section.
  27 +
  28 +== Adding new sub-trees (going deeper)
  29 +
  30 +System specific scans are based on loading and executing nested list
  31 +of commands and Perl modules. For example, for Linux sub-trees of .cfg
  32 +and .pm files can be following:
  33 +
  34 + ./cfg/Linux.cfg
  35 + ./cfg/Linux/Debian.cfg
  36 + ./cfg/Linux/Debian/5.cfg
  37 + ./cfg/Linux/Debian/5/4.cfg
  38 + ./cfg/Linux/Debian/5/5.cfg
  39 + ./cfg/Linux/Debian/6.cfg
  40 + ./cfg/Linux/RedHat/5.cfg
  41 + ./cfg/Linux/RedHat/5/4.cfg
  42 + ./cfg/Linux/RedHat/5/5.cfg
  43 + ./cfg/Linux/RedHat/6.cfg
  44 +
  45 + ./lib/Uliska.pm
  46 + ./lib/Uliska/Linux.pm
  47 + ./lib/Uliska/Linux/Debian.pm
  48 + ./lib/Uliska/Linux/Debian/5.pm
  49 + ./lib/Uliska/Linux/Debian/5/4.pm
  50 + ./lib/Uliska/Linux/Debian/6.pm
  51 + ./lib/Uliska/Linux/RedHat.pm
  52 + ./lib/Uliska/Linux/RedHat/5.pm
  53 + ./lib/Uliska/Linux/RedHat/5/1.pm
  54 + ./lib/Uliska/Linux/RedHat/5/2.pm
  55 + ./lib/Uliska/Linux/RedHat/5/3.pm
  56 + ./lib/Uliska/Linux/RedHat/5/4.pm
  57 + ./lib/Uliska/Linux/RedHat/5/5.pm
  58 + ./lib/Uliska/Linux/RedHat/6.pm
  59 +
  60 +Modules are loaded and executed on demand. I.e. for RedHat Linux 5.4
  61 +modules will be tried and loaded in the following order:
  62 +
  63 + ./lib/Uliska/Linux.pm
  64 + ./lib/Uliska/Linux/RedHat.pm
  65 + ./lib/Uliska/Linux/RedHat/5.pm
  66 + ./lib/Uliska/Linux/RedHat/5/4.pm
  67 +
  68 +
  69 +Accordingly lists of commands will be executed from files in the
  70 +similar order:
  71 +
  72 + ./cfg/Linux.cfg
  73 + ./cfg/Linux/RedHat.cfg
  74 + ./cfg/Linux/RedHat/5.cfg
  75 + ./cfg/Linux/RedHat/5/4.cfg
  76 +
  77 +All these files are not required to exist. In both cases (modules and
  78 +commands) files are loaded only if they present, if not they are
  79 +silently ignored.
  80 +
  81 +TODO: Add warnings for missing files for debugging.
  82 +
  83 +Additional granularity can be added based on DNS or NIS domain-names,
  84 +hostnames, hardware types, etc.
  85 +
  86 +=== Adding your own modules
  87 +
  88 +This example is based on Linux.pm module. In this module we can detect
  89 +Linux distribution name, execute commands and load modules specific
  90 +for the distribution.
  91 +
  92 +- detect your distribution:
  93 +
  94 + $distro = 'RedHat' if -f '/etc/redhat-release';
  95 + $distro = 'Debian' if -f '/etc/debian_version';
  96 +
  97 +- add files in cfg directory and execute commands
  98 +
  99 + This will execute commands from files Uliska/Linux/RedHat.cfg or
  100 + Uliska/Linux/Debian.cfg:
  101 +
  102 + main::executeList(main::read_commands("Linux/$distro"));
  103 +
  104 +- initialize and execute more nested modules
  105 +
  106 + This will try to load and execute module Uliska::Linux::RedHat or
  107 + Uliska::Linux::Debian. Module will be executed only if load is
  108 + successful:
  109 +
  110 + my $module = Uliska->init("Linux/$result->{distro}");
  111 + $module->run() if defined $module;
  112 +
  113 +Nesting modules can go arbitrary levels deep.
  114 +
  115 +
  116 +=== Example
  117 +
  118 +Full text of module Linux.pm
  119 +
  120 + use strict;
  121 +
  122 + package Uliska::Linux;
  123 +
  124 +
  125 + sub new {
  126 + my $self = shift;
  127 + return $self;
  128 + }
  129 +
  130 + sub run {
  131 + my $result = \%main::result;
  132 + $result->{'distro'} = &detect_distro();
  133 + main::executeList(main::read_commands("Linux/".$result->{distro}));
  134 +
  135 + Uliska->init("Linux/$result->{distro}")->run();
  136 + };
  137 +
  138 + sub detect_distro {
  139 + return 'RedHat' if -f '/etc/redhat-release';
  140 + return 'Debian' if -f '/etc/debian_version';
  141 + 'unknown';
  142 + }
  143 + 1;
  144 +
0  scanner/cfg/Linux_2.cfg → scanner/cfg/Linux/2.cfg
File renamed without changes
5 scanner/cfg/Linux/Debian.cfg
... ... @@ -0,0 +1,5 @@
  1 +#
  2 +# Commands for Debian
  3 +#
  4 +cat /etc/debian_version # release
  5 +dpkg --list # packages
6 scanner/cfg/Linux/RedHat.cfg
... ... @@ -0,0 +1,6 @@
  1 +
  2 +#
  3 +# RedHat commands
  4 +#
  5 +cat /etc/redhat-release # release
  6 +rpm -qa --queryformat="%{NAME} %{ARCH} %{VERSION} %{RELEASE} %{INSTALLTIME} %{SUMMARY}\n" # packages
5 scanner/cfg/RedHat.cfg
... ... @@ -1,5 +0,0 @@
1   -
2   -#
3   -# RedHat commands
4   -#
5   -cat /etc/redhat-release # release
24 scanner/lib/Uliska.pm
@@ -81,16 +81,36 @@ returned. If module file does not exist, undef is returned.
81 81
82 82 Loaded module must provide run() function.
83 83
  84 +=head3 Parameter
  85 +
  86 +=over
  87 +
  88 +=item $module_file
  89 +
  90 +Module PATH starting from $config{lib_dir}/Uliska/. Can include nested
  91 +subdirectories.
  92 +
  93 +=back
  94 +
84 95 =head3 Example
85 96
86 97 my $os = Uliska->init($result{kernel_name});
87 98 $os->run() if defined $os;
88 99
  100 + my $os = Uliska->init("Linux");
  101 +
  102 + my $os = Uliska->init("Linux/RedHat");
  103 +
  104 + my $os = Uliska->init("Linux/RedHat/5");
  105 +
89 106 =cut
90 107
91 108 sub init {
92   - my ($class,$module) = (shift, shift);
93   - my $require_file = "$config{lib_dir}/Uliska/$module.pm";
  109 + my ($class,$module_file) = (shift, shift);
  110 +
  111 + $module =~ s/\//::/g;
  112 +
  113 + my $require_file = "$config{lib_dir}/Uliska/$module_file.pm";
94 114 $class = "Uliska::$module";
95 115 if (-f $require_file ) {
96 116 require $require_file;
5 scanner/lib/Uliska/Linux.pm
@@ -11,7 +11,10 @@ sub new {
11 11 sub run {
12 12 my $result = \%main::result;
13 13 $result->{'distro'} = &detect_distro();
14   - main::executeList(main::read_commands($result->{distro}));
  14 + main::executeList(main::read_commands("Linux/".$result->{distro}));
  15 +
  16 + my $module = Uliska->init("Linux/$result->{distro}");
  17 + $module->run() if defined $module;
15 18 };
16 19
17 20 sub detect_distro {
31 scanner/lib/Uliska/Linux/RedHat.pm
... ... @@ -0,0 +1,31 @@
  1 +use strict;
  2 +
  3 +package Uliska::Linux::RedHat;
  4 +
  5 +
  6 +sub new {
  7 + my $self = shift;
  8 + return $self;
  9 +}
  10 +
  11 +sub run {
  12 + my $result = \%main::result;
  13 + $result->{'distro_version'} = &detect_version();
  14 +
  15 + main::executeList(main::read_commands("Linux/RedHat/$result->{version}->{major}"));
  16 + main::executeList(main::read_commands("Linux/RedHat/$result->{version}->{major}/$result->{version}->{minor}"))
  17 +
  18 +};
  19 +
  20 +sub detect_version {
  21 + open(my $fh, '<', '/etc/redhat-release') or die $!;
  22 + my $rel = <$fh>;
  23 + if ($rel =~ /\s(\d+)\.(\d+)\s/) {
  24 + return {
  25 + major => $1,
  26 + minor => $2
  27 + };
  28 + }
  29 + undef;
  30 +}
  31 +1;
4 scanner/uliska.pl
@@ -43,8 +43,8 @@ =head1 DESCRIPTION
43 43 };
44 44
45 45 executeList(read_commands($result{kernel}{name}));
46   -executeList(read_commands($result{kernel}{name}."_".$result{kernel}{major}));
47   -executeList(read_commands($result{kernel}{name}."_".$result{kernel}{major}."_".$result{kernel}{minor}));
  46 +executeList(read_commands($result{kernel}{name}."/".$result{kernel}{major}));
  47 +executeList(read_commands($result{kernel}{name}."/".$result{kernel}{major}."/".$result{kernel}{minor}));
48 48
49 49 my $os = Uliska->init($result{kernel}{name});
50 50

0 comments on commit f153d48

Please sign in to comment.
Something went wrong with that request. Please try again.