Skip to content

Commit

Permalink
ZCS-11191: Corrected intendation issues and domain value
Browse files Browse the repository at this point in the history
  • Loading branch information
swaatiTelus committed Nov 10, 2022
1 parent 0e57fdf commit bb62b0b
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 103 deletions.
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);

static {
// Automatically set the key name with the variable name.
for (Field field : LC.class.getFields()) {
Expand Down
199 changes: 97 additions & 102 deletions store/src/java/com/zimbra/cs/service/admin/ModifyServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand All @@ -37,7 +36,6 @@
import com.zimbra.cs.account.AccountServiceException;
import com.zimbra.cs.account.Provisioning;
import com.zimbra.cs.account.Server;
import com.zimbra.cs.account.ZAttrServer;
import com.zimbra.cs.account.accesscontrol.AdminRight;
import com.zimbra.cs.account.accesscontrol.Rights.Admin;
import com.zimbra.soap.ZimbraSoapContext;
Expand Down Expand Up @@ -87,107 +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 {

ZimbraLog.mailbox.info("==============================");

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_EXE = LC.zimbra_home.value() + "/common/sbin/postsrsd";
final String POSTSRSD_SECRET = "/opt/zimbra/common/etc/postsrsd.secret";

try {

if(!attrs.isEmpty()) {
Collections.addAll(attrsUI, (String[]) attrs.get("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", "mydomain", "-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);
}


ZimbraLog.mailbox.info("==============================");


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;

}

}
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;
}
}

0 comments on commit bb62b0b

Please sign in to comment.