-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
262 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/main/java/de/lars/remotelightweb/backend/utils/UpdateUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package de.lars.remotelightweb.backend.utils; | ||
|
||
import java.io.File; | ||
import java.net.URL; | ||
import java.net.URLDecoder; | ||
import java.nio.file.Paths; | ||
|
||
import org.springframework.boot.system.ApplicationHome; | ||
import org.tinylog.Logger; | ||
|
||
import de.lars.remotelightclient.settings.types.SettingString; | ||
import de.lars.remotelightclient.utils.DirectoryUtil; | ||
import de.lars.remotelightweb.RemoteLightWeb; | ||
import de.lars.updater.sites.GitHubParser; | ||
import de.lars.updater.utils.FileDownloader; | ||
|
||
public class UpdateUtil { | ||
|
||
public final String API_URL = "https://api.github.com/repos/Drumber/RemoteLightWeb/releases"; | ||
private final String UPDATER_NAME = "updater.jar"; | ||
private GitHubParser parser; | ||
|
||
public UpdateUtil(String currentVersion) { | ||
parser = new GitHubParser(currentVersion, API_URL); | ||
} | ||
|
||
public void check() { | ||
try { | ||
parser.check(); | ||
} catch (Exception e) { | ||
Logger.error(e, "Error while checking for updates."); | ||
} | ||
} | ||
|
||
public GitHubParser getParser() { | ||
return parser; | ||
} | ||
|
||
|
||
/** | ||
* Download latest release and replace current jar file | ||
* <!> This method will EXIT the application! | ||
* @param shutdown Should the system be shut down after the update? | ||
* @throws Exception | ||
*/ | ||
public void install(boolean shutdown) throws Exception { | ||
File jarDir = new ApplicationHome(RemoteLightWeb.class).getSource(); | ||
|
||
String rootDir = DirectoryUtil.getRootPath(); | ||
rootDir = Paths.get(".").toAbsolutePath().normalize().toString(); | ||
|
||
//download updater jar | ||
File updaterFile = downloadUpdater(rootDir); | ||
|
||
// execute updater | ||
String args = "-cv " + RemoteLightWeb.VERSION + " -o \"" + jarDir.getAbsolutePath() + | ||
"\" -u " + API_URL + " -w -cmd \""; | ||
|
||
if(shutdown) { | ||
args += "\"shutdown -h now\""; | ||
} else { | ||
String runCmd = ((SettingString) RemoteLightWeb.getInstance().getAPI().getSettingsManager().getSettingFromId("rlweb.runcmd")).getValue(); | ||
if(runCmd == null || runCmd.isEmpty()) { | ||
runCmd = "\"java -jar " + jarDir.getAbsolutePath() + "\""; | ||
} | ||
args += runCmd; | ||
} | ||
args += "\""; | ||
|
||
String command = String.format("java -jar %s %s", updaterFile.getAbsoluteFile(), args); | ||
Logger.info("Run Updater with the following command: " + command); | ||
Runtime.getRuntime().exec(command); | ||
|
||
RemoteLightWeb.exitApplication(); | ||
} | ||
|
||
private File downloadUpdater(String rootDir) throws Exception { | ||
File updaterFile = new File(rootDir + File.separator + UPDATER_NAME); | ||
if(!updaterFile.exists()) { | ||
GitHubParser parser = new GitHubParser("0", "Drumber", "Updater"); | ||
try { | ||
|
||
parser.check(); | ||
Logger.info("Downloading Uploader version " + parser.getNewestVersionTag() +" to " + updaterFile.getAbsolutePath()); | ||
FileDownloader downloader = new FileDownloader(parser.getNewestDownloadUrl(), updaterFile.getAbsolutePath()); | ||
if(downloader.isDownloadSuccessful()) { | ||
return updaterFile; | ||
} | ||
|
||
} catch (Exception e) { | ||
throw new Exception("Could not download Updater.", e); | ||
} | ||
return null; | ||
} | ||
return updaterFile; | ||
} | ||
|
||
} |
108 changes: 108 additions & 0 deletions
108
src/main/java/de/lars/remotelightweb/ui/components/UpdateDialog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package de.lars.remotelightweb.ui.components; | ||
|
||
import org.tinylog.Logger; | ||
|
||
import com.vaadin.flow.component.button.Button; | ||
import com.vaadin.flow.component.dialog.Dialog; | ||
import com.vaadin.flow.component.html.Anchor; | ||
import com.vaadin.flow.component.html.H3; | ||
import com.vaadin.flow.component.html.Label; | ||
import com.vaadin.flow.component.orderedlayout.VerticalLayout; | ||
|
||
import de.lars.remotelightweb.RemoteLightWeb; | ||
import de.lars.remotelightweb.backend.utils.UpdateUtil; | ||
import de.lars.remotelightweb.ui.utils.UIUtils; | ||
import de.lars.updater.sites.GitHubParser; | ||
|
||
public class UpdateDialog extends Dialog { | ||
|
||
public UpdateDialog(GitHubParser parser) { | ||
VerticalLayout l = new VerticalLayout(); | ||
l.setSizeFull(); | ||
add(l); | ||
l.add(new H3("Updater")); | ||
|
||
l.add(new Label(parser.isNewVersionAvailable() ? "New version available!" : "No new version available.")); | ||
l.add(new Label("Installed: " + RemoteLightWeb.VERSION)); | ||
l.add(new Label("Latest: " + parser.getNewestVersionTag())); | ||
l.add(new Anchor(parser.getNewestUrl(), parser.getNewestUrl())); | ||
|
||
if(parser.isNewVersionAvailable()) { | ||
Button btnIgnore = UIUtils.createButton("Ignore", "5px 5px"); | ||
btnIgnore.addClickListener(e -> ignore()); | ||
add(btnIgnore); | ||
|
||
Button btnUpdate = UIUtils.createButton("Update now", "5px 5px"); | ||
btnUpdate.addClickListener(e -> update()); | ||
add(btnUpdate); | ||
} else { | ||
add(new Button("Close", e -> close())); | ||
} | ||
} | ||
|
||
|
||
private void ignore() { | ||
close(); | ||
removeAll(); | ||
VerticalLayout l = new VerticalLayout(); | ||
l.setSizeFull(); | ||
add(l); | ||
|
||
l.add(new Label("The update can also be installed later in the settings.")); | ||
l.add(new Button("Ok", e -> close())); | ||
open(); | ||
} | ||
|
||
|
||
private void update() { | ||
close(); | ||
removeAll(); | ||
VerticalLayout l = new VerticalLayout(); | ||
l.setSizeFull(); | ||
add(l); | ||
|
||
l.add(new H3("Important information")); | ||
l.add(new Label("The program will be closed while the new version is downloaded in the background. " | ||
+ "Please do not power off the system! RemoteLightWeb will automatically restart after the update is complete.")); | ||
l.add(new Label("You can choose between two options:")); | ||
l.add(new Label("1. (Update and restart) RemoteLightWeb will automatically restart after the update is complete.")); | ||
l.add(new Label("2. (Update and shutdown) The system will shut down after the update has been successfully completed.")); | ||
|
||
UpdateUtil updateUtil = RemoteLightWeb.getInstance().getUpdateUtil(); | ||
|
||
add(UIUtils.createButton("Cancel", "5px 5px", e -> close())); | ||
add(UIUtils.createButton("Update and restart", "5px 5px", e -> { | ||
try { | ||
updateUtil.install(false); | ||
} catch (Exception e1) { | ||
Logger.error(e1, "Error while updating"); | ||
error("Error while updating", e1); | ||
} | ||
})); | ||
add(UIUtils.createButton("Update and shutdown", "5px 5px", e -> { | ||
try { | ||
updateUtil.install(true); | ||
} catch (Exception e1) { | ||
Logger.error("Error while updating", e); | ||
error("Error while updating", e1); | ||
} | ||
})); | ||
open(); | ||
} | ||
|
||
|
||
private void error(String message, Exception ex) { | ||
close(); | ||
removeAll(); | ||
VerticalLayout l = new VerticalLayout(); | ||
l.setSizeFull(); | ||
add(l); | ||
|
||
l.add(new H3("An error has occurred")); | ||
l.add(new Label(message)); | ||
l.add(new Label(ex.getMessage())); | ||
l.add(new Button("Close", e -> close())); | ||
open(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters