1
+ package com .djrapitops .plan .command .commands ;
2
+
3
+ import com .djrapitops .plan .api .exceptions .connection .*;
4
+ import com .djrapitops .plan .api .exceptions .database .DBException ;
5
+ import com .djrapitops .plan .command .commands .manage .ManageConDebugCommand ;
6
+ import com .djrapitops .plan .system .database .databases .Database ;
7
+ import com .djrapitops .plan .system .database .databases .operation .FetchOperations ;
8
+ import com .djrapitops .plan .system .info .InfoSystem ;
9
+ import com .djrapitops .plan .system .info .request .CheckConnectionRequest ;
10
+ import com .djrapitops .plan .system .info .request .UpdateCancelRequest ;
11
+ import com .djrapitops .plan .system .info .server .Server ;
12
+ import com .djrapitops .plan .system .settings .Permissions ;
13
+ import com .djrapitops .plan .system .update .VersionCheckSystem ;
14
+ import com .djrapitops .plan .system .update .VersionInfo ;
15
+ import com .djrapitops .plan .system .webserver .WebServerSystem ;
16
+ import com .djrapitops .plugin .api .utility .log .Log ;
17
+ import com .djrapitops .plugin .command .CommandNode ;
18
+ import com .djrapitops .plugin .command .CommandType ;
19
+ import com .djrapitops .plugin .command .ISender ;
20
+
21
+ import java .util .List ;
22
+ import java .util .Map ;
23
+ import java .util .Optional ;
24
+ import java .util .UUID ;
25
+
26
+ /**
27
+ * Command that updates all servers in the network
28
+ *
29
+ * @author Rsl1122
30
+ */
31
+ public class UpdateCommand extends CommandNode {
32
+
33
+ public UpdateCommand () {
34
+ super ("update" , Permissions .MANAGE .getPermission (), CommandType .ALL );
35
+ setArguments ("[-update]/[cancel]" );
36
+ setShortHelp ("Get change log link or update plugin." );
37
+ setInDepthHelp (
38
+ "/plan update" ,
39
+ " Used to update the plugin on the next shutdown\n " ,
40
+ " /plan update - get change log link" ,
41
+ " /plan update -update - Schedule update to happen on all network servers that are online next time they reboot." ,
42
+ " /plan update cancel - Cancel scheduled update on servers that haven't rebooted yet."
43
+ );
44
+ }
45
+
46
+ @ Override
47
+ public void onCommand (ISender sender , String commandLabel , String [] args ) {
48
+ if (!VersionCheckSystem .isNewVersionAvailable ()) {
49
+ sender .sendMessage ("§aYou're running the latest version of Plan." );
50
+ return ;
51
+ }
52
+
53
+ VersionInfo available = VersionCheckSystem .getInstance ().getNewVersionAvailable ();
54
+ String downloadUrl = available .getDownloadUrl ();
55
+
56
+ if (!available .isTrusted ()) {
57
+ sender .sendMessage ("§cVersion download url did not start with " +
58
+ "https://github.com/Rsl1122/Plan-PlayerAnalytics/releases/ " +
59
+ "and might not be trusted. You can download this version manually here (Direct download):" );
60
+ sender .sendLink (downloadUrl , downloadUrl );
61
+ return ;
62
+ }
63
+
64
+ if (args .length == 0 ) {
65
+ sender .sendLink ("Change Log v" + available .getVersion ().toString () + ": " , "Click me" , available .getChangeLogUrl ());
66
+ return ;
67
+ }
68
+
69
+ String firstArgument = args [0 ];
70
+ if ("-update" .equals (firstArgument )) {
71
+ handleUpdate (sender , args );
72
+ } else if ("cancel" .equals (firstArgument )) {
73
+ cancel (sender );
74
+ } else {
75
+ throw new IllegalArgumentException ("Unknown argument, use '-update' or 'cancel'" );
76
+ }
77
+ }
78
+
79
+ private void cancel (ISender sender ) {
80
+ try {
81
+ cancel (sender , Database .getActive ().fetch ().getServers ());
82
+ sender .sendMessage ("§aUpdate has been cancelled." );
83
+ } catch (DBException e ) {
84
+ sender .sendMessage ("§cDatabase error occurred, cancel could not be performed." );
85
+ Log .toLog (this .getClass ().getName (), e );
86
+ }
87
+ }
88
+
89
+ private void handleUpdate (ISender sender , String [] args ) {
90
+ sender .sendMessage ("§aYou can cancel the update on servers that haven't rebooted yet with /plan update cancel." );
91
+ sender .sendMessage ("Checking that all servers are online.." );
92
+ if (!checkNetworkStatus (sender )) {
93
+ sender .sendMessage ("§cNot all servers were online or accessible, you can still update available servers using -force as a 2nd argument." );
94
+ if (args .length <= 1 || !"-force" .equals (args [1 ])) {
95
+ return ;
96
+ }
97
+ }
98
+ try {
99
+ List <Server > servers = Database .getActive ().fetch ().getServers ();
100
+ update (sender , servers );
101
+ } catch (DBException e ) {
102
+ Log .toLog (this .getClass ().getName (), e );
103
+ }
104
+ }
105
+
106
+ private void update (ISender sender , List <Server > servers ) {
107
+ for (Server server : servers ) {
108
+ if (update (sender , server )) {
109
+ sender .sendMessage ("§a" + server .getName () + " scheduled for update." );
110
+ } else {
111
+ sender .sendMessage ("§cUpdate failed on a server, cancelling update on all servers.." );
112
+ cancel (sender , servers );
113
+ sender .sendMessage ("§cUpdate cancelled." );
114
+ break ;
115
+ }
116
+ }
117
+ }
118
+
119
+ private void cancel (ISender sender , List <Server > servers ) {
120
+ for (Server server : servers ) {
121
+ cancel (sender , server );
122
+ }
123
+
124
+ }
125
+
126
+ private void cancel (ISender sender , Server server ) {
127
+ try {
128
+ InfoSystem .getInstance ().getConnectionSystem ().sendInfoRequest (new UpdateCancelRequest (), server );
129
+ } catch (ForbiddenException | GatewayException | InternalErrorException e ) {
130
+ sender .sendMessage ("§cCancel failed on " + server .getName () + ": Odd Exception: " + e .getClass ().getSimpleName ());
131
+ } catch (UnauthorizedServerException e ) {
132
+ sender .sendMessage ("§cCancel failed on " + server .getName () + ": Unauthorized. " + server .getName () + " might be using different database." );
133
+ } catch (ConnectionFailException e ) {
134
+ sender .sendMessage ("§cCancel failed on " + server .getName () + ": " + e .getCause ().getClass ().getSimpleName () + " " + e .getCause ().getMessage ());
135
+ String address = server .getWebAddress ();
136
+ boolean local = address .contains ("localhost" )
137
+ || address .startsWith ("https://:" ) // IP empty = Localhost
138
+ || address .startsWith ("http://:" ) // IP empty = Localhost
139
+ || address .contains ("127.0.0.1" );
140
+ if (!local ) {
141
+ sender .sendMessage ("§cNon-local address, check that port is open" );
142
+ }
143
+ } catch (NotFoundException e ) {
144
+ /* Ignored, older version */
145
+ } catch (WebException e ) {
146
+ sender .sendMessage ("§cCancel failed on " + server .getName () + ": Odd Exception:" + e .getClass ().getSimpleName ());
147
+ }
148
+ }
149
+
150
+ private boolean update (ISender sender , Server server ) {
151
+ try {
152
+ InfoSystem .getInstance ().getConnectionSystem ().sendInfoRequest (new CheckConnectionRequest (), server );
153
+ return true ;
154
+ } catch (BadRequestException e ) {
155
+ sender .sendMessage ("§c" + server .getName () + " has Allow-Update set to false, aborting update." );
156
+ return false ;
157
+ } catch (ForbiddenException | GatewayException | InternalErrorException e ) {
158
+ sender .sendMessage ("§c" + server .getName () + ": Odd Exception: " + e .getClass ().getSimpleName ());
159
+ return false ;
160
+ } catch (UnauthorizedServerException e ) {
161
+ sender .sendMessage ("§cFail reason: Unauthorized. " + server .getName () + " might be using different database." );
162
+ return false ;
163
+ } catch (ConnectionFailException e ) {
164
+ sender .sendMessage ("§cFail reason: " + e .getCause ().getClass ().getSimpleName () + " " + e .getCause ().getMessage ());
165
+ String address = server .getWebAddress ();
166
+ boolean local = address .contains ("localhost" )
167
+ || address .startsWith ("https://:" ) // IP empty = Localhost
168
+ || address .startsWith ("http://:" ) // IP empty = Localhost
169
+ || address .contains ("127.0.0.1" );
170
+ if (!local ) {
171
+ sender .sendMessage ("§cNon-local address, check that port is open" );
172
+ }
173
+ return false ;
174
+ } catch (NotFoundException e ) {
175
+ sender .sendMessage ("§e" + server .getName () + " is using older version and can not be scheduled for update. " +
176
+ "You can update it manually, update will proceed." );
177
+ return true ;
178
+ } catch (WebException e ) {
179
+ sender .sendMessage ("§eOdd Exception: " + e .getClass ().getSimpleName ());
180
+ return false ;
181
+ }
182
+ }
183
+
184
+ private boolean checkNetworkStatus (ISender sender ) {
185
+ try {
186
+ FetchOperations fetch = Database .getActive ().fetch ();
187
+ Optional <Server > bungeeInformation = fetch .getBungeeInformation ();
188
+ if (!bungeeInformation .isPresent ()) {
189
+ return true ;
190
+ }
191
+ Map <UUID , Server > bukkitServers = fetch .getBukkitServers ();
192
+ String accessAddress = WebServerSystem .getInstance ().getWebServer ().getAccessAddress ();
193
+ boolean success = true ;
194
+ for (Server server : bukkitServers .values ()) {
195
+ if (!ManageConDebugCommand .testServer (sender , accessAddress , server )) {
196
+ success = false ;
197
+ }
198
+ }
199
+ Server bungee = bungeeInformation .get ();
200
+ if (!ManageConDebugCommand .testServer (sender , accessAddress , bungee )) {
201
+ success = false ;
202
+ }
203
+ return success ;
204
+ } catch (DBException e ) {
205
+ sender .sendMessage ("§cDatabase error occurred, update has been cancelled." );
206
+ Log .toLog (this .getClass ().getName (), e );
207
+ return false ;
208
+ }
209
+ }
210
+ }
0 commit comments