diff options
-rw-r--r-- | .bzr-builddeb/default.conf | 2 | ||||
-rw-r--r-- | debian/changelog | 179 | ||||
-rw-r--r-- | debian/control | 6 | ||||
-rw-r--r-- | debian/dirs | 3 | ||||
-rw-r--r-- | debian/ec2-config.cfg | 2 | ||||
-rw-r--r-- | debian/ec2-init.rightscale-init.init | 103 | ||||
-rw-r--r-- | debian/init | 39 | ||||
-rw-r--r-- | debian/install | 2 | ||||
-rwxr-xr-x | debian/rules | 10 | ||||
-rwxr-xr-x | ec2-fetch-credentials.py | 53 | ||||
-rwxr-xr-x | ec2-get-info.py | 124 | ||||
-rwxr-xr-x | ec2-run-user-data.py | 42 | ||||
-rwxr-xr-x | ec2-set-apt-sources.py | 73 | ||||
-rwxr-xr-x | ec2-set-defaults.py | 46 | ||||
-rwxr-xr-x | ec2-set-hostname.py | 39 | ||||
-rw-r--r-- | templates/hosts.tmpl | 10 | ||||
-rw-r--r-- | templates/sources.list.tmpl | 6 |
17 files changed, 707 insertions, 32 deletions
diff --git a/.bzr-builddeb/default.conf b/.bzr-builddeb/default.conf new file mode 100644 index 00000000..3a08d607 --- /dev/null +++ b/.bzr-builddeb/default.conf @@ -0,0 +1,2 @@ +[BUILDDEB] +native = True diff --git a/debian/changelog b/debian/changelog index a0b4ab07..b45b28c5 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,6 +1,183 @@ +ec2-init (0.3.3ubuntu12) jaunty; urgency=low + + * ec2-run-user-data.py: Fix error. + + -- Chuck Short <zulcss@ubuntu.com> Tue, 07 Apr 2009 08:14:07 -0400 + +ec2-init (0.3.3ubuntu11) jaunty; urgency=low + + * debian/control: + - Add python-cheetah and python-apt as a dependency. + * debian/ec2-config.cfg: + - Remove distro due to the change in ec2-set-apt-sources.py + * debian/inistall + - Install the templates in the right place. + * ec2-set-apt-sources.py: + - Use python-apt to update the sources.list. + + -- Chuck Short <zulcss@ubuntu.com> Wed, 01 Apr 2009 13:58:43 -0400 + +ec2-init (0.3.3ubuntu10) jaunty; urgency=low + + * ec2-set-hostname.py: + - Use template for /etc/hosts creation. + - Dont use public_hostname in /etc/hosts. (LP: #352745) + + -- Chuck Short <zulcss@ubuntu.com> Wed, 01 Apr 2009 08:48:05 -0400 + +ec2-init (0.3.3ubuntu9) jaunty; urgency=low + + * ec2-set-apt-sources.py: + - Use a template to generate the sources.list and generate it based on the lsb_release. + + -- Chuck Short <zulcss@ubuntu.com> Tue, 31 Mar 2009 15:15:55 -0400 + +ec2-init (0.3.3ubuntu8) jaunty; urgency=low + + * ec2-set-apt-sources.py: + - Add the ubuntu-on-ec2 ppa. + + -- Chuck Short <zulcss@ubuntu.com> Tue, 31 Mar 2009 09:37:13 -0400 + +ec2-init (0.3.3ubuntu7) jaunty; urgency=low + + * debian/rules: Fix typo. + + -- Chuck Short <zulcss@ubuntu.com> Sun, 22 Mar 2009 17:14:16 -0400 + +ec2-init (0.3.3ubuntu6) jaunty; urgency=low + + * Set the configuration file to jaunty. + * ec2-fetch-credentials: Fix typo. + * ec2-set-defaults.py: + - Remove timezone change when booting the instance. + - Redirect output to /dev/null. + * ec2-set-apt-sources.py: + - Run apt-get update after the /etc/apt/sources.list and + redirect the output to /dev/null. + * rightscale-init: Updated rightscale-init + + -- Chuck Short <zulcss@ubuntu.com> Thu, 19 Mar 2009 20:52:59 -0400 + +ec2-init (0.3.3ubuntu5) jaunty; urgency=low + + * debian/ec2-config.cfg: + - Add disable root option. + * debian/ec2-init.rightscale-init.init: + - Add rightscale detection script. + * ec2-get-info.py: + - Display the information about an AMI instance. + + -- Chuck Short <zulcss@ubuntu.com> Mon, 16 Mar 2009 08:54:49 -0400 + +ec2-init (0.3.3ubuntu4) jaunty; urgency=low + + * ec2-fetch-credentials.py: + - Allow user to choose which user they wish to configure for. + - Allow user to disable root user if they wish to. + * ec2-set-defaults.py: + - Set default timezone to UTC. + - Set locale depending on zone. + * debian/init: + - Removed nash plugin. + - Add ec2-set-defaults. + + -- Chuck Short <zulcss@ubuntu.com> Wed, 04 Mar 2009 08:33:01 -0500 + +ec2-init (0.3.3ubuntu3~intrepid4) intrepid; urgency=low + + * set distro to intrepid. + + -- Chuck Short <zulcss@ubuntu.com> Thu, 26 Feb 2009 10:28:06 -0500 + +ec2-init (0.3.3ubuntu3) jaunty; urgency=low + + * debian/ec2-init: Log results of ec2-run-user-data to syslog. + * ec2-run-user-data.py :Dont leave files around and log the + output to syslog. + + -- Chuck Short <zulcss@ubuntu.com> Thu, 26 Feb 2009 10:24:35 -0500 + +ec2-init (0.3.3ubuntu2) jaunty; urgency=low + + * ec2-set-apt-sources.py: + - Use the ec2 mirrors. (LP: #317065, #333897) + - Update the /etc/apt/sources.list (LP: #333904) + * ec2-fetch-credentials.py: + - Better error checking (LP: #325067) + + -- Chuck Short <zulcss@ubuntu.com> Tue, 24 Feb 2009 14:02:37 -0500 + +ec2-init (0.3.3ubuntu1) jaunty; urgency=low + + * debian/init: Fix init script. + + -- Chuck Short <zulcss@ubuntu.com> Fri, 20 Feb 2009 09:22:54 -0500 + +ec2-init (0.3.3) jaunty; urgency=low + + * ec2-set-apt-sources.py + - Determine the zone that the user is in and generate + a /etc/apt/sources.list.d/ based on that. + * debian/init: + - Check to see if there is an /var/run/ec2 and create + it if it doesnt exist. + - Start ec2-set-apt-sources at first bootup. + * debian/rules: + - Install ec2-set-apt-sources. + * debian/control: + - Add python-configobj as a dependency. + * debian/{install,dirs} + - Create an /etc/ec2-init to read the configuration file and install it. + + -- Chuck Short <zulcss@ubuntu.com> Mon, 09 Feb 2009 10:35:56 -0500 + +ec2-init (0.3.2) jaunty; urgency=low + + * debian/init: + - Remove already ran detection + - Log the running of ec2-run-user-data to /var/log/ec2-user-data.log + * ec2-set-hostname.py: + - set hostname to the Ec2 local-hostname + - Update the /etc/hosts to change the ubuntu hostname to the + public hostname. + * ec2-fetch-credentials: + - Copy the ssh keys to the ubuntu user. + - Setup authorized keys for root to tell the user to login as the + ubuntu user when they try to connect. + * ec2-run-user-data: + - Create an .already-ran file to check to see if ec2-run-user-data + already ran. + - Save the ec2-run-user-data script in /var/ec2. + + -- Chuck Short <zulcss@ubuntu.com> Wed, 04 Feb 2009 09:32:08 -0500 + +ec2-init (0.3.1) jaunty; urgency=low + + * debian/dir: Install /var/ec2 to save user-data scripts. + * debian/rules: Start ec2-init after ssh. + * ec2-run-user-data.py: Save run-user-data script with ami-id. + + -- Chuck Short <zulcss@ubuntu.com> Mon, 26 Jan 2009 10:40:52 -0500 + +ec2-init (0.3) jaunty; urgency=low + + * ec2-run-user-data: Fix python error when writing a file to the disk. + + -- Chuck Short <zulcss@ubuntu.com> Thu, 15 Jan 2009 11:49:08 -0500 + +ec2-init (0.2) jaunty; urgency=low + + * debian/init: Run fetch-credentials before anything else. + (LP: #308533) + * Add ec2-set-hostname.py: Queries ec2 metdada for public-hostname + and then sets it (LP: #316201) + + -- Chuck Short <zulcss@ubuntu.com> Tue, 13 Jan 2009 15:20:21 -0500 + ec2-init (0.1) intrepid; urgency=low - * Initial release. + * Initial release (LP: #269434). -- Soren Hansen <soren@ubuntu.com> Fri, 12 Sep 2008 15:30:32 +0200 diff --git a/debian/control b/debian/control index c579e89e..e51482e0 100644 --- a/debian/control +++ b/debian/control @@ -6,8 +6,8 @@ Build-Depends: cdbs, debhelper (>= 5) Standards-Version: 3.8.0 Package: ec2-init -Architecture: all -Depends: python, procps -Description: Init scripts for EC2 instances +Architecture: i386 amd64 +Depends: python, procps, python-configobj, python-cheetah, python-apt +Description: Init scripts for EC2 instances, EC2 instances need special scripts to run during initialisation to retrieve and install ssh keys and to let the user run various scripts. diff --git a/debian/dirs b/debian/dirs new file mode 100644 index 00000000..3c349733 --- /dev/null +++ b/debian/dirs @@ -0,0 +1,3 @@ +var/ec2 +usr/sbin +etc/ec2-init diff --git a/debian/ec2-config.cfg b/debian/ec2-config.cfg new file mode 100644 index 00000000..76c81b77 --- /dev/null +++ b/debian/ec2-config.cfg @@ -0,0 +1,2 @@ +user="ubuntu" +DISABLE_ROOT="1" diff --git a/debian/ec2-init.rightscale-init.init b/debian/ec2-init.rightscale-init.init new file mode 100644 index 00000000..10ba39d3 --- /dev/null +++ b/debian/ec2-init.rightscale-init.init @@ -0,0 +1,103 @@ +#!/bin/bash +# +# This script will configure an Ubuntu server for use with RightScale +# + +start() { + ## First check to see if we were launched through RightScale. Exit and remove if not... + user_data=`curl -s -S -f -L --retry 7 -w ' %{http_code}' http://169.254.169.254/latest/user-data` + if `echo $user_data | grep -v "RS_token=" 1>/dev/null 2>&1` ; then + ## Remove this init script so that it does not run again + remove + exit 0 + fi + + ## ok, we were launched through RightScale, so let's continue + echo "Detected a RightScale instance..." + echo "Beginning configuration..." + + ## figure out which version of RightScale to install... + export rs_release=Ubuntu_`lsb_release -rs` + + ## Install some necessary packages + echo "Installing necessary packages..." + export DEBIAN_FRONTEND=NONINTERACTIVE + apt-get update + apt-get install -y binutils ruby1.8 sysv-rc-conf unzip ruby1.8-dev build-essential autoconf automake libtool logrotate rsync openssl ca-certificates libopenssl-ruby1.8 + + ## Add rightscale customizations + echo "Installing RightScale" + curl -s -S -f -L --retry 7 -w ' %{http_code}' -o /tmp/rightscale_scripts.tgz http://s3.amazonaws.com/rightscale_scripts/rightscale_scripts_"$rs_release".tgz + tar -xzf /tmp/rightscale_scripts.tgz -C /opt/ + ln -f /opt/rightscale/etc/init.d/rightscale /etc/init.d/rightscale + chmod +x /opt/rightscale/etc/init.d/rightscale + chmod +x /etc/init.d/rightscale + mkdir -p /etc/rightscale.d + update-rc.d rightscale start 98 2 3 4 5 . stop 1 0 1 6 . + ln -sf /usr/bin/ruby1.8 /usr/bin/ruby + ln -f /opt/rightscale/etc/motd /etc/motd + echo $rs_release > /etc/rightscale-release + + ## Add rubygems 1.8 + echo "Installing RubyGems 1.8..." + curl -s -S -f -L --retry 7 -w ' %{http_code}' -o /tmp/rubygems.tgz http://rubyforge.org/frs/download.php/45905/rubygems-1.3.1.tgz + mkdir -p /tmp/rubygems + tar -xzvf /tmp/rubygems.tgz -C $root/tmp/rubygems + cd /tmp/rubygems/rubygems-1.3.1/ + ruby setup.rb --no-rdoc --no-ri + ln -s /usr/bin/gem1.8 /usr/bin/gem + gem source -a http://ec2-us-east-mirror.rightscale.com/rubygems/archive/latest/ + gem source -r http://mirror.rightscale.com + gem install --no-rdoc --no-ri xml-simple net-ssh net-sftp s3sync + updatedb + + ## Add some links to keep things inline + ln -s /usr/lib/site_ruby/aes /usr/local/lib/site_ruby/1.8/aes + ln -s /usr/lib/site_ruby/ec2 /usr/local/lib/site_ruby/1.8/ec2 + ln -s /usr/bin/env /bin/env + + ## Insert 'ec2' as the cloud + echo "ec2" > /etc/rightscale.d/cloud + + ## Enable root logins + echo "Enabling root logins..." + cp -f /home/ubuntu/.ssh/authorized_keys /root/.ssh/. + + ## Remove the default sources.list becasue RightScale will create a better one anyways... + rm -f /etc/apt/sources.list.d/amazon.list + + ## Remove this init script so that it does not run again + remove + + ## Start the rightscale service + echo "Starting RightScale..." + /etc/init.d/rightscale start + echo "RightScale services started properly" + +} + +stop() { + exit 0 +} + +remove() { + my_name=`readlink -e $0` + update-rc.d -f `basename $my_name` remove + rm -f $my_name +} + + +# See how we were called. +case "$1" in + start) + start >>/var/log/install 2>&1 + ;; + stop) + stop >>/var/log/install 2>&1 + ;; + *) + echo $"Usage: $0 {start|stop}" + exit 1 +esac + +exit $? diff --git a/debian/init b/debian/init index d084c568..ea4f0acf 100644 --- a/debian/init +++ b/debian/init @@ -1,7 +1,7 @@ #!/bin/sh ### BEGIN INIT INFO # Provides: ec2-init -# Required-Start: $network $local_fs +# Required-Start: $network $local_fs $ssh # Required-Stop: # Should-Start: $named # Should-Stop: @@ -16,10 +16,14 @@ NAME=ec2-init . /lib/lsb/init-functions +if [ ! -d /var/run/ec2 ]; then + mkdir /var/run/ec2 +fi + case "$1" in start) - log_daemon_msg "Running EC2 user data" - if ec2-run-user-data 2> /dev/null + log_daemon_msg "Setting EC2 defaults" + if ec2-set-defaults 2> /dev/null then log_end_msg 0 else @@ -34,16 +38,29 @@ case "$1" in log_end_msg 1 fi - if pgrep nash-hotplug > /dev/null + log_daemon_msg "Running EC2 user data" + if ec2-run-user-data 2>&1 | logger -t "user-data" then - log_daemon_msg "Killing nash-hotplug" - if pkill nash-hotplug; - then - log_end_msg 0 - else - log_end_msg 1 - fi + log_end_msg 0 + else + log_end_msg 1 fi + + log_daemon_msg "Setting hostname to EC2 public_hostname" + if ec2-set-hostname 2> /dev/null + then + log_end_msg 0 + else + log_end_msg 1 + fi + log_daemon_msg "Determining EC2 availability zone" + if ec2-set-apt-sources 2> /dev/null + then + log_end_msg 0 + else + log_end_msg 1 + fi + ;; stop) exit 0 diff --git a/debian/install b/debian/install index 54a21487..33e2382f 100644 --- a/debian/install +++ b/debian/install @@ -1 +1,3 @@ debian/tmp/usr/sbin/* +debian/tmp/etc/ec2-init/templates/* +debian/ec2-config.cfg etc/ec2-init diff --git a/debian/rules b/debian/rules index 8c9f1074..641694d9 100755 --- a/debian/rules +++ b/debian/rules @@ -2,9 +2,17 @@ include /usr/share/cdbs/1/rules/debhelper.mk -DEB_UPDATE_RCD_PARAMS:=start 90 S . +DEB_UPDATE_RCD_PARAMS:= start 90 2 3 4 5 . stop 20 1 . build/ec2-init:: install -d debian/tmp/usr/sbin + mkdir -p debian/tmp/var/ec2 + mkdir -p debian/tmp/etc/ec2-init/templates install -m 775 ec2-fetch-credentials.py debian/tmp/usr/sbin/ec2-fetch-credentials install -m 775 ec2-run-user-data.py debian/tmp/usr/sbin/ec2-run-user-data + install -m 755 ec2-set-hostname.py debian/tmp/usr/sbin/ec2-set-hostname + install -m 755 ec2-set-apt-sources.py debian/tmp/usr/sbin/ec2-set-apt-sources + install -m755 ec2-get-info.py debian/tmp/usr/sbin/ec2-get-info + install -m755 ec2-set-defaults.py debian/tmp/usr/sbin/ec2-set-defaults + install -m755 templates/* debian/tmp/etc/ec2-init/templates + dh_installinit -p ec2-init --name rightscale-init -- defaults 99 09 diff --git a/ec2-fetch-credentials.py b/ec2-fetch-credentials.py index 80980924..68922878 100755 --- a/ec2-fetch-credentials.py +++ b/ec2-fetch-credentials.py @@ -20,9 +20,17 @@ # import urllib import os +import socket +import sys +from configobj import ConfigObj api_ver = '2008-02-01' metadata = None +filename='/etc/ec2-init/ec2-config.cfg' + +config = ConfigObj(filename) +user = config['user'] +config_root = config['DISABLE_ROOT'] def get_ssh_keys(): base_url = 'http://169.254.169.254/%s/meta-data' % api_ver @@ -30,14 +38,45 @@ def get_ssh_keys(): keyids = [line.split('=')[0] for line in data.split('\n')] return [urllib.urlopen('%s/public-keys/%d/openssh-key' % (base_url, int(keyid))).read().rstrip() for keyid in keyids] -keys = get_ssh_keys() +def setup_user_keys(k,user): + if not os.path.exists('/home/%s/.ssh' %(user)): + os.mkdir('/home/%s/.ssh' %(user)) -os.umask(077) + authorized_keys = '/home/%s/.ssh/authorized_keys' % user + fp = open(authorized_keys, 'a') + fp.write(''.join(['%s\n' % key for key in keys])) + fp.close() + os.system('chown -R %s:%s /home/%s/.ssh' %(user,user,user)) + +def setup_root_user(k,root_config): + if root_config == "1": + if not os.path.exists('/root/.ssh'): + os.mkdir('/root/.ssh/') -if not os.path.exists('/root/.ssh'): - os.mkdir('/root/.ssh') + fp = open('/root/.ssh/authorized_keys', 'a') + fp.write("command=\"echo \'Please login as the ubuntu user rather than root user.\';echo;sleep 10\" ") + fp.write(''.join(['%s\n' % key for key in keys])) + fp.close() + elif root_config == "0": + print "You choose to disable the root user, god help you." + else: + print "%s - I dont understand that opion." -fp = open('/root/.ssh/authorized_keys', 'a') -fp.write(''.join(['%s\n' % key for key in keys])) -fp.close() +def checkServer(): + s = socket.socket() + try: + address = '169.254.169.254' + port = 80 + s.connect((address,port)) + except socket.error, e: + print "!!! Unable to connect to %s" % address + sys.exit(0) +os.umask(077) +if user == "": + print "User must exist in %s" %(filename) + sys.exit(0) + +keys = get_ssh_keys() +setup_user_keys(keys,user) +setup_root_user(keys,config_root) diff --git a/ec2-get-info.py b/ec2-get-info.py new file mode 100755 index 00000000..b05e2408 --- /dev/null +++ b/ec2-get-info.py @@ -0,0 +1,124 @@ +#!/usr/bin/python +# +# Fetch the information about an AMI instance. +# Copyright 2009 Canonical Ltd. +# +# Author: Chuck Short <chuck.short@canonical.com> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# + +from optparse import OptionParser +import urllib + +ec2Data = ['ami-id', + 'ami-launch-index', + 'ami-manifest-path', + 'ancestor-ami-id', + 'block-device-mapping', + 'instance-id', + 'instance-type', + 'local-hostname', + 'local-ipv4', + 'kernel-id', + 'product-codes', + 'public-hostname', + 'public-ipv4', + 'ramdisk-id', + 'reserveration-id', + 'security-groups'] + +def ec2Version(): + os.system("cat /etc/ec2_version") + +def getData(ec2data): + api_ver = '2008-02-01' + metadata = None + base_url = 'http://169.254.169.254/%s/meta-data' % api_ver + data = urllib.urlopen('%s/%s/' %(base_url,ec2data)).read() + print "%s: %s" %(ec2data,data) + +def showAllData(ec2Data): + api_ver = '2008-02-01' + metadata = None + base_url = 'http://169.254.169.254/%s/meta-data' % api_ver + for x in ec2Data: + data = urllib.urlopen('%s/%s/' %(base_url,x)).read() + print "%s: %s" %(ec2data,data) + +def main(): + usage = "usage: %prog [options]" + + parser = OptionParser(prog='ec2-get-info', usage=usage) + parser.add_option('--ami-id', dest='amiid', action='store_true', help='Display the ami-id.') + parser.add_option('--launch-index', dest='launch', action='store_true', help='Display the AMI launch index.') + parser.add_option('--manifest', dest='manifest', action='store_true', help='Display the AMI manifest path.') + parser.add_option('--ancestor-id', dest='ancestor', action='store_true', help='Display the AMI ancestor id.') + parser.add_option('--block-device', dest='block', action='store_true', help='Display the block device id.') + parser.add_option('--instance-id', dest='id', action='store_true', help='Display the instance id.') + parser.add_option('--instance-type', dest='type', action='store_true', help='Display the instance type.') + parser.add_option('--local-hostname', dest='lhostname', action='store_true', help='Display the local hostname.') + parser.add_option('--local-ipv4', dest='lipv4', action='store_true', help='Display the local ipv4 IP address.') + parser.add_option('--kernel-id', dest='aki', action='store_true', help='List the AKI.') + parser.add_option('--product-codes', dest='code', action='store_true', help='List the product codes associated with thsi AMI.') + parser.add_option('--public-hostname', dest='phostname', action='store_true', help='Show the public hostname.') + parser.add_option('--public_ipv4', dest='pipv4', action='store_true', help='Show the public IPV4 IP address.') + parser.add_option('--ramdisk-id', dest='ari', action='store_true', help='Display the ARI.') + parser.add_option('--reservation-id', dest='rid', action='store_true', help='Display the reservation id.') + parser.add_option('--security-groups', dest='security', action='store_true', help='Display the security groups.') + parser.add_option('--ec2-version', dest='ec2', action='store_true', help='Display the current Ubuntu EC2 version') + parser.add_option('--all',dest='all', action='store_true', help='Display all informantion.') + + options, args = parser.parse_args() + + if options.amiid: + getData(ec2Data[0]) + if options.launch: + getData(ec2Data[1]) + if options.manifest: + getData(ec2Data[2]) + if options.ancestor: + getData(ec2Data[3]) + if options.block: + getData(ec2Data[4]) + if options.id: + getData(ec2Data[5]) + if options.type: + getData(ec2Data[6]) + if options.lhostname: + getData(ec2Data[7]) + if options.lipv4: + getData(ec2Data[8]) + if options.aki: + getData(ec2Data[9]) + if options.code: + getData(ec2Data[10]) + if options.phostname: + getData(ec2Data[11]) + if options.pipv4: + getData(ec2Data[12]) + if options.ari: + getData(ec2Data[13]) + if options.rid: + getData(ec2Data[14]) + if options.security: + getData(ec2Data[15]) + if options.ec2: + ec2Version() + if options.all: + for i in range(0,15): + getData(ec2Data[i]) + +if __name__ == "__main__": + main() diff --git a/ec2-run-user-data.py b/ec2-run-user-data.py index 6638ef37..513c64ff 100755 --- a/ec2-run-user-data.py +++ b/ec2-run-user-data.py @@ -3,7 +3,7 @@ # Fetch and run user-data from EC2 # Copyright 2008 Canonical Ltd. # -# Author: Soren Hansen <soren@canonical.com> +# Original-Author: Soren Hansen <soren@canonical.com> # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -23,10 +23,22 @@ import os import sys import tempfile import urllib +import socket +from time import gmtime, strftime api_ver = '2008-02-01' metadata = None +def checkServer(): + s = socket.socket() + try: + address = '169.254.169.254' + port = 80 + s.connect((address,port)) + except socket.error, e: + print "!!!! Unable to connect to %s" % address + sys.exit(0) + def get_user_data(): url = 'http://169.254.169.254/%s/user-data' % api_ver fp = urllib.urlopen(url) @@ -34,15 +46,27 @@ def get_user_data(): fp.close() return data +def get_ami_id(): + url = 'http://169.254.169.254/%s/meta-data' % api_ver + ami_id = urllib.urlopen('%s/ami-id/' %url).read() + return ami_id + +checkServer() user_data = get_user_data() +amiId = get_ami_id() +filename = '/var/ec2/.already-ran.%s' % amiId -if user_data.startswith('#!'): - (fp, path) = tempfile.mkstemp() - fp.write(data) - fp.close() - os.chmod(path, 0700) - status = os.system('%s' % path) - os.unlink(path) - sys.exit(os.WIFEXITSTATUS(status)) +if os.path.exists(filename): + print "ec2-run-user-data already ran for this instance." + sys.exit(0) +elif user_data.startswith('#!'): + # run it + (fp, path) = tempfile.mkstemp() + os.write(fp,user_data) + os.close(fp); + os.chmod(path, 0700) + status = os.system('%s | logger -t "user-data" ' % path) + os.unlink(path) + os.system('touch %s' %(filename)) sys.exit(0) diff --git a/ec2-set-apt-sources.py b/ec2-set-apt-sources.py new file mode 100755 index 00000000..46ca0a48 --- /dev/null +++ b/ec2-set-apt-sources.py @@ -0,0 +1,73 @@ +#!/usr/bin/python +# +# Fetch the availabity zone and create the sources.list +# Copyright 2009 Canonical Ltd. +# +# Author: Chuck Short <chuck.short@canonical.com> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +import os +import sys +import urllib +import socket +import apt +import apt_pkg +from Cheetah.Template import Template + +def checkServer(): + s = socket.socket() + try: + address = '169.254.169.254' + port = 80 + s.connect((address,port)) + except socket.error, e: + print "!!! Unable to connect to %s." % address + sys.exit(0) + +def detectZone(): + api_ver = '2008-02-01' + metadat = None + + base_url = 'http://169.254.169.254/%s/meta-data' % api_ver + zone = urllib.urlopen('%s/placement/availability-zone' % base_url).read() + if zone.startswith("us"): + archive = "http://us.ec2.archive.ubuntu.com/ubuntu/" + elif zone.startswith("eu"): + archive = "http://eu.ec2.archive.ubuntu.com/ubuntu/" + + return(archive) + +def updateList(): + mirror = detectZone() + if not os.path.exists("/var/run/ec2/sources.lists"): + t = os.popen("lsb_release -c").read() + codename = t.split() + distro = codename[1] + + mp = {'mirror' : mirror, 'codename' : distro} + t = Template(file="/etc/ec2-init/templates/sources.list.tmpl", searchList=[mp]) + f = open("/var/run/ec2/sources.list", "w") + f.write('%s' %(t)) + f.close() + + if not os.path.exists("/etc/apt/sources.list-ec2-init"): + os.system("mv /etc/apt/sources.list /etc/apt/sources.list-ec2-init") + os.symlink("/var/run/ec2/sources.list", "/etc/apt/sources.list") + cache = apt.Cache(apt.progress.OpProgress()) + prog = apt.progress.FetchProgress() + cache.update(prog) + +checkServer() +updateList() diff --git a/ec2-set-defaults.py b/ec2-set-defaults.py new file mode 100755 index 00000000..7d410031 --- /dev/null +++ b/ec2-set-defaults.py @@ -0,0 +1,46 @@ +#!/usr/bin/python +# +# Fetch the availabity zone and create the sources.list +# Copyright 2009 Canonical Ltd. +# +# Author: Chuck Short <chuck.short@canonical.com> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# + +import urllib +import os + +api_ver = '2008-02-01' +metadata = None + +base_url = 'http://169.254.169.254/%s/meta-data' % api_ver +zone = urllib.urlopen('%s/placement/availability-zone' % base_url).read() + +if zone.startswith("us"): + archive = "http://us.ec2.archive.ubuntu.com/ubuntu" +elif zone.startswith("eu"): + archive = "http://eu.ec2.archive.ubuntu.com/ubuntu" + +def set_language(location): + if location.startswith("us"): + lang='en_US.UTF-8' + os.system('locale-gen %s 2>&1 > /dev/null' %(lang)) + os.system('update-locale %s 2>&1 > /dev/null' %(lang)) + elif location.startswith("eu"): + lang='en_GB.UTF-8' + os.system('locale-gen %s 2>&1 > /dev/null' %(lang)) + os.system('update-locale %s 2>&1 > /dev/null' %(lang)) + +set_language(zone) diff --git a/ec2-set-hostname.py b/ec2-set-hostname.py new file mode 100755 index 00000000..6858a8b0 --- /dev/null +++ b/ec2-set-hostname.py @@ -0,0 +1,39 @@ +#!/usr/bin/python +# +# Fetch login credentials for EC2 +# Copyright 2008 Canonical Ltd. +# +# Author: Chuck Short <chuck.short@canonical.com> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +import urllib +import os +from Cheetah.Template import Template + +api_ver = '2008-02-01' +metadata = None + +base_url = 'http://169.254.169.254/%s/meta-data' % api_ver +my_hostname = urllib.urlopen('%s/local-hostname/' % base_url).read() +os.system('hostname %s' % my_hostname) + +# replace the ubuntu hostname in /etc/hosts +mp = {'hostname': my_hostname} +t = Template(file="/etc/ec2-init/templates/hosts.tmpl", searchList=[mp]) + +os.system("rm /etc/hosts") +f = open("/etc/hosts", "w") +f.write('%s' %(t)) +f.close() diff --git a/templates/hosts.tmpl b/templates/hosts.tmpl new file mode 100644 index 00000000..642e7a7e --- /dev/null +++ b/templates/hosts.tmpl @@ -0,0 +1,10 @@ +127.0.0.1 localhost +127.0.1.1 $hostname + +# The following lines are desirable for IPv6 capable hosts +::1 ip6-localhost ip6-loopback +fe00::0 ip6-localnet +ff00::0 ip6-mcastprefix +ff02::1 ip6-allnodes +ff02::2 ip6-allrouters +ff02::3 ip6-allhosts diff --git a/templates/sources.list.tmpl b/templates/sources.list.tmpl new file mode 100644 index 00000000..59dfa959 --- /dev/null +++ b/templates/sources.list.tmpl @@ -0,0 +1,6 @@ +deb $mirror $codename main universe +deb-src $mirror $codename main universe +deb $mirror $codename-updates main universe +deb-src $mirror $codename-updates main universe +deb http://security.ubuntu.com/ubuntu $codename-security main restricted +deb-src http://security.ubuntu.com/ubuntu $codename-security main restricted |