#!/usr/bin/env python2.6

# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright (c) 2011 Citrix Systems, 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.

import xmlrpclib
import os
import socket
import sys

# Set connection timeout to 30 secs
socket.setdefaulttimeout(30)

os.environ['PYTHON_EGG_CACHE'] = '/tmp/puppet'
# Set django settings as it's required by service_proxy
os.environ['DJANGO_SETTINGS_MODULE'] = 'geppetto.settings'

from geppetto.core.views import service_proxy


if __name__ == "__main__":
    if(len(sys.argv) == 2):
        try:
            node_name = sys.argv[1]
            proxy = service_proxy.create_proxy('127.0.0.1', 8080,
                                               service_proxy.Proxy.Classifier)
            print proxy.get_configuration(node_name.lower())
            sys.exit(0)
        except xmlrpclib.ProtocolError, err:
            print >> sys.stderr, "A protocol error occurred"
            print >> sys.stderr, "URL: %s" % err.url
            print >> sys.stderr, "HTTP/HTTPS headers: %s" % err.headers
            print >> sys.stderr, "Error code: %d" % err.errcode
            print >> sys.stderr, "Error message: %s" % err.errmsg
            sys.exit(1)
    else:
        print "You must supply a hostname as the first argument of this script"
        sys.exit(2)
