Skip to content

Commit

Permalink
Update command #539
Browse files Browse the repository at this point in the history
  • Loading branch information
AuroraLS3 committed Apr 14, 2018
1 parent 23a3fc4 commit 4e0f1b3
Show file tree
Hide file tree
Showing 17 changed files with 592 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.djrapitops.plan.command.commands.*;
import com.djrapitops.plan.command.commands.manage.ManageConDebugCommand;
import com.djrapitops.plan.system.settings.Permissions;
import com.djrapitops.plan.system.settings.Settings;
import com.djrapitops.plan.system.settings.locale.Locale;
import com.djrapitops.plan.system.settings.locale.Msg;
import com.djrapitops.plugin.command.CommandNode;
Expand Down Expand Up @@ -49,6 +50,7 @@ public PlanBungeeCommand(PlanBungee plugin) {
new BungeeSetupToggleCommand(),
new ReloadCommand(plugin),
new StatusCommand<>(plugin, Permissions.MANAGE.getPermission(), plugin.getColorScheme()),
(Settings.ALLOW_UPDATE.isTrue() ? new UpdateCommand() : null)
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public PlanCommand(PlanPlugin plugin) {
new ReloadCommand(plugin),
new ManageCommand(plugin, this),
new StatusCommand<>(plugin, Permissions.MANAGE.getPermission(), plugin.getColorScheme()),
(Settings.DEV_MODE.isTrue() ? new DevCommand() : null)
(Settings.DEV_MODE.isTrue() ? new DevCommand() : null),
(Settings.ALLOW_UPDATE.isTrue() ? new UpdateCommand() : null)
}
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
package com.djrapitops.plan.command.commands;

import com.djrapitops.plan.api.exceptions.connection.*;
import com.djrapitops.plan.api.exceptions.database.DBException;
import com.djrapitops.plan.command.commands.manage.ManageConDebugCommand;
import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plan.system.database.databases.operation.FetchOperations;
import com.djrapitops.plan.system.info.InfoSystem;
import com.djrapitops.plan.system.info.request.CheckConnectionRequest;
import com.djrapitops.plan.system.info.request.UpdateCancelRequest;
import com.djrapitops.plan.system.info.server.Server;
import com.djrapitops.plan.system.settings.Permissions;
import com.djrapitops.plan.system.update.VersionCheckSystem;
import com.djrapitops.plan.system.update.VersionInfo;
import com.djrapitops.plan.system.webserver.WebServerSystem;
import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.command.CommandNode;
import com.djrapitops.plugin.command.CommandType;
import com.djrapitops.plugin.command.ISender;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

/**
* Command that updates all servers in the network
*
* @author Rsl1122
*/
public class UpdateCommand extends CommandNode {

public UpdateCommand() {
super("update", Permissions.MANAGE.getPermission(), CommandType.ALL);
setArguments("[-update]/[cancel]");
setShortHelp("Get change log link or update plugin.");
setInDepthHelp(
"/plan update",
" Used to update the plugin on the next shutdown\n",
" /plan update - get change log link",
" /plan update -update - Schedule update to happen on all network servers that are online next time they reboot.",
" /plan update cancel - Cancel scheduled update on servers that haven't rebooted yet."
);
}

@Override
public void onCommand(ISender sender, String commandLabel, String[] args) {
if (!VersionCheckSystem.isNewVersionAvailable()) {
sender.sendMessage("§aYou're running the latest version of Plan.");
return;
}

VersionInfo available = VersionCheckSystem.getInstance().getNewVersionAvailable();
String downloadUrl = available.getDownloadUrl();

if (!available.isTrusted()) {
sender.sendMessage("§cVersion download url did not start with " +
"https://github.com/Rsl1122/Plan-PlayerAnalytics/releases/ " +
"and might not be trusted. You can download this version manually here (Direct download):");
sender.sendLink(downloadUrl, downloadUrl);
return;
}

if (args.length == 0) {
sender.sendLink("Change Log v" + available.getVersion().toString() + ": ", "Click me", available.getChangeLogUrl());
return;
}

String firstArgument = args[0];
if ("-update".equals(firstArgument)) {
handleUpdate(sender, args);
} else if ("cancel".equals(firstArgument)) {
cancel(sender);
} else {
throw new IllegalArgumentException("Unknown argument, use '-update' or 'cancel'");
}
}

private void cancel(ISender sender) {
try {
cancel(sender, Database.getActive().fetch().getServers());
sender.sendMessage("§aUpdate has been cancelled.");
} catch (DBException e) {
sender.sendMessage("§cDatabase error occurred, cancel could not be performed.");
Log.toLog(this.getClass().getName(), e);
}
}

private void handleUpdate(ISender sender, String[] args) {
sender.sendMessage("§aYou can cancel the update on servers that haven't rebooted yet with /plan update cancel.");
sender.sendMessage("Checking that all servers are online..");
if (!checkNetworkStatus(sender)) {
sender.sendMessage("§cNot all servers were online or accessible, you can still update available servers using -force as a 2nd argument.");
if (args.length <= 1 || !"-force".equals(args[1])) {
return;
}
}
try {
List<Server> servers = Database.getActive().fetch().getServers();
update(sender, servers);
} catch (DBException e) {
Log.toLog(this.getClass().getName(), e);
}
}

private void update(ISender sender, List<Server> servers) {
for (Server server : servers) {
if (update(sender, server)) {
sender.sendMessage("§a" + server.getName() + " scheduled for update.");
} else {
sender.sendMessage("§cUpdate failed on a server, cancelling update on all servers..");
cancel(sender, servers);
sender.sendMessage("§cUpdate cancelled.");
break;
}
}
}

private void cancel(ISender sender, List<Server> servers) {
for (Server server : servers) {
cancel(sender, server);
}

}

private void cancel(ISender sender, Server server) {
try {
InfoSystem.getInstance().getConnectionSystem().sendInfoRequest(new UpdateCancelRequest(), server);
} catch (ForbiddenException | GatewayException | InternalErrorException e) {
sender.sendMessage("§cCancel failed on " + server.getName() + ": Odd Exception: " + e.getClass().getSimpleName());
} catch (UnauthorizedServerException e) {
sender.sendMessage("§cCancel failed on " + server.getName() + ": Unauthorized. " + server.getName() + " might be using different database.");
} catch (ConnectionFailException e) {
sender.sendMessage("§cCancel failed on " + server.getName() + ": " + e.getCause().getClass().getSimpleName() + " " + e.getCause().getMessage());
String address = server.getWebAddress();
boolean local = address.contains("localhost")
|| address.startsWith("https://:") // IP empty = Localhost
|| address.startsWith("http://:") // IP empty = Localhost
|| address.contains("127.0.0.1");
if (!local) {
sender.sendMessage("§cNon-local address, check that port is open");
}
} catch (NotFoundException e) {
/* Ignored, older version */
} catch (WebException e) {
sender.sendMessage("§cCancel failed on " + server.getName() + ": Odd Exception:" + e.getClass().getSimpleName());
}
}

private boolean update(ISender sender, Server server) {
try {
InfoSystem.getInstance().getConnectionSystem().sendInfoRequest(new CheckConnectionRequest(), server);
return true;
} catch (BadRequestException e) {
sender.sendMessage("§c" + server.getName() + " has Allow-Update set to false, aborting update.");
return false;
} catch (ForbiddenException | GatewayException | InternalErrorException e) {
sender.sendMessage("§c" + server.getName() + ": Odd Exception: " + e.getClass().getSimpleName());
return false;
} catch (UnauthorizedServerException e) {
sender.sendMessage("§cFail reason: Unauthorized. " + server.getName() + " might be using different database.");
return false;
} catch (ConnectionFailException e) {
sender.sendMessage("§cFail reason: " + e.getCause().getClass().getSimpleName() + " " + e.getCause().getMessage());
String address = server.getWebAddress();
boolean local = address.contains("localhost")
|| address.startsWith("https://:") // IP empty = Localhost
|| address.startsWith("http://:") // IP empty = Localhost
|| address.contains("127.0.0.1");
if (!local) {
sender.sendMessage("§cNon-local address, check that port is open");
}
return false;
} catch (NotFoundException e) {
sender.sendMessage("§e" + server.getName() + " is using older version and can not be scheduled for update. " +
"You can update it manually, update will proceed.");
return true;
} catch (WebException e) {
sender.sendMessage("§eOdd Exception: " + e.getClass().getSimpleName());
return false;
}
}

private boolean checkNetworkStatus(ISender sender) {
try {
FetchOperations fetch = Database.getActive().fetch();
Optional<Server> bungeeInformation = fetch.getBungeeInformation();
if (!bungeeInformation.isPresent()) {
return true;
}
Map<UUID, Server> bukkitServers = fetch.getBukkitServers();
String accessAddress = WebServerSystem.getInstance().getWebServer().getAccessAddress();
boolean success = true;
for (Server server : bukkitServers.values()) {
if (!ManageConDebugCommand.testServer(sender, accessAddress, server)) {
success = false;
}
}
Server bungee = bungeeInformation.get();
if (!ManageConDebugCommand.testServer(sender, accessAddress, bungee)) {
success = false;
}
return success;
} catch (DBException e) {
sender.sendMessage("§cDatabase error occurred, update has been cancelled.");
Log.toLog(this.getClass().getName(), e);
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@
*/
public class ManageConDebugCommand extends CommandNode {

private final ColorScheme cs;

public ManageConDebugCommand() {
super("con", Permissions.MANAGE.getPermission(), CommandType.ALL);
setShortHelp("Debug Bukkit-Bungee Connections");
cs = PlanPlugin.getInstance().getColorScheme();
}

@Override
Expand Down Expand Up @@ -68,18 +65,19 @@ private void testServers(ISender sender) {
}
}

private void testServer(ISender sender, String accessAddress, Server server) {
public static boolean testServer(ISender sender, String accessAddress, Server server) {
String address = server.getWebAddress().toLowerCase();
boolean usingHttps = address.startsWith("https");
boolean local = address.contains("localhost")
|| address.startsWith("https://:")
|| address.startsWith("http://:")
|| address.startsWith("https://:") // IP empty = Localhost
|| address.startsWith("http://:") // IP empty = Localhost
|| address.contains("127.0.0.1");

try {

InfoSystem.getInstance().getConnectionSystem().sendInfoRequest(new CheckConnectionRequest(accessAddress), server);
sender.sendMessage(getMsgFor(address, usingHttps, local, true, true));
return true;

} catch (ForbiddenException | BadRequestException | InternalErrorException e) {
sender.sendMessage(getMsgFor(address, usingHttps, local, false, false));
Expand All @@ -102,9 +100,11 @@ private void testServer(ISender sender, String accessAddress, Server server) {
sender.sendMessage(getMsgFor(address, usingHttps, local, false, false));
sender.sendMessage("§eOdd Exception: " + e.getClass().getSimpleName());
}
return false;
}

private String getMsgFor(String address, boolean usingHttps, boolean local, boolean successTo, boolean successFrom) {
private static String getMsgFor(String address, boolean usingHttps, boolean local, boolean successTo, boolean successFrom) {
ColorScheme cs = PlanPlugin.getInstance().getColorScheme();
String tCol = cs.getTertiaryColor();
String sCol = cs.getSecondaryColor();
return tCol + address + sCol + ": "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ private Map<String, InfoRequest> loadDataRequests() {
putRequest(requests, SaveDBSettingsRequest.createHandler());
putRequest(requests, SendDBSettingsRequest.createHandler());
putRequest(requests, CheckConnectionRequest.createHandler());

putRequest(requests, UpdateRequest.createHandler());
putRequest(requests, UpdateCancelRequest.createHandler());
return requests;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package com.djrapitops.plan.system.info.request;

import com.djrapitops.plan.system.update.ShutdownUpdateHook;
import com.djrapitops.plan.system.webserver.response.DefaultResponses;
import com.djrapitops.plan.system.webserver.response.Response;

import java.util.Map;

/**
* InfoRequest used for Updating the plugin on a network.
*
* @author Rsl1122
*/
public class UpdateCancelRequest implements InfoRequest {

public UpdateCancelRequest() {
}

public static UpdateCancelRequest createHandler() {
return new UpdateCancelRequest();
}

@Override
public void runLocally() {
ShutdownUpdateHook.deActivate();
}

@Override
public Response handleRequest(Map<String, String> variables) {
ShutdownUpdateHook.deActivate();
return DefaultResponses.SUCCESS.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package com.djrapitops.plan.system.info.request;

import com.djrapitops.plan.system.settings.Settings;
import com.djrapitops.plan.system.update.ShutdownUpdateHook;
import com.djrapitops.plan.system.webserver.response.DefaultResponses;
import com.djrapitops.plan.system.webserver.response.Response;
import com.djrapitops.plan.system.webserver.response.api.BadRequestResponse;

import java.util.Map;

/**
* InfoRequest used for Updating the plugin on a network.
*
* @author Rsl1122
*/
public class UpdateRequest implements InfoRequest {

public UpdateRequest() {
}

public static UpdateRequest createHandler() {
return new UpdateRequest();
}

@Override
public void runLocally() {
new ShutdownUpdateHook().register();
}

@Override
public Response handleRequest(Map<String, String> variables) {
if (Settings.ALLOW_UPDATE.isTrue()) {
new ShutdownUpdateHook().register();
return DefaultResponses.SUCCESS.get();
} else {
return new BadRequestResponse("Update not allowed on this server");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public enum Settings {
DISPLAY_PLAYER_IPS("Customization.Display.PlayerIPs"),
DISPLAY_GAPS_IN_GRAPH_DATA("Customization.Display.GapsInGraphData"),
DATA_GEOLOCATIONS("Data.Geolocations"),
ALLOW_UPDATE("Plugin.Allow-Update-Command"),
NOTIFY_ABOUT_DEV_RELEASES("Plugin.Notify-About-DEV-Releases"),

// Integer
WEBSERVER_PORT("WebServer.Port"),
Expand Down
Loading

0 comments on commit 4e0f1b3

Please sign in to comment.