This file is indexed.

/usr/share/perl5/Rex/Service/Base.pm is in rex 1.4.1-1.

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
#
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:

package Rex::Service::Base;

use strict;
use warnings;

our $VERSION = '1.4.1'; # VERSION

use Rex::Commands::Run;
use Rex::Helper::Run;
use Rex::Logger;

my $known_services = {};

sub new {
  my $that  = shift;
  my $proto = ref($that) || $that;
  my $self  = {@_};

  bless( $self, $proto );

  $self->{__cmd_output__} = '';

  return $self;
}

sub get_output { shift->{__cmd_output__}; }

sub _prepare_service_name {
  my ( $self, $service_name ) = @_;

  if ( !$self->service_exists($service_name)
    && Rex::Config->get_check_service_exists )
  {
    die "Service $service_name not found.";
  }

  return $service_name;
}

sub _filter_options {
  my ( $self, $service, $options ) = @_;

  for my $key (qw/start stop status restart reload ensure_stop ensure_start/) {
    if ( exists $options->{$key} ) {
      $known_services->{$service}->{$key} = $options->{$key};
    }
  }
}

sub _execute {
  my ( $self, $cmd ) = @_;

  $self->{__cmd_output__} = i_run $cmd, nohup => 1;

  if ( $? == 0 ) {
    return 1;
  }

  return 0;
}

sub start {
  my ( $self, $service, $options ) = @_;
  $service = $self->_prepare_service_name($service);
  $self->_filter_options( $service, $options );

  my $cmd = sprintf $self->{commands}->{start}, $service;

  if ( exists $known_services->{$service}->{start} ) {
    $cmd = $known_services->{$service}->{start};
  }

  return $self->_execute($cmd);
}

sub restart {
  my ( $self, $service, $options ) = @_;
  $service = $self->_prepare_service_name($service);
  $self->_filter_options( $service, $options );

  my $cmd = sprintf $self->{commands}->{restart}, $service;

  if ( exists $known_services->{$service}->{restart} ) {
    $cmd = $known_services->{$service}->{restart};
  }

  return $self->_execute($cmd);
}

sub stop {
  my ( $self, $service, $options ) = @_;
  $service = $self->_prepare_service_name($service);
  $self->_filter_options( $service, $options );

  my $cmd = sprintf $self->{commands}->{stop}, $service;

  if ( exists $known_services->{$service}->{stop} ) {
    $cmd = $known_services->{$service}->{stop};
  }

  return $self->_execute($cmd);
}

sub reload {
  my ( $self, $service, $options ) = @_;
  $service = $self->_prepare_service_name($service);
  $self->_filter_options( $service, $options );

  my $cmd = sprintf $self->{commands}->{reload}, $service;

  if ( exists $known_services->{$service}->{reload} ) {
    $cmd = $known_services->{$service}->{reload};
  }

  return $self->_execute($cmd);
}

sub status {
  my ( $self, $service, $options ) = @_;
  $service = $self->_prepare_service_name($service);
  $self->_filter_options( $service, $options );

  my $cmd = sprintf $self->{commands}->{status}, $service;

  if ( exists $known_services->{$service}->{status} ) {
    $cmd = $known_services->{$service}->{status};
  }

  return $self->_execute($cmd);
}

sub ensure {
  my ( $self, $service, $options ) = @_;
  $service = $self->_prepare_service_name($service);
  $self->_filter_options( $service, $options );

  my $what = $options->{ensure};

  if ( $what =~ /^stop/ ) {
    $self->stop( $service, $options );
    my $cmd = sprintf $self->{commands}->{ensure_stop}, $service;

    if ( exists $known_services->{$service}->{ensure_stop} ) {
      $cmd = $known_services->{$service}->{ensure_stop};
    }

    return $self->_execute($cmd);
  }
  elsif ( $what =~ /^start/ || $what =~ m/^run/ ) {
    $self->start( $service, $options );
    my $cmd = sprintf $self->{commands}->{ensure_start}, $service;

    if ( exists $known_services->{$service}->{ensure_start} ) {
      $cmd = $known_services->{$service}->{ensure_start};
    }

    return $self->_execute($cmd);
  }
}

sub action {
  my ( $self, $service, $action ) = @_;
  $service = $self->_prepare_service_name($service);

  my $cmd = sprintf $self->{commands}->{action}, $service, $action;
  return $self->_execute($cmd);
}

sub service_exists {
  my ( $self, $service ) = @_;

  # always return true if we can't verify if a service exists
  if ( !exists $self->{commands}->{service_exists} ) {
    return 1;
  }

  my $cmd = sprintf $self->{commands}->{service_exists}, $service;
  return $self->_execute($cmd);
}

1;