AWS EC2 用户数据未正确解码

Rab*_*820 0 bash amazon-ec2 amazon-web-services user-data

我正在尝试创建一个 EC2 实例来托管 Perforce 服务器。我的实例配置了以下用户数据:

\n\n
#!/bin/bash\n\n# Add a newline to the ec2-user prompt string\necho PS1=\\"\\\\n\\$PS1\\" >> /home/ec2-user/.bashrc\n\n# Update all packages\nyum update \xe2\x80\x93y\n\n# Install Perforce packages\n# The RHEL/7 part of the baseurl should be replaced with\n# the latest RHEL version that both Amazon and Perforce support\nrpm \xe2\x80\x93import https://package.perforce.com/perforce.pubkey\ncd /etc/yum.repos.d/\necho [perforce] > perforce.repo\necho name=Perforce >> perforce.repo\necho baseurl=http://package.perforce.com/yum/rhel/7/x86_64 >> perforce.repo\necho enabled=1 >> perforce.repo\necho gpgcheck=1 >> perforce.repo\nyum install \xe2\x80\x93y helix-p4d\n\n# Make directories for the server, owned by new \xe2\x80\x9cperforce\xe2\x80\x9d user\ncd /opt/perforce/servers/\nmkdir danware\ncd danware\nmkdir danware-db danware-chkpts journal\nchown \xe2\x80\x93R perforce:perforce danware\n
Run Code Online (Sandbox Code Playgroud)\n\n

我已经测试了上述每个命令,并且知道它们按此顺序手动执行时可以工作。然而,亚马逊的 base64 编码/解码系统的某些方面似乎出现了障碍。启动(并通过所有系统检查)后,当我从 EC2 控制台转到“操作 > 实例设置 > 查看/更改用户数据”时,我看到以下用户数据。请注意几乎每个连字符“-”都被一些奇怪的“a”字符替换。

\n\n

在此输入图像描述

\n\n

但是,我不确定这就是问题所在,因为 /var/log/cloud-init-output.log 处的日志文件提供了以下输出(我用 [...] 替换了一些重复文本以保存空间)。请注意这行内容:Failed running /var/lib/cloud/instance/scripts/part-001我已验证此part-001 文件实际上确实具有正确显示的连字符。

\n\n
[...]\nCloud-init v. 0.7.6 running \'modules:final\' at Fri, 09 Sep 2016 06:23:39 +0000. Up 86.66 seconds.\nLoaded plugins: priorities, update-motd, upgrade-helper\nNo Match for argument: \xe2\x80\x93y\nNo packages marked for update\nRPM version 4.11.2\nCopyright (C) 1998-2002 - Red Hat, Inc.\nThis program may be freely redistributed under the terms of the GNU GPL\n\nUsage: rpm [-aKfgpqVcdLilsiv?] [-a|--all] [-f|--file] [-g|--group] [...]\nLoaded plugins: priorities, update-motd, upgrade-helper\nResolving Dependencies\n--> Running transaction check\n---> [...]\n\nDependencies Resolved\n\n================================================================================\n Package                  Arch        Version               Repository     Size\n================================================================================\nInstalling:\n helix-p4d                x86_64      2016.1-1429894        perforce       24 k\nInstalling for dependencies:\n helix-cli                x86_64      2016.1-1429894        perforce      8.8 k\n helix-cli-base           x86_64      2016.1-1429894        perforce      1.4 M\n helix-p4d-base           x86_64      2016.1-1429894        perforce      3.1 k\n helix-p4d-base-16.1      x86_64      2016.1-1429894        perforce      2.4 M\n helix-p4dctl             x86_64      2016.1-1429894        perforce      1.2 M\n\nTransaction Summary\n================================================================================\nInstall  1 Package (+5 Dependent packages)\n\nTotal download size: 5.0 M\nInstalled size: 13 M\nIs this ok [y/d/N]: Exiting on user command\nYour transaction was saved, rerun it with:\n yum load-transaction /tmp/yum_save_tx.2016-09-09.06-23.dRP_r2.yumtx\n/var/lib/cloud/instance/scripts/part-001: line 22: cd: /opt/perforce/servers/: No such file or directory\nchown: invalid user: \xe2\x80\x98\xe2\x80\x93R\xe2\x80\x99\nSep 09 06:23:41 cloud-init[2517]: util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/part-001 [1]\nSep 09 06:23:41 cloud-init[2517]: cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)\nSep 09 06:23:41 cloud-init[2517]: util.py[WARNING]: Running module scripts-user (<module \'cloudinit.config.cc_scripts_user\' from \'/usr/lib/python2.7/dist-packages/cloudinit/config/cc_scripts_user.pyc\'>) failed\nCloud-init v. 0.7.6 finished at Fri, 09 Sep 2016 06:23:41 +0000. Datasource DataSourceEc2.  Up 88.53 seconds\n
Run Code Online (Sandbox Code Playgroud)\n\n

更烦人的是,我认为No Match for argument: \xe2\x80\x93y日志文件中的早期行指的是yum update -y我的用户数据中的行。果然,只需从 EC2文档页面运行示例用户数据脚本(该脚本也使用yum update -y),就会给我同样的错误/警告!亚马逊自己的示例脚本不起作用!?那么谁能回答为什么 A) AWS 没有正确显示用户数据代码,以及 B) 为什么我的用户数据会产生上面显示的错误?非常感谢您的帮助!

\n

Vor*_*ung 5

对于诸如

\n\n
yum update \xe2\x80\x93y\n
Run Code Online (Sandbox Code Playgroud)\n\n

您使用的字符是“EN DASH U+2013”

\n\n

连字符的常用字符是“HYPHEN-MINUS U+002D”

\n\n

修复您的用户数据源以使用“连字符减号”并再次尝试

\n\n

我通过剪切和粘贴到该在线站点来检查字符代码http://www.fileformat.info/info/unicode/char/search.htm?q=-&preview=entity

\n\n

不知道你是否能看出区别,但这是你的连字符

\n\n
yum update \xe2\x80\x93y\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是一个“连字符减号”

\n\n
yum update -y\n
Run Code Online (Sandbox Code Playgroud)\n