2013-07-22 42 views
10

我的用戶數據腳本執行用戶數據(雲-INIT)腳本不是EC2

#! 
set -e -x 
echo `whoami` 
su root 
yum update -y 
touch ~/PLEASE_WORK.txt 

其在從命令饋送:

ec2-run-instances ami-05355a6c -n 1 -g mongo-group -k mykey -f myscript.sh -t t1.micro -z us-east-1a 

但是當我檢查文件/var/log/cloud-init.log ,該tail -n 5是:

[CLOUDINIT] 2013-07-22 16:02:29,566 - cloud-init-cfg[INFO]: cloud-init-cfg ['runcmd'] 
[CLOUDINIT] 2013-07-22 16:02:29,583 - __init__.py[DEBUG]: restored from cache type DataSourceEc2 
[CLOUDINIT] 2013-07-22 16:02:29,686 - cloud-init-cfg[DEBUG]: handling runcmd with freq=None and args=[] 
[CLOUDINIT] 2013-07-22 16:02:33,691 - cloud-init-run-module[INFO]: cloud-init-run-module ['once-per-instance', 'user-scripts', 'execute', 'run-parts', '/var/lib/cloud/data/scripts'] 
[CLOUDINIT] 2013-07-22 16:02:33,699 - __init__.py[DEBUG]: restored from cache type DataSourceEc2 

我也驗證了curl http://169.254.169.254/latest/user-data返回我的文件準備編輯。

並沒有其他錯誤或腳本的輸出發生。我怎樣才能讓用戶數據腳本在啓動時正確執行?

回答

14

Cloud-init不接受純粹的bash腳本,就像那樣。這是一個吃YAML文件的野獸,它定義了你的實例(包,SSH密鑰和其他東西)。

使用MIME你也可以發送任意的shell腳本,但是你必須對它們進行MIME編碼。

$ cat my-boothook.txt 
#!/bin/sh 
echo "Hello World!" 
echo "This will run as soon as possible in the boot sequence" 

$ cat my-user-script.txt 
#!/usr/bin/perl 
print "This is a user script (rc.local)\n" 

$ cat my-include.txt 
# these urls will be read pulled in if they were part of user-data 
# comments are allowed. The format is one url per line 
http://www.ubuntu.com/robots.txt 
http://www.w3schools.com/html/lastpage.htm 

$ cat my-upstart-job.txt 
description "a test upstart job" 
start on stopped rc RUNLEVEL=[2345] 
console output 
task 
script 
echo "====BEGIN=======" 
echo "HELLO From an Upstart Job" 
echo "=====END========" 
end script 

$ cat my-cloudconfig.txt 
#cloud-config 
ssh_import_id: [smoser] 
apt_sources: 
- source: "ppa:smoser/ppa" 

$ ls 
my-boothook.txt  my-include.txt  my-user-script.txt 
my-cloudconfig.txt my-upstart-job.txt 

$ write-mime-multipart --output=combined-userdata.txt \ 
    my-boothook.txt:text/cloud-boothook \ 
    my-include.txt:text/x-include-url \ 
    my-upstart-job.txt:text/upstart-job \ 
    my-user-script.txt:text/x-shellscript \ 
    my-cloudconfig.txt 

$ ls -l combined-userdata.txt 
-rw-r--r-- 1 smoser smoser 1782 2010-07-01 16:08 combined-userdata.txt 

combined-userdata.txt是您要在其中粘貼的文件。

此處瞭解詳情:

https://help.ubuntu.com/community/CloudInit

另外請注意,這在很大程度上取決於你所使用的圖像上。但是你說它實際上是基於雲初始化的映像,所以這適用。還有其他雲啓動器,它們不是以cloud-init命名 - 那麼它可能會有所不同。

+2

ah我看到了 - 所有內容都必須經過MIME編碼。 我使用的是通用的亞馬遜linux AMI,但我在啓動日誌中看到'cloud-init'進程,因此我認爲它與ubuntu風格相同。 – lollercoaster

+1

您不必爲ec2-run-instances使用MIME編碼腳本。 Cloud-init可能需要特定格式的數據,但亞馬遜會爲您負責 - 請參閱http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AmazonLinuxAMIBasics.html#CloudInit MIME/Base64/gzip編碼是可選的 - 您的腳本只需要以#開頭! –

+0

好點,我使用OpenStack ... – lzap

12

實際上,cloud-init允許將單個shell腳本作爲輸入(儘管您可能希望將MIME存檔用於更復雜的設置)。

OP腳本的問題是第一行不正確。你應該使用這樣的事情:

#!/bin/sh 

這樣做的原因是,雖然雲init將使用#!識別用戶腳本,操作系統需要以執行腳本的完整家當線。

因此,OP的情況是,cloud-init的行爲正確(即下載並嘗試運行腳本),但操作系統無法真正執行它。


參見:Shebang (Unix)在維基百科上

+2

[有關cloud-init的文檔在這裏有點誤導](http://cloudinit.readthedocs.org/en/latest/topics/format.html#user-data-script),其中指出「開始於:'# !'或'Content-Type:text/x-shellscript'當使用MIME檔案。「我可以看到有人可能沒有意識到#!需要包括shebang之後的路徑。 –

+0

不僅具有誤導性,而且非常差。有很少的例子說明如何將它們放在一起工作,然後對於某些圖像來說它被打破了? gcloud「正常工作」。爲什麼缺少(或不正確的)文檔?應該有一個文件使用的例子,但是即使是這樣的語法也不清楚。 – mckenzm

2

現在這是一對夫婦歲,但對於其他人受益我有同樣的問題,事實證明,雲計算的初始化正在運行兩次,從內/etc/rc3.d。刪除文件夾中的這些文件可以使用戶數據正確運行:

lrwxrwxrwx 1 root root 22 Jun 5 02:49 S-1cloud-config -> ../init.d/cloud-config 
lrwxrwxrwx 1 root root 20 Jun 5 02:49 S-1cloud-init -> ../init.d/cloud-init 
lrwxrwxrwx 1 root root 26 Jun 5 02:49 S-1cloud-init-local -> ../init.d/cloud-init-local 
相關問題