From 7606f85ee41a0e123e5e753f3a5b6a8ae31dc098 Mon Sep 17 00:00:00 2001 From: Sebastian Schmelzer Date: Sun, 14 Dec 2008 23:16:12 +0000 Subject: * first ideas of distro based utilities * usage: use OpenSLX::DistroUtils; # get initfile object my $initfile = newInitFile(); # modify the object $initfile->addToBlock('head', 'set some variables'); # 3rd parameter is setting the priority of the entry # default is 5 $initfile->addToBlock('head', 'set sth at the very beginning', 1); $initfile->addToBlock('start', '/bin/startsomething'); $initfile->addToBlock('stop', '/bin/stopsomething'); $initfile->setName('foo'); .. # get generated content of initfile $source = getInitFileForDistro($initfile, 'Ubuntu'); git-svn-id: http://svn.openslx.org/svn/openslx/openslx/trunk@2405 95ad53e4-c205-0410-b2fa-d234c58c8868 --- lib/OpenSLX/DistroUtils.pm | 58 +++++++++++++++ lib/OpenSLX/DistroUtils/Base.pm | 136 ++++++++++++++++++++++++++++++++++++ lib/OpenSLX/DistroUtils/Engine.pm | 30 ++++++++ lib/OpenSLX/DistroUtils/InitFile.pm | 92 ++++++++++++++++++++++++ 4 files changed, 316 insertions(+) create mode 100644 lib/OpenSLX/DistroUtils.pm create mode 100644 lib/OpenSLX/DistroUtils/Base.pm create mode 100644 lib/OpenSLX/DistroUtils/Engine.pm create mode 100644 lib/OpenSLX/DistroUtils/InitFile.pm diff --git a/lib/OpenSLX/DistroUtils.pm b/lib/OpenSLX/DistroUtils.pm new file mode 100644 index 00000000..454ffbd3 --- /dev/null +++ b/lib/OpenSLX/DistroUtils.pm @@ -0,0 +1,58 @@ +# Copyright (c) 2008 - OpenSLX GmbH +# +# This program is free software distributed under the GPL version 2. +# See http://openslx.org/COPYING +# +# If you have any feedback please consult http://openslx.org/feedback and +# send your suggestions, praise, or complaints to feedback@openslx.org +# +# General information about OpenSLX can be found at http://openslx.org/ +# ----------------------------------------------------------------------------- +# DistroUtils.pm +# - provides utility distro based functions for OpenSLX +# ----------------------------------------------------------------------------- +package OpenSLX::DistroUtils; + +use strict; +use warnings; + +use OpenSLX::Utils; +use OpenSLX::Basics; + +use OpenSLX::DistroUtils::Engine; +use OpenSLX::DistroUtils::InitFile; + +use Exporter; + +use vars qw(@ISA @EXPORT $VERSION); + +use Exporter; +$VERSION = 1.01; +@ISA = qw(Exporter); + +@EXPORT = qw( + newInitFile + getInitFileForDistro +); + + + +sub newInitFile { + return OpenSLX::DistroUtils::InitFile->new(); +} + + +sub getInitFileForDistro { + my $initFile = shift; + my $distroName = shift; + + my $engine = OpenSLX::DistroUtils::Engine->new(); + my $distro = $engine->loadDistro($distroName); + + return $distro->generateInitFile($initFile); +} + + + + +1; diff --git a/lib/OpenSLX/DistroUtils/Base.pm b/lib/OpenSLX/DistroUtils/Base.pm new file mode 100644 index 00000000..bc6f6f69 --- /dev/null +++ b/lib/OpenSLX/DistroUtils/Base.pm @@ -0,0 +1,136 @@ +package OpenSLX::DistroUtils::Base; + +use Data::Dumper; +use OpenSLX::Utils; + + +sub new +{ + my $class = shift; + my $self = {}; + return bless $self, $class; +} + +sub dumpInit +{ + my $self = shift; + my $initFile = shift; + + print Dumper($initFile->{'configHash'}); + + print $self->generateInitFile($initFile); +} + +sub _combineBlock +{ + my $self = shift; + my $block = shift; + + my $output; + + $output = "#"; + $output .= $block->{'blockDesc'}; + $output .= "\n "; + + my $content = $block->{'content'}; + while ( ($priority, $contentArray) = each %$content ) + { + $output .= join("\n ", @$contentArray); + $output .= "\n "; + } + + $output .= "\n"; + return $output; +} + +sub _renderInfoBlock +{ + my $self = shift; + my $config = shift; + + my $tpl = unshiftHereDoc(<<' End-of-Here'); + ### BEGIN INIT INFO + # Provides: %s + # Required-Start: %s + # Required-Stop: %s + # Default-Start: %s + # Default-Stop: %s + # Short-Description: %s + ### END INIT INFO + + End-of-Here + + return sprintf( + $tpl, + $config->{'name'}, + $config->{'requiredStart'}, + $config->{'requiredStop'}, + $config->{'defaultStart'}, + $config->{'defaultStop'}, + $config->{'shortDesc'} + ); +} + +sub generateInitFile +{ + my $self = shift; + my $initFile = shift; + + my $config = $initFile->{'configHash'}; + + $output = "#!/bin/sh\n\n"; + $output .= $self->_renderInfoBlock($config); + $output .= "set -e \n\n"; + if (keys(%{$config->{'head'}->{'content'}}) > 0) { + $output .= $self->_combineBlock($config->{'head'}); + } + if (keys(%{$config->{'functions'}->{'content'}}) > 0) { + $output .= $self->_combineBlock($config->{'functions'}); + } + $output .= "case \"\$1\" in \n"; + if (keys(%{$config->{'start'}->{'content'}}) > 0) { + $output .= " start)\n"; + $output .= $self->_combineBlock($config->{'start'}); + $output .= " ;;\n"; + } else { + # trigger error + # start is essential + } + if (keys(%{$config->{'stop'}->{'content'}}) > 0) { + $output .= " stop)\n"; + $output .= $self->_combineBlock($config->{'stop'}); + $output .= " ;;\n"; + } else { + # trigger error + # stop is essential + } + if (keys(%{$config->{'reload'}->{'content'}}) > 0) { + $output .= " reload)\n"; + $output .= $self->_combineBlock($config->{'relaod'}); + $output .= " ;;\n"; + } + if (keys(%{$config->{'restart'}->{'content'}}) > 0) { + $output .= " restart)\n"; + $output .= $self->_combineBlock($config->{'restart'}); + $output .= " ;;\n"; + } + if (keys(%{$config->{'status'}->{'content'}}) > 0) { + $output .= " status)\n"; + $output .= $self->_combineBlock($config->{'status'}); + $output .= " ;;\n"; + } + if (keys(%{$config->{'usage'}->{'content'}}) > 0) { + $output .= " *)\n"; + $output .= $self->_combineBlock($config->{'usage'}); + $output .= " exit 1\n"; + } else { + # try to generate usage + # $this->_generateUsage(); + } + $output .= "esac\n\n"; + $output .= "exit 0\n"; + return $output; + +} + +1; \ No newline at end of file diff --git a/lib/OpenSLX/DistroUtils/Engine.pm b/lib/OpenSLX/DistroUtils/Engine.pm new file mode 100644 index 00000000..5482f1d2 --- /dev/null +++ b/lib/OpenSLX/DistroUtils/Engine.pm @@ -0,0 +1,30 @@ +package OpenSLX::DistroUtils::Engine; + +use OpenSLX::Basics; + +sub new +{ + my $class = shift; + my $self = {}; + return bless $self, $class; +} + + +sub loadDistro { + my $self = shift; + my $distroName = shift; + + my $distro; + my $loaded = eval { + $distro = instantiateClass("OpenSLX::DistroUtils::${distroName}"); + return 0 if !$distro; # module does not exist, try next + 1; + }; + + if (!$loaded) { + $distro = instantiateClass("OpenSLX::DistroUtils::Base"); + } + return $distro; +} + +1; \ No newline at end of file diff --git a/lib/OpenSLX/DistroUtils/InitFile.pm b/lib/OpenSLX/DistroUtils/InitFile.pm new file mode 100644 index 00000000..e26ec6ad --- /dev/null +++ b/lib/OpenSLX/DistroUtils/InitFile.pm @@ -0,0 +1,92 @@ +package OpenSLX::DistroUtils::InitFile; + +use strict; +use warnings; + +sub new { + my $class = shift; + my $params = shift || {}; + my $self = { + }; + + $self->{'configHash'} = _initialConfigHash(); + + return bless $self, $class; +} + +sub _initialConfigHash() { + return { + 'name' => "", + 'requiredStart' => "", + 'requiredStop' => "", + 'defaultStart' => "2 3 4 5", + 'defaultStop' => "1", + 'shortDesc' => "", + + 'head' => { + 'blockDesc' => "head: file existing checks, etc.", + 'content' => {} + }, + 'functions' => { + 'blockDesc' => "functions: helper functions", + 'content' => {} + }, + 'start' => { + 'blockDesc' => "start: defines start function for initscript", + 'content' => {} + }, + 'stop' => { + 'blockDesc' => "stop: defines stop function for initscript", + 'content' => {} + }, + 'reload' => { + 'blockDesc' => "reload: defines reload function for initscript", + 'content' => {} + }, + 'restart' => { + 'blockDesc' => "restart: defines restart function for initscript", + 'content' => {} + }, + 'status' => { + 'blockDesc' => "status: defines status function for initscript", + 'content' => {} + }, + 'usage' => { + 'blockDesc' => "usage: defines usage function for initscript", + 'content' => {} + } + }; +} + +sub addToBlock { + my $self = shift; + my $blockName = shift; + my $content = shift; + my $priority = shift || 5; + + #check if block is valid.. + + push(@{$self->{'configHash'}->{$blockName}->{'content'}->{$priority}}, $content); + + return $self; +} + +sub setName { + my $self = shift; + my $name = shift; + + $self->{'configHash'}->{'name'} = $name; + return $self; +} + +sub setDesc { + my $self = shift; + my $desc = shift; + + $self->{'configHash'}->{'shortDesc'} = $desc; + return $self; +} + + + +1; \ No newline at end of file -- cgit v1.2.3-55-g7522