Skip to content

Commit

Permalink
Add error handling around commands
Browse files Browse the repository at this point in the history
  • Loading branch information
alufers committed Mar 25, 2023
1 parent 9319224 commit 6f51bb0
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 29 deletions.
15 changes: 12 additions & 3 deletions app/app/src/main/java/com/octo4a/repository/BootstrapRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class BootstrapRepositoryImpl(
val symlinks = ArrayList<Pair<String, String>>(50)

val urlPrefix = asset!!.browserDownloadUrl
logger.log(this) { "Downloading bootstrap ${release?.tagName} from ${urlPrefix}" }
logger.log(this) { "Downloading bootstrap ${release?.tagName} from $urlPrefix" }

val sslcontext = SSLContext.getInstance("TLSv1")
sslcontext.init(null, null, null)
Expand Down Expand Up @@ -267,9 +267,18 @@ class BootstrapRepositoryImpl(

override fun resetSSHPassword(newPassword: String) {
logger.log(this) { "Deleting password just in case" }
runCommand("passwd -d octoprint").waitAndPrintOutput(logger)
try {
runCommand("passwd -d octoprint").waitAndPrintOutput(logger)
} catch (e: java.lang.Exception) {
logger.log(this) { "Failed to delete password: $e" }
}

runCommand("passwd octoprint").setPassword(newPassword)
runCommand("passwd -d root").waitAndPrintOutput(logger)
try {
runCommand("passwd -d root").waitAndPrintOutput(logger)
} catch (e: java.lang.Exception) {
logger.log(this) { "Failed to delete password: $e" }
}
runCommand("passwd root").setPassword(newPassword)
runCommand("touch .ssh_configured", root = false)
runCommand("touch .ssh_configured", root = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ enum class ExtrasStatus {
data class UsbDeviceStatus(val isAttached: Boolean, val port: String = "")

fun ServerStatus.getInstallationProgress(): Int {
return max(((value.toDouble() / 4) * 100).roundToInt(), 0)
return max(((value.toDouble() / 4) * 100).roundToInt(), 0)
}

fun ServerStatus.isInstallationFinished(): Boolean {
Expand Down Expand Up @@ -152,7 +152,7 @@ class OctoPrintHandlerRepositoryImpl(
runCommand("ls -lah").waitAndPrintOutput(logger)
try {
runCommand("7z x -y octoprint.zip").waitAndPrintOutput(logger)
} catch(e: java.lang.Exception) {
} catch (e: java.lang.Exception) {
logger.log { "7zip extraction failed: $e" }
logger.log { "Trying to use unzip" }
runCommand("unzip octoprint.zip").waitAndPrintOutput(logger)
Expand Down Expand Up @@ -187,7 +187,7 @@ class OctoPrintHandlerRepositoryImpl(
val pw = PrintWriter(sw)
e.printStackTrace(pw)

logger.log{ "An exception has occurred at:\n$sw\nException:\n${e.toString()}"}
logger.log { "An exception has occurred at:\n$sw\nException:\n${e.toString()}" }
}
}

Expand All @@ -205,10 +205,15 @@ class OctoPrintHandlerRepositoryImpl(
if (_extrasStatus.value == ExtrasStatus.NotInstalled) {
Thread {
_extrasStatus.value = ExtrasStatus.Installing
bootstrapRepository.runCommand("curl -s https://raw.githubusercontent.com/feelfreelinux/octo4a/master/scripts/setup-plugin-extras.sh | bash -s")
.waitAndPrintOutput(
logger
)
try {
bootstrapRepository.runCommand("curl -s https://raw.githubusercontent.com/feelfreelinux/octo4a/master/scripts/setup-plugin-extras.sh | bash -s")
.waitAndPrintOutput(
logger
)
} catch (e: java.lang.Exception) {
logger.log { "Failed to install plugin extras: $e" }
}

_extrasStatus.value = ExtrasStatus.Installed
}.start()
}
Expand All @@ -234,11 +239,16 @@ class OctoPrintHandlerRepositoryImpl(

fun ensureCommFixExists() {
if (!commPyFixFile.exists()) {
// OctoPrint 1.8.0 breaks android compatibility, force install a plugin that monkey-fixes comm.py
bootstrapRepository.runCommand("mkdir -p ~/.octoprint/plugins; curl -o ~/.octoprint/plugins/comm-fix.py -L https://raw.githubusercontent.com/feelfreelinux/octo4a/master/scripts/comm-fix.py")
.waitAndPrintOutput(
logger
)
try {
// OctoPrint 1.8.0 breaks android compatibility, force install a plugin that monkey-fixes comm.py
bootstrapRepository.runCommand("mkdir -p ~/.octoprint/plugins; curl -o ~/.octoprint/plugins/comm-fix.py -L https://raw.githubusercontent.com/feelfreelinux/octo4a/master/scripts/comm-fix.py")
.waitAndPrintOutput(
logger
)
} catch (e: java.lang.Exception) {
logger.log { "Failed to apply comm fix: $e" }
}

}
}

Expand All @@ -257,17 +267,23 @@ class OctoPrintHandlerRepositoryImpl(
}
if (!bootstrapRepository.isArgonFixApplied) {
logger.log { "Applying argon fix..." }
bootstrapRepository.runCommand("pip3 install -U packaging --ignore-installed")
.waitAndPrintOutput(
logger
)
bootstrapRepository.runCommand("pip3 install https://github.com/feelfreelinux/octo4a-argon2-mock/archive/main.zip")
.waitAndPrintOutput(
logger
)
bootstrapRepository.runCommand("touch /home/octoprint/.argon-fix").waitAndPrintOutput(
logger
)
try {
bootstrapRepository.runCommand("pip3 install -U packaging --ignore-installed")
.waitAndPrintOutput(
logger
)
bootstrapRepository.runCommand("pip3 install https://github.com/feelfreelinux/octo4a-argon2-mock/archive/main.zip")
.waitAndPrintOutput(
logger
)
bootstrapRepository.runCommand("touch /home/octoprint/.argon-fix")
.waitAndPrintOutput(
logger
)
} catch (e: java.lang.Exception) {
logger.log { "Failed to apply argon fix: $e" }
}

}
ensureCommFixExists()
_serverState.value = ServerStatus.BootingUp
Expand Down Expand Up @@ -331,8 +347,13 @@ class OctoPrintHandlerRepositoryImpl(

override fun stopSSH() {
// Kills ssh demon
bootstrapRepository.runCommand("pkill sshd").waitAndPrintOutput(logger)
logger.log(this) { "killed sshd" }
try {
bootstrapRepository.runCommand("pkill sshd").waitAndPrintOutput(logger)
logger.log(this) { "killed sshd" }
} catch (e: java.lang.Exception) {
logger.log(this) { "Killing sshd failed: $e" }
}

}

override fun usbAttached(port: String) {
Expand Down
12 changes: 11 additions & 1 deletion app/app/src/main/java/com/octo4a/utils/ProcessUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,20 @@ fun Process.waitAndPrintOutput(logger: LoggerRepository, type: LogType = LogType

val exitCode = waitFor()
if (exitCode != 0) {
val uselessWarnings = arrayListOf(
"proot warning: can't sanitize binding \"/data/data/com.octo4a/files/serialpipe\": No such file or directory",
"WARNING: linker: ./root/bin/proot: unused DT entry:"
)
val logLines = outputStr
// replace useless proot warning
.replace("proot warning: can't sanitize binding \"/data/data/com.octo4a/files/serialpipe\": No such file or directory", "")
.let {
uselessWarnings.runningFold(it) { curr, valueToReplace ->
curr.replace(valueToReplace, "")
}
}
.toString()
.lines()
.filter { it.trim() != "" }
.takeLast(2)
.joinToString("\n") { it.take(50) }
throw RuntimeException("Process exited with error code ${exitCode}. $logLines")
Expand Down

0 comments on commit 6f51bb0

Please sign in to comment.