summaryrefslogtreecommitdiffstats
path: root/lib/cgi
diff options
context:
space:
mode:
authorttate <ttate@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-07-20 18:22:03 +0000
committerttate <ttate@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-07-20 18:22:03 +0000
commitba779c751faf35e098939fb4701e94e22d79470e (patch)
tree9b5ed624733486797e808144bf3c82c530ea67c2 /lib/cgi
parent09cc6c6947289d4bcdaba7d82c6151956f245747 (diff)
downloadruby-ba779c751faf35e098939fb4701e94e22d79470e.tar.gz
ruby-ba779c751faf35e098939fb4701e94e22d79470e.tar.xz
ruby-ba779c751faf35e098939fb4701e94e22d79470e.zip
bugfix for Importable.callback().
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@6680 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/cgi')
0 files changed, 0 insertions, 0 deletions
5 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
#!/usr/bin/python

##
## func command line interface & client lib
##
## Copyright 2007, Red Hat, Inc
## Michael DeHaan <mdehaan@redhat.com>
## +AUTHORS
##
## This software may be freely redistributed under the terms of the GNU
## general public license.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
##

import optparse
import sys
import glob
import pprint

from func.commonconfig import CMConfig
from func.config import read_config, CONFIG_FILE

import sslclient

import command

# ===================================
# defaults
# TO DO: some of this may want to come from config later

DEFAULT_PORT = 51234
FUNC_USAGE = "Usage: %s [ --help ] [ --verbose ] target.example.org module method arg1 [...]"

# ===================================

class CommandAutomagic(object):
    """
    This allows a client object to act as if it were one machine, when in
    reality it represents many.
    """

    def __init__(self, clientref, base):
        self.base = base
        self.clientref = clientref

    def __getattr__(self,name):
        base2 = self.base[:]
        base2.append(name)
        return CommandAutomagic(self.clientref, base2)

    def __call__(self, *args):
        if not self.base:
            raise AttributeError("something wrong here")
        if len(self.base) < 2:
            raise AttributeError("no method called: %s" % ".".join(self.base))
        module = self.base[0]
        method = ".".join(self.base[1:])
        return self.clientref.run(module,method,args)

# ===================================



# this is a module level def so we can use it and isServer() from
# other modules with a Client class
def expand_servers(spec, port=51234, noglobs=None, verbose=None, just_fqdns=False):
    config  = read_config(CONFIG_FILE, CMConfig)
    """
    Given a regex/blob of servers, expand to a list
    of server ids.
    """

    if noglobs:
        if not just_fqdns:
            return [ "https://%s:%s" % (spec, port) ]
        else:
            return spec

    all_hosts = []
    all_certs = []
    seperate_gloobs = spec.split(";")
    for each_gloob in seperate_gloobs:
        actual_gloob = "%s/%s.cert" % (config.certroot, each_gloob)
        certs = glob.glob(actual_gloob)
        for cert in certs:
            all_certs.append(cert)
            host = cert.replace(config.certroot,"")[1:-5]
            all_hosts.append(host)

    all_urls = []
    for x in all_hosts:
        if not just_fqdns:
            all_urls.append("https://%s:%s" % (x, port))
        else:
            all_urls.append(x)

    if verbose and len(all_urls) == 0:
        sys.stderr.write("no hosts matched\n")

    return all_urls

# does the hostnamegoo actually expand to anything?
def isServer(server_string):
    servers = expand_servers(server_string)
    if len(servers) > 0:
        return True
    return False

class Client(object):

    def __init__(self, server_spec, port=DEFAULT_PORT, interactive=False,
        verbose=False, noglobs=False, config=None):
        """
        Constructor.
        @server_spec -- something like "*.example.org" or "foosball"
        @port -- is the port where all funcd processes should be contacted
        @verbose -- whether to print unneccessary things
        @noglobs -- specifies server_spec is not a glob, and run should return single values
        @config -- optional config object
        """
        self.config  = config
        if config is None:
            self.config  = read_config(CONFIG_FILE, CMConfig)

        self.server_spec = server_spec
        self.port        = port
        self.verbose     = verbose
        self.interactive = interactive
        self.noglobs     = noglobs
        self.servers     = expand_servers(self.server_spec,port=self.port,
                                          noglobs=self.noglobs,verbose=self.verbose)

        # default cert/ca/key is the same as the certmaster ca - need to
        # be able to change that on the cli
        self.key = '%s/funcmaster.key' % self.config.cadir
        self.cert = '%s/funcmaster.crt' % self.config.cadir
        # yes, they're the same, that's the point
        self.ca = '%s/funcmaster.crt' % self.config.cadir


    def __getattr__(self, name):
        """
        This getattr allows manipulation of the object as if it were
        a XMLRPC handle to a single machine, when in reality it is a handle
        to an unspecified number of machines.

        So, it enables stuff like this:

        Client("*.example.org").yum.install("foo")

        # WARNING: any missing values in Client's source will yield
        # strange errors with this engaged.  Be aware of that.
        """

        return CommandAutomagic(self, [name])