# Path: orka/orka/utils.py
# def get_user_clusters(token, server_url, choice='clusters'):
#     """
#     Get by default the clusters of the user. If choice argument is different
#     e.g vreservers, returns info of user's VRE servers.
#     """
#     try:
#         escience_token = authenticate_escience(token, server_url)
#     except TypeError:
#         msg = ' Authentication error: Invalid Token'
#         raise ClientError(msg, error_authentication)
#     except Exception, e:
#         logging.error(' ' + str(e.args[0]))
# 
#     payload = {"user": {"id": 1}}
#     orka_request = ClusterRequest(escience_token, server_url, payload, action='login')
#     user_data = orka_request.retrieve()
#     user_clusters = user_data['user']['{0}'.format(choice)]
#     return user_clusters
# 
# def ssh_call_hadoop(user, master_IP, func_arg, hadoop_path=HADOOP_PATH):
#     """
#         SSH to master VM
#         and make Hadoop calls
#     """
#     response = subprocess.call( "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no " + user + "@" + master_IP + " \'" + hadoop_path
#                      + func_arg + "\'", stderr=FNULL, shell=True)
#     
#     return response
# 
# def ssh_check_output_hadoop(user, master_IP, func_arg, hadoop_path=HADOOP_PATH):
#     """
#         SSH to master VM
#         and check output of Hadoop calls
#     """
#     response = subprocess.check_output( "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no " + user + "@" + master_IP + " \'" + hadoop_path
#                      + func_arg + "\'", stderr=FNULL, shell=True).splitlines()
#     
#     return response
# 
# def ssh_stream_to_hadoop(user, master_IP, source_file, dest_dir, hadoop_path=HADOOP_PATH):
#     """
#         SSH to master VM
#         and stream files to hadoop
#     """
#     str_command = "cat " + "\"{0}\"".format(source_file) \
#     + " | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no " \
#     + "{0}@{1} ".format(user,master_IP) \
#     + "\"" + hadoop_path + " dfs -put - " + "\'{0}\'".format(dest_dir) + "\""
#     
#     response = subprocess.call(str_command, stderr=FNULL, shell=True)
# 
#     return response
# 
# Path: orka/orka/cluster_errors_constants.py
# SUCCESS = 0
# REPORT = 25  # Define logging level of REPORT
# SUMMARY = 29  # Define logging level of SUMMARY
# DEFAULT_SSL_VALUE = False
# HADOOP_PATH = '/usr/local/hadoop/bin/hdfs'
# DEFAULT_HDFS_DIR = ['/user/hduser', '/user/hduser/']
# DEFAULT_HADOOP_USER ='hduser'
# FNULL = open(os.devnull, 'w')
# ORKA_IMAGES = {'action':'orka_images','resource_name':'orkaimage'}
# VRE_IMAGES = {'action':'vre_images','resource_name':'vreimage'}
# REPLAY_ACTIONS_PREFIX = 'REPLAY'


