#!/usr/bin/python
#
# Copyright (c) 2014-2016 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#

# The following environment Variables are set,
#    NOTIFICATION_SEQNUM=<unsigned long string>
#    SERVICE_GROUP_AGGREGATE_NAME=<service-aggregate-name>
#    SERVICE_GROUP_AGGREGATE_DESIRED_STATE=<service-group-state>
#    SERVICE_GROUP_AGGREGATE_STATE=<service-group-state>
#
#    SERVICE_GROUP_NAME=<service-group-name>
#    SERVICE_GROUP_DESIRED_STATE=<service-group-state>
#    SERVICE_GROUP_STATE=<service-group-state>
#    SERVICE_GROUP_NOTIFICATION=<service-group-notification>
#
#    where service-group-state is one of:
#        unknown, standby, go-standby, go-active, active, disabling,
#        disabled, shutdown
#
#    where service-group-notification is one of:
#        unknown, standby, go-standby, go-active, active, disabling,
#        disabled, shutdown
#
import six
import os
import sys
import subprocess
import datetime
import tsconfig.tsconfig as tsconfig


def get_time():
    return datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")


def main():
    want_log_file = False

    notification_seqnum = os.environ.get('NOTIFICATION_SEQNUM', '-1')
    sg_aggregate_name = os.environ.get('SERVICE_GROUP_AGGREGATE_NAME', '')
    sg_aggregate_desired_state = \
        os.environ.get('SERVICE_GROUP_AGGREGATE_DESIRED_STATE', '')
    sg_aggregate_state = os.environ.get('SERVICE_GROUP_AGGREGATE_STATE', '')

    sg_name = os.environ.get('SERVICE_GROUP_NAME', '')
    sg_desired_state = os.environ.get('SERVICE_GROUP_DESIRED_STATE', '')
    sg_state = os.environ.get('SERVICE_GROUP_STATE', '')
    sg_notification = os.environ.get('SERVICE_GROUP_NOTIFICATION', '')

    sm_log_file = "/tmp/sm-notification.log"

    if want_log_file:
        with open(sm_log_file, 'a') as f:
            six.print_("=======================================", file=f)
            six.print_("           TIME: %s" % get_time(), file=f)
            six.print_("SEQUENCE_NUMBER: %s" % notification_seqnum, file=f)
            six.print_("", file=f)

            if 0 < len(sg_aggregate_name):
                six.print_("SERVICE_GROUP_AGGREGATE", file=f)
                six.print_("           name: %s" % sg_aggregate_name, file=f)
                six.print_("  desired_state: %s" % sg_aggregate_desired_state,
                           file=f)
                six.print_("          state: %s" % sg_aggregate_state, file=f)
                six.print_("", file=f)

            six.print_("SERVICE_GROUP", file=f)
            six.print_("           name: %s" % sg_name, file=f)
            six.print_("  desired_state: %s" % sg_desired_state, file=f)
            six.print_("          state: %s" % sg_state, file=f)
            six.print_("   notification: %s" % sg_notification, file=f)

    # Script to start/stop worker services. Called here for CPE upgrade
    # support. Scripts will be run in a separate process so it does not
    # block sm.
    if 'worker' in tsconfig.subfunctions:
        worker_services_script = "/etc/init.d/worker_services"
        if sg_name == 'vim-services':
            if sg_desired_state == "active" and sg_state == "active":
                if want_log_file:
                    with open(sm_log_file, 'a') as f:
                        six.print_("Called script: %s start" %
                                   worker_services_script, file=f)
                subprocess.Popen([worker_services_script, "start"])
            elif sg_aggregate_state == "go-standby" \
                    and sg_desired_state == "standby" \
                    and sg_state == "standby":
                if want_log_file:
                    with open(sm_log_file, 'a') as f:
                        six.print_("Called script: %s stop" %
                                   worker_services_script, file=f)
                subprocess.Popen([worker_services_script, "stop"])


if __name__ == '__main__':
    try:
        main()
    except Exception as e:
        print "Exception occurred: %s" % e

    sys.exit(0)
