# Copyright 2011 Gilt Groupe, 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.
"""
cobbler functionality
"""

import os
import re
import sys
import types
import xmlrpclib
import subprocess
import mothership.validate
import mothership.network_mapper
from mothership.transkey import transkey

class CobblerAPI:
    def __init__(self, cfg, site_id=None):

        # transkey map for cobbler interface to mothership hardware
        self.map_hardware = {
            'power_address': 'power_switch',
#            'hw_tag'       : 'hw_tag',
            }

        # transkey map for cobbler interface to mothership servers
        self.map_server = {
            'name'          : 'hostname',
            'profile'       : 'cobbler_profile',
            'virtual'       : 'virtual',
            'virt_cpus'     : 'cores',
            'virt_ram'      : 'ram',
            'virt_file_size': 'disk',
            'virt_path'     : 'storage',
#            'site_id'      : 'site_id',
#            'realm'        : 'realm',
#            'hw_tag'       : 'hw_tag',
            }

        # transkey map for cobbler interface to mothership network
        self.map_network = {
            'mac_address'  : 'mac',
            'dhcp_tag'     : 'vlan',
            'subnet'       : 'netmask',
            'ip_address'   : 'ip',
            'bonding_opts' : 'bond_options',
            'static_route' : 'static_route',
            'interface'    : 'interface',
            'dns_name'     : 'dns',
#            'server_id'    : 'server_id',
#            'site_id'      : 'site_id',
#            'realm'        : 'realm',
#            'hw_tag'       : 'hw_tag',
            }

        # transkey map for mothership to cobbler interface
        self.map_interface = {
            'mac'           : 'macaddress',
            'ip'            : 'ipaddress',
            'vlan'          : 'dhcptag',
            'netmask'       : 'subnet',
            'dns_name'      : 'dnsname',
            'bonding'       : 'bonding',
            'bond_master'   : 'bondingmaster',
            'bond_options'  : 'bondingopts',
            'static'        : 'static',
            'static_routes' : 'staticroutes',
            }

        # transkey map for mothership to cobbler system
        self.map_system =  {
            'hostname'            :'hostname',
            'netboot_enabled'     : False,
            'power_type'          : 'power_type',
            'power_switch'        : 'power_address',
            'power_port'          : 'power_id',
            'name'                : [ 'hostname', 'realm', 'site_id' ],
            'cobbler_profile'     : 'profile',
            'virtual'             : 'virtual',
            'cores'               : 'virt_cpus',
            'ram'                 : 'virt_ram',
            'disk'                : 'virt_file_size',
            'storage'             : 'virt_path',
            'kernel_options_post' : 'crashkernel=128M@16M',
#            'gateway'             : 'gateway',
#            '' :'comment',
#            '' :'ctime',
#            '' :'depth',
#            '' :'kernel_options',
#            '' :'kernel_options_post',
#            '' :'ks_meta',
#            '' :'mgmt_classes',
#            '' :'mtime',
#            '' :'name_servers',
#            '' :'name_servers_search',
#            '' :'owners',
#            '' :'template_remote_kickstarts',
#            '' :'uid',
            }

        # set up primary cobbler control
        self.cfg = cfg
        self.coblive = cfg.coblive
        self.cobremote = None
        self.cobtoken = None
        if self.coblive:
            try:
                self.cobremote = xmlrpclib.Server('http://%s/cobbler_api' % cfg.cobconfig['host'])
                self.cobtoken = self.cobremote.login(cfg.cobconfig['user'], cfg.cobconfig['pass'])
            except:
                sys.stderr.write('Cobbler configuration error.  Check cobbler API server')