Skip to content

Commit

Permalink
Minor refactoring and updater execution (still WIN only)
Browse files Browse the repository at this point in the history
  • Loading branch information
jendakol committed Dec 29, 2018
1 parent 567b575 commit 4371a08
Show file tree
Hide file tree
Showing 18 changed files with 382 additions and 131 deletions.
10 changes: 1 addition & 9 deletions .travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,4 @@ function client_test() {
docker-compose down
}

dir=$(pwd)

client_test &&
if $(test "${TRAVIS_REPO_SLUG}" == "jendakol/rbackup-scala-client" && test "${TRAVIS_PULL_REQUEST}" == "false" && test "$TRAVIS_TAG" != ""); then
cd $pwd
sbt +publish
else
exit 0 # skipping publish, it's regular build
fi
client_test
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,17 @@

This is Scala implementation of client for [RBackup](https://github.com/jendakol/rbackup).

Readme TBD :-)
Readme TBD :-)

## Build (release)
```
#!/usr/bin/fish
./.travis.sh
env VERSION=$argv[1] \
SENTRY_DSN="https://[email protected]/1234" \
sbt ";clean;setVersionInSources;setSentryDsnInSources;dist"
```

The SENTRY_DSN is optional.
37 changes: 23 additions & 14 deletions app/AppModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import org.http4s.client.middleware.FollowRedirect
import play.api.{Configuration, Environment}
import scalikejdbc._
import scalikejdbc.config.DBs
import updater._
import updater.{GithubConnector, LinuxServiceUpdaterExecutor, ServiceUpdaterExecutor, WindowsServiceUpdaterExecutor}
import utils.AllowedWsApiOrigins

import scala.collection.JavaConverters._
Expand All @@ -33,15 +33,21 @@ class AppModule(environment: Environment, configuration: Configuration)
with StrictLogging {
private val config = configuration.underlying

if (config.getBoolean("sentry.enabled") && config.getString("sentry.environment") != "dev") {
logger.info("Sentry configured")
val sentry = Sentry.init(config.getString("sentry.dsn"))
sentry.setRelease(App.versionStr)
sentry.setEnvironment(config.getString("sentry.environment"))
sentry.setServerName(config.getString("deviceId"))
sentry.setDist(config.getString("sentry.dist"))
} else {
logger.info("Sentry NOT configured")
App.SentryDsn match {
case Some(dsn) =>
if (config.getBoolean("sentry.enabled") && config.getString("environment") != "dev") {
logger.info("Sentry configured")
val sentry = Sentry.init(dsn)
sentry.setRelease(App.versionStr)
sentry.addTag("app", "client")
sentry.setEnvironment(config.getString("environment"))
sentry.setServerName(config.getString("deviceId"))
sentry.setDist(if (SystemUtils.IS_OS_WINDOWS) "win" else "linux")
} else {
logger.info("Sentry NOT enabled")
}

case None => logger.info("Sentry NOT configured")
}

DBs.setupAll()
Expand All @@ -67,10 +73,13 @@ class AppModule(environment: Environment, configuration: Configuration)

bind[AllowedWsApiOrigins].toInstance(AllowedWsApiOrigins(config.getStringList("allowedWsApiOrigins").asScala))

val deviceId = DeviceId(config.getString("deviceId"))
bind[DeviceId].toInstance(deviceId)

val cloudConnector = CloudConnector.fromConfig(config.getConfig("cloudConnector"), blockingScheduler)
val dao = new Dao(blockingScheduler)
val settings = new Settings(dao)
val stateManager = new StateManager(DeviceId(config.getString("deviceId")), cloudConnector, dao, settings)
val stateManager = new StateManager(deviceId, cloudConnector, dao, settings)

bind[CloudConnector].toInstance(cloudConnector)
bind[Dao].toInstance(dao)
Expand Down Expand Up @@ -116,9 +125,9 @@ class AppModule(environment: Environment, configuration: Configuration)

private def bindServiceUpdater(): Unit = {
val updater = if (SystemUtils.IS_OS_WINDOWS) {
new WindowsServiceUpdater
} else new LinuxServiceUpdater
new WindowsServiceUpdaterExecutor
} else new LinuxServiceUpdaterExecutor

bind[ServiceUpdater].toInstance(updater)
bind[ServiceUpdaterExecutor].toInstance(updater)
}
}
10 changes: 7 additions & 3 deletions app/lib/App.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import monix.eval.Task
import monix.execution.{Cancelable, Scheduler}
import org.http4s.Uri
import play.api.inject.ApplicationLifecycle
import updater.{AppVersion, Updater}
import updater.Updater

