Amazon EC2 Pattern

This provides defines for some Amazon EC2 tools.

# ec2utils: puppet recipes for use with Amazon's Elastic Compute Cloud (and
#           associated services)
#
# Copyright 2009 Flock, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

class ec2utils {

    # This must include the path to the Amazon EC2 tools
    $ec2path = ["/usr/bin", "/bin", "/usr/sbin", "/sbin",
                "/opt/ec2/ec2-api-tools/bin",
                "/opt/ec2/aws-elb-tools/bin"]

    $ec2env  = ["JAVA_HOME=/usr/java/default",
                "EC2_PRIVATE_KEY=/path/to/private-key.pem",
                "EC2_CERT=/path/to/certificate.pem",
                "EC2_HOME=/opt/ec2/ec2-api-tools",
                "AWS_ELB_HOME=/opt/ec2/aws-elb-tools"]


    define elasticip ($instanceid, $ip)
    {
        exec { "ec2-associate-address-$name":
            logoutput   => on_failure,
            environment => $ec2utils::ec2env,
            path        => $ec2utils::ec2path,
            command     => "ec2assocaddr $ip -i $instanceid",
            # Only do this when necessary
            unless => "test `ec2daddr $ip | awk '{print \$3}'` == $instanceid",
        }
    }

    define ebsvolume ($instanceid, $volumeid, $devicetomount,
                      $mountpoint = "/mnt", $owner = "root", $group = "root",
                      $mode = "755", $fstype = "ext3",
                      $mountoptions = "defaults")
    {
        exec { "ec2-attach-volume-$name":
            logoutput   => on_failure,
            environment => $ec2utils::ec2env,
            path        => $ec2utils::ec2path,
            command     => "ec2attvol $volumeid \
                                      -i $instanceid \
                                      -d $devicetomount
                            until [ -b $devicetomount ] ; do sleep 2 ; done",
            # Only do this when necessary
            unless      => "test -b $devicetomount",
            before      => Mount["$mountpoint"],
        }

        file { "$mountpoint":
            ensure  => directory,
            owner   => $owner,
            group   => $group,
            mode    => $mode,
        }

        mount { "$mountpoint":
            device  => $devicetomount,
            ensure  => mounted,
            fstype  => $fstype,
            options => $mountoptions,
            require => [ Exec["ec2-attach-volume-$name"],
                         File["$mountpoint"]
            ],
        }
    }

    define loadbalancer ($instanceid, $lbname)
    {
        exec { "elb-register-instances-with-lb-$name":
            logoutput   => on_failure,
            environment => $ec2utils::ec2env,
            path        => $ec2utils::ec2path,
            command     => "elb-register-instances-with-lb $lbname \
                                --instances $instanceid",
            # Only do this when necessary
            unless => "elb-describe-instance-health $lbname | \
                       grep $instanceid",
        }
    }

}

# vim: nowrap sw=4 sts=4 ts=8 tw=80 ff=unix ft=ruby expandtab: