delete pac

This commit is contained in:
Peter Hormanns 2011-07-27 15:20:56 +00:00
parent be69a332b7
commit ac52ad8cdf
2 changed files with 142 additions and 65 deletions

View File

@ -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<String, String> 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();

View File

@ -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<UnixUser> unixUserSet = pac.getUnixUser();
if (unixUserSet.size() == 1) {
unixUser = unixUserSet.iterator().next();
}
Hive hive = pac.getHive();
Map<String, String> hiveValues = new HashMap<String, String>();
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<Map<String, String>> pacValuesList = new ArrayList<Map<String, String>>();
Set<Pac> pacsSet = hive.getPacs();
for (Pac p : pacsSet) {
HashMap<String, String> pacValues = new HashMap<String, String>();
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<String, String> hiveValues = fillHiveValues(hive);
List<Map<String, String>> pacValuesList = fillPacValuesList(hive);
Map<String, String> 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 <T extends AbstractEntity> Processor createUpdateProcessor(EntityManager em, T entity)
throws ProcessorException {
Pac pac = (Pac) entity;
Hive hive = pac.getHive();
UnixUser unixUser = getPacAdminUser(pac);
Map<String, String> hiveValues = fillHiveValues(hive);
List<Map<String, String>> pacValuesList = fillPacValuesList(hive);
return new CompoundProcessor(
createEtcHostsProc(hiveValues, pacValuesList),
createNetworkInterfacesProc(hiveValues, pacValuesList),
createSudouersProc(hiveValues, pacValuesList),
createProftpdConfProc(hiveValues, pacValuesList),
createMakePacDirectoryStructure(unixUser));
}
@Override
public <T extends AbstractEntity> Processor createDeleteProcessor(
EntityManager em, T entity) throws ProcessorException {
throw new ProcessorException("pac.delete not implemented");
}
private TemplateProcessor 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(
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(
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(
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) {
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<String, String> 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<UnixUser> unixUserSet = pac.getUnixUser();
if (unixUserSet.size() == 1) {
unixUser = unixUserSet.iterator().next();
} else {
throw new ProcessorException("package contains users");
}
return unixUser;
}
@Override
public <T extends AbstractEntity> Processor createUpdateProcessor(
EntityManager em, T newEntity) throws ProcessorException {
throw new ProcessorException("pac.update not implemented");
private Map<String, String> fillHiveValues(Hive hive) {
Map<String, String> hiveValues = new HashMap<String, String>();
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 <T extends AbstractEntity> Processor createDeleteProcessor(
EntityManager em, T entity) throws ProcessorException {
throw new ProcessorException("pac.delete not implemented");
private List<Map<String, String>> fillPacValuesList(Hive hive) {
List<Map<String, String>> pacValuesList = new ArrayList<Map<String, String>>();
Set<Pac> pacsSet = hive.getPacs();
for (Pac p : pacsSet) {
HashMap<String, String> pacValues = new HashMap<String, String>();
pacValues.put("PAC", p.getName());
pacValues.put("PAC_IP", p.getCurINetAddr().getInetAddr());
pacValuesList.add(pacValues);
}
return pacValuesList;
}
private Map<String, String> fillEMailValues(String pacName,
String customerEMail, String password) {
Map<String, String> emailVars = new HashMap<String, String>();
emailVars.put("PAC", pacName);
emailVars.put("PASSWORD", password);
emailVars.put("CUST_EMAIL", customerEMail);
return emailVars;
}
}