From 3557098f101745cd6be32527f1c628e9d6d9959d Mon Sep 17 00:00:00 2001 From: Zach Smith Date: Thu, 30 Aug 2018 16:04:40 -0700 Subject: Add AliCloud Platform support --- .../scala/xyz/driver/core/init/CloudServices.scala | 14 +++++-- .../main/scala/xyz/driver/core/init/Platform.scala | 44 ++++++++++++++++++---- 2 files changed, 48 insertions(+), 10 deletions(-) (limited to 'core-init/src') diff --git a/core-init/src/main/scala/xyz/driver/core/init/CloudServices.scala b/core-init/src/main/scala/xyz/driver/core/init/CloudServices.scala index 857dd4c..8419f90 100644 --- a/core-init/src/main/scala/xyz/driver/core/init/CloudServices.scala +++ b/core-init/src/main/scala/xyz/driver/core/init/CloudServices.scala @@ -3,10 +3,10 @@ package init import java.nio.file.Paths -import xyz.driver.core.messaging.{CreateOnDemand, GoogleBus, QueueBus, StreamBus} +import xyz.driver.core.messaging.{AliyunBus, CreateOnDemand, GoogleBus, QueueBus, StreamBus} import xyz.driver.core.reporting._ import xyz.driver.core.rest.DnsDiscovery -import xyz.driver.core.storage.{BlobStorage, FileSystemBlobStorage, GcsBlobStorage} +import xyz.driver.core.storage.{AliyunBlobStorage, BlobStorage, FileSystemBlobStorage, GcsBlobStorage} import scala.collection.JavaConverters._ @@ -52,6 +52,10 @@ trait CloudServices extends AkkaBootable { self => new GoogleReporter(p.credentials, p.namespace) with ScalaLoggingCompat with GoogleMdcLogger { val logger = ScalaLoggingCompat.defaultScalaLogger(true) } + case _: Platform.AliCloud => + new NoTraceReporter with ScalaLoggingCompat { + val logger = ScalaLoggingCompat.defaultScalaLogger(true) + } case Platform.Dev => new NoTraceReporter with ScalaLoggingCompat { val logger = ScalaLoggingCompat.defaultScalaLogger(false) @@ -64,7 +68,7 @@ trait CloudServices extends AkkaBootable { self => /** Object storage. * * When running on a cloud platform, prepends `$project-` to bucket names, where `$project` - * is the project ID (for example 'driverinc-production` or `driverinc-sandbox`). + * is the project ID (for example `driverinc-production` or `driverinc-sandbox`). * * @group utilities */ @@ -72,6 +76,8 @@ trait CloudServices extends AkkaBootable { self => platform match { case p @ Platform.GoogleCloud(keyfile, _) => GcsBlobStorage.fromKeyfile(keyfile, s"${p.project}-$bucketName") + case Platform.AliCloud(_, accessId, accessKey, region, _) => + AliyunBlobStorage(accessId, accessKey, region, bucketName, java.time.Clock.systemDefaultZone()) case Platform.Dev => new FileSystemBlobStorage(Paths.get(s".data-$bucketName")) } @@ -82,6 +88,8 @@ trait CloudServices extends AkkaBootable { self => def messageBus: StreamBus = platform match { case p @ Platform.GoogleCloud(_, namespace) => new GoogleBus(p.credentials, namespace) with StreamBus with CreateOnDemand + case Platform.AliCloud(accountId, accessId, accessKey, region, namespace) => + new AliyunBus(accountId, accessId, accessKey, region, namespace) with StreamBus with CreateOnDemand case Platform.Dev => new QueueBus()(self.system) with StreamBus } diff --git a/core-init/src/main/scala/xyz/driver/core/init/Platform.scala b/core-init/src/main/scala/xyz/driver/core/init/Platform.scala index 2daa2c8..dced0e9 100644 --- a/core-init/src/main/scala/xyz/driver/core/init/Platform.scala +++ b/core-init/src/main/scala/xyz/driver/core/init/Platform.scala @@ -13,17 +13,47 @@ object Platform { ) def project: String = credentials.getProjectId } - // case object AliCloud extends Platform - case object Dev extends Platform - lazy val fromEnv: Platform = { - def isGoogle = sys.env.get("GOOGLE_APPLICATION_CREDENTIALS").map { value => - val keyfile = Paths.get(value) - require(Files.isReadable(keyfile), s"Google credentials file $value is not readable.") + object GoogleCloud { + lazy val fromEnv: GoogleCloud = { + val credentialsFile = + sys.env.getOrElse( + "GOOGLE_APPLICATION_CREDENTIALS", + sys.error("Expected GOOGLE_APPLICATION_CREDENTIALS file to be set in gcp environment")) + + val keyfile = Paths.get(credentialsFile) + require(Files.isReadable(keyfile), s"Google credentials file $credentialsFile is not readable.") + val namespace = sys.env.getOrElse("SERVICE_NAMESPACE", sys.error("Namespace not set")) GoogleCloud(keyfile, namespace) } - isGoogle.getOrElse(Dev) + } + + case class AliCloud(accountId: String, accessId: String, accessKey: String, region: String, namespace: String) + extends Platform + + object AliCloud { + lazy val fromEnv: AliCloud = { + AliCloud( + accountId = sys.env("ALICLOUD_ACCOUNT_ID"), + accessId = sys.env("ALICLOUD_ACCESS_ID"), + accessKey = sys.env("ALICLOUD_ACCESS_KEY"), + region = sys.env("CLOUD_REGION"), + namespace = sys.env("SERVICE_NAMESPACE") + ) + } + } + + case object Dev extends Platform + + lazy val fromEnv: Platform = { + sys.env.get("CLOUD_PROVIDER") match { + case Some("alicloud") => AliCloud.fromEnv + case Some("gcp") => GoogleCloud.fromEnv + + // For backwards compat, try instantiating GCP first, falling back to Dev + case _ => util.Try(GoogleCloud.fromEnv).getOrElse(Dev) + } } def current: Platform = fromEnv -- cgit v1.2.3