Rex

 view release on metacpan or  search on metacpan

ChangeLog  view on Meta::CPAN

 - added ubuntu support (tested with lts 10.04)
 - added can_run function, to test if a command is present

0.8.1 2011-07-03 Jan Gehring <jan.gehring, gmail.com>
 - fixed mageia detection
 - fixed bug if dnsdomainname returns no domainname
 - fixed mkdir bug on setting permissions, caused by a wrong merge

0.8.0 2011-06-26 Jan Gehring <jan.gehring, gmail.com>
 - added mageia support for services and packages
 - added chown, chgrp and chmod functions
 - mkdir, added possibility to specify the permission, the user and the
   group
 - added function delete_lines_matching
 - added function append_if_no_such_line
 - added reload action for services
 - extended db module to support insert, delete, update

0.7.1 2011-06-25 Jan Gehring <jan.gehring, gmail.com>
 - restored the backward compatibility with perl 5.8.x
 - suppress warning if no parameter is given

lib/Rex/Commands/File.pm  view on Meta::CPAN

      #### check and run before_change hook
      Rex::Hook::run_hook( file => "before_change", $file, %{$option} );
      ##############################

      if (Rex::is_sudo) {
        my $current_options =
          Rex::get_current_connection_object()->get_current_sudo_options;
        Rex::get_current_connection_object()->push_sudo_options( {} );

        if ( exists $current_options->{user} ) {
          $fs->chown( "$current_options->{user}:", $tmp_file_name );
        }
      }

      $fs->rename( $tmp_file_name, $file );
      Rex::get_current_connection_object()->pop_sudo_options()
        if (Rex::is_sudo);

      $__ret = { changed => 1 };

      Rex::get_current_connection()->{reporter}->report(

lib/Rex/Commands/File.pm  view on Meta::CPAN


    if ( exists $option->{"mode"} ) {
      $fs->chmod( $option->{"mode"}, $file );
    }

    if ( exists $option->{"group"} ) {
      $fs->chgrp( $option->{"group"}, $file );
    }

    if ( exists $option->{"owner"} ) {
      $fs->chown( $option->{"owner"}, $file );
    }

    my %stat_new = $fs->stat($file);

    if ( %stat_old && %stat_new && $stat_old{mode} ne $stat_new{mode} ) {
      Rex::get_current_connection()->{reporter}->report(
        changed => 1,
        message =>
          "File-System permissions changed from $stat_old{mode} to $stat_new{mode}.",
      );

lib/Rex/Commands/File.pm  view on Meta::CPAN

  }
  else {
    Rex::Logger::info("File not supported.");
    die("File ($file) not supported.");
  }

  $exec->exec($cmd);

  my $fs = Rex::Interface::Fs->create;
  if ( $option{owner} ) {
    $fs->chown( $option{owner}, $to, recursive => 1 );
  }

  if ( $option{group} ) {
    $fs->chgrp( $option{group}, $to, recursive => 1 );
  }

  if ( $option{mode} ) {
    $fs->chmod( $option{mode}, $to, recursive => 1 );
  }

lib/Rex/Commands/Fs.pm  view on Meta::CPAN

 rename("oldname", "newname");

 chdir("/tmp");

 is_file("/etc/passwd");
 is_dir("/etc");
 is_writeable("/tmp");
 is_writable("/tmp");

 chmod 755, "/tmp";
 chown "user", "/tmp";
 chgrp "group", "/tmp";


=head1 EXPORTED FUNCTIONS

=cut

package Rex::Commands::Fs;

use v5.14.4;

lib/Rex/Commands/Fs.pm  view on Meta::CPAN

use Rex::Interface::Exec;
use Rex::Interface::File;
use File::Basename;
use Rex::Commands::MD5;

use vars qw(@EXPORT);
use base qw(Rex::Exporter);

@EXPORT = qw(list_files ls
  unlink rm rmdir mkdir stat readlink symlink ln rename mv chdir cd cp
  chown chgrp chmod
  is_file is_dir is_readable is_writeable is_writable is_symlink
  df du
  mount umount
  glob);

use vars qw(%file_handles);

=head2 Changing content

These commands are supposed to change the contents of the file system.

lib/Rex/Commands/Fs.pm  view on Meta::CPAN

  my $owner         = $options->{"owner"}         || "";
  my $group         = $options->{"group"}         || "";
  my $not_recursive = $options->{"not_recursive"} || 0;

  if ($not_recursive) {
    if ( !$fs->mkdir($dir) ) {
      Rex::Logger::debug("Can't create directory $dir");
      die("Can't create directory $dir");
    }

    &chown( $owner, $dir ) if $owner;
    &chgrp( $group, $dir ) if $group;
    &chmod( $mode, $dir )  if $mode;
  }
  else {
    if ( !Rex::Helper::File::Spec->file_name_is_absolute($dir) ) {
      $dir = Rex::Helper::File::Spec->rel2abs($dir);
    }

    my @directories = __splitdir($dir);
    my $path_so_far = shift @directories;

    for my $part (@directories) {
      $path_so_far = Rex::Helper::File::Spec->join( $path_so_far, $part );

      if ( !is_dir($path_so_far) && !is_file($path_so_far) ) {
        if ( !$fs->mkdir($path_so_far) ) {
          Rex::Logger::debug("Can't create directory $dir");
          die("Can't create directory $dir");
        }

        &chown( $owner, $path_so_far ) if $owner;
        &chgrp( $group, $path_so_far ) if $group;
        &chmod( $mode, $path_so_far )  if $mode;
      }
    }
  }

  my %new_stat = &stat($dir);

  if ( !$not_created ) {
    Rex::get_current_connection()->{reporter}

lib/Rex/Commands/Fs.pm  view on Meta::CPAN

  Rex::get_current_connection()->{reporter}
    ->report_resource_end( type => "mkdir", name => $dir );

  return 1;
}

sub __splitdir {
  return Rex::Helper::File::Spec->splitdir(shift);
}

=head3 chown($owner, $path)

Change the owner of a file or a directory.

 chown "www-data", "/var/www/html";

 chown "www-data", "/var/www/html",
                recursive => 1;


This command will not be reported.

If you want to use reports, please use the L<file|Rex::Commands::File#file> resource instead.

=cut

sub chown {
  my ( $user, $file, @opts ) = @_;

  $file = resolv_path($file);
  my $fs = Rex::Interface::Fs->create;
  $fs->chown( $user, $file, @opts ) or die("Can't chown $file");
}

=head3 chgrp($group, $path)

Change the group of a file or a directory.

 chgrp "nogroup", "/var/www/html";

 chgrp "nogroup", "/var/www/html",
              recursive => 1;

lib/Rex/Commands/Pkg.pm  view on Meta::CPAN

        }
        else {
          Rex::Logger::debug("MD5 is equal. Not uploading $source -> $package");
        }

        eval { $new_md5 = md5($package); };
      }
    }

    if ( exists $option->{"owner"} ) {
      chown $option->{"owner"}, $package;
    }

    if ( exists $option->{"group"} ) {
      chgrp $option->{"group"}, $package;
    }

    if ( exists $option->{"mode"} ) {
      chmod $option->{"mode"}, $package;
    }

