start_agent.py 3.09 KB
#!/usr/bin/env python

import os
import sys

from django.conf import settings as djangosettings

AGENTS = {
    "agentX" : "AgentX", 
    "agentA" : "AgentA",
    "agentM" : "AgentM",
    "webserver" : "webserver", 
    "monitoring" : "monitoring", 
    "majordome" : "majordome", 
    "scheduler" : "scheduler", 
    "alert_manager" : "alert_manager"
}

configfile = None

if len(sys.argv) < 2:
    print("Error: you must give an agent name to start")
    exit(1)

# arg 1 : agent name
agent_name = sys.argv[1]

# arg 2 : config file
if len(sys.argv) == 3:
    configfile = sys.argv[2]
    
# Conseil sur le net:
#https://stackoverflow.com/questions/16853649/how-to-execute-a-python-script-from-the-django-shell
"""
import sys, os
sys.path.append('/path/to/your/django/app')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from django.conf import settings
"""


"""
TODO:
- communiquer avec BD
- lire config XML
- gestion des logs, fich texte, 1/jour, mode 30 last lines, + BD, stdout/stderr 
- thread de run
- thread de wrapper
- gestion des commandes dans le run
- table dediee à l agent, infos dynamiques volatiles
"""


# To avoid a "ModuleNotFoundError: No module named 'dashboard'"... (not even 1 app found) :
##sys.path.insert(0, os.path.abspath(".."))
##sys.path.insert(0, os.path.abspath("src"))
##sys.path.insert(0, "../src")
##sys.path.insert(0, "src")
# To avoid a "ModuleNotFoundError: No module named 'src'"
##sys.path.append("..")
sys.path.append("src")
print("Starting with this sys.path", sys.path)

# DJANGO setup
# print("file is", __file__)
# mypath = os.getcwd()
# Go into src/
##os.chdir("..")
##os.chdir("src")
print("Current directory : " + str(os.getcwd()))

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "src.pyros.settings")
# os.environ['SECRET_KEY'] = 'abc'
# os.environ['ENVIRONMENT'] = 'production'
import django

django.setup()

print("DB2 used is:", djangosettings.DATABASES["default"]["NAME"])
print()


if agent_name not in AGENTS: 
    print("Error: this agent does not exists")
    sys.exit(1)

if agent_name == "majordome":
    from src.majordome.tasks import Majordome
    Majordome().run(FOR_REAL=True)
    sys.exit(0)

if agent_name == "monitoring":
    from src.monitoring.tasks import Monitoring
    Monitoring().run()
    sys.exit(0)

if agent_name == "agentA":
    from src.agent.AgentA import AgentA
    agentA = AgentA(name="agentA", config_filename=configfile)
    # Run agent without actual commands sent to devices (FOR_REAL=False)
    agentA.run(FOR_REAL=True)
    sys.exit(0)

if agent_name == "agentM":
    from src.monitoring.AgentM import AgentM
    agentM = AgentM(name="agentM", config_filename=configfile)
    # Run agent without actual commands sent to devices (FOR_REAL=False)
    agentM.run(FOR_REAL=True)
    sys.exit(0)

"""
from src.agent.Agent import Agent
agent = Agent(name="agent", config_filename=configfile)
agent.run(FOR_REAL=True)
"""

# Default agent is AgentX

from src.agent.AgentX import AgentX
# AgentX().run(FOR_REAL=False)
agentX = AgentX(name="agentX", config_filename=configfile)

# Run agent without actual commands sent to devices (FOR_REAL=False)
agentX.run(FOR_REAL=True)