#!/usr/bin/env python
from __future__ import division, print_function

import os
import time
from pprint import pformat

from WMCore.Agent.DefaultConfig import DEFAULT_AGENT_CONFIG
from WMCore.Configuration import loadConfigurationFile
from WMCore.Services.ReqMgrAux.ReqMgrAux import ReqMgrAux


def insertWMAgentConfig(reqMgrAux, agentName, agentConfig):
    print("\nUploading new agent configuration to ReqMgrAux. Data is\n%s" % pformat(agentConfig))
    res = reqMgrAux.postWMAgentConfig(agentName, agentConfig)
    print(res)

def removeWMAgentConfig(reqMgrAux, agentName):
    print("Deleting previous config...")
    reqMgrAux.deleteWMAgentConfig(agentName)
    time.sleep(2)

if __name__ == "__main__":
    wmConfig = loadConfigurationFile(os.environ['WMAGENT_CONFIG'])
    reqMgrAux = ReqMgrAux(wmConfig.General.ReqMgr2ServiceURL)

    removeWMAgentConfig(reqMgrAux, wmConfig.Agent.hostName)
    insertWMAgentConfig(reqMgrAux, wmConfig.Agent.hostName, DEFAULT_AGENT_CONFIG)

    # check whether the document was properly inserted (for some reason,
    # it doesn't get from time to time
    if not reqMgrAux.getWMAgentConfig(wmConfig.Agent.hostName):
        print("Agent config not found, trying to insert it again...")
        insertWMAgentConfig(reqMgrAux, wmConfig.Agent.hostName, DEFAULT_AGENT_CONFIG)
