Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.7 kB
1
Indexable
Never
def restart_services(i, vm_details):
    vm_id = i
    vm_details = get_vm_details()
    vm_ip = vm_details[i][i]['vm_ip'][0]
    username = vm_details[i][i]['vm_username'][0]
    Services = ['CP-AGENT-DEV-SAP-COLLECTOR-MS.service','CP-AGENT-DEV-SAP-PERFORMER-MS.service','CP-AGENT-DEV-VM-COLLECTOR-MS.service']
    os.chdir('/etc/systemd/system/')
    print(os.getcwd())
    logging.info("OS CWD %s", os.getcwd)
    for i in Services:
        service_name = i
        subprocess.run(["sudo", "systemctl", "stop", service_name])
        subprocess.run(["sudo", "systemctl", "start", service_name])
        subprocess.run(["sudo", "systemctl", "status", service_name])
    print("Services have been restarted...")
    logging.info("Services have been restarted...")
    print("Notifying the support team on password reset, update in agent.properties, and restart of services...")
    logging.info("Notifying the support team on password reset, update in agent.properties, and restart of services...")
    #notify_support_team(vm_id,vm_ip,username)
    return None


Here notify_support_team(vm_id,vm_ip,username) function I want to call from B.py and then again come back in A.py

This is code for notify_support_team

def notify_support_team(vm_id,vm_ip,username):
    config = param_config(configdata=True,subject='CAPE Agent')
    cape_agent = config.agent_ip
    vm_id = vm_id
    vm_ip = vm_ip
    vm_username = username
    to = 'DL.CE.FR.TCS.INFRA@sodexo.com'
    #cc = 'Himanshu.Pandey.ext@sodexo.com'
    name = 'CAPE_ADMIN'
    #from_ = 'TCSCAPEAGENT@capeagent.uyhfzyzna0iuzi1pcljxyflv4c.fx.internal.cloudapp.net'
    subject = 'Test Mail for VM User Password Reset'+" on CAPE Agent server - "+cape_agent
    message1='Password was reset for VM User-'+vm_username+' with VM_ID-'+str(vm_id)+' and VM_IP-'+vm_ip+'.\n'
    message2='New password was updated in agent.properties file on CAPE Agent-'+cape_agent+'.'+' Also, the SAP Services were restarted.'
    message = message1+"\n"+message2
    body = 'Dear Team,\n\n'+message+'\n\nRegards,\nCAPE ADMIN.'
    #body = 'This is a test email sent from a Python script.'
    command = f"echo '{body}' | mutt -e 'my_hdr From:{name} <{from_}>' -s '{subject}' {to}"   ## Changes made from here
    try:

        result = subprocess.run(command, shell=True, capture_output=True, text=True)
        print("Command output:", result.stdout)
        print("Command error:", result.stderr)
    except Exception as e:

        print("Exception raised:", e)
        logging.error("Exception raised: %s", e)
    return None


Can you help in calling this function notify_support_team from different .py file.