util

Clone or download

Fully working start on boot for linux

Modified Files

package com.StreamPi.Util.StartAtBoot;
package com.StreamPi.Util.StartAtBoot;
import com.StreamPi.Util.Exception.MinorException;
import com.StreamPi.Util.Exception.MinorException;
import com.StreamPi.Util.Platform.Platform;
import com.StreamPi.Util.Platform.Platform;
import java.io.BufferedWriter;
import java.io.BufferedWriter;
import java.io.File;
import java.io.File;
import java.io.FileWriter;
import java.io.FileWriter;
public class StartAtBoot {
public class StartAtBoot {
SoftwareType softwareType;
SoftwareType softwareType;
Platform platform;
Platform platform;
public StartAtBoot(SoftwareType softwareType, Platform platform)
public StartAtBoot(SoftwareType softwareType, Platform platform)
{
{
this.softwareType = softwareType;
this.softwareType = softwareType;
this.platform = platform;
this.platform = platform;
}
}
public void create(File runnerFile) throws MinorException
public void create(File runnerFile) throws MinorException
{
{
if(platform == Platform.WINDOWS)
if(platform == Platform.WINDOWS)
createStarterForWindows(runnerFile);
createStarterForWindows(runnerFile);
else if(platform == Platform.LINUX || platform == Platform.LINUX_RPI)
else if(platform == Platform.LINUX || platform == Platform.LINUX_RPI)
createStarterForLinux(runnerFile);
createStarterForLinux(runnerFile);
else if(platform == Platform.MAC)
else if(platform == Platform.MAC)
createStarterForMac(runnerFile);
createStarterForMac(runnerFile);
else if(platform == Platform.UNKNOWN)
else if(platform == Platform.UNKNOWN)
unknownPlatformException();
unknownPlatformException();
}
}
public boolean delete() throws MinorException {
public boolean delete() throws MinorException {
if(platform == Platform.WINDOWS)
if(platform == Platform.WINDOWS)
return deleteStarterForWindows();
return deleteStarterForWindows();
else if (platform == Platform.LINUX || platform == Platform.LINUX_RPI)
else if (platform == Platform.LINUX || platform == Platform.LINUX_RPI)
return deleteStarterForLinux();
return deleteStarterForLinux();
else if(platform == Platform.MAC)
else if(platform == Platform.MAC)
deleteStarterForMac();
deleteStarterForMac();
else if(platform == Platform.UNKNOWN)
else if(platform == Platform.UNKNOWN)
unknownPlatformException();
unknownPlatformException();
return false;
return false;
}
}
private void createStarterForLinux(File runnerFile) throws MinorException
private void createStarterForLinux(File runnerFile) throws MinorException
{
{
File initFile = new File("/etc/init.d/streampi_starter_"+ softwareType);
try
try
{
{
FileWriter fw = new FileWriter(initFile);
String sysDDirectoryPath = System.getProperty("user.home")+"/.local/share/systemd/user/";
File sysDDirectoryFile = new File(sysDDirectoryPath);
if(!sysDDirectoryFile.exists())
sysDDirectoryFile.mkdirs();
File sysDServiceFile = new File(sysDDirectoryPath+"stream-pi-"+ softwareType+".service");
FileWriter fw = new FileWriter(sysDServiceFile);
BufferedWriter bw = new BufferedWriter(fw);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("#! /bin/sh\n" +
bw.write("[Unit]\n" +
"cd "+runnerFile.getAbsoluteFile().getParent()+"\n" +
"Description=Stream-Pi "+softwareType+"\n" +
"sudo ./"+runnerFile.getName()+"\n" +
"[Service]\n" +
"exit 0\n");
"Type=oneshot\n" +
"WorkingDirectory="+runnerFile.getAbsoluteFile().getParent()+"\n" +
"ExecStart="+runnerFile.getName()+"\n" +
"[Install]\n" +
"WantedBy=default.target");
bw.close();
bw.close();
Runtime.getRuntime().exec("systemctl --user daemon-reload");
Runtime.getRuntime().exec("systemctl --user enable stream-pi-"+softwareType+".service");
}
}
catch (Exception e)
catch (Exception e)
{
{
e.printStackTrace();
e.printStackTrace();
throw new MinorException(e.getMessage()+"\nTry running as root and try again");
throw new MinorException("Unable to set start at boot",e.getMessage());
}
}
}
}
private boolean deleteStarterForLinux()
private boolean deleteStarterForLinux() throws MinorException
{
{
return new File("/etc/init.d/streampi_starter_"+ softwareType).delete();
try
{
boolean f1 = new File(System.getProperty("user.home")+"/.local/share/systemd/user/stream-pi-"+
softwareType+".service").delete();
Runtime.getRuntime().exec("systemctl --user daemon-reload");
return f1;
}
catch (Exception e)
{
e.printStackTrace();
throw new MinorException("Unable to unset start at boot",e.getMessage());
}
}
}
private void createStarterForWindows(File runnerFile) throws MinorException
private void createStarterForWindows(File runnerFile) throws MinorException
{
{
File initFile = new File(System.getenv("APPDATA")+"/Microsoft/Windows/Start Menu/Programs/Startup/streampi_starter_"+ softwareType +".bat");
File initFile = new File(System.getenv("APPDATA")+"/Microsoft/Windows/Start Menu/Programs/Startup/streampi_starter_"+ softwareType +".bat");
try
try
{
{
FileWriter fw = new FileWriter(initFile);
FileWriter fw = new FileWriter(initFile);
BufferedWriter bw = new BufferedWriter(fw);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("cd "+runnerFile.getAbsoluteFile().getParent()+"\n" +
bw.write("cd "+runnerFile.getAbsoluteFile().getParent()+"\n" +
runnerFile.getName());
runnerFile.getName());
bw.close();
bw.close();
}
}
catch (Exception e)
catch (Exception e)
{
{
throw new MinorException(e.getMessage());
throw new MinorException(e.getMessage());
}
}
}
}
private boolean deleteStarterForWindows()
private boolean deleteStarterForWindows()
{
{
return new File(System.getenv("APPDATA")+"/Microsoft/Windows/Start Menu/Programs/Startup/streampi_starter_"+ softwareType +".bat").delete();
return new File(System.getenv("APPDATA")+"/Microsoft/Windows/Start Menu/Programs/Startup/streampi_starter_"+ softwareType +".bat").delete();
}
}
private void createStarterForMac(File runnerFile) throws MinorException
private void createStarterForMac(File runnerFile) throws MinorException
{
{
throw new MinorException("Mac Starter feature is not implemented yet.");
throw new MinorException("Mac Starter feature is not implemented yet.");
}
}
private void deleteStarterForMac() throws MinorException
private void deleteStarterForMac() throws MinorException
{
{
throw new MinorException("Mac Starter feature is not implemented yet.");
throw new MinorException("Mac Starter feature is not implemented yet.");
}
}
private void unknownPlatformException() throws MinorException
private void unknownPlatformException() throws MinorException
{
{
throw new MinorException("Cannot implemented starter feature. Unknown platform.");
throw new MinorException("Cannot implemented starter feature. Unknown platform.");
}
}
}
}