implemented restart after installation

This commit is contained in:
Sebastian Sdorra
2019-08-21 11:22:49 +02:00
parent 05d7e0bd1e
commit 25cb0d6a25
8 changed files with 143 additions and 77 deletions

View File

@@ -77,6 +77,7 @@ public interface PluginManager {
* Installs the plugin with the given name from the list of available plugins.
*
* @param name plugin name
* @param restartAfterInstallation restart context after plugin installation
*/
void install(String name);
void install(String name, boolean restartAfterInstallation);
}

View File

@@ -34,11 +34,15 @@
"title": "{{name}} Plugin installieren",
"restart": "Neustarten um Plugin zu aktivieren",
"install": "Installieren",
"installAndRestart": "Installieren und Neustarten",
"abort": "Abbrechen",
"author": "Autor",
"version": "Version",
"dependencyNotification": "Mit diesem Plugin werden folgende Abhängigkeiten mit installieren wenn sie noch nicht vorhanden sind!",
"dependencies": "Abhängigkeiten"
"dependencies": "Abhängigkeiten",
"successNotification": "Das Plugin wurde erfolgreich installiert. Um Änderungen an der UI zu sehen, muss die Seite neu geladen werden:",
"reload": "jetzt new laden",
"restartNotification": "Der SCM-Manager Kontext sollte nur neu gestartet werden, wenn aktuell niemand damit arbeitet."
}
},
"repositoryRole": {

View File

@@ -34,11 +34,15 @@
"title": "Install {{name}} Plugin",
"restart": "Restart to activate",
"install": "Install",
"installAndRestart": "Install and Restart",
"abort": "Abort",
"author": "Author",
"version": "Version",
"dependencyNotification": "With this plugin, the following dependencies are installed if they are not available yet!",
"dependencies": "Dependencies"
"dependencies": "Dependencies",
"successNotification": "Successful installed plugin. You have to reload the page, to see ui changes:",
"reload": "reload now",
"restartNotification": "Restarting the scm-manager context, should only be done if no one else is currently working with it."
}
},
"repositoryRole": {

View File

@@ -8,9 +8,10 @@ import {
apiClient,
Button,
ButtonGroup,
Checkbox, ErrorNotification,
Checkbox,
ErrorNotification,
Modal,
SubmitButton
Notification
} from "@scm-manager/ui-components";
import classNames from "classnames";
@@ -25,14 +26,13 @@ type Props = {
};
type State = {
success: boolean,
restart: boolean,
loading: boolean,
error?: Error
};
const styles = {
titleVersion: {
marginLeft: "0.75rem"
},
userLabelAlignment: {
textAlign: "left",
marginRight: 0,
@@ -40,37 +40,48 @@ const styles = {
},
userFieldFlex: {
flexGrow: 4
},
listSpacing: {
marginTop: "0 !important"
},
error: {
marginTop: "1em"
}
};
class PluginModal extends React.Component<Props,State> {
class PluginModal extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
loading: false
loading: false,
restart: false,
success: false
};
}
onInstallSuccess = () => {
const { restart } = this.state;
const { onClose } = this.props;
const newState = {
loading: false,
error: undefined
};
if (restart) {
this.setState({
...newState,
success: true
});
} else {
this.setState(newState, onClose);
}
};
install = (e: Event) => {
const { plugin, onClose } = this.props;
const { restart } = this.state;
const { plugin } = this.props;
this.setState({
loading: true
});
e.preventDefault();
apiClient.post(plugin._links.install.href)
.then(() => {
this.setState({
loading: false,
error: undefined
}, onClose);
})
apiClient
.post(plugin._links.install.href + "?restart=" + restart.toString())
.then(this.onInstallSuccess)
.catch(error => {
this.setState({
loading: false,
@@ -81,14 +92,25 @@ class PluginModal extends React.Component<Props,State> {
footer = () => {
const { onClose, t } = this.props;
const { loading, error } = this.state;
const { loading, error, restart, success } = this.state;
let color = "primary";
let label = "plugins.modal.install";
if (restart) {
color = "warning";
label = "plugins.modal.installAndRestart";
}
return (
<form>
<ButtonGroup>
<SubmitButton label={t("plugins.modal.install")} loading={loading} action={this.install} disabled={!!error} />
<Button label={t("plugins.modal.abort")} action={onClose} />
</ButtonGroup>
</form>
<ButtonGroup>
<Button
label={t(label)}
color={color}
action={this.install}
loading={loading}
disabled={!!error || success}
/>
<Button label={t("plugins.modal.abort")} action={onClose} />
</ButtonGroup>
);
};
@@ -98,51 +120,61 @@ class PluginModal extends React.Component<Props,State> {
let dependencies = null;
if (plugin.dependencies && plugin.dependencies.length > 0) {
dependencies = (
<>
<strong>{t("plugins.modal.dependencyNotification")}</strong>
<div className="field is-horizontal">
<div
className={classNames(
classes.userLabelAlignment,
"field-label is-inline-flex"
)}
>
{t("plugins.modal.dependencies")}:
</div>
<div
className={classNames(
classes.userFieldFlex,
"field-body is-inline-flex"
)}
>
<ul className={classes.listSpacing}>
{plugin.dependencies.map((dependency, index) => {
return <li key={index}>{dependency}</li>;
})}
</ul>
</div>
</div>
</>
<div className="media">
<Notification type="warning">
<strong>{t("plugins.modal.dependencyNotification")}</strong>
<ul className={classes.listSpacing}>
{plugin.dependencies.map((dependency, index) => {
return <li key={index}>{dependency}</li>;
})}
</ul>
</Notification>
</div>
);
}
return dependencies;
}
renderError = () => {
const { classes } = this.props;
const { error } = this.state;
renderNotifications = () => {
const { t } = this.props;
const { restart, error, success } = this.state;
if (error) {
return (
<div className={classes.error}>
<div className="media">
<ErrorNotification error={error} />
</div>
);
} else if (success) {
return (
<div className="media">
<Notification type="success">
{t("plugins.modal.successNotification")}{" "}
<a onClick={e => window.location.reload()}>
{t("plugins.modal.reload")}
</a>
</Notification>
</div>
);
} else if (restart) {
return (
<div className="media">
<Notification type="warning">
{t("plugins.modal.restartNotification")}
</Notification>
</div>
);
}
return null;
};
render() {
handleRestartChange = (value: boolean) => {
this.setState({
restart: value
});
};
render() {
const { restart } = this.state;
const { plugin, onClose, classes, t } = this.props;
const body = (
@@ -197,14 +229,14 @@ class PluginModal extends React.Component<Props,State> {
<div className="media">
<div className="media-content">
<Checkbox
checked={false}
checked={restart}
label={t("plugins.modal.restart")}
onChange={null}
disabled={null}
onChange={this.handleRestartChange}
disabled={false}
/>
</div>
</div>
{this.renderError()}
{this.renderNotifications()}
</>
);

View File

@@ -16,6 +16,7 @@ import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Optional;
@@ -90,9 +91,9 @@ public class AvailablePluginResource {
@ResponseCode(code = 200, condition = "success"),
@ResponseCode(code = 500, condition = "internal server error")
})
public Response installPlugin(@PathParam("name") String name) {
public Response installPlugin(@PathParam("name") String name, @QueryParam("restart") boolean restartAfterInstallation) {
PluginPermissions.manage().check();
pluginManager.install(name);
pluginManager.install(name, restartAfterInstallation);
return Response.ok().build();
}
}

View File

@@ -40,6 +40,8 @@ import com.google.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.NotFoundException;
import sonia.scm.event.ScmEventBus;
import sonia.scm.lifecycle.RestartEvent;
//~--- JDK imports ------------------------------------------------------------
import javax.inject.Inject;
@@ -61,12 +63,14 @@ public class DefaultPluginManager implements PluginManager {
private static final Logger LOG = LoggerFactory.getLogger(DefaultPluginManager.class);
private final ScmEventBus eventBus;
private final PluginLoader loader;
private final PluginCenter center;
private final PluginInstaller installer;
@Inject
public DefaultPluginManager(PluginLoader loader, PluginCenter center, PluginInstaller installer) {
public DefaultPluginManager(ScmEventBus eventBus, PluginLoader loader, PluginCenter center, PluginInstaller installer) {
this.eventBus = eventBus;
this.loader = loader;
this.center = center;
this.installer = installer;
@@ -112,7 +116,7 @@ public class DefaultPluginManager implements PluginManager {
}
@Override
public void install(String name) {
public void install(String name, boolean restartAfterInstallation) {
PluginPermissions.manage().check();
List<AvailablePlugin> plugins = collectPluginsToInstall(name);
List<PendingPluginInstallation> pendingInstallations = new ArrayList<>();
@@ -125,6 +129,9 @@ public class DefaultPluginManager implements PluginManager {
throw ex;
}
}
if (restartAfterInstallation) {
eventBus.post(new RestartEvent(PluginManager.class, "plugin installation"));
}
}
private void cancelPending(List<PendingPluginInstallation> pendingInstallations) {

View File

@@ -136,7 +136,7 @@ class AvailablePluginResourceTest {
dispatcher.invoke(request, response);
verify(pluginManager).install("pluginName");
verify(pluginManager).install("pluginName", false);
assertThat(HttpServletResponse.SC_OK).isEqualTo(response.getStatus());
}
}

View File

@@ -15,6 +15,8 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.NotFoundException;
import sonia.scm.event.ScmEventBus;
import sonia.scm.lifecycle.RestartEvent;
import java.util.List;
import java.util.Optional;
@@ -26,6 +28,9 @@ import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class DefaultPluginManagerTest {
@Mock
private ScmEventBus eventBus;
@Mock
private PluginLoader loader;
@@ -144,9 +149,10 @@ class DefaultPluginManagerTest {
AvailablePlugin git = createAvailable("scm-git-plugin");
when(center.getAvailable()).thenReturn(ImmutableSet.of(git));
manager.install("scm-git-plugin");
manager.install("scm-git-plugin", false);
verify(installer).install(git);
verify(eventBus, never()).post(any());
}
@Test
@@ -156,7 +162,7 @@ class DefaultPluginManagerTest {
AvailablePlugin mail = createAvailable("scm-mail-plugin");
when(center.getAvailable()).thenReturn(ImmutableSet.of(review, mail));
manager.install("scm-review-plugin");
manager.install("scm-review-plugin", false);
verify(installer).install(mail);
verify(installer).install(review);
@@ -172,7 +178,7 @@ class DefaultPluginManagerTest {
InstalledPlugin installedMail = createInstalled("scm-mail-plugin");
when(loader.getInstalledPlugins()).thenReturn(ImmutableList.of(installedMail));
manager.install("scm-review-plugin");
manager.install("scm-review-plugin", false);
verify(installer).install(review);
}
@@ -194,7 +200,7 @@ class DefaultPluginManagerTest {
doThrow(new PluginChecksumMismatchException("checksum does not match")).when(installer).install(review);
assertThrows(PluginInstallException.class, () -> manager.install("scm-review-plugin"));
assertThrows(PluginInstallException.class, () -> manager.install("scm-review-plugin", false));
verify(pendingNotification).cancel();
verify(pendingMail).cancel();
@@ -208,11 +214,22 @@ class DefaultPluginManagerTest {
when(mail.getDescriptor().getDependencies()).thenReturn(ImmutableSet.of("scm-notification-plugin"));
when(center.getAvailable()).thenReturn(ImmutableSet.of(review, mail));
assertThrows(NotFoundException.class, () -> manager.install("scm-review-plugin"));
assertThrows(NotFoundException.class, () -> manager.install("scm-review-plugin", false));
verify(installer, never()).install(any());
}
@Test
void shouldSendRestartEventAfterInstallation() {
AvailablePlugin git = createAvailable("scm-git-plugin");
when(center.getAvailable()).thenReturn(ImmutableSet.of(git));
manager.install("scm-git-plugin", true);
verify(installer).install(git);
verify(eventBus).post(any(RestartEvent.class));
}
}
@Nested
@@ -255,7 +272,7 @@ class DefaultPluginManagerTest {
@Test
void shouldThrowAuthorizationExceptionsForInstallMethod() {
assertThrows(AuthorizationException.class, () -> manager.install("test"));
assertThrows(AuthorizationException.class, () -> manager.install("test", false));
}
}