-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ZCS-11191: [Backend] Integrate postsrsd #1274
base: develop
Are you sure you want to change the base?
Changes from all commits
1ad4563
fa57fac
0e57fdf
bb62b0b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,19 +16,28 @@ | |
*/ | ||
package com.zimbra.cs.service.admin; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import com.zimbra.cs.account.AccountServiceException; | ||
import com.zimbra.cs.account.Server; | ||
import com.zimbra.cs.account.Provisioning; | ||
import com.zimbra.cs.account.accesscontrol.AdminRight; | ||
import com.zimbra.cs.account.accesscontrol.Rights.Admin; | ||
import com.zimbra.common.account.Key; | ||
import com.zimbra.common.localconfig.LC; | ||
import com.zimbra.common.service.ServiceException; | ||
import com.zimbra.common.util.ZimbraLog; | ||
import com.zimbra.common.soap.AdminConstants; | ||
import com.zimbra.common.soap.Element; | ||
import com.zimbra.common.util.ZimbraLog; | ||
import com.zimbra.cs.account.AccountServiceException; | ||
import com.zimbra.cs.account.Provisioning; | ||
import com.zimbra.cs.account.Server; | ||
import com.zimbra.cs.account.accesscontrol.AdminRight; | ||
import com.zimbra.cs.account.accesscontrol.Rights.Admin; | ||
import com.zimbra.soap.ZimbraSoapContext; | ||
|
||
/** | ||
|
@@ -58,6 +67,8 @@ public Element handle(Element request, Map<String, Object> context) throws Servi | |
} | ||
checkRight(zsc, context, server, attrs); | ||
|
||
startOrStopPostSRSd(server, attrs); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this going to be called with every modifyServer call? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @silentsakky, I think the same this code should not be here, as zmcontrol and all other service's stop/start functionality is written in zm-core-utils repo, which is in Perl script. This logic should also be written in that repo, also would like to confirm the same with @desouzas . |
||
|
||
// pass in true to checkImmutable | ||
prov.modifyAttrs(server, attrs, true); | ||
|
||
|
@@ -74,4 +85,104 @@ public void docRights(List<AdminRight> relatedRights, List<String> notes) { | |
notes.add(String.format(AdminRightCheckPoint.Notes.MODIFY_ENTRY, | ||
Admin.R_modifyServer.getName(), "server")); | ||
} | ||
} | ||
|
||
/** | ||
* Enable/disable postsrs service | ||
* @param server to check what services are available | ||
* @param attrs existing map to populate services | ||
* @return nothing | ||
*/ | ||
public void startOrStopPostSRSd(Server server, Map<String, Object> attrs) throws ServiceException { | ||
|
||
List<String> command = new ArrayList<>(); | ||
List<String> response = new ArrayList<>(); | ||
List<String> attrsUI = new ArrayList<>(); | ||
List<String> attrsLDAP = new ArrayList<>(); | ||
boolean UIWantsToEnablePostsrs = false; | ||
boolean isPostsrsEnabledInLDAP = false; | ||
final String POSTSRSD_SECRET = "/opt/zimbra/common/etc/postsrsd.secret"; | ||
final String POSTSRSD_EXE = LC.zimbra_home.value() + "/common/sbin/postsrsd"; | ||
|
||
try { | ||
if (!attrs.isEmpty()) { | ||
Collections.addAll(attrsUI, (String[]) attrs.get(Provisioning.A_zimbraServiceEnabled)); | ||
ZimbraLog.mailbox.info("attrsUI: " + attrsUI); | ||
UIWantsToEnablePostsrs = attrsUI.contains("postsrs"); | ||
} | ||
|
||
if (!server.getAttrs().isEmpty()) { | ||
Collections.addAll(attrsLDAP, server.getServiceEnabled()); | ||
ZimbraLog.mailbox.info("attrsLDAP: " + attrsLDAP); | ||
isPostsrsEnabledInLDAP = attrsLDAP.contains("postsrs"); | ||
} | ||
|
||
if (UIWantsToEnablePostsrs && !isPostsrsEnabledInLDAP) { | ||
command = Stream.of(POSTSRSD_EXE, "-s", POSTSRSD_SECRET, "-d", server.getName(), "-D") | ||
.collect(Collectors.toList()); | ||
response = executeLinuxCommand(command); | ||
ZimbraLog.mailbox.info(response); | ||
ZimbraLog.mailbox.info("postsrsd has been enabled"); | ||
} else if (UIWantsToEnablePostsrs && isPostsrsEnabledInLDAP) { | ||
ZimbraLog.mailbox.info("postsrsd is already enabled"); | ||
} else if (!UIWantsToEnablePostsrs && isPostsrsEnabledInLDAP) { | ||
// There is no command to disable SRS so far. The only way is killing the | ||
// process. | ||
command = Stream.of("pgrep", "-f", "postsrsd").collect(Collectors.toList()); | ||
response = executeLinuxCommand(command); | ||
ZimbraLog.mailbox.info("response: " + response); | ||
if (response.isEmpty()) { | ||
ZimbraLog.mailbox.info("postsrsd is already disabled"); | ||
} else { | ||
String postSrsdPID = response.get(0); | ||
command.clear(); | ||
response.clear(); | ||
command = Stream.of("kill", "-9", postSrsdPID).collect(Collectors.toList()); | ||
response = executeLinuxCommand(command); | ||
ZimbraLog.mailbox.info("response: " + response); | ||
ZimbraLog.mailbox.info("postsrsd has been disabled"); | ||
} | ||
} else if (!UIWantsToEnablePostsrs && !isPostsrsEnabledInLDAP) { | ||
ZimbraLog.mailbox.info("postsrsd is already disabled"); | ||
} | ||
} catch (IOException e) { | ||
ZimbraLog.mailbox.warn(e); | ||
} catch (InterruptedException e) { | ||
ZimbraLog.mailbox.warn(e); | ||
} | ||
} | ||
|
||
/** | ||
* Execute linux command | ||
* @param command to be executed | ||
* @return list of string | ||
*/ | ||
public List<String> executeLinuxCommand(List<String> command) throws IOException, InterruptedException { | ||
|
||
InputStream is = null; | ||
ProcessBuilder pb = null; | ||
Process ps = null; | ||
List<String> lines = new ArrayList<>(); | ||
|
||
ZimbraLog.mailbox.info("command: " + command); | ||
// Executing the linux command | ||
pb = new ProcessBuilder(command); | ||
ps = pb.start(); | ||
int exitValue = ps.waitFor(); | ||
ZimbraLog.mailbox.info("command executed"); | ||
// Getting executed command response as List | ||
if (exitValue == 0) { | ||
is = ps.getInputStream(); | ||
} else { | ||
is = ps.getErrorStream(); | ||
} | ||
InputStreamReader isr = new InputStreamReader(is); | ||
BufferedReader br = new BufferedReader(isr); | ||
String line; | ||
while ((line = br.readLine()) != null) { | ||
lines.add(line); | ||
} | ||
ps.destroy(); | ||
|
||
return lines; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason to put this into LC instead of LDAP? are we planning to release this feature in a patch?