From ac52ad8cdf8b2f2755aecaa47940bc762d25a3af Mon Sep 17 00:00:00 2001 From: Peter Hormanns Date: Wed, 27 Jul 2011 15:20:56 +0000 Subject: [PATCH] delete pac --- .../hsadmin/core/qserv/MailerProcessor.java | 25 +++ .../hsadmin/mods/pac/PacProcessorFactory.java | 182 +++++++++++------- 2 files changed, 142 insertions(+), 65 deletions(-) diff --git a/hsarback/src/de/hsadmin/core/qserv/MailerProcessor.java b/hsarback/src/de/hsadmin/core/qserv/MailerProcessor.java index 3084c41..f7dfc7f 100644 --- a/hsarback/src/de/hsadmin/core/qserv/MailerProcessor.java +++ b/hsarback/src/de/hsadmin/core/qserv/MailerProcessor.java @@ -1,5 +1,12 @@ package de.hsadmin.core.qserv; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.Map; + +import net.sf.jtpl.Template; + import de.hsadmin.core.model.HSAdminException; import de.hsadmin.core.util.Config; @@ -17,6 +24,24 @@ public class MailerProcessor extends AbstractProcessor { this.aBody = aBody; } + public MailerProcessor(String aTo, String aSubject, String templateName, Map templateValues) throws ProcessorException { + this.aTo = aTo; + this.aSubject = aSubject; + try { + InputStream stream = MailerProcessor.class.getClassLoader().getResourceAsStream(templateName); + Template template = new Template(new InputStreamReader(stream)); + for (String key : templateValues.keySet()) { + template.assign(key, templateValues.get(key)); + } + template.parse("main"); + this.aBody = template.out(); + } catch (IllegalArgumentException e) { + throw new ProcessorException(e); + } catch (IOException e) { + throw new ProcessorException(e); + } + } + public Object process() throws ProcessorException { try { Config config = Config.getInstance(); diff --git a/hsarback/src/de/hsadmin/mods/pac/PacProcessorFactory.java b/hsarback/src/de/hsadmin/mods/pac/PacProcessorFactory.java index 273cb95..16c52b4 100644 --- a/hsarback/src/de/hsadmin/mods/pac/PacProcessorFactory.java +++ b/hsarback/src/de/hsadmin/mods/pac/PacProcessorFactory.java @@ -1,8 +1,5 @@ package de.hsadmin.mods.pac; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -12,7 +9,6 @@ import java.util.Set; import javax.persistence.EntityManager; -import net.sf.jtpl.Template; import de.hsadmin.core.model.AbstractEntity; import de.hsadmin.core.qserv.CompoundProcessor; import de.hsadmin.core.qserv.EntityProcessorFactory; @@ -44,37 +40,82 @@ public class PacProcessorFactory implements EntityProcessorFactory { Pac pac = (Pac) entity; String pacName = pac.getName(); String customerEMail = pac.getCustomer().getContractualContact().getEmail(); - UnixUser unixUser = null; - Set unixUserSet = pac.getUnixUser(); - if (unixUserSet.size() == 1) { - unixUser = unixUserSet.iterator().next(); - } Hive hive = pac.getHive(); - Map hiveValues = new HashMap(); - hiveValues.put("HIVE", hive.getName()); - String hiveIP = hive.getInetAddr().getInetAddr(); - hiveValues.put("HIVE_IP", hiveIP); - hiveValues.put("HIVE_GATEWAY", hiveIP.substring(0, hiveIP.lastIndexOf('.')) + ".1"); - List> pacValuesList = new ArrayList>(); - Set pacsSet = hive.getPacs(); - for (Pac p : pacsSet) { - HashMap pacValues = new HashMap(); - pacValues.put("PAC", p.getName()); - pacValues.put("PAC_IP", p.getCurINetAddr().getInetAddr()); - pacValuesList.add(pacValues); - } - Processor hostsAppender = new TemplateProcessor("/de/hsadmin/mods/pac/hosts.jtpl", hiveValues, pacValuesList.iterator(), "/etc/hosts", true); - Processor interfacesAppender = new TemplateProcessor("/de/hsadmin/mods/pac/interfaces.jtpl", hiveValues, pacValuesList.iterator(), "/etc/network/interfaces", true); - Processor sudoersAppender = new TemplateProcessor("/de/hsadmin/mods/pac/sudoers.jtpl", hiveValues, pacValuesList.iterator(), "/etc/sudoers", true); - Processor ftpdconfAppender = new TemplateProcessor("/de/hsadmin/mods/pac/proftpd-conf.jtpl", hiveValues, pacValuesList.iterator(), "/etc/proftpd/proftpd.conf", true); + UnixUser unixUser = getPacAdminUser(pac); String password = PwGenerator.generatePassword(7, flagBuilder.build(), 100, random); - Processor newUsersProc = new ShellProcessor( + Map hiveValues = fillHiveValues(hive); + List> pacValuesList = fillPacValuesList(hive); + Map emailVars = fillEMailValues(pacName, customerEMail, password); + Processor priProcessor = new CompoundProcessor( + createAddUserProc(pacName, unixUser, password), + createEtcHostsProc(hiveValues, pacValuesList), + createNetworkInterfacesProc(hiveValues, pacValuesList), + createSudouersProc(hiveValues, pacValuesList), + createProftpdConfProc(hiveValues, pacValuesList), + createMakePacDirectoryStructure(unixUser)); + WaitingTasksProcessor secProcessor = new WaitingTasksProcessor(priProcessor); + secProcessor.appendProcessor(pac.getHiveName(), createSendEMailProc(pacName, customerEMail, emailVars), "send email to customer"); + return secProcessor; + } + + @Override + public Processor createUpdateProcessor(EntityManager em, T entity) + throws ProcessorException { + Pac pac = (Pac) entity; + Hive hive = pac.getHive(); + UnixUser unixUser = getPacAdminUser(pac); + Map hiveValues = fillHiveValues(hive); + List> pacValuesList = fillPacValuesList(hive); + return new CompoundProcessor( + createEtcHostsProc(hiveValues, pacValuesList), + createNetworkInterfacesProc(hiveValues, pacValuesList), + createSudouersProc(hiveValues, pacValuesList), + createProftpdConfProc(hiveValues, pacValuesList), + createMakePacDirectoryStructure(unixUser)); + } + + @Override + public Processor createDeleteProcessor( + EntityManager em, T entity) throws ProcessorException { + throw new ProcessorException("pac.delete not implemented"); + } + + private TemplateProcessor createEtcHostsProc( + Map hiveValues, + List> pacValuesList) throws ProcessorException { + return new TemplateProcessor("/de/hsadmin/mods/pac/hosts.jtpl", hiveValues, pacValuesList.iterator(), "/etc/hosts", true); + } + + private TemplateProcessor createNetworkInterfacesProc( + Map hiveValues, + List> pacValuesList) throws ProcessorException { + return new TemplateProcessor("/de/hsadmin/mods/pac/interfaces.jtpl", hiveValues, pacValuesList.iterator(), "/etc/network/interfaces", true); + } + + private TemplateProcessor createSudouersProc( + Map hiveValues, + List> pacValuesList) throws ProcessorException { + return new TemplateProcessor("/de/hsadmin/mods/pac/sudoers.jtpl", hiveValues, pacValuesList.iterator(), "/etc/sudoers", true); + } + + private TemplateProcessor createProftpdConfProc( + Map hiveValues, + List> 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) { + return new ShellProcessor( "newusers", pacName + ":" + password + ":" + unixUser.getUserId() + ":" + unixUser.getUserId() + ":" + unixUser.getComment() + ":" + unixUser.getHomedir() + ":" + unixUser.getShell() + "\n"); - Processor mkdirsProc = new ShellProcessor("chmod 711 " + unixUser.getHomedir() + " && " + - "su -l " + unixUser.getName() + " -s \"/bin/bash\" -c \"mkdir " + + } + + private ShellProcessor createMakePacDirectoryStructure(UnixUser unixUser) { + return new ShellProcessor("chmod 711 " + unixUser.getHomedir() + " && " + + "su -l " + unixUser.getName() + " -s \"/bin/bash\" -c \"mkdir -p " + unixUser.getHomedir() + "/doms " + unixUser.getHomedir() + "/etc " + unixUser.getHomedir() + "/users " + @@ -84,48 +125,59 @@ public class PacProcessorFactory implements EntityProcessorFactory { "chmod 1550 " + unixUser.getHomedir() + "/doms && " + "chmod 2700 " + unixUser.getHomedir() + "/etc && " + "chmod 2750 " + unixUser.getHomedir() + "/var && " + - "chmod 2750 " + unixUser.getHomedir() + "/users" + "chmod 2755 " + unixUser.getHomedir() + "/users" ); - try { - InputStream stream = TemplateProcessor.class.getClassLoader().getResourceAsStream("/de/hsadmin/mods/pac/email_new_pac_account.jtpl"); - Template template = new Template(new InputStreamReader(stream)); - template.assign("PAC", pacName); - template.assign("PASSWORD", password); - template.assign("CUST_EMAIL", customerEMail); - template.parse("main"); - Processor emailPasswordProc = new WaitingTasksProcessor( - new MailerProcessor( - customerEMail, - "Zugangsdaten des neue Hostsharing Pakets " + pacName, - template.out()) - ); - Processor priProcessor = new CompoundProcessor( - newUsersProc, - hostsAppender, - interfacesAppender, - sudoersAppender, - ftpdconfAppender, - mkdirsProc); - WaitingTasksProcessor secProcessor = new WaitingTasksProcessor(priProcessor); - secProcessor.appendProcessor(pac.getHiveName(), emailPasswordProc, "send email to customer"); - return secProcessor; - } catch (IllegalArgumentException e) { - throw new ProcessorException(e); - } catch (IOException e) { - throw new ProcessorException(e); + } + + private Processor createSendEMailProc(String pacName, + String customerEMail, Map emailVars) throws ProcessorException { + return new MailerProcessor( + customerEMail, + "Zugangsdaten des neue Hostsharing Pakets " + pacName, + "/de/hsadmin/mods/pac/email_new_pac_account.jtpl", + emailVars + ); + } + + private UnixUser getPacAdminUser(Pac pac) throws ProcessorException { + UnixUser unixUser = null; + Set unixUserSet = pac.getUnixUser(); + if (unixUserSet.size() == 1) { + unixUser = unixUserSet.iterator().next(); + } else { + throw new ProcessorException("package contains users"); } + return unixUser; } - @Override - public Processor createUpdateProcessor( - EntityManager em, T newEntity) throws ProcessorException { - throw new ProcessorException("pac.update not implemented"); + private Map fillHiveValues(Hive hive) { + Map hiveValues = new HashMap(); + hiveValues.put("HIVE", hive.getName()); + String hiveIP = hive.getInetAddr().getInetAddr(); + hiveValues.put("HIVE_IP", hiveIP); + hiveValues.put("HIVE_GATEWAY", hiveIP.substring(0, hiveIP.lastIndexOf('.')) + ".1"); + return hiveValues; } - @Override - public Processor createDeleteProcessor( - EntityManager em, T entity) throws ProcessorException { - throw new ProcessorException("pac.delete not implemented"); + private List> fillPacValuesList(Hive hive) { + List> pacValuesList = new ArrayList>(); + Set pacsSet = hive.getPacs(); + for (Pac p : pacsSet) { + HashMap pacValues = new HashMap(); + pacValues.put("PAC", p.getName()); + pacValues.put("PAC_IP", p.getCurINetAddr().getInetAddr()); + pacValuesList.add(pacValues); + } + return pacValuesList; + } + + private Map fillEMailValues(String pacName, + String customerEMail, String password) { + Map emailVars = new HashMap(); + emailVars.put("PAC", pacName); + emailVars.put("PASSWORD", password); + emailVars.put("CUST_EMAIL", customerEMail); + return emailVars; } }