使用Java SDK将Shell脚本作为custom_data传递给Azure

huangapple go评论104阅读模式
英文:

pass shell script as custom_data in Azure using java sdk

问题

  1. 使用 Java SDK 从共享镜像库创建 Linux 虚拟机,自定义镜像。
  2. ```java
  3. virtualMachine = azure.virtualMachines()
  4. .define(linuxVMName)
  5. .withRegion(location)
  6. .withExistingResourceGroup(resourceGroup)
  7. .withExistingPrimaryNetworkInterface(networkInterface)
  8. .withLinuxCustomImage(customImageUrl)
  9. .withRootUsername(username)
  10. .withRootPassword(password)
  11. .withCustomData(custDatastring)
  12. .withComputerName(linuxVMName)
  13. .withExistingStorageAccount(storageAccount)
  14. .withSize(vmSize())
  15. .create();

如何在 withCustomData 部分传递 Linux Shell 脚本。我知道该方法的输入是 Base64 编码的字符串。
我正在尝试传递一个示例脚本

  1. #!/bin/bash
  2. echo "This is some text" > randomtext.txt

附注:我不能在此处为脚本创建文件,我必须直接将上述命令传递给 API。这是我的要求。

在这里应该如何处理 customData 部分。任何帮助将不胜感激。

虚拟机创建请求中的操作系统配置 JSON

  1. "osProfile": {
  2. "computerName": "xxxx",
  3. "adminUsername": "xxx",
  4. "adminPassword": "xxx",
  5. "customData": "IyEvYmluL2Jhc2gKIGVjaG8gIlRoaXMgaXMgc29tZSB0ZXh0IiA+IHJhbmRvbXRleHQudHh0",
  6. "linuxConfiguration": {
  7. "disablePasswordAuthentication": false
  8. }
  9. }

响应中的操作系统配置

  1. "osProfile": {
  2. "computerName": "xxxx",
  3. "adminUsername": "xxxx",
  4. "linuxConfiguration": {
  5. "disablePasswordAuthentication": false,
  6. "provisionVMAgent": true
  7. },
  8. "secrets": [],
  9. "allowExtensionOperations": true,
  10. "requireGuestProvisionSignal": true
  11. }

我在这里使用的镜像是基于 CentOS 8 构建的自定义 Linux 镜像。

  1. <details>
  2. <summary>英文:</summary>
  3. I am creating a linux vm using a custom image from an shared image gallery using java sdk.

virtualMachine = azure.virtualMachines()
.define(linuxVMName)
.withRegion(location)
.withExistingResourceGroup(resourceGroup)
.withExistingPrimaryNetworkInterface(networkInterface)
.withLinuxCustomImage(customImageUrl)
.withRootUsername(username)
.withRootPassword(password)
.withCustomData(custDatastring)
.withComputerName(linuxVMName)
.withExistingStorageAccount(storageAccount)
.withSize(vmSize())
.create();

  1. How to pass a linux shell script here in the withCustomData part. I know the input for the method is Base64 encoded string.
  2. I am trying to pass an example script

#!/bin/bash
echo "This is some text" > randomtext.txt

  1. p.s: I can&#39;t create a file for the script here, i have to pass the above command directly to the api. That is my requirement.
  2. How should i handle the customdata part here. Any help is appreciated.
  3. vm create request osprofile json

"osProfile":{"computerName":"xxxx","adminUsername":"xxx","adminPassword":"xxx","customData":"IyEvYmluL2Jhc2gKIGVjaG8gIlRoaXMgaXMgc29tZSB0ZXh0IiA+IHJhbmRvbXRleHQudHh0","linuxConfiguration":{"disablePasswordAuthentication":false}}

  1. response os profile

"osProfile": {
"computerName": "xxxx",
"adminUsername": "xxxx",
"linuxConfiguration": {
"disablePasswordAuthentication": false,
"provisionVMAgent": true
},
"secrets": [],
"allowExtensionOperations": true,
"requireGuestProvisionSignal": true
}

  1. The image i use here is a custom linux image built on centos 8
  2. </details>
  3. # 答案1
  4. **得分**: 1
  5. 请参考[Java 8基本Base64][1],您可以使用`java.util.Base64`。首先,像平常一样导入它:
  6. ```java
  7. import java.util.Base64;

然后:

  1. String originalInput = "#!/bin/bash\n echo \"This is some text\" > randomtext.txt";
  2. String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes());

接着像这样使用它:

  1. virtualMachine = azure.virtualMachines()
  2. .define(linuxVMName)
  3. .withRegion(location)
  4. .withExistingResourceGroup(resourceGroup)
  5. .withExistingPrimaryNetworkInterface(networkInterface)
  6. .withLinuxCustomImage(customImageUrl)
  7. .withRootUsername(username)
  8. .withRootPassword(password)
  9. .withCustomData(encodedString)
  10. .withComputerName(linuxVMName)
  11. .withExistingStorageAccount(storageAccount)
  12. .withSize(vmSize())
  13. .create();

我尝试过了。

在Linux上,这些数据通过ovf-env.xml文件传递给虚拟机,在部署过程中,该文件会被复制到/var/lib/waagent目录中。新版本的Microsoft Azure Linux代理还会将Base64编码的数据复制到/var/lib/waagent/CustomData目录,以提供更方便的访问。

在我的Ubuntu 16.04 LTS系统中,我可以在路径/var/lib/waagent/ovf-env.xml中找到自定义数据。

关于更多示例,您可以参考此线程以及Microsoft Azure上的自定义数据和Cloud-Init

英文:

Refer to Java 8 Basic Base64, you could use java.util.Base64. First, import it as you normally do:

  1. import java.util.Base64;

then

  1. String originalInput = &quot;#!/bin/bash\n echo \&quot;This is some text\&quot; &gt; randomtext.txt&quot;;
  2. String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes());

Then use it like this,

  1. virtualMachine = azure.virtualMachines()
  2. .define(linuxVMName)
  3. .withRegion(location)
  4. .withExistingResourceGroup(resourceGroup)
  5. .withExistingPrimaryNetworkInterface(networkInterface)
  6. .withLinuxCustomImage(customImageUrl)
  7. .withRootUsername(username)
  8. .withRootPassword(password)
  9. .withCustomData(encodedString)
  10. .withComputerName(linuxVMName)
  11. .withExistingStorageAccount(storageAccount)
  12. .withSize(vmSize())
  13. .create();

I tried it.

使用Java SDK将Shell脚本作为custom_data传递给Azure

On Linux, this data is passed to the VM via the ovf-env.xml file, which is copied to the /var/lib/waagent directory during provisioning. Newer versions of the Microsoft Azure Linux Agent will also copy the base64-encoded data to /var/lib/waagent/CustomData as well for convenience.

I can find the custom data in the path /var/lib/waagent/ovf-env.xml on My Ubuntu16.04LTS.

使用Java SDK将Shell脚本作为custom_data传递给Azure

For more examples, you could refer to this thread and Custom Data and Cloud-Init on Microsoft Azure.

huangapple
  • 本文由 发表于 2020年8月25日 22:09:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63580714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定