#!/usr/bin/env python
#
print("""
################################################################################
# The goal of this program is to sync the runnning configuration into device-group-failover
#
################################################################################
# Version: 0.1, Date: September 27 2018
# Author: Sam Li 
# Usage:
#         $ f5-sync --node xxx
###############################################################################
# Prerequisite:
# a. F5 remote SSH access service are setup.
# b. You have privileged access to the F5 TMOS configuration.
###############################################################################
""")
import f5_admin
from f5_admin.util import *
import argparse
import getpass

# command argument switch setup
parser = argparse.ArgumentParser(description='This program will sync the running configuration into device-group-failover. ')
parser.add_argument('-n','--node', help=' F5 hostname or IP address',required=False)
parser.add_argument('-a','--all', help='Sync all known F5 nodes',action='store_true', default=False,required=False)
parser.add_argument('-u','--user', help='(Optional) F5 box logon user ID. Default to root. ',required=False)
parser.add_argument('-p','--password', nargs='+', help='(Optional) F5 box password. ',required=True)
parser.add_argument('-v','--verbose', help='(Optional) Verbose mode',action='store_true', default=False, required=False)
args = parser.parse_args()

################################################################################
#      Main Program
################################################################################
if __name__ == '__main__':
    # simple program argument check
    if not args.node and not args.all:
        print("Usage: ", __file__, " -h")
        exit(1)
    elif args.node and args.all:
        print("Usage: ", __file__, " -h")
        exit(1)
    #else:
        # do nothing. program proceeding
    # Setup F5 connection string
    if args.password:
        credential = set_credential_1(args.user, *args.password)
    else:
        credential = set_credential_2(args.user)


    #  Initialize a F5 instance
    exceptions=[]
    with f5_admin.F5Client(credential, 7, args.verbose) as my_f5:
        if my_f5.credential["user_name"] == "root":
            cmd_prefix = "tmsh "
        else:
            cmd_prefix = ""
        # sync to failover device group
        cmd_set= [cmd_prefix + "run /cm config-sync to-group device-group-failover",
                  cmd_prefix + "save /sys config partitions all"]
        if args.node:
            my_f5.load(args.node)
            conn = my_f5.ssh_connect()
            for y in cmd_set:
                my_f5.ssh_command(conn,y,"")
            conn.close()
        else:
            all_nodes = my_f5.get_node_list()
            exceptions = ['aws-f5-rhwebprd1','aws-f5-rhwebprd2','xx-toolsprd1', 'xx-toolsprd2', 'xx-lab1', 'xx-vcmp-1',
                            'xx-vcmp-2', 'xx-vcmp-1', 'xx-vcmp-2', 'xx-gtmprd1', 'xx-gtmprd1']
            print("Skip these nodes as they are either not in a fail-over pair nor have the standard root password: ", exceptions)
            my_nodes = [node for node in all_nodes if node not in exceptions]
            for node in my_nodes:
                my_f5.load(node)
                conn = my_f5.ssh_connect()
                for y in cmd_set:
                    my_f5.ssh_command(conn,y,"")
                conn.close()

    # Print the confirmation
    print("""
All done. Bye!""")
