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

Fish 5785 create systemd service #5450

Merged
merged 2 commits into from
Oct 21, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2021] Payara Foundation and/or affiliates

package com.sun.enterprise.admin.servermgmt.cli;

Expand Down Expand Up @@ -66,6 +67,11 @@
@org.jvnet.hk2.annotations.Service(name = "create-service")
@PerLookup
public final class CreateServiceCommand extends CLICommand {
public static final String SYSTEM_TYPE_SOLARIS = "solaris";
public static final String SYSTEM_TYPE_SYSTEMD = "systemd";
public static final String SYSTEM_TYPE_SYSTEMV = "systemv";
public static final String SYSTEM_TYPE_WINDOWS = "windows";

@Param(name = "name", optional = true)
private String serviceName;
@Param(name = "serviceproperties", optional = true)
Expand All @@ -79,6 +85,8 @@ public final class CreateServiceCommand extends CLICommand {
private File userSpecifiedDomainDirParent;
@Param(name = "serviceuser", optional = true)
private String serviceUser;
@Param(name = "system-type", optional = true)
private String systemType;

/*
* The following parameters allow an unattended start-up any number of
Expand Down Expand Up @@ -117,6 +125,7 @@ protected void validate() throws CommandException {
dirs = selector.dirs();

validateServiceName();
validateSystemType();
validateAsadmin();
}
catch (CommandException e) {
Expand All @@ -131,21 +140,24 @@ protected void validate() throws CommandException {
@Override
protected int executeCommand() throws CommandException {
try {
final Service service = ServiceFactory.getService(dirs, getType());
final Service service = ServiceFactory.getService(dirs, getType(), systemType);
PlatformServicesInfo info = service.getInfo();
info.setTrace(logger.isLoggable(Level.FINER));
info.setDryRun(dry_run);
info.setForce(force);
info.setAppServerUser(getProgramOptions().getUser());
if (ok(serviceName))
if (ok(serviceName)) {
info.setServiceName(serviceName);
}

if (ok(serviceUser))
if (ok(serviceUser)) {
info.setServiceUser(serviceUser);
}

if (programOpts.getPasswordFile() != null)
if (programOpts.getPasswordFile() != null) {
info.setPasswordFile(SmartFile.sanitize(
new File(programOpts.getPasswordFile())));
}

service.setServiceProperties(serviceProperties);
service.createService();
Expand All @@ -157,22 +169,20 @@ protected int executeCommand() throws CommandException {
new File(dirs.getServerDir(), "PlatformServices.log"));
logger.info(tellUserAboutHelp);
service.writeReadmeFile(help);

}
catch (Exception e) {
} catch (Exception e) {
// We only want to wrap the string -- not the Exception.
// Otherwise the message that is printed out to the user will be like this:
// java.lang.IllegalArgumentException: The passwordfile blah blah blah
// What we want is:
// The passwordfile blah blah blah
// IT 8882

String msg = e.getMessage();

if (ok(msg))
if (ok(msg)) {
throw new CommandException(msg);
else
} else {
throw new CommandException(e);
}
}
return 0;
}
Expand Down Expand Up @@ -206,6 +216,17 @@ private void validateAsadmin() throws CommandException {
}
}

private void validateSystemType() throws CommandException {
if (ok(systemType)
&& !(SYSTEM_TYPE_SOLARIS.equals(systemType)
|| SYSTEM_TYPE_SYSTEMD.equals(systemType)
|| SYSTEM_TYPE_SYSTEMV.equals(systemType)
|| SYSTEM_TYPE_WINDOWS.equals(systemType))) {
throw new CommandException(
strings.get("create.service.invalidSystemType", systemType));
}
}

private AppserverServiceType getType() {
if (selector.isInstance())
return AppserverServiceType.Instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2018] Payara Foundation and/or affiliates
// Portions Copyright [2018-2021] Payara Foundation and/or affiliates

package com.sun.enterprise.admin.servermgmt.cli;

Expand Down Expand Up @@ -84,6 +84,9 @@ public final class DeleteServiceCommand extends CLICommand {
private String userSpecifiedNodeDir; // nodeDirRoot
@Param(name = "node", optional = true, alias = "nodeagent")
private String userSpecifiedNode;
@Param(name = "system-type", optional = true)
private String systemType;

private static final LocalStringsImpl STRINGS = new LocalStringsImpl(DeleteServiceCommand.class);
private ServerDirs dirs;
private ServerDirsSelector selector = null;
Expand All @@ -103,6 +106,7 @@ protected void validate() throws CommandException {
userSpecifiedNode);
dirs = selector.dirs();

validateSystemType();
validateServiceName();
}
catch (CommandException e) {
Expand All @@ -117,33 +121,34 @@ protected void validate() throws CommandException {
@Override
protected int executeCommand() throws CommandException {
try {
final Service service = ServiceFactory.getService(dirs, getType());
final Service service = ServiceFactory.getService(dirs, getType(), systemType);
PlatformServicesInfo info = service.getInfo();
info.setTrace(logger.isLoggable(Level.FINER));

if (ok(serviceName))
if (ok(serviceName)) {
info.setServiceName(serviceName);
}

if (programOpts.getPasswordFile() != null)
if (programOpts.getPasswordFile() != null) {
info.setPasswordFile(SmartFile.sanitize(
new File(programOpts.getPasswordFile())));
}

service.deleteService();
}
catch (Exception e) {
} catch (Exception e) {
// We only want to wrap the string -- not the Exception.
// Otherwise the message that is printed out to the user will be like this:
// java.lang.IllegalArgumentException: The passwordfile blah blah blah
// What we want is:
// The passwordfile blah blah blah
// IT 8882

String msg = e.getMessage();

if (ok(msg))
if (ok(msg)) {
throw new CommandException(msg);
else
} else {
throw new CommandException(e);
}
}
return 0;
}
Expand All @@ -160,6 +165,17 @@ private void validateServiceName() throws CommandException {
logger.log(Level.FINER, "service name = {0}", serviceName);
}

private void validateSystemType() throws CommandException {
if (ok(systemType)
&& !(CreateServiceCommand.SYSTEM_TYPE_SOLARIS.equals(systemType)
|| CreateServiceCommand.SYSTEM_TYPE_SYSTEMD.equals(systemType)
|| CreateServiceCommand.SYSTEM_TYPE_SYSTEMV.equals(systemType)
|| CreateServiceCommand.SYSTEM_TYPE_WINDOWS.equals(systemType))) {
throw new CommandException(
STRINGS.get("create.service.invalidSystemType", systemType));
}
}

private AppserverServiceType getType() {
if (selector.isInstance())
return AppserverServiceType.Instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
# only if the new code is made subject to such option by the copyright
# holder.
#
# Portions Copyright [2016-2019] [Payara Foundation and/or its affiliates]
# Portions Copyright [2016-2021] [Payara Foundation and/or its affiliates]

## list-domains
## domain1 running
Expand All @@ -56,6 +56,7 @@ create.service.noAsadminScript=Can not locate the administration command script.
create.service.noDomainDirs=No domains exist in {0}
create.service.tooManyDomainDirs=There is more than one domain in {0}. Try again but specify the domain name as the last argument.
create.service.runtimeHelp={0}\nFor your convenience this message has also been saved to this file: {1}
create.service.invalidSystemType=Invalid system type {0}, valid values are: solaris, systemd, systemv, windows
###################
## login
AdminUserPrompt=Enter admin user name [default: {0}]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class Constants {
static final String CREDENTIALS_STOP_TN = "CREDENTIALS_STOP";
static final String SERVICEUSER_STOP_TN = "SERVICEUSER_STOP";
static final String SERVICEUSER_START_TN = "SERVICEUSER_START";
static final String SERVICE_USER_TN = "SERVICE_USER";
static final String SERVICE_PROPERTIES_TN = "SERVICE_PROPERTIES";
static final String PID_FILE_TN = "PID_FILE";
///////////////////////////////////////////////////////////////////////////
///// Other Constants ///////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
Expand All @@ -89,6 +92,7 @@ class Constants {
static final String SERVICE_NAME_PREFIX = "application/Payara/";
static final String ETC = "/etc";
static final String INITD = "/etc/init.d";
static final String SYSTEMD_CONFIG_DIR = "/etc/systemd/system";
static final String REGEXP_PATTERN_BEGIN = "[KS][0-9][0-9]?";

}
Loading