Skip to content

Commit

Permalink
Merge pull request #30 from civitaspo/develop
Browse files Browse the repository at this point in the history
v0.0.6
  • Loading branch information
civitaspo authored Nov 13, 2018
2 parents a8ec6d8 + d01dd78 commit b57b021
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.0.6 (2018-11-13)
==================

* [Enhancement] Enable to use params as env for `ecs_task.sh`

0.0.5 (2018-11-13)
==================

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ _export:
repositories:
- https://jitpack.io
dependencies:
- pro.civitaspo:digdag-operator-ecs_task:0.0.5
- pro.civitaspo:digdag-operator-ecs_task:0.0.6
ecs_task:
auth_method: profile

Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = 'pro.civitaspo'
version = '0.0.5'
version = '0.0.6'

def digdagVersion = '0.9.31'
def scalaSemanticVersion = "2.12.6"
Expand Down Expand Up @@ -37,6 +37,7 @@ shadowJar {
classifier = null
dependencies {
exclude(dependency('io.digdag:.*'))
exclude(dependency('.*:jackson.*:.*'))
}
}

Expand Down
2 changes: 1 addition & 1 deletion example/example.dig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ _export:
- file://${repos}
# - https://jitpack.io
dependencies:
- pro.civitaspo:digdag-operator-ecs_task:0.0.5
- pro.civitaspo:digdag-operator-ecs_task:0.0.6
ecs_task:
auth_method: profile

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ cd workspace
# Unset e option for returning embulk results to digdag
set +e

# envs
export ${ECS_TASK_SH_EXPORT_ENV}

# Run
${ECS_TASK_SH_COMMAND} \
2>> ../stderr.log \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import pro.civitaspo.digdag.plugin.ecs_task.util.TryWithResource

import scala.collection.JavaConverters._
import scala.util.Random
import scala.util.matching.Regex

abstract class AbstractEcsTaskCommandOperator(operatorName: String, context: OperatorContext, systemConfig: Config, templateEngine: TemplateEngine)
extends AbstractEcsTaskOperator(operatorName, context, systemConfig, templateEngine) {

protected val validEnvKeyRegex: Regex = "[a-zA-Z_][a-zA-Z_0-9]*".r

protected val mainScriptName: String

private lazy val tmpStorageConfig: Config = {
Expand Down Expand Up @@ -53,7 +56,20 @@ abstract class AbstractEcsTaskCommandOperator(operatorName: String, context: Ope
protected def collectEnvironments(): Map[String, String] = {
val vars: PrivilegedVariables = context.getPrivilegedVariables
vars.getKeys.asScala.foldLeft(Map.empty[String, String]) { (env, key) =>
env ++ Map(key -> vars.get(key))
if (isValidEnvKey(key)) {
env ++ Map(key -> vars.get(key))
}
else {
logger.info(s"$key is invalid env key.")
env
}
}
}

protected def isValidEnvKey(key: String): Boolean = {
key match {
case validEnvKeyRegex() => true
case _ => false
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package pro.civitaspo.digdag.plugin

package object ecs_task {

val VERSION: String = "0.0.5"
val VERSION: String = "0.0.6"

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package pro.civitaspo.digdag.plugin.ecs_task.sh
import com.fasterxml.jackson.databind.JsonNode
import io.digdag.client.config.Config
import io.digdag.spi.{OperatorContext, TemplateEngine}
import pro.civitaspo.digdag.plugin.ecs_task.aws.AmazonS3UriWrapper
import pro.civitaspo.digdag.plugin.ecs_task.command.{AbstractEcsTaskCommandOperator, TmpStorage}
import pro.civitaspo.digdag.plugin.ecs_task.util.TryWithResource

import scala.collection.JavaConverters._
import scala.io.Source
import scala.util.Try

class EcsTaskShOperotar(operatorName: String, context: OperatorContext, systemConfig: Config, templateEngine: TemplateEngine)
extends AbstractEcsTaskCommandOperator(operatorName, context, systemConfig, templateEngine) {
Expand All @@ -25,11 +28,29 @@ class EcsTaskShOperotar(operatorName: String, context: OperatorContext, systemCo
val dup: Config = params.deepCopy()
dup.set("ECS_TASK_SH_BUCKET", AmazonS3UriWrapper(tmpStorage.getLocation).getBucket)
dup.set("ECS_TASK_SH_PREFIX", AmazonS3UriWrapper(tmpStorage.getLocation).getKey)
dup.set("ECS_TASK_SH_EXPORT_ENV", convertParamsAsEnv().map { case (k: String, v: String) => s"$k=$v" }.mkString(" "))
dup.set("ECS_TASK_SH_COMMAND", command)

TryWithResource(classOf[EcsTaskShOperotar].getResourceAsStream(runShResourcePath)) { is =>
val runShContentTemplate: String = Source.fromInputStream(is).mkString
templateEngine.template(runShContentTemplate, dup)
}
}

protected def convertParamsAsEnv(params: Config = params): Map[String, String] = {
val keys: Seq[String] = params.getKeys.asScala
keys.foldLeft(Map.empty[String, String]) { (env, key) =>
if (isValidEnvKey(key)) {
val jn: JsonNode = params.getInternalObjectNode.get(key)
val v: String =
if (jn.isTextual) s""""${jn.textValue().replace("\"", "\\\"")}""""
else jn.toString
env ++ Map(key -> v)
}
else {
logger.info(s"$key is invalid env key.")
env
}
}
}
}

0 comments on commit b57b021

Please sign in to comment.