#!/usr/bin/env python

import sys

from igs.config_manage.policy import *

if len(sys.argv) < 2:
    raise Exception('Must provide start|stop|restart')

if sys.argv[1] == 'start':
    executeTemplate('/opt/hadoop/conf/core-site.xml.tmpl')
    executeTemplate('/opt/hadoop/conf/hdfs-site.xml.tmpl')
    executeTemplate('/opt/hadoop/conf/mapred-site.xml.tmpl')

    dirExists('/mnt/hadoop')
    dirOwner('/mnt/hadoop', 'www-data', 'www-data')
    dirPermissions('/mnt/hadoop', '777', ignoreError=True)
    
    run('/opt/hadoop/bin/hadoop-daemon.sh start datanode')
    run('/opt/hadoop/bin/hadoop-daemon.sh start tasktracker')

elif sys.argv[1] == 'stop':
    run('kill `cat /tmp/hadoop*tasktracker.pid`', ignoreError=True)
    run('kill `cat /tmp/hadoop*datanode.pid`', ignoreError=True)
elif sys.argv[1] == 'restart':
    pass
else:
    raise Exception('Unknown argument: ' + sys.argv[1])
