[vhffs-dev] [2043] reworked cron_create, cron_delete, bazaar_create, bazaar_delete

[ Thread Index | Date Index | More vhffs.org/vhffs-dev Archives ]


Revision: 2043
Author:   gradator
Date:     2012-02-24 01:01:17 +0100 (Fri, 24 Feb 2012)
Log Message:
-----------
reworked cron_create, cron_delete, bazaar_create, bazaar_delete

Modified Paths:
--------------
    trunk/vhffs-api/src/Vhffs/Object.pm
    trunk/vhffs-api/src/Vhffs/Robots/Bazaar.pm
    trunk/vhffs-api/src/Vhffs/Robots/Cron.pm
    trunk/vhffs-robots/src/bazaar_create.pl
    trunk/vhffs-robots/src/bazaar_delete.pl
    trunk/vhffs-robots/src/bazaar_public.pl
    trunk/vhffs-robots/src/cron_create.pl
    trunk/vhffs-robots/src/cron_delete.pl

Modified: trunk/vhffs-api/src/Vhffs/Object.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Object.pm	2012-02-23 21:31:57 UTC (rev 2042)
+++ trunk/vhffs-api/src/Vhffs/Object.pm	2012-02-24 00:01:17 UTC (rev 2043)
@@ -270,16 +270,16 @@
 
 =head2 commit
 
-Apply all changes that were made on this object. Returns negative value if failed, positive if success.
+Apply all changes that were made on this object. Returns undef value if failed, true if success.
 
 =cut
 sub commit {
 	my $self = shift;
-	my $request;
 
-	$request = 'UPDATE vhffs_object SET state=?, description=?, refuse_reason=?, owner_uid=?, owner_gid=? WHERE object_id=?';
+	my $request = 'UPDATE vhffs_object SET state=?, description=?, refuse_reason=?, owner_uid=?, owner_gid=? WHERE object_id=?';
 	my $result = $self->{'db'}->prepare($request);
-	$result->execute( $self->{'state'} , $self->{'description'} , $self->{'refuse_reason'}, $self->{'owner_uid'} , $self->{'owner_gid'} , $self->{'object_id'} );
+	$result->execute( $self->{'state'} , $self->{'description'} , $self->{'refuse_reason'}, $self->{'owner_uid'} , $self->{'owner_gid'} , $self->{'object_id'} ) or return undef;
+	return 1;
 }
 
 =pod
@@ -471,15 +471,30 @@
 	elsif( $value == Vhffs::Constants::WAITING_FOR_CREATION ) {
 		$self->add_history( 'Validation accepted. Will be created' );
 	}
+	elsif( $value == Vhffs::Constants::CREATION_ERROR ) {
+		$self->add_history( 'An error occured while creating this object' );
+	}
 	elsif( $value == Vhffs::Constants::ACTIVATED ) {
 		$self->add_history( 'Is now active for production' );
 	}
-	elsif( $value == Vhffs::Constants::CREATION_ERROR ) {
-		$self->add_history( 'An error occured while creating the object' );
+	elsif( $value == Vhffs::Constants::SUSPEND_PENDING ) {
+		$self->add_history( 'Waiting for suspension' );
 	}
-	if( $value == Vhffs::Constants::WAITING_FOR_DELETION ) {
+	elsif( $value == Vhffs::Constants::SUSPENDED ) {
+		$self->add_history( 'Suspended' );
+	}
+	elsif( $value == Vhffs::Constants::WAITING_FOR_MODIFICATION ) {
+		$self->add_history( 'Waiting for modification' );
+	}
+	elsif( $value == Vhffs::Constants::MODIFICATION_ERROR ) {
+		$self->add_history( 'An error occured while modifying this object' );
+	}
+	elsif( $value == Vhffs::Constants::WAITING_FOR_DELETION ) {
 		$self->add_history( 'Will be deleted' );
 	}
+	elsif( $value == Vhffs::Constants::DELETION_ERROR ) {
+		$self->add_history( 'An error occured while deleting this objet' );
+	}
 }
 
 =pod

Modified: trunk/vhffs-api/src/Vhffs/Robots/Bazaar.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Robots/Bazaar.pm	2012-02-23 21:31:57 UTC (rev 2042)
+++ trunk/vhffs-api/src/Vhffs/Robots/Bazaar.pm	2012-02-24 00:01:17 UTC (rev 2043)
@@ -36,31 +36,34 @@
 use File::Basename;
 
 package Vhffs::Robots::Bazaar;
-
-use Vhffs::Services::Bazaar;
 use Vhffs::Constants;
 use Vhffs::Functions;
+use Vhffs::Robots;
+use Vhffs::Services::Bazaar;
 
-sub create_repo
-{
+sub create {
 	my $bazaar = shift;
-	return -1 unless defined $bazaar;
-	return -1 if $bazaar->get_status != Vhffs::Constants::WAITING_FOR_CREATION;
+	return undef unless defined $bazaar and $bazaar->get_status == Vhffs::Constants::WAITING_FOR_CREATION;
 
 	my $dir = $bazaar->get_dir;
 
 	if( -e $dir ) {
 		$bazaar->set_status( Vhffs::Constants::CREATION_ERROR );
-		$bazaar->commit();
-		$bazaar->add_history('Error, directory of this bazaar repository already exists! Administrators must fix the problem.');
-		return -1;
+		$bazaar->commit;
+		Vhffs::Robots::vhffs_log( $vhffs, 'An error occured while creating bazaar repository '.$bazaar->get_reponame.' to the filesystem' );
+		return undef;
 	}
 
-	# TODO: check make_path
-	File::Path::make_path( $dir );
+	File::Path::make_path( $dir, { error => \my $errors });
+	if(@$errors) {
+		$bazaar->set_status( Vhffs::Constants::CREATION_ERROR );
+		$bazaar->commit;
+		Vhffs::Robots::vhffs_log( $vhffs, 'An error occured while creating bazaar repository '.$bazaar->get_reponame.' to the filesystem' );
+		return undef;
+	}
+
 	Vhffs::Robots::chmod_recur( $dir , 0664 , 02775 );
 	Vhffs::Robots::chown_recur( $dir , $bazaar->get_owner_uid , $bazaar->get_owner_gid );
-	$bazaar->add_history('Ok, robots find the empty directory and will create bazaar repository');
 
 	my $oldcwd = Cwd::getcwd();
 	if( chdir($dir) ) {
@@ -79,50 +82,63 @@
 
 	unless( -d $dir.'/.bzr' ) {
 		$bazaar->set_status( Vhffs::Constants::CREATION_ERROR );
-		$bazaar->commit();
-		$bazaar->add_history('Error, nothing was created after calling the bazaar binary, something is wrong.');
-		return -1;
+		$bazaar->commit;
+		Vhffs::Robots::vhffs_log( $vhffs, 'An error occured while creating bazaar repository '.$bazaar->get_reponame.' to the filesystem' );
+		return undef;
 	}
 
-	Vhffs::Robots::chmod_recur( $dir , 0664 , 02775 );
-	Vhffs::Robots::chown_recur( $dir , $bazaar->get_owner_uid , $bazaar->get_owner_gid );
+	Vhffs::Robots::chmod_recur( $dir, 0664, 02775 );
+	Vhffs::Robots::chown_recur( $dir, $bazaar->get_owner_uid, $bazaar->get_owner_gid );
 
-	$bazaar->add_history('The Robots created the bazaar repository');
 	$bazaar->set_status( Vhffs::Constants::ACTIVATED );
 	$bazaar->commit;
+	Vhffs::Robots::vhffs_log( $vhffs, 'Created bazaar repository '.$bazaar->get_reponame );
 
-	return 0;
+	return 1;
 }
 
-sub change_conf {
+sub modify {
 	my $bazaar = shift;
-	return -1 unless defined $bazaar;
+	return undef unless defined $bazaar and $bazaar->get_status == Vhffs::Constants::WAITING_FOR_MODIFICATION;
 
 	my $dir = $bazaar->get_dir;
 	my $mail_from = $bazaar->get_main->get_config->get_service('bazaar')->{notify_from};
 
 	if( $bazaar->get_ml_name !~ /^\s*$/ ) {
+		# TODO: Bazaar mail on commit
 	}
 
-	return 0;
+	return 1;
 }
 
-sub delete_repo
-{
+sub delete {
 	my $bazaar = shift;
-	return -1 unless defined $bazaar;
+	return undef unless defined $bazaar and $bazaar->get_status == Vhffs::Constants::WAITING_FOR_DELETION;
 
-	# Remove Bazaar dir and group dir if empty
 	my $dir = $bazaar->get_dir;
 	Vhffs::Robots::archive_targz( $bazaar, $dir );
 
-	# TODO: check remove_tree
-	File::Path::remove_tree($dir) if(-d $dir);
+	File::Path::remove_tree( $dir, { error => \my $errors });
 	my $groupdir = File::Basename::dirname($dir);
 	rmdir($groupdir);
 
-	return 0;
+	if(@$errors) {
+		$bazaar->set_status( Vhffs::Constants::DELETION_ERROR );
+		$bazaar->commit;
+		Vhffs::Robots::vhffs_log( $vhffs, 'An error occured while removing bazaar repository '.$bazaar->get_reponame.' from the filesystem' );
+		return undef;
+	}
+
+	if( $bazaar->delete ) {
+		Vhffs::Robots::vhffs_log( $vhffs, 'Deleted bazaar repository '.$bazaar->get_reponame );
+	} else {
+		$bazaar->set_status( Vhffs::Constants::DELETION_ERROR );
+		$bazaar->commit;
+		Vhffs::Robots::vhffs_log( $vhffs, 'An error occured while deleting bazaar repository '.$bazaar->get_reponame.' object' );
+		return undef;
+	}
+
+	return 1;
 }
 
-
 1;

Modified: trunk/vhffs-api/src/Vhffs/Robots/Cron.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Robots/Cron.pm	2012-02-23 21:31:57 UTC (rev 2042)
+++ trunk/vhffs-api/src/Vhffs/Robots/Cron.pm	2012-02-24 00:01:17 UTC (rev 2043)
@@ -33,13 +33,11 @@
 use utf8;
 
 package Vhffs::Robots::Cron;
-
-use Vhffs::Services::Cron;
 use Vhffs::Constants;
 use Vhffs::Functions;
 use Vhffs::Robots;
+use Vhffs::Services::Cron;
 
-
 sub get_runnable_jobs {
 	my $main = shift;
 
@@ -71,4 +69,30 @@
 	return $repos;
 }
 
+sub create {
+	my $cron = shift;
+	return undef unless defined $cron and $cron->get_status == Vhffs::Constants::WAITING_FOR_CREATION;
+
+	my $vhffs = $cron->get_main;
+
+	$cron->set_status( Vhffs::Constants::ACTIVATED );
+	$cron->commit;
+	Vhffs::Robots::vhffs_log( $vhffs, 'Created cron job '.$cron->get_cronpath );
+}
+
+sub delete {
+	my $cron = shift;
+	return undef unless defined $cron and $cron->get_status == Vhffs::Constants::WAITING_FOR_DELETION;
+
+	my $vhffs = $cron->get_main;
+
+	if( $cron->delete ) {
+		Vhffs::Robots::vhffs_log( $vhffs, 'Deleted cron job '.$cron->get_cronpath );
+	} else {
+		Vhffs::Robots::vhffs_log( $vhffs, 'An error occured while deleting cron job '.$cron->get_cronpath );
+		$cron->set_status( Vhffs::Constants::DELETION_ERROR );
+		$cron->commit();
+	}
+}
+
 1;

Modified: trunk/vhffs-robots/src/bazaar_create.pl
===================================================================
--- trunk/vhffs-robots/src/bazaar_create.pl	2012-02-23 21:31:57 UTC (rev 2042)
+++ trunk/vhffs-robots/src/bazaar_create.pl	2012-02-24 00:01:17 UTC (rev 2043)
@@ -33,27 +33,17 @@
 use utf8;
 
 use lib '%VHFFS_LIB_DIR%';
-use Vhffs::Main;
-use Vhffs::Robots;
 use Vhffs::Robots::Bazaar;
-use Vhffs::Services::Bazaar;
-use Vhffs::Constants;
 
-
 my $vhffs = init Vhffs::Main;
+exit 1 unless defined $vhffs;
 
-Vhffs::Robots::lock( $vhffs , 'bazaar' );
+Vhffs::Robots::lock( $vhffs, 'bazaar' );
 
-my $repos = Vhffs::Services::Bazaar::getall( $vhffs , Vhffs::Constants::WAITING_FOR_CREATION);
-foreach my $bazaar ( @{$repos} )
-{
-	if( Vhffs::Robots::Bazaar::create_repo( $bazaar ) != 0 ) {
-		Vhffs::Robots::vhffs_log( $vhffs, sprintf( 'Cannot create Bazaar %s' , $bazaar->get_reponame ));
-	} else {
-		Vhffs::Robots::vhffs_log( $vhffs, sprintf( 'Created Bazaar %s' , $bazaar->get_reponame ));
-	}
+my $repos = Vhffs::Services::Bazaar::getall( $vhffs, Vhffs::Constants::WAITING_FOR_CREATION );
+foreach ( @{$repos} ) {
+	Vhffs::Robots::Bazaar::create( $_ );
 }
 
-
-Vhffs::Robots::unlock( $vhffs , 'bazaar' );
+Vhffs::Robots::unlock( $vhffs, 'bazaar' );
 exit 0;

Modified: trunk/vhffs-robots/src/bazaar_delete.pl
===================================================================
--- trunk/vhffs-robots/src/bazaar_delete.pl	2012-02-23 21:31:57 UTC (rev 2042)
+++ trunk/vhffs-robots/src/bazaar_delete.pl	2012-02-24 00:01:17 UTC (rev 2043)
@@ -33,32 +33,17 @@
 use utf8;
 
 use lib '%VHFFS_LIB_DIR%';
-use Vhffs::Main;
-use Vhffs::Robots;
 use Vhffs::Robots::Bazaar;
-use Vhffs::Services::Bazaar;
-use Vhffs::Constants;
 
-
 my $vhffs = init Vhffs::Main;
+exit 1 unless defined $vhffs;
 
-Vhffs::Robots::lock( $vhffs , 'bazaar' );
+Vhffs::Robots::lock( $vhffs, 'bazaar' );
 
-my $repos = Vhffs::Services::Bazaar::getall( $vhffs , Vhffs::Constants::WAITING_FOR_DELETION );
-foreach my $bazaar ( @{$repos} )
-{
-	if( Vhffs::Robots::Bazaar::delete_repo( $bazaar ) != 0 )	{
-		Vhffs::Robots::vhffs_log( $vhffs, sprintf( 'Cannot delete files from Bazaar repository %s' , $bazaar->get_reponame ));
-	} else {
-		Vhffs::Robots::vhffs_log( $vhffs, sprintf( 'Delete files from Bazaar repository %s' , $bazaar->get_reponame ));
-	}
-
-	if( $bazaar->delete < 0 ) {
-		Vhffs::Robots::vhffs_log( $vhffs, sprintf( 'Cannot delete Bazaar repository object %s' , $bazaar->get_reponame ));
-	} else {
-		Vhffs::Robots::vhffs_log( $vhffs, sprintf( 'Delete Bazaar repository object %s' , $bazaar->get_reponame ));
-	}
+my $repos = Vhffs::Services::Bazaar::getall( $vhffs, Vhffs::Constants::WAITING_FOR_DELETION );
+foreach ( @{$repos} ) {
+	Vhffs::Robots::Bazaar::delete( $_ );
 }
 
-Vhffs::Robots::unlock( $vhffs , 'bazaar' );
+Vhffs::Robots::unlock( $vhffs, 'bazaar' );
 exit 0;

Modified: trunk/vhffs-robots/src/bazaar_public.pl
===================================================================
--- trunk/vhffs-robots/src/bazaar_public.pl	2012-02-23 21:31:57 UTC (rev 2042)
+++ trunk/vhffs-robots/src/bazaar_public.pl	2012-02-24 00:01:17 UTC (rev 2043)
@@ -55,7 +55,7 @@
 		Vhffs::Robots::vhffs_log( $vhffs, sprintf( 'Bazaar status %s is now private' , $bazaar->get_dir ) );
 	}
 
-	Vhffs::Robots::Bazaar::change_conf( $bazaar );
+	Vhffs::Robots::Bazaar::modify( $bazaar );
 	$bazaar->set_status( Vhffs::Constants::ACTIVATED );
 	
 	if( $bazaar->commit < 0 ) {

Modified: trunk/vhffs-robots/src/cron_create.pl
===================================================================
--- trunk/vhffs-robots/src/cron_create.pl	2012-02-23 21:31:57 UTC (rev 2042)
+++ trunk/vhffs-robots/src/cron_create.pl	2012-02-24 00:01:17 UTC (rev 2043)
@@ -29,40 +29,21 @@
 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 # POSSIBILITY OF SUCH DAMAGE.
 
-#This robot create cron jobs
-#It actives it
-
-# FIXME : This robot is unnecessary, we just have to activate the cron job on creation
-
 use strict;
 use utf8;
 
 use lib '%VHFFS_LIB_DIR%';
-use Vhffs::Services::Cron;
-use Vhffs::Robots;
-use Vhffs::Main;
+use Vhffs::Robots::Cron;
 
-
 my $vhffs = init Vhffs::Main;
 exit 1 unless defined $vhffs;
 
-Vhffs::Robots::lock( $vhffs , 'cron' );
+Vhffs::Robots::lock( $vhffs, 'cron' );
 
-my $crons = Vhffs::Services::Cron::getall( $vhffs , Vhffs::Constants::WAITING_FOR_CREATION );
-
-foreach my $c ( @{$crons} )
-{
-	$c->set_status( Vhffs::Constants::ACTIVATED );
-	if( $c->commit > 0 )
-	{
-		Vhffs::Robots::vhffs_log( $vhffs, sprintf( 'Created cron job %s' , $c->get_cronpath ) );
-		$c->add_history( 'Robots activated this cron job' );
-	}
-	else
-	{
-		Vhffs::Robots::vhffs_log( $vhffs, sprintf( 'Cannot create cron job %s' , $c->get_cronpath ) );
-	}
+my $crons = Vhffs::Services::Cron::getall( $vhffs, Vhffs::Constants::WAITING_FOR_CREATION );
+foreach ( @{$crons} ) {
+	Vhffs::Robots::Cron::create( $_ );
 }
 
-Vhffs::Robots::unlock( $vhffs , 'cron' );
+Vhffs::Robots::unlock( $vhffs, 'cron' );
 exit 0;

Modified: trunk/vhffs-robots/src/cron_delete.pl
===================================================================
--- trunk/vhffs-robots/src/cron_delete.pl	2012-02-23 21:31:57 UTC (rev 2042)
+++ trunk/vhffs-robots/src/cron_delete.pl	2012-02-24 00:01:17 UTC (rev 2043)
@@ -33,23 +33,17 @@
 use utf8;
 
 use lib '%VHFFS_LIB_DIR%';
+use Vhffs::Robots::Cron;
 
-use Vhffs::Services::Cron;
-use Vhffs::Robots;
-use Vhffs::Main;
-
 my $vhffs = init Vhffs::Main;
 exit 1 unless defined $vhffs;
 
-Vhffs::Robots::lock( $vhffs , 'cron' );
+Vhffs::Robots::lock( $vhffs, 'cron' );
 
-my $crons = Vhffs::Services::Cron::getall( $vhffs , Vhffs::Constants::WAITING_FOR_DELETION );
-
-foreach my $c ( @{$crons} )
-{
-	Vhffs::Robots::vhffs_log( $vhffs, sprintf( 'Deleted cron job %s' , $c->get_cronpath ) );
-	$c->delete;
+my $crons = Vhffs::Services::Cron::getall( $vhffs, Vhffs::Constants::WAITING_FOR_DELETION );
+foreach ( @{$crons} ) {
+	Vhffs::Robots::Cron::delete( $_ );
 }
 
-Vhffs::Robots::unlock( $vhffs , 'cron' );
+Vhffs::Robots::unlock( $vhffs, 'cron' );
 exit 0;


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/