This file is indexed.

/usr/share/perl5/Octopussy/ServiceGroup.pm is in octopussy 1.0.6-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# $HeadURL$
# $Revision: 579 $
# $Date: 2012-06-24 02:11:57 +0100 (Sun, 24 Jun 2012) $
# $Author: sebthebert $

=head1 NAME

Octopussy::ServiceGroup - Octopussy ServiceGroup Module

=cut

package Octopussy::ServiceGroup;

use strict;
use warnings;
use Readonly;

use List::MoreUtils qw(any);

use AAT;
use AAT::Utils qw( ARRAY NOT_NULL );
use AAT::XML;
use Octopussy::FS;

Readonly my $FILE_SERVICEGROUPS => 'servicegroups';
Readonly my $XML_ROOT           => 'octopussy_servicegroups';

=head1 FUNCTIONS

=head2 Add($conf_sg)

Adds a new ServiceGroup

=cut 

sub Add
{
  my $conf_sg = shift;
  my @sgs     = ();

  my $file = Octopussy::FS::File($FILE_SERVICEGROUPS);
  my $conf = AAT::XML::Read($file);
  if (any { $_->{sg_id} eq $conf_sg->{sg_id} } ARRAY($conf->{servicegroup}))
  {
    return ('_MSG_SERVICEGROUP_ALREADY_EXISTS');
  }
  push @{$conf->{servicegroup}}, $conf_sg;
  AAT::XML::Write($file, $conf, $XML_ROOT);

  return (undef);
}

=head1 Remove($servicegroup)

Removes ServiceGroup '$servicegroup'

=cut

sub Remove
{
  my $servicegroup = shift;

  my $file = Octopussy::FS::File($FILE_SERVICEGROUPS);
  my $conf = AAT::XML::Read($file);
  my @sgs =
    grep { $_->{sg_id} ne $servicegroup } ARRAY($conf->{servicegroup});
  $conf->{servicegroup} = \@sgs;
  AAT::XML::Write($file, $conf, $XML_ROOT);

  return ($servicegroup);
}

=head2 List()

Gets list of ServiceGroup

=cut

sub List
{
  my @sgs =
    AAT::XML::File_Array_Values(Octopussy::FS::File($FILE_SERVICEGROUPS),
    'servicegroup', 'sg_id');

  return (@sgs);
}

=head2 Configuration($servicegroup)

Gets the configuration for the ServiceGroup '$servicegroup'

=cut

sub Configuration
{
  my $servicegroup = shift;

  my $conf = AAT::XML::Read(Octopussy::FS::File($FILE_SERVICEGROUPS));
  foreach my $sg (ARRAY($conf->{servicegroup}))
  {
    return ($sg) if ($sg->{sg_id} eq $servicegroup);
  }

  return (undef);
}

=head2 Configurations($sort)

Gets the configuration for all ServiceGroups

=cut

sub Configurations
{
  my $sort = shift || 'sg_id';
  my (@configurations, @sorted_configurations) = ((), ());
  my @sgs = List();

  foreach my $sg (@sgs)
  {
    my $conf = Configuration($sg);
    push @configurations, $conf;
  }
  foreach my $c (sort { $a->{$sort} cmp $b->{$sort} } @configurations)
  {
    push @sorted_configurations, $c;
  }

  return (@sorted_configurations);
}

=head2 Services($servicegroup)

Returns list of Services (sorted by rank) in ServiceGroup '$servicegroup'

=cut

sub Services
{
  my $servicegroup = shift;
  my $conf         = Configuration($servicegroup);
  my @services     = ();

  foreach my $s (sort { $a->{rank} cmp $b->{rank} } ARRAY($conf->{service}))
  {
    push @services, $s;
  }

  return (@services);
}

=head2 Add_Service($servicegroup, $service)

Adds Service '$service' to ServiceGroup '$servicegroup'

=cut

