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

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

pass shell script as custom_data in Azure using java sdk

问题

使用 Java SDK 从共享镜像库创建 Linux 虚拟机,自定义镜像。

```java
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();

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

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

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

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

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

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

响应中的操作系统配置

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

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


<details>
<summary>英文:</summary>

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();

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

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

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.

How should i handle the customdata part here. Any help is appreciated.


vm create request osprofile json

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

response os profile

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


The image i use here is a custom linux image built on centos 8

</details>


# 答案1
**得分**: 1

请参考[Java 8基本Base64][1],您可以使用`java.util.Base64`。首先,像平常一样导入它:

```java
import java.util.Base64;

然后:

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

接着像这样使用它:

virtualMachine = azure.virtualMachines()
                    .define(linuxVMName)
                    .withRegion(location)
                    .withExistingResourceGroup(resourceGroup)
                    .withExistingPrimaryNetworkInterface(networkInterface)
                    .withLinuxCustomImage(customImageUrl)
                    .withRootUsername(username)
                    .withRootPassword(password)
                    .withCustomData(encodedString)
                    .withComputerName(linuxVMName)
                    .withExistingStorageAccount(storageAccount)
                    .withSize(vmSize())
                    .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:

import java.util.Base64;

then

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

Then use it like this,

virtualMachine = azure.virtualMachines()
                        .define(linuxVMName)
                        .withRegion(location)
                        .withExistingResourceGroup(resourceGroup)
                        .withExistingPrimaryNetworkInterface(networkInterface)
                        .withLinuxCustomImage(customImageUrl)
                        .withRootUsername(username)
                        .withRootPassword(password)
                        .withCustomData(encodedString)
                        .withComputerName(linuxVMName)
                        .withExistingStorageAccount(storageAccount)
                        .withSize(vmSize())
                        .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:

确定