From f5c673054e023574bcfb2758a0032ba5bfbee93a Mon Sep 17 00:00:00 2001
From: Peter Hormanns <peter.hormanns@hostsharing.net>
Date: Wed, 03 Aug 2011 19:05:20 +0200
Subject: [PATCH] setquota, traffic accounting

---
 hsarback/test/de/hsadmin/remote/PgsqlUserTest.java           |    1 
 hsarback/test/de/hsadmin/remote/RemoteTest.java              |   14 ++--
 hsarback/src/de/hsadmin/mods/pac/Pac.java                    |    4 +
 hsarback/src/de/hsadmin/mods/pac/PacProcessorFactory.java    |   62 ++++++++++++--------
 hsarback/test/de/hsadmin/remote/InitDataTest.java            |    2 
 hsarback/test/de/hsadmin/remote/MysqlUserTest.java           |    1 
 hsarback/src/de/hsadmin/core/util/PasswordTool.java          |   25 ++++++++
 hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java |   35 +++++++----
 8 files changed, 95 insertions(+), 49 deletions(-)

diff --git a/hsarback/src/de/hsadmin/core/util/PasswordTool.java b/hsarback/src/de/hsadmin/core/util/PasswordTool.java
new file mode 100644
index 0000000..7cb7cc0
--- /dev/null
+++ b/hsarback/src/de/hsadmin/core/util/PasswordTool.java
@@ -0,0 +1,25 @@
+package de.hsadmin.core.util;
+
+import java.util.Random;
+
+import de.rrze.jpwgen.flags.PwGeneratorFlagBuilder;
+import de.rrze.jpwgen.impl.PwGenerator;
+
+public class PasswordTool {
+
+	public static PwGeneratorFlagBuilder flagBuilder = new PwGeneratorFlagBuilder();
+	public static Random random = new Random();
+	
+	static {
+		flagBuilder.setIncludeCapitals();
+		flagBuilder.setIncludeNumerals();
+//		flagBuilder.setIncludeReducedSymbols();
+		flagBuilder.setFilterAmbiguous();
+	}
+	
+	public static String generatePassword() {
+		String password = PwGenerator.generatePassword(7, flagBuilder.build(), 100, random);
+		return password;
+	}
+
+}
diff --git a/hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java b/hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java
index f11050b..5b320f0 100644
--- a/hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java
+++ b/hsarback/src/de/hsadmin/mods/dom/DomainProcessorFactory.java
@@ -25,6 +25,9 @@
 
 public class DomainProcessorFactory implements EntityProcessorFactory {
 
+	private static final String[] DW_STRUCTURE = new String[] { "htdocs", "htdocs-ssl", "subs", "subs/www", "subs-ssl", "subs-ssl/www", "cgi", "fastcgi", "cgi-ssl", "fastcgi-ssl", "etc", "var" };
+	private static final String[] SW_STRUCTURE = new String[] { "htdocs", "htdocs-ssl", "subs", "subs/www", "subs-ssl", "subs-ssl/www", "etc", "var" };
+
 	public <T extends AbstractEntity> Processor createCreateProcessor(EntityManager em, T entity) throws ProcessorException {
 		String hiveName = entity.getHiveName();
 		Domain dom = (Domain) entity;
@@ -187,11 +190,13 @@
 	private CompoundProcessor createDomainDirectoriesProcessor(Domain dom, Map<String, String> templateVars) throws ProcessorException {
 		UnixUser domUser = dom.getUser();
 		String domName = dom.getName();
-		String pacName = domUser.getPac().getName();
+		Pac pac = domUser.getPac();
+		boolean dynamicWeb = pac.isDynamicWeb();
+		String pacName = pac.getName();
 		String domsDir = domUser.getHomedir() + "/doms";
 		String userName = domUser.getName();
 		String domainDir = domsDir + "/" + dom.getName();
-		String[] subDirs = new String[] { "htdocs", "htdocs-ssl", "subs", "subs/www", "subs-ssl", "subs-ssl/www", "cgi", "fastcgi", "cgi-ssl", "fastcgi-ssl", "etc", "var" };
+		String[] subDirs = dynamicWeb ? DW_STRUCTURE : SW_STRUCTURE;
 		Processor mkDomainDirProzessor = 
 			new ShellProcessor(
 					"mkdir --mode=1550 --parents " + domsDir + " && " +
@@ -220,18 +225,20 @@
 		domDirsProcessor.appendProcessor(
 				new CreateFileProcessor("/de/hsadmin/mods/dom/index.html.jtpl", templateVars, domainDir + "/subs-ssl/www/index.html", userName, pacName, "644")
 			);
-		domDirsProcessor.appendProcessor(
-				new CreateFileProcessor("/de/hsadmin/mods/dom/test.cgi.jtpl", templateVars, domainDir + "/cgi/test.cgi", userName, pacName, "755")
+		if (dynamicWeb) {
+			domDirsProcessor.appendProcessor(
+					new CreateFileProcessor("/de/hsadmin/mods/dom/test.cgi.jtpl", templateVars, domainDir + "/cgi/test.cgi", userName, pacName, "755")
+				);
+			domDirsProcessor.appendProcessor(
+					new CreateFileProcessor("/de/hsadmin/mods/dom/test.cgi.jtpl", templateVars, domainDir + "/cgi-ssl/test.cgi", userName, pacName, "755")
+				);
+			domDirsProcessor.appendProcessor(
+					new CopyFileProcessor("/usr/local/src/phpstub/phpstub", domainDir + "/fastcgi/phpstub", userName, pacName, "755")
+				);
+			domDirsProcessor.appendProcessor(
+					new CopyFileProcessor("/usr/local/src/phpstub/phpstub", domainDir + "/fastcgi-ssl/phpstub", userName, pacName, "755")
 			);
-		domDirsProcessor.appendProcessor(
-				new CreateFileProcessor("/de/hsadmin/mods/dom/test.cgi.jtpl", templateVars, domainDir + "/cgi-ssl/test.cgi", userName, pacName, "755")
-			);
-		domDirsProcessor.appendProcessor(
-				new CopyFileProcessor("/usr/local/src/phpstub/phpstub", domainDir + "/fastcgi/phpstub", userName, pacName, "755")
-			);
-		domDirsProcessor.appendProcessor(
-				new CopyFileProcessor("/usr/local/src/phpstub/phpstub", domainDir + "/fastcgi-ssl/phpstub", userName, pacName, "755")
-		);
+		}
 		domDirsProcessor.appendProcessor(
 				new ShellProcessor("ln -s " + domainDir + " /home/doms/ && " +
 						"chown --no-dereference " + userName + ":httpd /home/doms/" + domName 
@@ -269,7 +276,7 @@
 		if (domName.equals(pac.getName() + ".hostsharing.net")) {
 			return "/de/hsadmin/mods/dom/httpd-vhost-dynamic.jtpl";
 		}
-		if (pac.getBasepac().getName().startsWith("DW")) {
+		if (pac.isDynamicWeb()) {
 			return "/de/hsadmin/mods/dom/httpd-vhost-dynamic.jtpl";
 		}
 		return "/de/hsadmin/mods/dom/httpd-vhost-static.jtpl";
diff --git a/hsarback/src/de/hsadmin/mods/pac/Pac.java b/hsarback/src/de/hsadmin/mods/pac/Pac.java
index 3a745fc..ab5bf61 100644
--- a/hsarback/src/de/hsadmin/mods/pac/Pac.java
+++ b/hsarback/src/de/hsadmin/mods/pac/Pac.java
@@ -272,6 +272,10 @@
 				|| super.isReadAllowedFor(loginUser);
 	}
 
+	public boolean isDynamicWeb() {
+		return getBasepac().getName().startsWith("DW");
+	}
+
 	public static String restriction() {
 		return
 			// all pacs of customer
diff --git a/hsarback/src/de/hsadmin/mods/pac/PacProcessorFactory.java b/hsarback/src/de/hsadmin/mods/pac/PacProcessorFactory.java
index c6dd4fd..29f8a09 100644
--- a/hsarback/src/de/hsadmin/mods/pac/PacProcessorFactory.java
+++ b/hsarback/src/de/hsadmin/mods/pac/PacProcessorFactory.java
@@ -4,7 +4,6 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Random;
 import java.util.Set;
 
 import javax.persistence.EntityManager;
@@ -18,22 +17,11 @@
 import de.hsadmin.core.qserv.ShellProcessor;
 import de.hsadmin.core.qserv.TemplateProcessor;
 import de.hsadmin.core.qserv.WaitingTasksProcessor;
+import de.hsadmin.core.util.PasswordTool;
 import de.hsadmin.mods.user.UnixUser;
-import de.rrze.jpwgen.flags.PwGeneratorFlagBuilder;
-import de.rrze.jpwgen.impl.PwGenerator;
 
 public class PacProcessorFactory implements EntityProcessorFactory {
 
-	private static PwGeneratorFlagBuilder flagBuilder = new PwGeneratorFlagBuilder();
-	private static Random random = new Random();
-	
-	static {
-		flagBuilder.setIncludeCapitals();
-		flagBuilder.setIncludeNumerals();
-//		flagBuilder.setIncludeReducedSymbols();
-		flagBuilder.setFilterAmbiguous();
-	}
-	
 	@Override
 	public <T extends AbstractEntity> Processor createCreateProcessor(
 			EntityManager em, T entity) throws ProcessorException {
@@ -42,14 +30,16 @@
 		String customerEMail = pac.getCustomer().getContractualContact().getEmail();
 		Hive hive = pac.getHive();
 		UnixUser unixUser = getPacAdminUser(pac);
-		String password = PwGenerator.generatePassword(7, flagBuilder.build(), 100, random);
+		String password = PasswordTool.generatePassword();
 		Map<String, String> hiveValues = fillHiveValues(hive);
 		List<Map<String, String>> pacValuesList = fillPacValuesList(hive, null);
 		Map<String, String> emailVars = fillEMailValues(pacName, customerEMail, password);
 		Processor priProcessor = new CompoundProcessor(
 				createAddUserProc(pacName, unixUser, password), 
+				createSetQuotaProc(pac),
 				createEtcHostsProc(hiveValues, pacValuesList), 
-				createNetworkInterfacesProc(hiveValues, pacValuesList), 
+				createNetworkInterfacesProc(hiveValues, pacValuesList),
+				createIPTablesProc(),
 				createSudouersProc(hiveValues, pacValuesList), 
 				createProftpdConfProc(hiveValues, pacValuesList), 
 				createMakePacDirectoryStructure(unixUser));
@@ -65,8 +55,7 @@
 	}
 
 	@Override
-	public <T extends AbstractEntity> Processor createDeleteProcessor(
-			EntityManager em, T entity) throws ProcessorException {
+	public <T extends AbstractEntity> Processor createDeleteProcessor(EntityManager em, T entity) throws ProcessorException {
 		Pac pac = (Pac) entity;
 		Hive hive = pac.getHive();
 		Map<String, String> hiveValues = fillHiveValues(hive);
@@ -80,32 +69,35 @@
 		return waitingProcessor;
 	}
 
-	private TemplateProcessor createEtcHostsProc(
+	private Processor createEtcHostsProc(
 			Map<String, String> hiveValues,
 			List<Map<String, String>> pacValuesList) throws ProcessorException {
 		return new TemplateProcessor("/de/hsadmin/mods/pac/hosts.jtpl", hiveValues, pacValuesList.iterator(), "/etc/hosts", true);
 	}
 
-	private TemplateProcessor createNetworkInterfacesProc(
+	private Processor createNetworkInterfacesProc(
 			Map<String, String> hiveValues,
 			List<Map<String, String>> pacValuesList) throws ProcessorException {
 		return new TemplateProcessor("/de/hsadmin/mods/pac/interfaces.jtpl", hiveValues, pacValuesList.iterator(), "/etc/network/interfaces", true);
 	}
 
-	private TemplateProcessor createSudouersProc(
+	private Processor createIPTablesProc() {
+		return new ShellProcessor("mk-iptables-rules Accounting");
+	}
+
+	private Processor createSudouersProc(
 			Map<String, String> hiveValues,
 			List<Map<String, String>> pacValuesList) throws ProcessorException {
 		return new TemplateProcessor("/de/hsadmin/mods/pac/sudoers.jtpl", hiveValues, pacValuesList.iterator(), "/etc/sudoers", true);
 	}
 
-	private TemplateProcessor createProftpdConfProc(
+	private Processor createProftpdConfProc(
 			Map<String, String> hiveValues,
 			List<Map<String, String>> pacValuesList) throws ProcessorException {
 		return new TemplateProcessor("/de/hsadmin/mods/pac/proftpd-conf.jtpl", hiveValues, pacValuesList.iterator(), "/etc/proftpd/proftpd.conf", true);
 	}
 
-	private ShellProcessor createAddUserProc(String pacName, UnixUser unixUser,
-			String password) {
+	private Processor createAddUserProc(String pacName, UnixUser unixUser, String password) {
 		return new ShellProcessor(
 				"newusers", pacName + ":" + password  + ":"
 						+ unixUser.getUserId() + ":" + unixUser.getUserId()
@@ -113,11 +105,31 @@
 						+ ":" + unixUser.getShell() + "\n");
 	}
 
-	private ShellProcessor createDelUserProc(String pacName) {
+	private Processor createSetQuotaProc(Pac pac) {
+		int quota = 128;
+		Set<PacComponent> pacComponents = pac.getPacComponents();
+		for (PacComponent pacComponent : pacComponents) {
+			BaseComponent baseComponent = pacComponent.getBaseComponent();
+			String feature = baseComponent.getFeature();
+			if ("QUOTA".equals(feature)) {
+				quota = pacComponent.getQuantity();
+			}
+		}
+		int blocksSoft = quota * 1024;
+		int blocksHard = quota * 1024 * 2;
+		int inodesSoft = blocksSoft / 4;
+		int inodesHard = blocksHard / 4;
+		return new ShellProcessor("setquota -g " + pac.getName() + " " 
+				+ blocksSoft + " " + blocksHard + " " 
+				+ inodesSoft + " " + inodesHard + " "
+				+ "`df /home/pacs/ | tail -n1 | cut -d' ' -f1`");
+	}
+
+	private Processor createDelUserProc(String pacName) {
 		return new ShellProcessor("rm -rf /home/pacs/" + pacName + "/* && deluser --remove-all-files " + pacName + ">/dev/null 2>&1");
 	}
 
-	private ShellProcessor createMakePacDirectoryStructure(UnixUser unixUser) {
+	private Processor createMakePacDirectoryStructure(UnixUser unixUser) {
 		return new ShellProcessor("chmod 711 " + unixUser.getHomedir() + " && " +
 				"su -l " + unixUser.getName() + " -s \"/bin/bash\" -c \"mkdir -p " +
 				unixUser.getHomedir() + "/doms " + 
diff --git a/hsarback/test/de/hsadmin/remote/InitDataTest.java b/hsarback/test/de/hsadmin/remote/InitDataTest.java
index 5775504..08dc843 100644
--- a/hsarback/test/de/hsadmin/remote/InitDataTest.java
+++ b/hsarback/test/de/hsadmin/remote/InitDataTest.java
@@ -55,7 +55,7 @@
 		setParams.put("contact_city", "Musterstadt");
 		setParams.put("contact_country", "D");
 		setParams.put("contact_phone_private", "+49 9999 123456");
-		setParams.put("contact_email", "rainer.mustermann@example.org");
+		setParams.put("contact_email", "test@ostwall195.de");
 		Object[] params = new Object[] { user, 
 				cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), 
 				setParams };
diff --git a/hsarback/test/de/hsadmin/remote/MysqlUserTest.java b/hsarback/test/de/hsadmin/remote/MysqlUserTest.java
index e9b2093..11a9230 100644
--- a/hsarback/test/de/hsadmin/remote/MysqlUserTest.java
+++ b/hsarback/test/de/hsadmin/remote/MysqlUserTest.java
@@ -12,7 +12,6 @@
 import org.apache.xmlrpc.client.XmlRpcClient;
 import org.junit.After;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 
 public class MysqlUserTest {
diff --git a/hsarback/test/de/hsadmin/remote/PgsqlUserTest.java b/hsarback/test/de/hsadmin/remote/PgsqlUserTest.java
index a337ead..8b0330e 100644
--- a/hsarback/test/de/hsadmin/remote/PgsqlUserTest.java
+++ b/hsarback/test/de/hsadmin/remote/PgsqlUserTest.java
@@ -12,7 +12,6 @@
 import org.apache.xmlrpc.client.XmlRpcClient;
 import org.junit.After;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 
 public class PgsqlUserTest {
diff --git a/hsarback/test/de/hsadmin/remote/RemoteTest.java b/hsarback/test/de/hsadmin/remote/RemoteTest.java
index db9e793..fbdc922 100644
--- a/hsarback/test/de/hsadmin/remote/RemoteTest.java
+++ b/hsarback/test/de/hsadmin/remote/RemoteTest.java
@@ -6,16 +6,16 @@
 @RunWith(Suite.class)
 @Suite.SuiteClasses({
 	InitDataTest.class,
-	PacTest.class,
-	UnixUserTest.class,
-	MysqlUserTest.class,
-	MysqlDbTest.class,
-	PgsqlUserTest.class,
-	PgsqlDbTest.class,
+//	PacTest.class,
+//	UnixUserTest.class,
+//	MysqlUserTest.class,
+//	MysqlDbTest.class,
+//	PgsqlUserTest.class,
+//	PgsqlDbTest.class,
 //	EMailAliasTest.class,
 //	DomainTest.class,
 //	EMailAddressTest.class,
-	CustomerTest.class
+//	CustomerTest.class
 //	HostmasterTest.class,
 //	QueueTaskTest.class
 })

--
Gitblit v1.9.3