lib/Rex/Interface/File/Sudo.pm  view on Meta::CPAN

      }
      else {
        $file = $link;
      }
      $link = $self->_fs->readlink($link);
    }
    $self->{file_stat} = { $self->_fs->stat( $self->{file} ) };

    $self->_fs->cp( $file, $self->{rndfile} );
    $self->_fs->chmod( 600, $self->{rndfile} );
    $self->_fs->chown( Rex::Commands::connection->get_auth_user,
      $self->{rndfile} );
  }

  $self->{fh}->open( $mode, $self->{rndfile} );

  Rex::get_current_connection_object()->pop_sudo_options();

  return $self->{fh};
}

lib/Rex/Interface/File/Sudo.pm  view on Meta::CPAN

  $self->{fh}->close;

  if ( exists $self->{mode}
    && ( $self->{mode} eq ">" || $self->{mode} eq ">>" ) )
  {

    my $exec = Rex::Interface::Exec->create;
    if ( $self->{file_stat} ) {
      my %stat = $self->_fs->stat( $self->{file} );
      $self->_fs->chmod( $stat{mode}, $self->{rndfile} );
      $self->_fs->chown( $stat{uid}, $self->{rndfile} );
      $self->_fs->chgrp( $stat{gid}, $self->{rndfile} );
    }
    $self->_fs->rename( $self->{rndfile}, $self->{file} );

    #$exec->exec("cat " . $self->{rndfile} . " >'" . $self->{file} . "'");
  }

  $self->{fh} = undef;

  $self->_fs->unlink( $self->{rndfile} );

lib/Rex/Interface/Fs/Base.pm  view on Meta::CPAN

  Rex::Logger::debug( "Removing directories: " . join( ", ", @dirs ) );
  my $exec = Rex::Interface::Exec->create;
  $exec->exec( "/bin/rm -rf " . join( " ", @dirs ) );

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

  die( "Error removing directory: " . join( ", ", @dirs ) )
    if ( Rex::Config->get_autodie );
}

sub chown {
  my ( $self, $user, $file, @opts ) = @_;
  my $options = {@opts};
  ($file) = $self->_normalize_path($file);

  my $recursive = "";
  if ( exists $options->{"recursive"} && $options->{"recursive"} == 1 ) {
    $recursive = " -R ";
  }

  my $exec = Rex::Interface::Exec->create;

  if ( $exec->can_run( ['chown'] ) ) {
    $exec->exec("chown $recursive $user $file");

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

    die("Error running chown $recursive $user $file")
      if ( Rex::Config->get_autodie );
  }
  else {
    Rex::Logger::debug("Can't find `chown`.");
    return 1; # fake success for windows
  }
}

sub chgrp {
  my ( $self, $group, $file, @opts ) = @_;
  my $options = {@opts};
  ($file) = $self->_normalize_path($file);

  my $recursive = "";

lib/Rex/Interface/Fs/HTTP.pm  view on Meta::CPAN

  Rex::Logger::debug( "Removing directories: " . join( ", ", @dirs ) );
  my $ok = 0;
  for my $dir (@dirs) {
    my $resp = connection->post( "/fs/rmdir", { path => $dir } );
    $ok = $resp->{ok};
  }

  return $ok;
}

sub chown {
  my ( $self, $user, $file, @opts ) = @_;

  my $resp = connection->post(
    "/fs/chown",
    {
      user    => $user,
      path    => $file,
      options => {@opts},
    }
  );

  return $resp->{ok};
}



( run in 1.709 second using v1.01-cache-2.11-cpan-5511b514fd6 )