如何对 Shell 脚本进行 MIME 编码以将其部署在 EC2 实例上
How to MIME encode a Shell Scrip to deploy it on EC2 intances
我正在 AWS 上创建一个 Auto Scaling 组,我需要我的 EC2 实例已经部署了我的应用程序映像,所以我创建了一个用户数据脚本来执行它
#!/bin/bash
before_reboot(){
sudo amazon-linux-extras install docker -y
sudo service docker start
sudo usermod -a -G docker ec2-user
sudo chkconfig docker on
sudo yum install -y git
sudo reboot
}
after_reboot(){
sudo curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose version
git clone https://github.com/augustoaccorsi/microservices.git
cd microservices
docker-compose -f docker-compose-front.yml up --build --detach
}
if [ -f /var/run/rebooting-for-updates ]; then
after_reboot
rm /var/run/rebooting-for-updates
update-rc.d myupdate remove
else
before_reboot
touch /var/run/rebooting-for-updates
update-rc.d myupdate defaults
sudo reboot
fi
但是当自动缩放创建一个实例时,我的图像没有部署,如果我 shh
实例并检查 /var/log/cloud-init-output.log
,我得到以下
__init__.py[WARNING]: Unhandled non-multipart (text/x-not-multipart) userdata: 'before_reboot(){...'
我有研究表明我需要对我的 shell 脚本进行 MIME 编码以使其对实例可执行,但我不知道在我的 windows 机器上这样做是否热,可以有人帮帮我吗?
仔细阅读 AWS docs 后,我注意到我的用户数据文件中缺少一些配置,例如 cloud-config,所以我添加了这些缺少的配置,然后我就可以开始了我的 EC2 实例和所需的代码。像这样:
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0
--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"
#cloud-config
cloud_final_modules:
- [scripts-user, always]
--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"
#!/bin/bash
sudo amazon-linux-extras install docker -y
sudo service docker start
sudo usermod -a -G docker ec2-user
sudo chkconfig docker on
sudo yum install -y git
sudo curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose version
git clone https://github.com/augustoaccorsi/microservices.git
cd microservices
docker-compose -f docker-compose-front.yml up --build --detach
--//
我的错误是我没有配置一些 header 信息、文件 MIME 版本等等。
我正在 AWS 上创建一个 Auto Scaling 组,我需要我的 EC2 实例已经部署了我的应用程序映像,所以我创建了一个用户数据脚本来执行它
#!/bin/bash
before_reboot(){
sudo amazon-linux-extras install docker -y
sudo service docker start
sudo usermod -a -G docker ec2-user
sudo chkconfig docker on
sudo yum install -y git
sudo reboot
}
after_reboot(){
sudo curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose version
git clone https://github.com/augustoaccorsi/microservices.git
cd microservices
docker-compose -f docker-compose-front.yml up --build --detach
}
if [ -f /var/run/rebooting-for-updates ]; then
after_reboot
rm /var/run/rebooting-for-updates
update-rc.d myupdate remove
else
before_reboot
touch /var/run/rebooting-for-updates
update-rc.d myupdate defaults
sudo reboot
fi
但是当自动缩放创建一个实例时,我的图像没有部署,如果我 shh
实例并检查 /var/log/cloud-init-output.log
,我得到以下
__init__.py[WARNING]: Unhandled non-multipart (text/x-not-multipart) userdata: 'before_reboot(){...'
我有研究表明我需要对我的 shell 脚本进行 MIME 编码以使其对实例可执行,但我不知道在我的 windows 机器上这样做是否热,可以有人帮帮我吗?
仔细阅读 AWS docs 后,我注意到我的用户数据文件中缺少一些配置,例如 cloud-config,所以我添加了这些缺少的配置,然后我就可以开始了我的 EC2 实例和所需的代码。像这样:
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0
--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"
#cloud-config
cloud_final_modules:
- [scripts-user, always]
--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"
#!/bin/bash
sudo amazon-linux-extras install docker -y
sudo service docker start
sudo usermod -a -G docker ec2-user
sudo chkconfig docker on
sudo yum install -y git
sudo curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose version
git clone https://github.com/augustoaccorsi/microservices.git
cd microservices
docker-compose -f docker-compose-front.yml up --build --detach
--//
我的错误是我没有配置一些 header 信息、文件 MIME 版本等等。