Пример #1
0
 def _config(self):
     """Get the endpoint configuration"""
     self.endpoint = "%s/api/%s/" % (self.baseurl, self.v)
     if reclient_config.get('use_kerberos', False):
         out.debug('Using kerberos per configuration.')
         self.connector = Connectors({
             "auth": HTTPKerberosAuth(mutual_authentication=OPTIONAL),
             "baseurl": self.endpoint,
         },
             format=self.format)
     else:
         out.debug('Using HTTPAuth per configuration.')
         self.connector = Connectors({
             "auth": (reclient_config['username'], getpass.getpass()),
             "baseurl": self.endpoint,
         },
             format=self.format)
Пример #2
0
class ReClient(object):
    def __init__(self, version='v0', debug=1, format='yaml'):
        self.v = version
        self.debug = debug
        self.baseurl = reclient_config['baseurl']
        self.format = format
        self._config()

    def _config(self):
        """Get the endpoint configuration"""
        self.endpoint = "%s/api/%s/" % (self.baseurl, self.v)
        if reclient_config.get('use_kerberos', False):
            out.debug('Using kerberos per configuration.')
            self.connector = Connectors({
                "auth": HTTPKerberosAuth(mutual_authentication=OPTIONAL),
                "baseurl": self.endpoint,
            },
                format=self.format)
        else:
            out.debug('Using HTTPAuth per configuration.')
            self.connector = Connectors({
                "auth": (reclient_config['username'], getpass.getpass()),
                "baseurl": self.endpoint,
            },
                format=self.format)

    def _get_playbook(self, project, pb_id=None):
        """project - name of the project to search for playbook with id
'pb_id'. Omit pb_id and you get all playbooks for 'project'.

Return a two-tuple of the serialized datastructure, as well as a
reference to the tempfile.NamedTemporaryFile object it has been
written out to.
        """
        if pb_id is None:
            # Get all playbooks for 'project'
            suffix = "%s/playbook/" % project
            key = "items"
        else:
            # Get a single playbook
            suffix = "%s/playbook/%s/" % (project, pb_id)
            key = "item"

        result = self.connector.get(suffix)
        if result.status_code == 200:
            pb_blob = reclient.utils.deserialize(result.content, self.format)[key]
            # Write it out to a temporary file
            pb_fp = reclient.utils.temp_blob(pb_blob, self.format)

            return (pb_blob, pb_fp)
        else:
            raise ReClientGETError(result)

    def _send_playbook(self, project, pb_fp, pb_id=None):
        """Send a playbook to the REST endpoint. Note the ordering of the
args/kwargs as they're ordered differently than most other methods.

`pb_fp` - File pointer to a playbook file
`pb_id` - OPTIONAL - if 'None' then this is interpreted as a NEW
playbook. If not 'None' then this is interpreted as an update to an
existing playbook.
        """
        if pb_id:
            # UPDATE
            print "Updating an existing playbook"
            suffix = "%s/playbook/%s/" % (project, pb_id)
            with open(pb_fp.name, 'r') as pb_open:
                result = self.connector.post(
                    suffix, data=pb_open)
        else:
            # NET-NEW
            print "Sending a new playbook"
            suffix = "%s/playbook/" % project
            with open(pb_fp.name, 'r') as pb_open:
                result = self.connector.put(
                    suffix, data=pb_open)

        code = result.status_code
        # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
        if code == 200:
            # 200 = "OK" - Updated a playbook
            print "[%d] Updated playbook" % code
        elif code == 201:
            # 201 = "Created";
            print "[%d] Created new playbook" % code
        else:
            print "[%d] Unexpected response from rerest endpoint" % (
                code)
            raise ReClientSendError(result)

        return result

    def get_all_playbooks_ever(self):
        """Get ALL THE PLAYBOOKS"""
        suffix = "playbooks/"
        result = self.connector.get(suffix)
        try:
            response_msg = reclient.utils.deserialize(result.content, self.format)
            if response_msg['status'] == 'error':
                print colorize(
                    "Error while fetching all playbooks", color="red",
                    background="lightgray")
                print colorize(
                    "%s - %s" % (str(result), response_msg), color="red",
                    background="lightgray")
                raise ReClientGETError(result)
        except Exception:
            return False
        else:
            view_file = reclient.utils.temp_blob(result, self.format)
            reclient.utils.less_file(view_file.name)

    def get_all_playbooks(self, project):
        """
        Get all playbooks that match `project`
        """
        try:
            (path, pb_fp) = self._get_playbook(project)
        except ReClientGETError, e:
            response_msg = reclient.utils.deserialize(e.args[0], self.format)
            print colorize("Error while fetching playbooks for %s:" % project,
                           color="red",
                           background="lightgray")
            print colorize(
                "%s - %s" % (
                    str(e),
                    response_msg['message']),
                color="red", background="lightgray")
        else: