[vhffs-dev] [2014] Reworked repository quota bot

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


Revision: 2014
Author:   gradator
Date:     2012-02-18 02:06:34 +0100 (Sat, 18 Feb 2012)
Log Message:
-----------
Reworked repository quota bot

This was not planned at all today but I wanted to remove Vhffs::Services::Repository::getall_per_group() and this was a dependancy.

Merged repository_setquota and repository_getquota to repository_quota.

Modified Paths:
--------------
    trunk/vhffs-robots/Makefile.am

Added Paths:
-----------
    trunk/vhffs-robots/src/repository_quota.pl

Removed Paths:
-------------
    trunk/vhffs-robots/src/repository_getquota.pl
    trunk/vhffs-robots/src/repository_setquota.pl

Modified: trunk/vhffs-robots/Makefile.am
===================================================================
--- trunk/vhffs-robots/Makefile.am	2012-02-17 20:23:45 UTC (rev 2013)
+++ trunk/vhffs-robots/Makefile.am	2012-02-18 01:06:34 UTC (rev 2014)
@@ -50,8 +50,7 @@
 	src/pgsql_modify.pl \
 	src/repository_create.pl \
 	src/repository_delete.pl \
-	src/repository_getquota.pl \
-	src/repository_setquota.pl \
+	src/repository_quota.pl \
 	src/repository_stats.pl \
 	src/svn_create.pl \
 	src/svn_delete.pl \

Deleted: trunk/vhffs-robots/src/repository_getquota.pl
===================================================================
--- trunk/vhffs-robots/src/repository_getquota.pl	2012-02-17 20:23:45 UTC (rev 2013)
+++ trunk/vhffs-robots/src/repository_getquota.pl	2012-02-18 01:06:34 UTC (rev 2014)
@@ -1,85 +0,0 @@
-#!%PERL%
-# Copyright (c) vhffs project and its contributors
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without 
-# modification, are permitted provided that the following conditions 
-# are met:
-#
-# 1. Redistributions of source code must retain the above copyright 
-#   notice, this list of conditions and the following disclaimer.
-#2. Redistributions in binary form must reproduce the above copyright
-#   notice, this list of conditions and the following disclaimer in 
-#   the documentation and/or other materials provided with the 
-#   distribution.
-#3. Neither the name of vhffs nor the names of its contributors 
-#   may be used to endorse or promote products derived from this 
-#   software without specific prior written permission.
-#
-#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
-#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
-#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
-#FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
-#COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
-#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
-#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
-#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
-#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
-# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
-# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
-# POSSIBILITY OF SUCH DAMAGE.
-
-require 5.004;
-use strict;
-use utf8;
-use POSIX;
-use locale;
-use Locale::gettext;
-use lib '%VHFFS_LIB_DIR%';
-use Vhffs::Main;
-use Vhffs::Robots;
-use Vhffs::Group;
-use Vhffs::Services::Repository;
-use Quota;
-
-my $vhffs = init Vhffs::Main;
-
-Vhffs::Robots::lock( $vhffs , "getquotarepository" );
-
-my $groups = Vhffs::Group::getall( $vhffs , Vhffs::Constants::ACTIVATED );
-my $group;
-my $blocks;
-my $inodes;
-my $repos;
-my $repo;
-my $su;
-my $dev = Quota::getqcarg($vhffs->get_config->get_datadir.'/repository');
-
-foreach $group ( @{$groups} )
-{
-	if( defined $group )
-	{
-		$repos = Vhffs::Services::Repository::getall_per_group( $vhffs, $group);
-		if( defined $repos )
-		{
-			($blocks,undef,undef,undef,$inodes,undef,undef,undef) = Quota::query($dev, $group->get_gid, 1);
-			if ( defined $blocks )
-			{
-				$su = ($blocks+1024-($blocks%1024))/1024;
-
-				# set this space usedto all repositories of this group
-				foreach $repo ( @{$repos} )
-				{
-					$repo->set_quota_used( $su );
-					$repo->commit;
-				}
-
-				Vhffs::Robots::vhffs_log( sprintf( "Update quota used for repositories of group %s to %s MB" , $group->get_groupname , $su ) , $vhffs);
-			}
-		}
-	}
-}
-
-Vhffs::Robots::unlock( $vhffs , "getquotarepository" );
-
-exit 0;

Copied: trunk/vhffs-robots/src/repository_quota.pl (from rev 2012, trunk/vhffs-robots/src/repository_setquota.pl)
===================================================================
--- trunk/vhffs-robots/src/repository_quota.pl	                        (rev 0)
+++ trunk/vhffs-robots/src/repository_quota.pl	2012-02-18 01:06:34 UTC (rev 2014)
@@ -0,0 +1,115 @@
+#!%PERL%
+# Copyright (c) vhffs project and its contributors
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without 
+# modification, are permitted provided that the following conditions 
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright 
+#   notice, this list of conditions and the following disclaimer.
+#2. Redistributions in binary form must reproduce the above copyright
+#   notice, this list of conditions and the following disclaimer in 
+#   the documentation and/or other materials provided with the 
+#   distribution.
+#3. Neither the name of vhffs nor the names of its contributors 
+#   may be used to endorse or promote products derived from this 
+#   software without specific prior written permission.
+#
+#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
+#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
+#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
+#FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
+#COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
+#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
+#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
+#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
+#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
+# POSSIBILITY OF SUCH DAMAGE.
+
+require 5.004;
+use strict;
+use utf8;
+use POSIX;
+use locale;
+use Locale::gettext;
+use lib '%VHFFS_LIB_DIR%';
+use Vhffs::Main;
+use Vhffs::Robots;
+use Vhffs::Group;
+use Vhffs::Services::Repository;
+use Quota;
+
+my $vhffs = init Vhffs::Main;
+
+Vhffs::Robots::lock( $vhffs , 'quotarepository' );
+
+my $dev = Quota::getqcarg($vhffs->get_config->get_datadir.'/repository');
+my $repos = Vhffs::Services::Repository::getall( $vhffs, Vhffs::Constants::ACTIVATED, undef, undef );
+my $groupsusingrepo = {};
+
+foreach my $repo ( @{$repos} ) {
+	my $setblocks = ($repo->get_quota*1000000)/1024;  # Filesystem quota block = 1024B
+	my $setinodes = ($repo->get_quota*1000000)/4096;  # Filesystem block = 4096B
+	$groupsusingrepo->{ $repo->get_owner_gid } = 1;	
+
+	my ($blocks,$softblocks,$hardblocks,undef,undef,$softinodes,$hardinodes,undef) = Quota::query($dev, $repo->get_owner_gid, 1);
+
+	# Set quota - only if database and filesystem are out of sync
+	unless( defined $softblocks and defined $hardblocks and defined $softinodes and defined $hardinodes
+	  and $softblocks == $hardblocks and $softinodes == $hardinodes
+	  and $hardblocks == $setblocks and $hardinodes == $setinodes) {
+
+		unless( Quota::setqlim($dev, $repo->get_owner_gid, $setblocks, $setblocks, $setinodes, $setinodes, 0, 1) ) {
+			Vhffs::Robots::vhffs_log( sprintf( 'Updated quota for repository %s to %d MB', $repo->get_name, $repo->get_quota ), $vhffs);
+		} else {
+			print 'Cannot update quota for repository '.$repo->get_name.', reason: '.Quota::strerr()."\n";
+		}
+	}
+
+	# Get quota - only push changes if filesystem and database have different values
+	next unless defined $blocks;
+	my $used = ceil( ($blocks*1024)/1000000 );
+	next if $used == $repo->get_quota_used;
+	$repo->set_quota_used( $used );
+	$repo->commit;
+	Vhffs::Robots::vhffs_log( sprintf( 'Updated quota used for repository %s to %d MB', $repo->get_name, $used ), $vhffs);
+}
+
+# Give no space for groups without download repository
+# So that a user in more than one group cannot abuse of quota by using a gid without quota yet
+my $groups = Vhffs::Group::getall( $vhffs, Vhffs::Constants::ACTIVATED );
+foreach my $group ( @{$groups} ) {
+	next if $groupsusingrepo->{ $group->get_gid };	
+
+	# Only set quota if filesystem quota is not currently set
+	my (undef,$softblocks,$hardblocks,undef,undef,$softinodes,$hardinodes,undef) = Quota::query($dev, $group->get_gid, 1);
+	next if defined $softblocks and defined $hardblocks and defined $softinodes and defined $hardinodes
+	  and $softblocks == 1 and $hardblocks == 0
+	  and $softinodes == 1 and $hardinodes == 0;
+
+	if( Quota::setqlim($dev, $group->get_gid, 1, 0, 1, 0, 0, 1) ) {
+		print 'Cannot set quota to zero for group '.$group->get_groupname.', reason: '.Quota::strerr()."\n";
+	}
+}
+
+# Give no space for users primary groups
+my $users = Vhffs::User::getall( $vhffs , Vhffs::Constants::ACTIVATED );
+foreach my $user ( @{$users} ) {
+	# Only set quota if filesystem quota is not currently set
+	my (undef,$softblocks,$hardblocks,undef,undef,$softinodes,$hardinodes,undef) = Quota::query($dev, $user->get_gid, 1);
+	next if defined $softblocks and defined $hardblocks and defined $softinodes and defined $hardinodes
+	  and $softblocks == 1 and $hardblocks == 0
+	  and $softinodes == 1 and $hardinodes == 0;
+
+	if( Quota::setqlim($dev, $user->get_gid, 1, 0, 1, 0, 0, 1) ) {
+		print 'Cannot set quota to zero for user '.$user->get_username.', reason: '.Quota::strerr()."\n";
+	}
+}
+
+
+Vhffs::Robots::unlock( $vhffs , 'quotarepository' );
+
+exit 0;

