如何在 jelastic 环境变量中保留 \n 字符?

How can I preserve \n characters in my jelastic environment variable?

我正在尝试安装以下 Jelastic 清单:

type: update
name: Not working

onInstall:
  - getPublicKey
  - forEach(nodes.cp):
    - api:
      - method: environment.control.SetContainerEnvVars
        params:
          nodeId: ${@i.id}
          vars:
            SOME_SECRET: '{"type": "RS256", "key": "${response.out}"}'
actions:
  getPublicKey:
    - cmd [cp]: |
        curl -s -H "Authorization: my-token" http://${nodes.auth.master.intIP}:9011/api/key | jq -r '.keys[] | select(.algorithm | contains("RS256")).publicKey'

getPublicKey 操作的 Jelastic 控制台中的响应输出为:

cmd [cp: 113094].response: {"result":0,"errOut":"","nodeid":113094,"exitStatus":0,"out":"-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAprg9fCTMSfm6psfOnhfL\nUDdlrV39LU9p8msWsYUjO4M2E6m5GcADYPHkLdLV/c7W+vgDvpHLfxU8peP/21BS\nCVQVYEFpYCRY2NcOTsP2zLj7PTAFiw8wyOwK7u05EM7CgK7LS6rDotMIZFNzIPG5\nfJNz+hDyhvvNhWg56dcmPIrBxN26Piv+N6vtWqJDuVQNXKwEk/w4uUxiz9gNSEi/\nhJlLgHxTsSMh9YXUIyKn8QBACF4GQKmToBPW7ScEnX/Bm6y9g4JbYYIwWBTRwUfy\nkhbojk6mAPcKY+diWM2PE385pyjIWshKUgtBKcgPNJXDU3RPXAdzN0hQ1sJbNV5z\nzwIDAQAB\n-----END PUBLIC KEY-----"}

这里我们可以清楚的看到换行符显示为\n个字符。由于某种原因,换行符 \n 在环境变量 SOME_SECRET 中被解释为真正的换行符,现在读取不正确(这是 env 命令的结果):

SOME_SECRET={"type": "RS256", "key": "-----BEGIN PUBLIC KEY-----

变量值一直显示到第一个换行符,这不是我想要的

如何确保 \n 输出到 SOME_SECRET 环境变量,并且它们不会被真正的新行替换?当我在我的 ubuntu 图像上执行 env 命令时,我想看到这个:

SOME_SECRET={"type": "RS256", "key": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAprg9fCTMSfm6psfOnhfL\nUDdlrV39LU9p8msWsYUjO4M2E6m5GcADYPHkLdLV/c7W+vgDvpHLfxU8peP/21BS\nCVQVYEFpYCRY2NcOTsP2zLj7PTAFiw8wyOwK7u05EM7CgK7LS6rDotMIZFNzIPG5\nfJNz+hDyhvvNhWg56dcmPIrBxN26Piv+N6vtWqJDuVQNXKwEk/w4uUxiz9gNSEi/\nhJlLgHxTsSMh9YXUIyKn8QBACF4GQKmToBPW7ScEnX/Bm6y9g4JbYYIwWBTRwUfy\nkhbojk6mAPcKY+diWM2PE385pyjIWshKUgtBKcgPNJXDU3RPXAdzN0hQ1sJbNV5z\nzwIDAQAB\n-----END PUBLIC KEY-----"}

以下清单完全符合我的要求:

type: update
name: Working

onInstall:
  - getPublicKey
  - api:
    - method: environment.control.AddContainerEnvVars
      params:
        nodeGroup: cp
        vars:
          SOME_SECRET: '{"type": "RS256", "key": "${response.out}"}'
actions:
  getPublicKey:
    - cmd [cp]: |
        curl -s -H "Authorization: some-key" http://${nodes.auth.master.intIP}:9011/api/key | jq -r '.keys[] | select(.algorithm | contains("RS256")).publicKey' | sed ':a;N;$!ba;s/\n/\n/g'

请注意在 getPublicKey 中使用 sed 以适当地格式化响应输出。另外,请注意使用 environment.control.AddContainerEnvVars 而不是 environment.control.SetContainerEnvVars。后者删除节点组上的所有环境变量(PATH除外),而前者真正添加新的环境变量(并用新值覆盖现有变量)。

最后要注意的是,必须要有getPublicKey这个动作。以下清单不起作用(我不知道为什么):

type: update
name: Not Working

onInstall:
  - cmd [cp]: |
      curl -s -H "Authorization: some-key" http://${nodes.auth.master.intIP}:9011/api/key | jq -r '.keys[] | select(.algorithm | contains("RS256")).publicKey' | sed ':a;N;$!ba;s/\n/\n/g'
  - api:
    - method: environment.control.AddContainerEnvVars
      params:
        nodeGroup: cp
        vars:
          SOME_SECRET: '{"type": "RS256", "key": "${response.out}"}'