我有一个简单的bash脚本我尝试使用AWS库通过Powershell脚本传递给AWS EC2 ubuntu实例。
#!/bin/bash
apt-get update
apt-get upgrade
下载了对base64中的文件内容进行编码的powershell脚本,并调用启动EC2实例的cmdlet:
$fileContent = Get-Content $UserDataTarget
$fileContentBytes = [System.Text.Encoding]::UTF8.GetBytes($fileContent)
$fileContentEncoded = [System.Convert]::ToBase64String($fileContentBytes)
$MasterInstance = New-EC2Instance -ImageId ami-4c7a3924 -MinCount 1 -MaxCount 1 -KeyName AWSKey -SecurityGroups $SecurityGroup -InstanceType "t1.micro" -UserData $fileContentEncoded
这是来自cloud init log的片段:
Cloud-init v. 0.7.5 running 'modules:final' at Fri, 02 Oct 2015 21:05:24 +0000. Up 33.88 seconds.
/bin/bash: apt-get update apt-get upgrade: No such file or directory
2015-10-02 21:05:24,294 - util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/part-001 [127]
2015-10-02 21:05:24,298 - cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
2015-10-02 21:05:24,298 - util.py[WARNING]: Running scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/python2.7/dist-packages/cloudinit/config/cc_scripts_user.pyc'>) failed
这是在/ var / lib / cloud / instance / scripts / part-001上的ubuntu实例上加载的用户数据脚本的片段:
#!/bin/bash apt-get update apt-get upgrade
我尝试使用010 Editor和Cygwin将windows文件转换为linux。我尝试用LF字节替换CRLF字节。结果是相同的:整个bash脚本压缩为1行,删除所有换行符,并且在初始启动时无法加载用户数据脚本。
更新:我已经包含了用于执行换行符转换的两个代码段。两者都是从同行来源(SO)审查。出于某种原因,该脚本仍然显示在linux实例中,没有换行符。
Snippet 1
function ConvertTo-LinuxLineEndings($path) {
$oldBytes = [io.file]::ReadAllBytes($path)
if (!$oldBytes.Length) {
return;
}
[byte[]]$newBytes = @()
[byte[]]::Resize([ref]$newBytes, $oldBytes.Length)
$newLength = 0
for ($i = 0; $i -lt $oldBytes.Length - 1; $i++) {
if (($oldBytes[$i] -eq [byte][char]"`r") -and ($oldBytes[$i + 1] -eq [byte][char]"`n")) {
continue;
}
$newBytes[$newLength++] = $oldBytes[$i]
}
$newBytes[$newLength++] = $oldBytes[$oldBytes.Length - 1]
[byte[]]::Resize([ref]$newBytes, $newLength)
[io.file]::WriteAllBytes($path, $newBytes)
}
ConvertTo-LinuxLineEndings($UserDataTarget)
Snippet 2
try{
# get the contents and replace line breaks by U+000A
$contents = [IO.File]::ReadAllText($UserDataSource) -replace "`r`n", "`n"
# create UTF-8 encoding without signature
$utf8 = New-Object System.Text.UTF8Encoding $false
# write the text back
[IO.File]::WriteAllText($UserDataTarget, $contents, $utf8)
}
catch [Exception]
{
echo $_.Exception|format-list -force
}
答案 0 :(得分:0)
我无法就Powershell代码的外观给出答案,但如果有帮助我可以告诉您Powershell脚本的输出需要什么,所以你需要确认一下当你进入模板时,这就是你在UserData中的结果:
"UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
"#!/bin/bash\n\n",
"apt-get update\n",
"apt-get upgrade\n"
]]}}