#coding:utf-8
import os
import sys
import string
import sunhpc
from sunhpc.db.mappings.base import *
class command(sunhpc.commands.HostArgumentProcessor, sunhpc.commands.add.command):
    pass
    
class Command(command):
    """
    Add an new host to the cluster.

    <arg type='string' name='host'>
    A single host name.  If the hostname is of the standard form of
    basename-rack-rank the default values for the membership, rack,
    and rank parameters are taken from the hostname.
    </arg>

    <param type='int' name='cpus'>
    Number of CPUs (cores) in the given host.  If not provided the
    default of 1 CPU is inserted into the database.
    </param>

    <param type='string' name='membership'>
    Appliance membership name.  If not provided and the host name is of
    the standard form the membership is taken from the basename of 
    the host.
    </param>

    <param type='int' name='rack'>
    The number of the rack where the machine is located. The convention
    in Rocks is to start numbering at 0. If not provided and the host
    name is of the standard form the rack number is taken from the host
    name.
    </param>

    <param type='int' name='rank'>
    The position of the machine in the rack. The convention in Rocks
    is to number from the bottom of the rack to the top starting at 0.
    If not provided and the host name is of the standard form the rank
    number is taken from the host name.
    </param>

    <param type='string' name='os'>
    The operating system name. The default is: linux.
    </param>

    <example cmd='add host compute-0-1'>
    Adds the host "compute-0-0" to the database with 1 CPU, a membership
    name of "compute", a rack number of 0, and rank of 1.
    </example>

    <example cmd='add host frontend rack=0 rank=0 membership=Frontend'>
    Adds the host "frontend" to the database with 1 CPU, a membership name
    of "Frontend", a rack number of 0, and rank of 1.
    </example>

    <related>add host interface</related>

    """

    def run(self, params, args):
    
        if len(args) != 1:
            self.abort('must supply one host')
        host = args[0]
        
        self.newdb.checkHostnameValidity(host)
        self.newdb.commit()

        s = self.newdb.getSession()
        try:
            basename, rack, rank = host.split('-')
            rack = int(rack)
            rank = int(rank)
        except:
            rack = None
            rank = None
            
        (numCPUs, rack, rank, osname) = self.fillParams([
            ('cpus', 1),
            ('rack', rack),
            ('rank', rank),
            ('os', None) ])

        if rack == None:
            self.abort('rack not specified')
        if rank == None:
            self.abort('rank not specified')

        if osname is None:
            osname = 'linux'

        n = Node(name=host, cpus=int(numCPUs), rack=int(rack), rank=int(rank), os=osname)
        s.add(n)
        
        self.newdb.commit()
        next_server = self.db.getHostAttr('localhost', 'Kickstart_PrivateAddress')

        self.newdb.setNewHostAttr(host, 'os', osname)
        self.newdb.setNewHostAttr(host, 'kickstartable', 'yes')
        self.newdb.setNewHostAttr(host, 'dhcp_filename', 'pxelinux.0')
        self.newdb.setNewHostAttr(host, 'dhcp_nextserver', next_server)
        self.newdb.setNewHostAttr(host, 'managed', 'false')

RollName = "base"

















