Skip to content
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

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion common/src/java/com/zimbra/common/localconfig/LC.java
Original file line number Diff line number Diff line change
Expand Up @@ -1527,7 +1527,9 @@ public enum PUBLIC_SHARE_VISIBILITY { samePrimaryDomain, all, none };
public static final KnownKey zimbra_strict_unclosed_comment_tag = KnownKey.newKey(true);
public static final KnownKey zimbra_skip_tags_with_unclosed_cdata = KnownKey.newKey("style");


//ZCS-11191 : Allow to enable/disable SRS feature
public static final KnownKey zimbra_srs_enabled = KnownKey.newKey(false);
Copy link
Member

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?


static {
// Automatically set the key name with the variable name.
for (Field field : LC.class.getFields()) {
Expand Down
125 changes: 118 additions & 7 deletions store/src/java/com/zimbra/cs/service/admin/ModifyServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -58,6 +67,8 @@ public Element handle(Element request, Map<String, Object> context) throws Servi
}
checkRight(zsc, context, server, attrs);

startOrStopPostSRSd(server, attrs);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this going to be called with every modifyServer call?

Copy link
Contributor

Choose a reason for hiding this comment

The 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 .
@desouzas : Could you please provide your inputs here? Thanks..!!


// pass in true to checkImmutable
prov.modifyAttrs(server, attrs, true);

Expand All @@ -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;
}
}