summaryrefslogtreecommitdiffstats
path: root/lib/OpenSLX/DistroUtils/Base.pm
diff options
context:
space:
mode:
authorSebastian Schmelzer2008-12-15 00:16:12 +0100
committerSebastian Schmelzer2008-12-15 00:16:12 +0100
commit7606f85ee41a0e123e5e753f3a5b6a8ae31dc098 (patch)
treeca842313c73a906c5302fb8bda7b52ba7e7dcdda /lib/OpenSLX/DistroUtils/Base.pm
parentxserver plugin: * make installation less verbose (diff)
downloadcore-7606f85ee41a0e123e5e753f3a5b6a8ae31dc098.tar.gz
core-7606f85ee41a0e123e5e753f3a5b6a8ae31dc098.tar.xz
core-7606f85ee41a0e123e5e753f3a5b6a8ae31dc098.zip
* 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
Diffstat (limited to 'lib/OpenSLX/DistroUtils/Base.pm')
-rw-r--r--lib/OpenSLX/DistroUtils/Base.pm136
1 files changed, 136 insertions, 0 deletions
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