import os, sys
import logging
import subprocess
import unittest
import requests
import re
import time
from os.path import join, dirname, abspath
from ConfigParser import RawConfigParser, NoSectionError
from orka.orka.utils import get_user_clusters, ssh_call_hadoop, ssh_check_output_hadoop, ssh_stream_to_hadoop
from constants_of_tests import *
from orka.orka.cluster_errors_constants import error_fatal, const_hadoop_status_started, FNULL
        """
        # stop oozie
        response = subprocess.call( "ssh " + "root" + "@" + self.master_IP + " \"" + 
                                    "service oozieserver stop" + "\""
                                    , stderr=FNULL, shell=True)                
        response = subprocess.call( "ssh " + self.user + "@" + self.master_IP + " \"" + 
                                    "/usr/local/oozie/bin/oozie admin -status -oozie http://" + self.master_IP + ":11000/oozie" + "\""
                                    , stderr=FNULL, shell=True)        
        self.assertEqual(response, 255) # Oozie down
        subprocess.call( "ssh " + "root" + "@" + self.master_IP + " \"" + 
                                    "service oozieserver start" + "\""
                                    , stderr=FNULL, shell=True)
        
    def test_oozie(self):
        """
        Test oozie for Ecosystem cluster
        """
        master_vm_hostname = ssh_check_output_hadoop(self.user, self.master_IP, 'cat /etc/hostname', hadoop_path='')[0]
        read_workflow = open("workflow_ecosystem.xml", "r").read()
        workflow_file = open("workflow_ecosystem.xml", "w")
        workflow_file.write( re.sub("hostname", master_vm_hostname, read_workflow) )
        workflow_file.close()
        ssh_call_hadoop(self.user, self.master_IP, 'dfs -mkdir oozie_app', hadoop_path=self.hdfs_path)
        ssh_stream_to_hadoop(self.user, self.master_IP, join(dirname(abspath(__file__)), "workflow_ecosystem.xml"),
                             self.VALID_DEST_DIR + "/oozie_app/workflow.xml", hadoop_path=self.hdfs_path)
        job_properties = JOB_PROPERTIES_ECOSYSTEM_TEMPLATE.format(master_vm_hostname)

        create_job_properties_file = 'echo -e "{0}" > job.properties'.format(job_properties)
        subprocess.call(create_job_properties_file, stderr=FNULL, shell=True)
        subprocess.call( "scp {0} {1}@{2}:/tmp/".format(JOB_PROPERTIES_PATH, self.user, self.master_IP),

# Path: orka/orka/utils.py
# def get_user_clusters(token, server_url, choice='clusters'):
#     """
#     Get by default the clusters of the user. If choice argument is different
#     e.g vreservers, returns info of user's VRE servers.
#     """
#     try:
#         escience_token = authenticate_escience(token, server_url)
#     except TypeError:
#         msg = ' Authentication error: Invalid Token'
#         raise ClientError(msg, error_authentication)
#     except Exception, e:
#         logging.error(' ' + str(e.args[0]))
# 
#     payload = {"user": {"id": 1}}
#     orka_request = ClusterRequest(escience_token, server_url, payload, action='login')
#     user_data = orka_request.retrieve()
#     user_clusters = user_data['user']['{0}'.format(choice)]
#     return user_clusters
# 
# def ssh_call_hadoop(user, master_IP, func_arg, hadoop_path=HADOOP_PATH):
#     """
#         SSH to master VM
#         and make Hadoop calls
#     """
#     response = subprocess.call( "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no " + user + "@" + master_IP + " \'" + hadoop_path
#                      + func_arg + "\'", stderr=FNULL, shell=True)
#     
#     return response
# 
# def ssh_check_output_hadoop(user, master_IP, func_arg, hadoop_path=HADOOP_PATH):
#     """
#         SSH to master VM
#         and check output of Hadoop calls
#     """
#     response = subprocess.check_output( "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no " + user + "@" + master_IP + " \'" + hadoop_path
#                      + func_arg + "\'", stderr=FNULL, shell=True).splitlines()
#     
#     return response
# 
# def ssh_stream_to_hadoop(user, master_IP, source_file, dest_dir, hadoop_path=HADOOP_PATH):
#     """
#         SSH to master VM
#         and stream files to hadoop
#     """
#     str_command = "cat " + "\"{0}\"".format(source_file) \
#     + " | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no " \
#     + "{0}@{1} ".format(user,master_IP) \
#     + "\"" + hadoop_path + " dfs -put - " + "\'{0}\'".format(dest_dir) + "\""
#     
#     response = subprocess.call(str_command, stderr=FNULL, shell=True)
# 
#     return response
# 
# Path: orka/orka/cluster_errors_constants.py
# SUCCESS = 0
# REPORT = 25  # Define logging level of REPORT
# SUMMARY = 29  # Define logging level of SUMMARY
# DEFAULT_SSL_VALUE = False
# HADOOP_PATH = '/usr/local/hadoop/bin/hdfs'
# DEFAULT_HDFS_DIR = ['/user/hduser', '/user/hduser/']
# DEFAULT_HADOOP_USER ='hduser'
# FNULL = open(os.devnull, 'w')
# ORKA_IMAGES = {'action':'orka_images','resource_name':'orkaimage'}
# VRE_IMAGES = {'action':'vre_images','resource_name':'vreimage'}
# REPLAY_ACTIONS_PREFIX = 'REPLAY'


# Path: tests/test_ecosystem_components.py
import os, sys
import logging
import subprocess
import unittest
import requests
import re
import time
from os.path import join, dirname, abspath
from ConfigParser import RawConfigParser, NoSectionError
from orka.orka.utils import get_user_clusters, ssh_call_hadoop, ssh_check_output_hadoop, ssh_stream_to_hadoop
from constants_of_tests import *
from orka.orka.cluster_errors_constants import error_fatal, const_hadoop_status_started, FNULL

        """
        # stop oozie
        response = subprocess.call( "ssh " + "root" + "@" + self.master_IP + " \"" + 
                                    "service oozieserver stop" + "\""
                                    , stderr=FNULL, shell=True)                
        response = subprocess.call( "ssh " + self.user + "@" + self.master_IP + " \"" + 
                                    "/usr/local/oozie/bin/oozie admin -status -oozie http://" + self.master_IP + ":11000/oozie" + "\""
                                    , stderr=FNULL, shell=True)        
        self.assertEqual(response, 255) # Oozie down
        subprocess.call( "ssh " + "root" + "@" + self.master_IP + " \"" + 
                                    "service oozieserver start" + "\""
                                    , stderr=FNULL, shell=True)
        
    def test_oozie(self):
        """
        Test oozie for Ecosystem cluster
        """
        master_vm_hostname = ssh_check_output_hadoop(self.user, self.master_IP, 'cat /etc/hostname', hadoop_path='')[0]
        read_workflow = open("workflow_ecosystem.xml", "r").read()
        workflow_file = open("workflow_ecosystem.xml", "w")
        workflow_file.write( re.sub("hostname", master_vm_hostname, read_workflow) )
        workflow_file.close()
        ssh_call_hadoop(self.user, self.master_IP, 'dfs -mkdir oozie_app', hadoop_path=self.hdfs_path)
        ssh_stream_to_hadoop(self.user, self.master_IP, join(dirname(abspath(__file__)), "workflow_ecosystem.xml"),
                             self.VALID_DEST_DIR + "/oozie_app/workflow.xml", hadoop_path=self.hdfs_path)
        job_properties = JOB_PROPERTIES_ECOSYSTEM_TEMPLATE.format(master_vm_hostname)

        create_job_properties_file = 'echo -e "{0}" > job.properties'.format(job_properties)
        subprocess.call(create_job_properties_file, stderr=FNULL, shell=True)
        subprocess.call( "scp {0} {1}@{2}:/tmp/".format(JOB_PROPERTIES_PATH, self.user, self.master_IP),

                         stderr=FNULL, shell=True)