sub Add_Service
{
  my ($servicegroup, $service) = @_;
  my $file = Octopussy::FS::File($FILE_SERVICEGROUPS);
  my $conf = AAT::XML::Read($file);
  my @sgs  = ();
  foreach my $sg (ARRAY($conf->{servicegroup}))
  {
    my @services = ARRAY($sg->{service});
    if ($sg->{sg_id} eq $servicegroup)
    {
      my $rank = scalar(@services) + 1;
      $rank = sprintf("%02d", $rank);
      if (any { $_->{sid} =~ /^$service$/ } @services)
      {
        return ();
      }
      push @services, {sid => $service, rank => $rank};
    }
    $sg->{service} = \@services;
    push @sgs, $sg;
  }
  $conf->{servicegroup} = \@sgs;

  AAT::XML::Write($file, $conf, $XML_ROOT);

  return ($service);
}

=head2 Remove_Service($servicegroup, $service)

Removes Service '$service' from ServiceGroup '$servicegroup'

=cut

sub Remove_Service
{
  my ($servicegroup, $service) = @_;
  my $file = Octopussy::FS::File($FILE_SERVICEGROUPS);
  my $conf = AAT::XML::Read($file);
  my @sgs  = ();
  foreach my $sg (ARRAY($conf->{servicegroup}))
  {
    if ($sg->{sg_id} eq $servicegroup)
    {
      my @services = ();
      my $rank     = undef;
      foreach my $s (ARRAY($sg->{service}))
      {
        if ($s->{sid} ne $service) { push @services, $s; }
        else                       { $rank = $s->{rank}; }
      }
      return () if (!defined $rank);
      foreach my $s (@services)
      {
        if ($s->{rank} > $rank)
        {
          $s->{rank} -= 1;
          $s->{rank} = sprintf("%02d", $s->{rank});
        }
      }
      $sg->{service} = \@services;
    }
    push @sgs, $sg;
  }
  $conf->{servicegroup} = \@sgs;
  AAT::XML::Write($file, $conf, $XML_ROOT);

  return (scalar @sgs);
}

=head2 Move_Service($servicegroup, $service, $direction)

Moves Service '$service' in ServiceGroup '$servicegroup' Services List
in Direction Up or Down ('$direction')

=cut

sub Move_Service
{
  my ($servicegroup, $service, $direction) = @_;
  my $rank = undef;

  my @sgs  = ();
  my $file = Octopussy::FS::File($FILE_SERVICEGROUPS);
  my $conf = AAT::XML::Read($file);
  foreach my $sg (ARRAY($conf->{servicegroup}))
  {
    if ($sg->{sg_id} eq $servicegroup)
    {
      my @services = ();
      my $max = (defined $sg->{service} ? scalar(@{$sg->{service}}) : 0);
      $max = ('0' x (2 - length $max)) . $max;
      foreach my $s (ARRAY($sg->{service}))
      {
        if ($s->{sid} eq $service)
        {
          return ('01') if (($s->{rank} eq '01') && ($direction eq 'up'));
          return ($max)
            if (($s->{rank} eq $max) && ($direction eq 'down'));
          $s->{rank} = ($direction eq 'up' ? $s->{rank} - 1 : $s->{rank} + 1);
          $s->{rank} = sprintf("%02d", $s->{rank});
          $rank = $s->{rank};
        }
        push @services, $s;
      }
      $sg->{service} = \@services;
      my @services2 = ();
      foreach my $s (ARRAY($sg->{service}))
      {
        if (($s->{rank} eq $rank) && ($s->{sid} ne $service))
        {
          $s->{rank} = ($direction eq 'up' ? $s->{rank} + 1 : $s->{rank} - 1);
          $s->{rank} = sprintf("%02d", $s->{rank});
        }
        push @services2, $s;
      }
      $sg->{service} = \@services2;
    }
    push @sgs, $sg;
  }
  $conf->{servicegroup} = \@sgs;

  AAT::XML::Write($file, $conf, $XML_ROOT);

  return ($rank);
}

=head2 Valid_Name($name)

Checks that '$name' is valid for a ServiceGroup name

=cut

sub Valid_Name
{
    my $name = shift;

    return (1)  if ((NOT_NULL($name)) && ($name =~ /^[a-z][a-z0-9_-]*$/i));

    return (0);
}

1;

=head1 AUTHOR

Sebastien Thebert <octo.devel@gmail.com>

=cut