From 40723717042782953fc94985b1d63802259e77af Mon Sep 17 00:00:00 2001 From: Sebastian Schmelzer Date: Tue, 14 Apr 2009 12:59:13 +0000 Subject: current state of work on runlevel generator.. (still not fully useable) git-svn-id: http://svn.openslx.org/svn/openslx/openslx/trunk@2813 95ad53e4-c205-0410-b2fa-d234c58c8868 --- lib/OpenSLX/DistroUtils/Base.pm | 302 +++++++++++++++++++++++++++++++--------- 1 file changed, 239 insertions(+), 63 deletions(-) (limited to 'lib/OpenSLX/DistroUtils/Base.pm') diff --git a/lib/OpenSLX/DistroUtils/Base.pm b/lib/OpenSLX/DistroUtils/Base.pm index e7e4f367..90cad7a0 100644 --- a/lib/OpenSLX/DistroUtils/Base.pm +++ b/lib/OpenSLX/DistroUtils/Base.pm @@ -1,4 +1,4 @@ -# Copyright (c) 2008 - OpenSLX GmbH +# Copyright (c) 2008, 2009 - OpenSLX GmbH # # This program is free software distributed under the GPL version 2. # See http://openslx.org/COPYING @@ -15,7 +15,11 @@ package OpenSLX::DistroUtils::Base; use Data::Dumper; use OpenSLX::Utils; +use Clone qw(clone); +use Switch; +use strict; +use warnings; sub new { @@ -34,7 +38,7 @@ sub dumpInit print $self->generateInitFile($initFile); } -sub _combineBlock +sub _concatContent { my $self = shift; my $block = shift; @@ -46,7 +50,7 @@ sub _combineBlock $output .= "\n"; my $content = $block->{'content'}; - while ( ($priority, $contentArray) = each %$content ) + while ( my ($priority, $contentArray) = each %$content ) { $output .= join("\n", @$contentArray); $output .= "\n"; @@ -103,82 +107,254 @@ sub _insertSystemHelperFunctions return $content; } -sub generateInitFile +sub _renderHighlevelConfig { my $self = shift; my $initFile = shift; - my $block; - my $config = $initFile->{'configHash'}; + my $element; + my $hlc = $initFile->{'configHash'}->{'highlevelConfig'}; - print Dumper($initFile->{'configHash'}); + while ( $element = shift(@$hlc)){ + switch ($element->{type}) { + case 'daemon' { + my $tpl; + $tpl = "%s_BIN=%s \n"; + $tpl .= "[ -x %s_BIN ] || exit 5\n\n"; + $tpl .= "%s_OPTS=\"%s\" \n"; + $tpl .= "[ -f /etc/sysconfig/%s ] . /etc/sysconfig/%s \n\n"; + $tpl .= "[ -f /etc/default/%s ] . /etc/default/%s \n\n"; + $tpl .= "%s_PIDFILE=\"/var/run/%s.init.pid\" \n\n"; + $initFile->addToBlock('head', + sprintf( + $tpl, + uc($element->{shortname}), + $element->{binary}, + uc($element->{shortname}), + uc($element->{shortname}), + $element->{parameters}, + $element->{shortname}, + $element->{shortname}, + $element->{shortname}, + $element->{shortname}, + uc($element->{shortname}), + $element->{shortname} + ) + ); + + $tpl = "echo -n \"Starting %s \"\n"; + $tpl .= "startproc -f -p \$%s_PIDFILE \$%s_BIN \$%s_OPTS\n"; + $tpl .= "rc_status -v"; + $initFile->addToCase('start', + sprintf( + $tpl, + $element->{desc}, + uc($element->{shortname}), + uc($element->{shortname}), + uc($element->{shortname}) + ) + ); + + $tpl = "echo -n \"Shutting down %s\" \n"; + $tpl .= "killproc -p \$%s_PIDFILE -TERM \$%s_BIN\n"; + $tpl .= "rc_status -v"; + $initFile->addToCase('stop', + sprintf( + $tpl, + $element->{desc}, + uc($element->{shortname}), + uc($element->{shortname}) + ) + ); + + $tpl = "## Stop the service and if this succeeds (i.e. the \n"; + $tpl .= "## service was running before), start it again.\n"; + $tpl .= "\$0 status >/dev/null && \$0 restart\n\n"; + $tpl .= "# Remember status and be quiet\n"; + $tpl .= "rc_status"; + $initFile->addToCase('try-restart', + $tpl + ); + + $tpl = "## Stop the service and regardless of whether it was \n"; + $tpl .= "## running or not, start it again.\n"; + $tpl .= "\$0 stop\n"; + $tpl .= "\$0 start\n\n"; + $tpl .= "# Remember status and be quiet\n"; + $tpl .= "rc_status"; + $initFile->addToCase('restart', + $tpl + ); + + $tpl = "echo -n \"Reload service %s\"\n"; + $tpl .= "killproc -p \$%s_PIDFILE -HUP \$%s_BIN\n"; + $tpl .= "rc_status -v"; + $initFile->addToCase('reload', + sprintf( + $tpl, + $element->{desc}, + uc($element->{shortname}), + uc($element->{shortname}), + uc($element->{shortname}) + ) + ); + + $tpl = "echo -n \"Checking for service %s\"\n"; + $tpl .= "checkproc -p \$%s_PIDFILE \$%s_BIN\n"; + $tpl .= "rc_status -v"; + $initFile->addToCase('status', + sprintf( + $tpl, + $element->{desc}, + uc($element->{shortname}), + uc($element->{shortname}) + ) + ); + + + } + } + } +} + + +sub _getInitsystemIncludes +{ + return "\n"; +} + +sub _renderCasePrefix +{ + return "\n"; +} + +sub _renderFooter +{ + return "exit 0\n"; +} + +sub _generateUsage +{ + my $self = shift; + my $usage = shift; + my $tpl; + + $tpl = "## print out usage \n"; + $tpl .= "echo \"Usage: \$0 {%s}\" >&2 \n"; + $tpl .= "exit 1"; + + return sprintf( + $tpl, + $usage + ); +} + +sub _getAuthorBlock +{ + my $tpl; + + $tpl = "# Copyright (c) 2009 - OpenSLX GmbH \n"; + $tpl .= "# \n"; + $tpl .= "# This program is free software distributed under the GPL version 2. \n"; + $tpl .= "# See http://openslx.org/COPYING \n"; + $tpl .= "# \n"; + $tpl .= "# If you have any feedback please consult http://openslx.org/feedback and \n"; + $tpl .= "# send your suggestions, praise, or complaints to feedback\@openslx.org \n"; + $tpl .= "# \n"; + $tpl .= "# General information about OpenSLX can be found at http://openslx.org/ \n"; + $tpl .= "# -----------------------------------------------------------------------------\n"; + $tpl .= "# §filename§ \n"; + $tpl .= "# - §desc§ \n"; + $tpl .= "# §generated§ \n"; + $tpl .= "# -----------------------------------------------------------------------------\n\n"; + + return sprintf( + $tpl + ); +} + +sub generateInitFile +{ + my $self = shift; + my $initFile = shift; + my $content; + my @usage; + + # get a copy of initFile object before modifying it.. + my $initFileCopy = clone($initFile); + + $self->_renderHighlevelConfig($initFileCopy); - $output = "#!/bin/sh\n\n"; + my $config = $initFileCopy->{'configHash'}; + my $output; + + # head + $output = "#!/bin/sh\n"; + $output .= $self->_getAuthorBlock(); $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"; - $block = $self->_combineBlock($config->{'start'}); - $block =~ s/^/ /mg; - $output .= $block; - $output .= " ;;\n"; - } else { - # trigger error - # start is essential - } - if (keys(%{$config->{'stop'}->{'content'}}) > 0) { - $output .= " stop)\n"; - $block = $self->_combineBlock($config->{'stop'}); - $block =~ s/^/ /mg; - $output .= $block; - $output .= " ;;\n"; - } else { - # trigger error - # stop is essential + $output .= $self->_getInitsystemIncludes(); + + if (keys(%{$config->{'blocks'}->{'head'}->{'content'}}) > 0) { + $output .= $self->_concatContent($config->{'blocks'}->{'head'}); } - if (keys(%{$config->{'reload'}->{'content'}}) > 0) { - $output .= " reload)\n"; - $block = $self->_combineBlock($config->{'reload'}); - $block =~ s/^/ /mg; - $output .= $block; - $output .= " ;;\n"; + + # functions + if (keys(%{$config->{'blocks'}->{'functions'}->{'content'}}) > 0) { + $output .= $self->_concatContent($config->{'blocks'}->{'functions'}); } - if (keys(%{$config->{'restart'}->{'content'}}) > 0) { - $output .= " restart)\n"; - $block = $self->_combineBlock($config->{'restart'}); - $block =~ s/^/ /mg; - $output .= $block; - $output .= " ;;\n"; + + # case block + $output .= $self->_renderCasePrefix(); + $output .= "\ncase \"\$1\" in \n"; + + # get caseBlocks in defined order + my @blocks = sort{ + $config->{'caseBlocks'}->{$a}->{'order'} <=> + $config->{'caseBlocks'}->{$b}->{'order'} + } + keys(%{$config->{'caseBlocks'}}); + + # case block + while (@blocks) + { + my $block= shift(@blocks); + if (keys(%{$config->{'caseBlocks'}->{$block}->{'content'}}) > 0) { + push(@usage, $block); + $output .= " $block)\n"; + $content = $self->_concatContent($config->{'caseBlocks'}->{$block}); + $content =~ s/^/ /mg; + $output .= $content; + $output .= " ;;\n"; + } else { + if ($config->{'caseBlocks'}->{$block}->{'required'}) { + print "required block $block undefined"; + } + } } - if (keys(%{$config->{'status'}->{'content'}}) > 0) { - $output .= " status)\n"; - $block = $self->_combineBlock($config->{'status'}); - $block =~ s/^/ /mg; - $output .= $block; + + # autogenerate usage + if (scalar(grep(/usage/, @usage)) == 0) { + $initFileCopy->addToCase( + 'usage', + $self->_generateUsage(join(', ',@usage)) + ); + + $output .= " *)\n"; + $content = $self->_concatContent($config->{'caseBlocks'}->{'usage'}); + $content =~ s/^/ /mg; + $output .= $content; $output .= " ;;\n"; + } - if (keys(%{$config->{'usage'}->{'content'}}) > 0) { - $output .= " *)\n"; - $block = $self->_combineBlock($config->{'usage'}); - $block =~ s/^/ /mg; - $output .= $block; - $output .= " exit 1\n"; - } else { - # try to generate usage - # $this->_generateUsage(); - } + + # footer $output .= "esac\n\n"; - $output .= "exit 0\n"; + $output .= $self->_renderFooter(); + return $output; } + 1; \ No newline at end of file -- cgit v1.2.3-55-g7522