def main(argv=None):
    if argv is None:
        argv = sys.argv
    try:
        try:
            opts, args = getopt.getopt(argv[1:], "h", ["help", "size=", "image=", "output_ip=", 'branch='])
        except getopt.error, msg:
             raise Usage(msg)
        # process options
        size = None
        image = None
        branch = 'master'
        output_ip = None

        for o, a in opts:
            if o in ("-h", "--help"):
                print __doc__
                return 0;
            if o in ("--size"):
                size = a
            if o in ("--image"):
                image = a
            if o in ("--output_ip"):
                output_ip = a
            if o in ("--branch"):
                branch = a


        # Check the command line options
        if size is None:
            raise Usage, "the --size option must be specified"
        if image is None:
            raise Usage, "the --image option must be specified"
        
        try:
            # Create a server with the specified image and size
            print 'Creating the server. This may take a minute or two...'
            print ""
            node = cm_libcloud.create_server(image, size)
            
            print_node_details(node)
    
            # Use Fabric to install mercury
            install_pergola(node, branch)
    
            # Finally, reboot the node
            print 'Rebooting the node...'
            cm_libcloud.reboot_node(node)
            print 'done'
            
            print_node_details(node)
    
            if output_ip is not None:
                with open(output_ip, 'w') as f:
                    f.write(node.public_ip[0])
            
        except:
            print "===> Test failure"
            raise
        finally:
            print "===> Destroying this node"
            cm_libcloud.destroy_node(node)

        return 0
def main(argv=None):
    if argv is None:
        argv = sys.argv
    try:
        try:
            opts, args = getopt.getopt(
                argv[1:],
                "h",
                [
                    "help",
                    "size=",
                    "image=",
                    "enable_apache=",
                    "enable_mysql=",
                    "enable_jenkins=",
                    "enable_memcache=",
                    "enable_tomcat=",
                    "output_ip=",
                ],
            )
        except getopt.error, msg:
            raise Usage(msg)
        # process options
        size = None
        image = None
        enable_apache = False
        enable_mysql = False
        enable_jenkins = False
        enable_memcache = False
        enable_tomcat = False
        output_ip = None

        for o, a in opts:
            if o in ("-h", "--help"):
                print __doc__
                return 0
            if o in ("--size"):
                size = a
            if o in ("--image"):
                image = a
            if o in ("--enable_apache"):
                if a.lower() in ("1", "true"):
                    enable_apache = True
            if o in ("--enable_mysql"):
                if a.lower() in ("1", "true"):
                    enable_mysql = True
            if o in ("--enable_jenkins"):
                if a.lower() in ("1", "true"):
                    enable_jenkins = True
            if o in ("--enable_memcache"):
                if a.lower() in ("1", "true"):
                    enable_memcache = True
            if o in ("--enable_tomcat"):
                if a.lower() in ("1", "true"):
                    enable_tomcat = True
            if o in ("--output_ip"):
                output_ip = a

        # Check the command line options
        if size is None:
            raise Usage, "the --size option must be specified"
        if image is None:
            raise Usage, "the --image option must be specified"

        # Create a server with the specified image and size
        print "Creating the server. This may take a minute or two..."
        print ""
        node = cm_libcloud.create_server(image, size)

        print_node_details(node)

        # Use Fabric to install mercury
        install_mercury(node)

        # Ensure that the correct services are present
        if enable_apache is False:
            disable_apache(node)
        if enable_mysql is False:
            disable_mysql(node)
        if enable_jenkins is False:
            disable_jenkins(node)
        if enable_memcache is False:
            disable_memcache(node)
        if enable_tomcat is False:
            disable_tomcat(node)

        # Disable mercury
        disable_mercury(node)

        # Finally, reboot the node
        print "Rebooting the node..."
        cm_libcloud.reboot_node(node)
        print "done"

        print_node_details(node)

        if output_ip is not None:
            with open(output_ip, "w") as f:
                f.write(node.public_ip[0])

        return 0