---
version: 20
data:

  - type: "WorkflowTemplate"
    variation: null
    replacePermissions: true
    replaceObject: true
    attributes:
      deleteExecutionFilesOnSuccess: "false"
      description: "This workflow migrates existing IceBox generations to an AWS S3\
      \ cloud storage. The workflow requires that the following configuration in the\
      \ XDM properties have been configured: \n- admin.db.url\n- admin.db.user\n-\
      \ admin.db.password\n- admin.db.schema\n- xdm.blob.store.s3.bucket\n- xdm.blob.store.s3.credentials.accessKey\n\
      - xdm.blob.store.s3.credentials.secretKey\n- xdm.blob.store.s3.region.static\n\
      - xdm.blob.store.s3.endpoint"
      displayName: "IceBoxToCloudMigrator"
      executionPlatform: "default"
      executionRetentionPeriod: "-1"
      libraries: []
      scriptLanguage: "GROOVY"
      taskExecutionReport: []
      workflow: "import java.sql.DriverManager\nimport java.sql.Connection\nimport java.sql.SQLException\n\
       import java.nio.file.Path\nimport groovy.json.JsonSlurper\nimport com.amazonaws.auth.AWSStaticCredentialsProvider;\n\
       import com.amazonaws.auth.BasicAWSCredentials;\nimport com.amazonaws.client.builder.AwsClientBuilder;\n\
       import com.amazonaws.services.s3.AmazonS3;\nimport com.amazonaws.services.s3.AmazonS3ClientBuilder;\n\
       \nvar url = System.getenv(\"admin.db.url\");\nvar user = System.getenv(\"admin.db.user\"\
       );\nvar password = System.getenv(\"admin.db.password\");\nvar schema = System.getenv(\"\
       admin.db.schema\");\nvar bucket = System.getenv(\"xdm.blob.store.s3.bucket\"\
       )\n\ntry (var connection = DriverManager.getConnection(url, user, password))\
       \ {\n    println \"Determine number of generations\"\n    var backupMap = getBackups(connection,\
       \ schema)\n    println \"Existing generations: \" + backupMap.size()\n\n   \
       \ // Save content in the cloud.     \n    var client = createClient();\n   \
       \ var jsonSlurper = new JsonSlurper()\n\n    backupMap.each { databaseId, backupPath\
       \ ->\n        // Upload the .meta file\n        var path = Path.of(backupPath)\n\
       \        var artifact = jsonSlurper.parse(path)    \n        var remoteMetaPath\
       \ = \"${artifact.classifier}/${artifact.uuid}.meta\" \n        client.putObject(bucket,\
       \ remoteMetaPath, path.toFile())\n    \n        // Upload the data of the generation\n\
       \        artifact.items.each { item -> \n            var remotePath = \"${artifact.classifier}/${item}\"\
       \ \n            client.putObject(bucket, remotePath, path.getParent().resolve(item).toFile())\n\
       \        }\n    \n        // Update the path of the generation in the database\
       \ \n        var newPath = \"s3://${bucket}/${artifact.classifier}/${artifact.uuid}.meta\"\
       \           \n        try (var stmt = connection.createStatement()) {\n    \
       \        stmt.executeUpdate(\"UPDATE  ${schema}.backup SET backup_path='${newPath}'\
       \ WHERE database_id = ${databaseId}\");\n        }\n        println \"Processed\
       \ backup file: ${backupPath}\"    \n    }\n} catch (SQLException e) {\n    throw\
       \ new RuntimeException(e);\n}\n\n\n/**\n * Get the icebox backups \n */\ndef\
       \ Map<Long, String> getBackups(Connection connection, String schema) {\n   \
       \ var backupMap = [:]    \n    var sql = \"SELECT database_id, backup_path FROM\
       \ ${schema}.backup WHERE backup_path LIKE '/%'\";\n        \n    try (var stmt\
       \ = connection.createStatement()) {\n        try (var rs = stmt.executeQuery(sql))\
       \ {\n            while (rs.next()) {\n                var databaseId = rs.getLong(1);\n\
       \                var backupPath = rs.getString(2);\n                backupMap.put(databaseId,\
       \ backupPath);\n            }\n        }\n    }\n    return backupMap;\n}\n\n\
       \n/**\n * Creates AWS S3 client to upload the data to the cloud. \n */\ndef\
       \ AmazonS3 createClient() {\n    var credentials = new BasicAWSCredentials(\n\
       \        System.getenv(\"xdm.blob.store.s3.credentials.accessKey\"),\n    \
       \    System.getenv(\"xdm.blob.store.s3.credentials.secretKey\")\n    );\n\n\
       \    var builder = AmazonS3ClientBuilder.standard()\n        .withCredentials(new\
       \ AWSStaticCredentialsProvider(credentials));\n\n    var endpoint = System.getenv(\"\
       xdm.blob.store.s3.endpoint\");\n    var region = System.getenv(\"xdm.blob.store.s3.region.static\"\
       );\n    if (endpoint != null) {\n        builder.withEndpointConfiguration(new\
       \ AwsClientBuilder.EndpointConfiguration(endpoint, region));\n    } else {\n\
       \        builder.withRegion(region);\n    }\n    return builder.build();\n}"
      workflowCallEntries: [ ]
    children:
      - type: "Workflow"
        variation: null
        replacePermissions: true
        replaceObject: true
        attributes:
          description: null
          displayName: "W1"