我正在使用boto将aut2实例作为自动缩放组的一部分启动。我读了一个简单的bash脚本并将base64编码的字符串传递给user_data。我正在使用Ubuntu AMI(ami-a73264ce),但我还尝试了另一个Ubuntu AMI(ami-ad184ac4)和Amazon Linux AMI(ami-bba18dd2),因为一个stackoverflow答案表明AMI可能是问题所在({ {3}})
这是我的脚本(startup.sh)
#!/bin/bash
mkdir newdir
这是我的boto代码:
conn = AutoScaleConnection(aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
f = open('startup.sh', 'r')
user_data = f.read()
lc = LaunchConfiguration(name=launch_config_name, image_id=ami_id,
key_name=key_name, security_groups=security_groups,
user_data=base64.b64encode(user_data))
conn.create_launch_configuration(lc)
ag = AutoScalingGroup(group_name=group_name, load_balancers=load_balancers,
availability_zones=availability_zones, launch_config=lc,
min_size=min_size, max_size=max_size, connection=conn)
conn.create_auto_scaling_group(ag)
这是我的/ var / log / syslog:
Jan 27 23:13:16 ip-10-122-219-105 [CLOUDINIT] cc_apt_update_upgrade.py[DEBUG]: mirror info: {'security': 'http://security.ubuntu.com/ubuntu', 'primary': 'http://us-east-1.ec2.archive.ubuntu.com/ubuntu/', 'mirror': 'http://us-east-1.ec2.archive.ubuntu.com/ubuntu/'}
Jan 27 23:13:16 ip-10-122-219-105 [CLOUDINIT] __init__.py[DEBUG]: handling landscape with freq=None and args=[]
Jan 27 23:13:16 ip-10-122-219-105 [CLOUDINIT] __init__.py[DEBUG]: handling timezone with freq=None and args=[]
Jan 27 23:13:16 ip-10-122-219-105 [CLOUDINIT] __init__.py[DEBUG]: handling puppet with freq=None and args=[]
Jan 27 23:13:16 ip-10-122-219-105 [CLOUDINIT] __init__.py[DEBUG]: handling chef with freq=None and args=[]
Jan 27 23:13:16 ip-10-122-219-105 [CLOUDINIT] __init__.py[DEBUG]: handling salt-minion with freq=None and args=[]
Jan 27 23:13:16 ip-10-122-219-105 [CLOUDINIT] __init__.py[DEBUG]: handling mcollective with freq=None and args=[]
Jan 27 23:13:16 ip-10-122-219-105 [CLOUDINIT] __init__.py[DEBUG]: handling disable-ec2-metadata with freq=None and args=[]
Jan 27 23:13:16 ip-10-122-219-105 [CLOUDINIT] __init__.py[DEBUG]: handling runcmd with freq=None and args=[]
Jan 27 23:13:16 ip-10-122-219-105 [CLOUDINIT] __init__.py[DEBUG]: handling byobu with freq=None and args=[]
Jan 27 23:13:17 ip-10-122-219-105 [CLOUDINIT] cloud-init-cfg[INFO]: cloud-init-cfg ['all', 'final']
Jan 27 23:13:17 ip-10-122-219-105 [CLOUDINIT] __init__.py[DEBUG]: handling rightscale_userdata with freq=None and args=[]
Jan 27 23:13:17 ip-10-122-219-105 [CLOUDINIT] __init__.py[DEBUG]: handling scripts-per-once with freq=None and args=[]
Jan 27 23:13:17 ip-10-122-219-105 [CLOUDINIT] __init__.py[DEBUG]: handling scripts-per-boot with freq=None and args=[]
Jan 27 23:13:17 ip-10-122-219-105 [CLOUDINIT] __init__.py[DEBUG]: handling scripts-per-instance with freq=None and args=[]
Jan 27 23:13:17 ip-10-122-219-105 [CLOUDINIT] __init__.py[DEBUG]: handling scripts-user with freq=None and args=[]
Jan 27 23:13:17 ip-10-122-219-105 [CLOUDINIT] __init__.py[DEBUG]: handling keys-to-console with freq=None and args=[]
Jan 27 23:13:17 ip-10-122-219-105 [CLOUDINIT] __init__.py[DEBUG]: handling phone-home with freq=None and args=[]
Jan 27 23:13:17 ip-10-122-219-105 [CLOUDINIT] __init__.py[DEBUG]: handling final-message with freq=None and args=[]
这是我的/var/log/cloud-init.log:
2014-01-27 23:12:54,362 - cloud-init[INFO]: cloud-init start-local running: Mon, 27 Jan 2014 23:12:54 +0000. up 14.07 seconds
2014-01-27 23:12:54,659 - __init__.py[DEBUG]: searching for data source in ['DataSourceNoCloud', 'DataSourceConfigDrive', 'DataSourceOVF']
2014-01-27 23:12:55,281 - __init__.py[DEBUG]: Did not find data source. searched classes: ['DataSourceNoCloud', 'DataSourceConfigDrive', 'DataSourceOVF']
2014-01-27 23:12:55,998 - cloud-init[INFO]: cloud-init start running: Mon, 27 Jan 2014 23:12:55 +0000. up 15.43 seconds
2014-01-27 23:12:56,050 - __init__.py[DEBUG]: searching for data source in ['DataSourceNoCloudNet', 'DataSourceConfigDriveNet', 'DataSourceOVFNet', 'DataSourceMAAS', 'DataSourceEc2']
2014-01-27 23:12:56,588 - DataSourceEc2.py[DEBUG]: Using metadata source: 'http://169.254.169.254'
2014-01-27 23:12:56,663 - DataSourceEc2.py[DEBUG]: crawl of metadata service took 0s
2014-01-27 23:12:56,664 - __init__.py[DEBUG]: found data source DataSourceEc2
2014-01-27 23:12:56,670 - cloud-init[DEBUG]: found data source: DataSourceEc2
2014-01-27 23:12:56,675 - __init__.py[WARNING]: Unhandled non-multipart userdata starting 'IyEvYmluL2Jhc2gKbWtkaXIg...'
2014-01-27 23:12:56,684 - __init__.py[DEBUG]: handling bootcmd with freq=None and args=[]
2014-01-27 23:12:56,687 - __init__.py[DEBUG]: handling resizefs with freq=None and args=[]
2014-01-27 23:12:56,712 - cc_resizefs.py[DEBUG]: resize took 0.0159449577332 seconds
2014-01-27 23:12:56,713 - cc_resizefs.py[DEBUG]: resizing root filesystem (type=ext4, maj=202, min=1, val=True)
2014-01-27 23:12:56,713 - __init__.py[DEBUG]: handling set_hostname with freq=None and args=[]
2014-01-27 23:12:56,781 - cc_set_hostname.py[DEBUG]: populated /etc/hostname with ip-10-122-219-105 on first boot
2014-01-27 23:12:56,782 - __init__.py[DEBUG]: handling update_hostname with freq=None and args=[]
2014-01-27 23:12:58,478 - cc_update_hostname.py[DEBUG]: wrote ip-10-122-219-105 to /var/lib/cloud/data/previous-hostname
2014-01-27 23:12:58,478 - __init__.py[DEBUG]: handling update_etc_hosts with freq=None and args=[]
2014-01-27 23:12:58,479 - cc_update_etc_hosts.py[DEBUG]: not managing /etc/hosts
2014-01-27 23:12:58,479 - __init__.py[DEBUG]: handling ca-certs with freq=None and args=[]
2014-01-27 23:12:58,482 - __init__.py[DEBUG]: handling rsyslog with freq=None and args=[]
2014-01-27 23:12:58,620 - __init__.py[DEBUG]: handling ssh with freq=None and args=[]
Jan 27 23:13:02 ip-10-122-219-105 [CLOUDINIT] cloud-init-cfg[INFO]: cloud-init-cfg ['all', 'config']
Jan 27 23:13:02 ip-10-122-219-105 [CLOUDINIT] __init__.py[DEBUG]: handling mounts with freq=None and args=[]
Jan 27 23:13:02 ip-10-122-219-105 [CLOUDINIT] DataSourceEc2.py[DEBUG]: remapped device name /dev/sdb => /dev/xvdb
Jan 27 23:13:02 ip-10-122-219-105 [CLOUDINIT] DataSourceEc2.py[DEBUG]: remapped device name /dev/sda3 => /dev/xvda3
Jan 27 23:13:02 ip-10-122-219-105 [CLOUDINIT] __init__.py[DEBUG]: handling ssh-import-id with freq=None and args=[]
Jan 27 23:13:02 ip-10-122-219-105 [CLOUDINIT] __init__.py[DEBUG]: handling locale with freq=None and args=[]
Jan 27 23:13:02 ip-10-122-219-105 [CLOUDINIT] cc_locale.py[DEBUG]: setting locale to en_US.UTF-8
Jan 27 23:13:13 ip-10-122-219-105 [CLOUDINIT] __init__.py[DEBUG]: handling set-passwords with freq=None and args=[]
Jan 27 23:13:13 ip-10-122-219-105 [CLOUDINIT] __init__.py[DEBUG]: handling grub-dpkg with freq=None and args=[]
Jan 27 23:13:13 ip-10-122-219-105 [CLOUDINIT] cc_grub_dpkg.py[DEBUG]: setting grub debconf-set-selections with '','true'
Jan 27 23:13:16 ip-10-122-219-105 [CLOUDINIT] __init__.py[DEBUG]: handling apt-pipelining with freq=None and args=[]
Jan 27 23:13:16 ip-10-122-219-105 [CLOUDINIT] cc_apt_pipelining.py[DEBUG]: Wrote /etc/apt/apt.conf.d/90cloud-init-pipelining with APT pipeline setting
Jan 27 23:13:16 ip-10-122-219-105 [CLOUDINIT] __init__.py[DEBUG]: handling apt-update-upgrade with freq=None and args=[]
修改
当我使用命令行工具时它可以工作,但我仍然想知道为什么它不适用于boto因为使用boto可能更方便
答案 0 :(得分:1)
检查您的用户数据是否在您的实例中填充:
/var/lib/cloud/data/scripts
每个声明都有换行符吗?
如何将此内容添加到脚本中以检查用户数据是否正确?
f = open('startup.sh', 'r')
user_data = f.read()
print user_data
答案 1 :(得分:0)
原来它在python中不一定是base64,这对我有用:
#cat make-a-dir.sh
#!/bin/bash
mkdir newdir
然后是我的python:
shell_script = 'make-a-dir.sh'
with open (shell_script, "r") as myfile:
data=myfile.read()
然后我将其传递给:
run_instances(ami_id,security_group_ids=["sg-1111111"],key_name="my-key",instance_type=instance_type,placement=az,subnet_id=subnet_id,dry_run=dry_run,user_data=data)
做了一个款待。祝你好运!