作为用户数据文件传递给AWS EC2实例的Bash脚本在首次启动时无法加载 [英] Bash script passed to AWS EC2 Instance as User Data file fails to load on initial boot

查看:159
本文介绍了作为用户数据文件传递给AWS EC2实例的Bash脚本在首次启动时无法加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的bash脚本,我正在尝试使用AWS库通过Powershell脚本传递给AWS EC2 ubuntu实例.

I have a simple bash script I'm trying to pass to an AWS EC2 ubuntu instance via a Powershell script using the AWS Library.

#!/bin/bash

apt-get update
apt-get upgrade

在此处使用powershell脚本对base64中的文件内容进行编码,并调用启动EC2实例的cmdlet:

Heres the powershell script that encodes the file contents in base64 and calls the cmdlet that starts the EC2 instance:

$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

这是云初始化日志中的一个片段:

This is a snippet from the 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

以下是ubuntu实例上已加载的用户数据脚本的代码段,位于/var/lib/cloud/instance/scripts/part-001:

Here's a snippet of the loaded user data script on the ubuntu instance at /var/lib/cloud/instance/scripts/part-001:

#!/bin/bash  apt-get update apt-get upgrade

我尝试使用010 Editor和Cygwin将Windows文件转换为linux.我尝试用LF字节替换CRLF字节.结果是相同的:整个bash脚本压缩为1行,删除了所有换行符,并且用户数据脚本在首次启动时无法加载.

I have tried converting the windows file to linux using 010 Editor and Cygwin. I've tried replacing the CRLF bytes with LF bytes. The result is the same: the entire bash script is condensed to 1 line, all line breaks are removed, and user data script fails to load on initial boot.

更新:我已经包含了我用来进行换行符转换的两个代码段.两者均从同行来源(SO)进行了审查.由于某种原因,该脚本仍然显示在linux实例中,但没有换行符.

UPDATE: I've included both code snippets I've used to do the line break conversion. Both were vetted from peer sources (SO). For some reason, the script still shows up in the linux instance without the line break characters.

代码段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)

代码段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
}

推荐答案

我无法为您提供有关Powershell代码的外观的答案,但是如果有帮助,我可以告诉您Powershell的输出是什么脚本需要看起来像,所以您需要确认这是您要在模板中放入UserData的最终结果:

I can't give you an answer on what your Powershell code needs to look like, but in case it helps I can tell you what the output from your Powershell script needs to look like, so you need to confirm that this is what you're ending up with in the UserData when it goes in the template:

"UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
            "#!/bin/bash\n\n",
            "apt-get update\n",
            "apt-get upgrade\n"
        ]]}}

这篇关于作为用户数据文件传递给AWS EC2实例的Bash脚本在首次启动时无法加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