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. * Installs the plugin with the given name from the list of available plugins.
* *
* @param name plugin name * @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", "title": "{{name}} Plugin installieren",
"restart": "Neustarten um Plugin zu aktivieren", "restart": "Neustarten um Plugin zu aktivieren",
"install": "Installieren", "install": "Installieren",
"installAndRestart": "Installieren und Neustarten",
"abort": "Abbrechen", "abort": "Abbrechen",
"author": "Autor", "author": "Autor",
"version": "Version", "version": "Version",
"dependencyNotification": "Mit diesem Plugin werden folgende Abhängigkeiten mit installieren wenn sie noch nicht vorhanden sind!", "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": { "repositoryRole": {

View File

@@ -34,11 +34,15 @@
"title": "Install {{name}} Plugin", "title": "Install {{name}} Plugin",
"restart": "Restart to activate", "restart": "Restart to activate",
"install": "Install", "install": "Install",
"installAndRestart": "Install and Restart",
"abort": "Abort", "abort": "Abort",
"author": "Author", "author": "Author",
"version": "Version", "version": "Version",
"dependencyNotification": "With this plugin, the following dependencies are installed if they are not available yet!", "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": { "repositoryRole": {

View File

@@ -8,9 +8,10 @@ import {
apiClient, apiClient,
Button, Button,
ButtonGroup, ButtonGroup,
Checkbox, ErrorNotification, Checkbox,
ErrorNotification,
Modal, Modal,
SubmitButton Notification
} from "@scm-manager/ui-components"; } from "@scm-manager/ui-components";
import classNames from "classnames"; import classNames from "classnames";
@@ -25,14 +26,13 @@ type Props = {
}; };
type State = { type State = {
success: boolean,
restart: boolean,
loading: boolean, loading: boolean,
error?: Error error?: Error
}; };
const styles = { const styles = {
titleVersion: {
marginLeft: "0.75rem"
},
userLabelAlignment: { userLabelAlignment: {
textAlign: "left", textAlign: "left",
marginRight: 0, marginRight: 0,
@@ -40,37 +40,48 @@ const styles = {
}, },
userFieldFlex: { userFieldFlex: {
flexGrow: 4 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) { constructor(props: Props) {
super(props); super(props);
this.state = { 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) => { install = (e: Event) => {
const { plugin, onClose } = this.props; const { restart } = this.state;
const { plugin } = this.props;
this.setState({ this.setState({
loading: true loading: true
}); });
e.preventDefault(); e.preventDefault();
apiClient.post(plugin._links.install.href) apiClient
.then(() => { .post(plugin._links.install.href + "?restart=" + restart.toString())
this.setState({ .then(this.onInstallSuccess)
loading: false,
error: undefined
}, onClose);
})
.catch(error => { .catch(error => {
this.setState({ this.setState({
loading: false, loading: false,
@@ -81,14 +92,25 @@ class PluginModal extends React.Component<Props,State> {
footer = () => { footer = () => {
const { onClose, t } = this.props; 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 ( return (
<form>
<ButtonGroup> <ButtonGroup>
<SubmitButton label={t("plugins.modal.install")} loading={loading} action={this.install} disabled={!!error} /> <Button
label={t(label)}
color={color}
action={this.install}
loading={loading}
disabled={!!error || success}
/>
<Button label={t("plugins.modal.abort")} action={onClose} /> <Button label={t("plugins.modal.abort")} action={onClose} />
</ButtonGroup> </ButtonGroup>
</form>
); );
}; };
@@ -98,51 +120,61 @@ class PluginModal extends React.Component<Props,State> {
let dependencies = null; let dependencies = null;
if (plugin.dependencies && plugin.dependencies.length > 0) { if (plugin.dependencies && plugin.dependencies.length > 0) {
dependencies = ( dependencies = (
<> <div className="media">
<Notification type="warning">
<strong>{t("plugins.modal.dependencyNotification")}</strong> <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}> <ul className={classes.listSpacing}>
{plugin.dependencies.map((dependency, index) => { {plugin.dependencies.map((dependency, index) => {
return <li key={index}>{dependency}</li>; return <li key={index}>{dependency}</li>;
})} })}
</ul> </ul>
</Notification>
</div> </div>
</div>
</>
); );
} }
return dependencies; return dependencies;
} }
renderError = () => { renderNotifications = () => {
const { classes } = this.props; const { t } = this.props;
const { error } = this.state; const { restart, error, success } = this.state;
if (error) { if (error) {
return ( return (
<div className={classes.error}> <div className="media">
<ErrorNotification error={error} /> <ErrorNotification error={error} />
</div> </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; return null;
}; };
render() { handleRestartChange = (value: boolean) => {
this.setState({
restart: value
});
};
render() {
const { restart } = this.state;
const { plugin, onClose, classes, t } = this.props; const { plugin, onClose, classes, t } = this.props;
const body = ( const body = (
@@ -197,14 +229,14 @@ class PluginModal extends React.Component<Props,State> {
<div className="media"> <div className="media">
<div className="media-content"> <div className="media-content">
<Checkbox <Checkbox
checked={false} checked={restart}
label={t("plugins.modal.restart")} label={t("plugins.modal.restart")}
onChange={null} onChange={this.handleRestartChange}
disabled={null} disabled={false}
/> />
</div> </div>
</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.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@@ -90,9 +91,9 @@ public class AvailablePluginResource {
@ResponseCode(code = 200, condition = "success"), @ResponseCode(code = 200, condition = "success"),
@ResponseCode(code = 500, condition = "internal server error") @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(); PluginPermissions.manage().check();
pluginManager.install(name); pluginManager.install(name, restartAfterInstallation);
return Response.ok().build(); return Response.ok().build();
} }
} }

View File

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

View File

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

View File

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