import scala.collection.generic.CanBuildFrom
import scala.concurrent.Future
Expand All @@ -38,9 +38,11 @@ class App @Inject()(backupSetsExecutor: BackupSetsExecutor, updater: Updater)(li
}

object App {
final val versionStr: String = "0.1.2"
final val versionStr: String = "0.1.3"
final val version: AppVersion = AppVersion(versionStr).getOrElse(throw new IllegalArgumentException("Could not parse versionStr"))

final val SentryDsn: Option[String] = Some("https://[email protected]/1340234")

type Result[A] = EitherT[Task, AppException, A]

def pureResult[A](a: => A): Result[A] = {
Expand Down Expand Up @@ -205,4 +207,6 @@ object App {

case class ServerSession(rootUri: Uri, sessionId: String, serverVersion: AppVersion)

case class DeviceId(value: String) extends AnyVal
case class DeviceId(value: String) {
override def toString: String = value
}
4 changes: 2 additions & 2 deletions app/updater/AppVersion.scala → app/lib/AppVersion.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package updater
package lib

import lib.AppException.ParsingFailure
import updater.AppVersion._
import lib.AppVersion._

case class AppVersion(major: Int, minor: Int, build: Int, suffix: Option[String] = None) {
def >(other: AppVersion): Boolean = {
Expand Down
3 changes: 1 addition & 2 deletions app/lib/client/clientapi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ import com.typesafe.scalalogging.StrictLogging
import io.circe.generic.extras.Configuration
import io.circe.syntax._
import io.circe.{Decoder, Json}
import lib.App
import lib.{App, AppVersion}
import lib.App._
import lib.App.StringOps
import lib.client.clientapi.FileTreeNode.{Directory, RegularFile}
import lib.server.serverapi
import lib.server.serverapi.{RemoteFile, RemoteFileVersion}
import org.http4s.Uri
import updater.AppVersion

object clientapi extends StrictLogging {

Expand Down
5 changes: 2 additions & 3 deletions app/lib/commands/CommandExecutor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import monix.eval.Task
import monix.execution.Scheduler
import org.http4s.Uri
import utils.CirceImplicits._
import utils.ConfigProperty

@Singleton
class CommandExecutor @Inject()(cloudConnector: CloudConnector,
Expand All @@ -35,7 +34,7 @@ class CommandExecutor @Inject()(cloudConnector: CloudConnector,
fileCommandExecutor: FileCommandExecutor,
settings: Settings,
stateManager: StateManager,
@ConfigProperty("deviceId") deviceId: String)(implicit scheduler: Scheduler)
deviceId: DeviceId)(implicit scheduler: Scheduler)
extends StrictLogging {

wsApiController.setEventCallback(processEvent)
Expand All @@ -51,7 +50,7 @@ class CommandExecutor @Inject()(cloudConnector: CloudConnector,

import scala.concurrent.duration._

tasksManager.start(RunningTask.FileUpload("theName"))(EitherT.right(Task.unit.delayResult(10.seconds))) >>
tasksManager.start(RunningTask.FileUpload(deviceId.value))(EitherT.right(Task.unit.delayResult(10.seconds))) >>
cloudConnector.status
.flatMap { str =>
parse(s"""{"serverResponse": "$str"}""").toResult
Expand Down
10 changes: 6 additions & 4 deletions app/lib/server/CloudConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import io.circe.Json
import io.circe.generic.extras.auto._
import lib.App._
import lib.AppException.ServerNotResponding
import lib._
import lib.{AppVersion, _}
import lib.server.serverapi.ListFilesResponse.FilesList
import lib.server.serverapi._
import monix.eval.Task
Expand All @@ -28,7 +28,6 @@ import org.http4s.multipart._
import org.http4s.{Headers, Method, Request, Response, Status, Uri}
import pureconfig.modules.http4s.uriReader
import pureconfig.{CamelCase, ConfigFieldMapping, ProductHint}
import updater.AppVersion
import utils.CirceImplicits._
import utils.{FileCopier, InputStreamWithSha256, StatsInputStream, StatsOutputStream}

Expand All @@ -50,12 +49,15 @@ class CloudConnector(httpClient: Client[Task], chunkSize: Int, blockingScheduler
}
}

def login(rootUri: Uri, deviceId: String, username: String, password: String): Result[LoginResponse] = {
def login(rootUri: Uri, deviceId: DeviceId, username: String, password: String): Result[LoginResponse] = {
logger.debug(s"Logging device $deviceId with username $username")
App.leaveBreadcrumb("Logging in", Map("uri" -> rootUri, "username" -> username))

val request =
plainRequestToHost(Method.GET, rootUri, "account/login", Map("device_id" -> deviceId, "username" -> username, "password" -> password))
plainRequestToHost(Method.GET,
rootUri,
"account/login",
Map("device_id" -> deviceId.value, "username" -> username, "password" -> password))

status(rootUri).flatMap { status =>
exec(request) {
Expand Down
4 changes: 2 additions & 2 deletions app/updater/GithubConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import cats.syntax.all._
import com.typesafe.scalalogging.StrictLogging
import io.circe.Decoder
import lib.App._
import lib.AppException
import lib.AppException._
import lib.{AppException, AppVersion}
import monix.eval.Task
import monix.execution.Scheduler
import org.http4s.client.Client
import org.http4s.headers.`Content-Length`
import org.http4s.{Response, Status, Uri}
import updater.GithubConnector._
import GithubConnector._

import scala.concurrent.ExecutionContext

Expand Down
39 changes: 0 additions & 39 deletions app/updater/ServiceUpdater.scala

This file was deleted.

56 changes: 56 additions & 0 deletions app/updater/ServiceUpdaterExecutor.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package updater

import better.files.File
import com.typesafe.scalalogging.StrictLogging
import lib.{AppVersion, DeviceId}

import scala.language.postfixOps

sealed trait ServiceUpdaterExecutor {
def executeUpdate(currentVersion: AppVersion, newVersion: AppVersion, env: String, deviceId: DeviceId, dirWithUpdate: File): Unit
}

class WindowsServiceUpdaterExecutor extends ServiceUpdaterExecutor with StrictLogging {
override def executeUpdate(currentVersion: AppVersion,
newVersion: AppVersion,
env: String,
deviceId: DeviceId,
dirWithUpdate: File): Unit = {
logger.info(s"Starting the update with args: $currentVersion, $newVersion, $env, $deviceId, $dirWithUpdate")

Runtime.getRuntime.exec(
Array(
"cmd",
"/C",
"start",
"\"\"",
"java",
"-jar",
"updater.jar",
currentVersion.toString,
newVersion.toString,
env,
deviceId.value,
dirWithUpdate.pathAsString
))
()
}
}

class LinuxServiceUpdaterExecutor extends ServiceUpdaterExecutor {
override def executeUpdate(currentVersion: AppVersion,
newVersion: AppVersion,
env: String,
deviceId: DeviceId,
dirWithUpdate: File): Unit = {
Runtime.getRuntime.exec(
Array(
"/bin/bash",
"-c",
"restart_replace.sh",
dirWithUpdate.pathAsString,
"&",
))
()
}
}
Loading

0 comments on commit 4371a08

Please sign in to comment.