Deleted: trunk/vhffs-robots/src/repository_setquota.pl
===================================================================
--- trunk/vhffs-robots/src/repository_setquota.pl	2012-02-17 20:23:45 UTC (rev 2013)
+++ trunk/vhffs-robots/src/repository_setquota.pl	2012-02-18 01:06:34 UTC (rev 2014)
@@ -1,120 +0,0 @@
-#!%PERL%
-# Copyright (c) vhffs project and its contributors
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without 
-# modification, are permitted provided that the following conditions 
-# are met:
-#
-# 1. Redistributions of source code must retain the above copyright 
-#   notice, this list of conditions and the following disclaimer.
-#2. Redistributions in binary form must reproduce the above copyright
-#   notice, this list of conditions and the following disclaimer in 
-#   the documentation and/or other materials provided with the 
-#   distribution.
-#3. Neither the name of vhffs nor the names of its contributors 
-#   may be used to endorse or promote products derived from this 
-#   software without specific prior written permission.
-#
-#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
-#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
-#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
-#FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
-#COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
-#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
-#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
-#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
-#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
-# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
-# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
-# POSSIBILITY OF SUCH DAMAGE.
-
-require 5.004;
-use strict;
-use utf8;
-use POSIX;
-use locale;
-use Locale::gettext;
-use lib '%VHFFS_LIB_DIR%';
-use Vhffs::Main;
-use Vhffs::Robots;
-use Vhffs::Group;
-use Vhffs::Services::Repository;
-use Quota;
-
-my $vhffs = init Vhffs::Main;
-
-Vhffs::Robots::lock( $vhffs , "setquotarepository" );
-
-my $groups = Vhffs::Group::getall( $vhffs , Vhffs::Constants::ACTIVATED );
-my $group;
-my $quota;
-my $blocks;
-my $inodes;
-my $repos;
-my $repo;
-my $users;
-my $user;
-my $dev = Quota::getqcarg($vhffs->get_config->get_datadir.'/repository');
-
-foreach $group ( @{$groups} )
-{
-	if( defined $group )
-	{
-		$quota = 0;
-		$blocks = 0;
-		$inodes = 0;
-
-		$repos = Vhffs::Services::Repository::getall_per_group( $vhffs, $group);
-		if( scalar(@{$repos}) > 0 )
-		{
-			foreach $repo ( @{$repos} )
-			{
-				if( defined $repo )
-				{
-					$quota += $repo->get_quota;
-					$blocks += $repo->get_quota * 1024;
-					$inodes += $repo->get_quota * 1126400 / 100;
-				}
-			}
-
-			unless( Quota::setqlim($dev, $group->get_gid, $blocks, $blocks, $inodes, $inodes, 0, 1) )
-			{
-				Vhffs::Robots::vhffs_log( sprintf( "Update quota for repositories of group %s to %s MB" , $group->get_groupname , $quota ) , $vhffs);
-			}
-			else
-			{
-				printf("Cannot update quota for repositories of group %s, reason: %s\n", $group->get_groupname, Quota::strerr);
-			}
-		}
-		else
-		{
-			# give no space for groups without download repository
-			if( Quota::setqlim($dev, $group->get_gid, 1, 1, 1, 1, 0, 1) )
-			{
-				printf("Cannot update quota for repositories of group %s, reason: %s\n", $group->get_groupname, Quota::strerr);
-			}
-		}
-	}
-}
-
-
-# give no space for users
-$users = Vhffs::User::getall( $vhffs , Vhffs::Constants::ACTIVATED );
-
-foreach $user ( @{$users} )
-{
-	if( defined $user )
-	{
-		# give no space for all primary groups of users
-		if( Quota::setqlim($dev, $user->get_gid, 1, 1, 1, 1, 0, 1) )
-		{
-			printf("Cannot update quota for repositories of primary group of user %s, reason %s\n", $user->get_username, Quota::strerr);
-		}
-	}
-}
-
-
-Vhffs::Robots::unlock( $vhffs , "setquotarepository" );
-
-exit 0;


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