Skip to content

Commit ebb9fe3

Browse files
committed
Log to stdout and fixes for findify#83 and findify#80
1 parent 8f6e43f commit ebb9fe3

File tree

3 files changed

+84
-9
lines changed

3 files changed

+84
-9
lines changed

build.sbt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,27 @@ libraryDependencies ++= Seq(
2020
"com.typesafe.akka" %% "akka-stream-testkit" % akkaVersion % "test",
2121
"org.scala-lang.modules" %% "scala-xml" % "1.0.6",
2222
"com.github.pathikrit" %% "better-files" % "2.17.1",
23-
"com.typesafe.scala-logging" %% "scala-logging" % "3.7.2",
23+
"com.typesafe.scala-logging" %% "scala-logging" % "3.8.0",
24+
"org.slf4j" % "slf4j-simple" % "1.8.0-beta1", // To send logging to standard out, so it can appear in the container's log
2425
"com.amazonaws" % "aws-java-sdk-s3" % "1.11.224",
2526
"org.scalatest" %% "scalatest" % "3.0.4" % "test",
2627
"ch.qos.logback" % "logback-classic" % "1.2.3" % "test",
27-
"org.iq80.leveldb" % "leveldb" % "0.9",
28+
"org.iq80.leveldb" % "leveldb" % "0.10", // See https://github.com/findify/s3mock/issues/83
2829
"com.lightbend.akka" %% "akka-stream-alpakka-s3" % "0.14" % "test"
2930
)
3031

3132
parallelExecution in Test := false
3233

3334
publishMavenStyle := true
3435

36+
// Fixes duplicate error on building docker image, caused by adding the slf4j-simple dependency above
37+
assemblyMergeStrategy in assembly := {
38+
case "module-info.class" => MergeStrategy.discard
39+
case x =>
40+
val oldStrategy = (assemblyMergeStrategy in assembly).value
41+
oldStrategy(x)
42+
}
43+
3544
publishTo := {
3645
val nexus = "https://oss.sonatype.org/"
3746
if (isSnapshot.value)
@@ -59,14 +68,14 @@ mainClass in assembly := Some("io.findify.s3mock.Main")
5968
test in assembly := {}
6069

6170
dockerfile in docker := new Dockerfile {
62-
from("openjdk:9.0.1-11-jre-slim")
71+
from("openjdk:8-alpine") // see https://github.com/findify/s3mock/issues/83
6372
expose(8001)
6473
add(assembly.value, "/app/s3mock.jar")
65-
entryPoint("java", "-Xmx128m", "-jar", "/app/s3mock.jar")
74+
entryPoint("sh", "-c", "java -Xmx128m $JAVA_ARGS -jar /app/s3mock.jar")
6675
}
6776
imageNames in docker := Seq(
68-
ImageName(s"findify/s3mock:${version.value.replaceAll("\\+", "_")}"),
69-
ImageName(s"findify/s3mock:latest")
77+
ImageName(s"uxforms/s3mock:${version.value.replaceAll("\\+", "_")}"),
78+
ImageName(s"uxforms/s3mock:latest")
7079
)
7180

7281
/*enablePlugins(JavaAppPackaging)
Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,74 @@
11
package io.findify.s3mock
22

3+
import java.util
4+
35
import better.files.File
6+
import com.amazonaws.auth.{AWSStaticCredentialsProvider, AnonymousAWSCredentials}
7+
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration
8+
import com.amazonaws.services.s3.{AmazonS3, AmazonS3ClientBuilder}
9+
import com.typesafe.config.{Config, ConfigFactory}
410
import io.findify.s3mock.provider.FileProvider
511

612
/**
713
* Created by shutty on 8/9/16.
814
*/
915
object Main {
16+
17+
private val options = ConfigOptions(ConfigFactory.load().getConfig("s3mock"))
18+
1019
def main(args: Array[String]): Unit = {
11-
val server = new S3Mock(8001, new FileProvider(File.newTemporaryDirectory(prefix = "s3mock").pathAsString))
20+
21+
println(s"JAVA_ARGS: ${System.getenv("JAVA_ARGS")}")
22+
println(s"Options: $options")
23+
24+
val server = new S3Mock(options.port, new FileProvider(options.dir))
25+
1226
server.start
27+
28+
createBuckets(buildClient(options.host, options.port, options.region), options.initBuckets)
29+
}
30+
31+
private def createBuckets(client: AmazonS3, buckets: Set[String]): Unit = {
32+
try {
33+
buckets.foreach(b => {
34+
client.createBucket(b)
35+
println(s"Created bucket: $b")
36+
})
37+
} finally {
38+
client.shutdown()
39+
}
40+
}
41+
42+
private def buildClient(host: String, port: Int, region: String): AmazonS3 = {
43+
val endpoint = new EndpointConfiguration(s"http://$host:$port", region)
44+
AmazonS3ClientBuilder.standard()
45+
.withPathStyleAccessEnabled(true)
46+
.withCredentials(new AWSStaticCredentialsProvider(new AnonymousAWSCredentials()))
47+
.withEndpointConfiguration(endpoint)
48+
.build()
1349
}
1450
}
51+
52+
53+
case class ConfigOptions(dir: String, region: String, host: String, port: Int, initBuckets: Set[String])
54+
55+
object ConfigOptions {
56+
57+
def apply(config: Config): ConfigOptions = {
58+
59+
def getWithDefault[T](path: String, default: T): T = {
60+
if (config.hasPath(path)) config.getAnyRef(path).asInstanceOf[T] else default
61+
}
62+
63+
import scala.collection.JavaConverters._
64+
65+
ConfigOptions(
66+
getWithDefault("dir", File.newTemporaryDirectory(prefix = "s3mock").pathAsString),
67+
getWithDefault("region", "eu-west-1"),
68+
getWithDefault("host", "localhost"),
69+
getWithDefault("port", 8001),
70+
getWithDefault("initBuckets", new util.HashMap[String, String]()).asScala.values.toSet
71+
)
72+
}
73+
74+
}

src/main/scala/io/findify/s3mock/route/PutObject.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,22 @@ case class PutObject(implicit provider:Provider, mat:Materializer) extends LazyL
5151
val metadata = populateObjectMetadata(request, bytes)
5252
Try(provider.putObject(bucket, path, bytes, metadata)) match {
5353
case Success(()) => HttpResponse(StatusCodes.OK)
54-
case Failure(e: NoSuchBucketException) =>
54+
case Failure(e: NoSuchBucketException) => {
55+
println(s"FAILURE - NoSuchBucket: $e")
56+
e.printStackTrace()
5557
HttpResponse(
5658
StatusCodes.NotFound,
5759
entity = e.toXML.toString()
5860
)
59-
case Failure(t) =>
61+
}
62+
case Failure(t) => {
63+
println(s"FAILURE: ${t}")
64+
t.printStackTrace()
6065
HttpResponse(
6166
StatusCodes.InternalServerError,
6267
entity = InternalErrorException(t).toXML.toString()
6368
)
69+
}
6470
}
6571
}).runWith(Sink.head[HttpResponse])
6672
result

0 commit comments

Comments
 (0)