Best Python code snippet using stestr_python
moonshot-readiness.py
Source:moonshot-readiness.py  
...40# Mac specific41cmd = os.popen('which sw_vers 2>/dev/null')42bin_swvers = (cmd.read()).strip()43#=================================  PRINT OKAY/WARN/FAIL BANNER  =============================44def print_summary(colour, text_string, endl):45    colour_tag = ""46    if (colour == bcolors.OKBLUE or colour == bcolors.OKGREEN):47        colour_tag = "[OKAY]"48    elif (colour == bcolors.WARNING):49        colour_tag = "[WARN]"50    elif (colour == bcolors.FAIL):51        colour_tag = "[FAIL]"52    print(INDENT + text_string.ljust(47) + colour + colour_tag + bcolors.ENDC + endl)53#=================================  TESTS BASIC  ===========================================54def test_basic():55    global results56    global is_rhel57    global is_mac58    print("\n\nTesting task basic...")59#Supported OS60    good_os = False61    is_rhel = False62    is_mac = False63    rel_os = ""64    rel_ver = ""65    if os.path.isfile("/etc/os-release") == True:66        fil = open("/etc/os-release", "r")67        text = fil.read()68        fil.close()69        lines = text.split("\n")70        i = 071        while i < len(lines):72            words = lines[i].split("=")73            if words[0] == "ID":74                rel_os = words[1].strip("\"")75            elif words[0] == "VERSION_ID":76                rel_ver = words[1].strip("\"").split(".")[0]77            i = i + 178    elif os.path.isfile("/etc/redhat-release") == True:79        fil = open("/etc/redhat-release", "r")80        name = (fil.read()).strip().split(".")[0].lower()81        fil.close()82        rel_ver = name.rsplit(" ", 1)[1]83        rel_os = name.split(" ")[0]84    elif os.path.isfile(bin_swvers) == True:85        cmd = os.popen('%s 2>&1' % bin_swvers)86        lines = cmd.read().strip().split("\n")87        i = 088        while i < len(lines):89            words = lines[i].split(":")90            if words[0].lower() == "productname":91                name = words[1].strip()92            elif words[0].lower() == "productversion":93                rel_ver = words[1].strip().split(".")[1]94            i = i + 195        rel_os = name.lower().split()[0]96#OS checking: We support Debian, Ubuntu, RHEL, CentOS, Scientific Linux, macOS (client)97    if (rel_os == 'debian'):98        good_os = (rel_ver == '8' or rel_ver == '9')99    elif (rel_os == 'ubuntu'):100        good_os = (rel_ver == '12' or rel_ver == '14' or rel_ver == '16')101    elif (rel_os == 'redhat' or rel_os == 'rhel' or rel_os == 'centos' or rel_os == 'scientific'):102        is_rhel = True103        good_os = (rel_ver == '6' or rel_ver == '7')104    elif (rel_os == 'mac'):105        is_mac = True106        good_os = (rel_ver == '11' or rel_ver == '12' or rel_ver == '13')107    if good_os == True:108        print_summary(bcolors.OKGREEN, "Supported OS...", "")109    else:110        print_summary(bcolors.WARNING, "Supported OS...", "")111        results = results + INDENT + "Supported OS:\n        You are not running a supported OS. Moonshot may not work as indicated in the documentation.\n"112#Check for prerequisites (like dig etc)113    fail_basic_req = (bin_hostname == "" or bin_dig == "" or bin_grep == "" or bin_echo == "")114    if (not is_mac):115        if (is_rhel):116             if (fail_basic_req or bin_yum == "" or bin_rpm == ""):117                 print_summary(bcolors.FAIL, "Some prerequisites couldn\'t be found.", "")118                 results = results + INDENT + "Prerequisites for this test:\n        One or more prerequisites for this test couldn\'t be found. Please check that dig, hostname, grep, echo, yum and rpm are installed.\n"119                 return120        else:121             if (fail_basic_req or bin_aptcache == "" or bin_aptget == "" or bin_aptkey == "" or bin_dpkg == ""):122                 print_summary(bcolors.FAIL, "Some prerequisites couldn\'t be found.", "")123                 results = results + INDENT + "Prerequisites for this test:\n        One or more prerequisites for this test couldn\'t be found. Please check that dig, hostname, grep, echo, apt-get, apt-key, apt-cache and dpkg are installed.\n"124                 return125#Hostname is FQDN126    cmd = os.popen("%s -f" % bin_hostname)127    fqdn1 = (cmd.read()).strip()128    cmd = os.popen("%s %s +short" % (bin_dig, fqdn1))129    address = (cmd.read()).strip()130    if len(address) == 0:131         print_summary(bcolors.FAIL, "Hostname is FQDN...", "")132         results = results + INDENT + "Hostname is FQDN:\n        Your server\'s hostname ("+fqdn1+") is not fully resolvable. This is required in order to prevent certain classes of attack.\n"133    else:      134        cmd = os.popen("%s -x %s +short" % (bin_dig, address))135        fqdn2 = (cmd.read()).strip()136        if fqdn1 + "." == fqdn2:137            print_summary(bcolors.OKGREEN, "Hostname is FQDN...", "")138        else:139            print_summary(bcolors.FAIL, "Hostname is FQDN...", "")140            results = results + INDENT + "Hostname is FQDN:\n        Your server\'s IP address " + address + " is not resolvable to '" + fqdn1 + "' instead script got '" + fqdn2.strip('.') + "'. This is required in order to prevent certain classes of attack.\n"141#Moonshot repository configuration142    if (not is_mac):143        if (is_rhel == True):144            cmd = os.popen('%s -q list all "moonshot-gss-eap" 2>&1' % bin_yum)145        else:146            cmd = os.popen('%s search -n "moonshot-gss-eap"' % bin_aptcache)147        cmd_result = cmd.read()148        if (cmd_result.lower().find('moonshot-gss-eap') >= 0):149            print_summary(bcolors.OKGREEN, "Moonshot repositories configured...", "")150        else:151            print_summary(bcolors.WARNING, "Moonshot repositories configured...", "")152            results = results + INDENT + "Moonshot repositories configured:\n        The Moonshot repositories do not appear to exist on this system. You will not be able to upgrade Moonshot using your distribution\'s package manager.\n"153#Moonshot Signing Key154    if (not is_mac):155        if (is_rhel == True):156            cmd = os.popen("%s %s" % (bin_rpm, " -q gpg-pubkey --qf '%{version} %{summary}\n'"))157        else:158            cmd = os.popen('%s --keyring /etc/apt/trusted.gpg list' % bin_aptkey)159        cmd = cmd.read()160        key1 = False161        key2 = False162        words=re.split(r'[ \t\n/]+', cmd.upper())163        i = 0164        while i < len(words):165            if words[i] == "5B8179FD":166                key1 = True167            elif words[i] == "CEA67BB6":168                key2 = True169            i = i + 1170        if (key1 == True or key2 == True):171            print_summary(bcolors.OKGREEN, "Moonshot Signing Key...", "")172        else:173            print_summary(bcolors.WARNING, "Moonshot Signing Key...", "")174            results = results + INDENT + "Moonshot Signing Key:\n        The Moonshot repository key is not installed, you will have difficulty updating packages.\n"175#Current Moonshot software176    if (not is_mac):177        if (is_rhel == True):178            cmd = os.popen('%s --assumeno install moonshot-gss-eap 2>&1' % bin_yum)179        else:180            cmd = os.popen('%s --assume-no install moonshot-gss-eap' % bin_aptget)181        cmd = cmd.read()182        if (cmd.find("0 newly installed") >= 0) or (cmd.find("0 to newly install") >= 0) or (cmd.find("already the newest version") >= 0) \183            or (cmd.find("already installed and latest version") >= 0) or ((cmd.find("Nothing to do") >= 0) and (cmd.find("Error: Nothing to do") < 0)):184            print_summary(bcolors.OKGREEN, "Moonshot current version...", "\n\n")185        else:186            print_summary(bcolors.WARNING, "Moonshot current version...", "\n\n")187            results = results + INDENT + "Moonshot current version:\n        You are not running the latest version of the Moonshot software.\n"188#=================================  TESTS RP  ===========================================189def test_rp():190    global results191    global is_mac192    test_basic()193    print("Testing task rp...")194#Not supported on macOS195    if (is_mac):196        print_summary(bcolors.FAIL, "Configuration not supported on macOS", "")197        results = results + INDENT + "Configuration not supported on macOS:\n        macOS is currently only supported in a client configuration.\n"198        return199#/etc/radsec.conf200    if os.path.isfile("/etc/radsec.conf") == True:201        print_summary(bcolors.OKGREEN, "radsec.conf...", "\n\n")202    else:203        print_summary(bcolors.FAIL, "radsec.conf...", "\n\n")204        results = results + INDENT + "radsec.conf:\n        /etc/radsec.conf could not be found - you may not be able to communicate with your rp-proxy.\n"205#=================================  TESTS RP-PROXY  ===========================================206def test_rp_proxy():207    global results208    test_rp()209    print("Testing task rp-proxy...")210#Not supported on macOS211    if (is_mac):212        print_summary(bcolors.FAIL, "Configuration not supported on macOS", "")213        results = results + INDENT + "Configuration not supported on macOS:\n        macOS is currently only supported in a client configuration.\n"214        return215#APC216    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)217    result = sock.connect_ex(('ov-apc.moonshot.ja.net', 2083))218    if result == 0:219        print_summary(bcolors.OKGREEN, "APC...", "")220    else:221        print_summary(bcolors.FAIL, "APC...", "")222        results = results + INDENT + "APC:\n        ov-apc.moonshot.ja.net does not seem to be accessible. Please check the servers network connection, and see status.moonshot.ja.net for any downtime or maintenance issues.\n"223#Trust Router224    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)225    result = sock.connect_ex(('tr.moonshot.ja.net', 12309))226    if result == 0:227        print_summary(bcolors.OKGREEN, "Trust Router...", "")228    else:229        print_summary(bcolors.FAIL, "Trust Router...", "")230        results = results + INDENT + "Trust Router:\n        tr.moonshot.ja.net does not seem to be accessible. Please check the servers network connection, and see status.moonshot.ja.net for any downtime or maintenance issues.\n"231#flatstore-users232    root = False233    freerad = False234    trustrouter = False235    if os.path.isfile("/etc/moonshot/flatstore-users") == True:236        fil = open("/etc/moonshot/flatstore-users", "r")237        for line in fil:238            if line.strip() == "root":239                root = True240            elif line.strip() == "trustrouter":241                trustrouter = True242            elif (line.strip() == "freerad" or line.strip() == "radiusd"):243                freerad = True244        fil.close()245    if root == True and freerad == True and trustrouter == True:246        print_summary(bcolors.OKGREEN, "Flatstore-users...", "")247    else:248        print_summary(bcolors.FAIL, "Flatstore-users...", "")249        results = results + INDENT + "Flatstore-users:\n        /etc/moonshot/flatstore-users could not be found, or does not contain all the user accounts it needs to. You may be unable to authenticate to the trust router.\n"250        251#Trust Identity for FreeRADIUS252    if (is_rhel == True):253        cmd = os.popen('%s ~radiusd' % bin_echo)254    else:255        cmd = os.popen('%s ~freerad' % bin_echo)256    raduserhome = cmd.read().strip()257    if (raduserhome == '~radiusd' or raduserhome == '~freerad'):258        print_summary(bcolors.FAIL, "Trust Identity (FreeRADIUS)...", "\n\n")259        results = results + INDENT + "Trust Identity (FreeRADIUS):\n        FreeRADIUS does not appear to be installed, or no home directory for the FreeRADIUS user could be found. You will not be able to authenticate to the trust router.\n"260    elif os.path.isfile(raduserhome + '/.local/share/moonshot-ui/identities.txt') == True:261        print_summary(bcolors.OKGREEN, "Trust Identity (FreeRADIUS)...", "\n\n")262    else:263        print_summary(bcolors.FAIL, "Trust Identity (FreeRADIUS)...", "\n\n")264        results = results + INDENT + "Trust Identity (FreeRADIUS):\n        No trust identity could be found for the FreeRADIUS user account. You will not be able to authenticate to the trust router.\n"265#=================================  TESTS IDP  ===========================================266def test_idp():267    global results268    global is_rhel269    test_rp()270    print("Testing task idp...")271#Not supported on macOS272    if (is_mac):273        print_summary(bcolors.FAIL, "Configuration not supported on macOS", "")274        results = results + INDENT + "Configuration not supported on macOS:\n        macOS is currently only supported in a client configuration.\n"275        return276#Port 2083277    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)278    result = sock.connect_ex(('localhost', 2083))279    if result == 0:280        print_summary(bcolors.OKGREEN, "Port 2083...", "")281    else:282        print_summary(bcolors.FAIL, "Port 2083...", "")283        results = results + INDENT + "Port 2083:\n        Port 2083 appears to be closed. RP's will not be able to initiate connections to your IDP.\n"284#Port 12309285    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)286    result = sock.connect_ex(('localhost', 12309))287    if result == 0:288        print_summary(bcolors.OKGREEN, "Port 12309...", "")289    else:290        print_summary(bcolors.FAIL, "Port 12309...", "")291        results = results + INDENT + "Port 12309:\n        Port 12309 appears to be closed. The trust router will not be able to initiate connections to your IDP.\n"292#flatstore-users293    root = False294    freerad = False295    trustrouter = False296    if os.path.isfile("/etc/moonshot/flatstore-users") == True:297        fil = open("/etc/moonshot/flatstore-users", "r")298        for line in fil:299            if line.strip() == "root":300                root = True301            elif line.strip() == "trustrouter":302                trustrouter = True303            elif (line.strip() == "freerad" or line.strip() == "radiusd"):304                freerad = True305        fil.close()306    if root == True and freerad == True and trustrouter == True:307        print_summary(bcolors.OKGREEN, "Flatstore-users...", "")308    else:309        print_summary(bcolors.FAIL, "Flatstore-users...", "")310        results = results + INDENT + "Flatstore-users:\n        /etc/moonshot/flatstore-users could not be found, or does not contain all the user accounts it needs to. You may be unable to authenticate to the trust router.\n"311        312#Trust Identity for FreeRADIUS313    if (is_rhel == True):314        cmd = os.popen('%s ~radiusd' % bin_echo)315    else:316        cmd = os.popen('%s ~freerad' % bin_echo)317    raduserhome = cmd.read().strip()318    if (raduserhome == '~radiusd' or raduserhome == '~freerad'):319        print_summary(bcolors.FAIL, "Trust Identity (FreeRADIUS)...", "")320        results = results + INDENT + "Trust Identity (FreeRADIUS):\n        FreeRADIUS does not appear to be installed, or no home directory for the FreeRADIUS user could be found. You will not be able to authenticate to the trust router.\n"321    elif os.path.isfile(raduserhome + '/.local/share/moonshot-ui/identities.txt') == True:322        print_summary(bcolors.OKGREEN, "Trust Identity (FreeRADIUS)...", "")323    else:324        print_summary(bcolors.FAIL, "Trust Identity (FreeRADIUS)...", "")325        results = results + INDENT + "Trust Identity (FreeRADIUS):\n        No trust identity could be found for the FreeRADIUS user account. You will not be able to authenticate to the trust router.\n"326#Trust Identity for TIDS327    cmd = os.popen('%s ~trustrouter' % bin_echo)328    trustrouterhome = cmd.read().strip()329    if (trustrouterhome == '~trustrouter'):330        print_summary(bcolors.FAIL, "Trust Identity (Trust Router)...", "\n\n")331        results = results + INDENT + "Trust Identity (Trust Router):\n        There either is no trustrouter user or no home directory for the trustrouter user could be found. You will not be able to authenticate to the trust router.\n"332    elif os.path.isfile(trustrouterhome + '/.local/share/moonshot-ui/identities.txt') == True:333        print_summary(bcolors.OKGREEN, "Trust Identity (Trust Router)...", "\n\n")334    else:335        print_summary(bcolors.FAIL, "Trust Identity (Trust Router)...", "\n\n")336        results = results + INDENT + "Trust Identity (Trust Router):\n        No trust identity could be found for the trustrouter user account. You will not be able to authenticate to the trust router.\n"337#=================================  TESTS CLIENT  ===========================================338def test_client():339    global results340    global is_rhel341    global is_mac342    test_basic()343    print("Testing task client...")344#gss/mech345    s1 = False346    s2 = False347    if (is_rhel or is_mac):348        gss_mech = '/etc/gss/mech'349    else:350        gss_mech = '/etc/gss/mech.d/moonshot-gss-eap.conf'351    if os.path.isfile(gss_mech) == True:352        mode = oct(stat.S_IMODE(os.stat(gss_mech)[stat.ST_MODE]))353        if mode.strip() == "0644":354            string1 = ['eap-aes128', '1.3.6.1.5.5.15.1.1.17', 'mech_eap.so'] 355            string2 = ['eap-aes256', '1.3.6.1.5.5.15.1.1.18', 'mech_eap.so']356            fil = open(gss_mech,"r")357            for line in fil:358                words=re.split(r'[ \t]+', line.strip())359                # The length of the list must be 3 (mechanism name, mechanism oid, mechanism binary)360                if (len(words)>2):361                    if (not s1):362                        s1 = (string1[0] in words[0]) and (string1[1] in words[1]) and (string1[2] in words[2])363                    if (not s2):364                        s2 = (string2[0] in words[0]) and (string2[1] in words[1]) and (string2[2] in words[2])365            fil.close()366            367    if (s1 == True and s2 == True):368        print_summary(bcolors.OKGREEN, "gss/mech...", "\n\n")369    else:370        print_summary(bcolors.FAIL, "gss/mech...", "\n\n")371        results = results + INDENT + "gss/mech:\n        The Moonshot mech file is missing or incomplete. mech_eap.so will not be loaded.\n"372#=================================  TESTS SSH-CLIENT  ===========================================373def test_ssh_client():374    global results375    global is_mac376    test_client()377    print("Testing task ssh-client...")378    cmd = os.popen('which ssh 2>/dev/null')379    cmd = (cmd.read()).strip()380    is_ssh_installed = (len(cmd) > 0)381    if (is_ssh_installed == False):382        print_summary(bcolors.FAIL, "Task ssh-client...", "\n\n")383        results = results + INDENT + "Task ssh-client:\n        You must have OpenSSH installed before attempting this test.\n"384    else:385#GSSAPIAuthentication must be enabled386        num = 0387        cmd = os.popen("%s %s" % (bin_grep, " GSSAPIAuthentication /etc/ssh/ssh_config |grep -v \#"))388        cmd = (cmd.read()).strip()389        if len(cmd) > 0:390            lines = cmd.split('\n')391            for line in lines:392                if (line.lower() == 'gssapiauthentication yes'):393                    num = num + 1394            if num > 0:395                print_summary(bcolors.OKGREEN, "GSSAPIAuthentication enabled...", "")396            else:397                print_summary(bcolors.FAIL, "GSSAPIAuthentication enabled...", "")398                results = results + INDENT + "GSSAPIAuthentication enabled:\n        GSSAPIAuthentication must be enabled for Moonshot to function when using SSH.\n"399        else:400            print_summary(bcolors.FAIL, "GSSAPIAuthentication enabled...", "")401            results = results + INDENT + "GSSAPIAuthentication enabled:\n        GSSAPIAuthentication must be enabled for Moonshot to function when using SSH.\n"402#GSSAPIKeyExchange (should be) enabled403        if (not is_mac):404            num = 0405            cmd = os.popen("%s %s" % (bin_grep, " GSSAPIKeyExchange /etc/ssh/ssh_config |grep -v \#"))406            cmd = (cmd.read()).strip()407            if len(cmd) > 0:408                lines = cmd.split('\n')409                for line in lines:410                    if (line.lower() == 'gssapikeyexchange yes'):411                        num = num + 1412                if num > 0:413                    print_summary(bcolors.OKGREEN, "GSSAPIKeyExchange enabled...", "\n\n")414                else:415                    print_summary(bcolors.WARNING, "GSSAPIKeyExchange enabled...", "\n\n")416                    results = results + INDENT + "GSSAPIKeyExchange enabled:\n        GSSAPIKeyExchange should be enabled for Moonshot to function correctly when using SSH.\n"417            else:418                print_summary(bcolors.WARNING, "GSSAPIKeyExchange enabled...", "\n\n")419                results = results + INDENT + "GSSAPIKeyExchange enabled:\n        GSSAPIKeyExchange should be enabled for Moonshot to function correctly when using SSH.\n"420#=================================  TESTS SSH-SERVER  ===========================================421def test_ssh_server():422    global results423    test_rp()424    print("Testing task ssh-server...")425#Not supported on macOS426    if (is_mac):427        print_summary(bcolors.FAIL, "Configuration not supported on macOS", "")428        results = results + INDENT + "Configuration not supported on macOS:\n        macOS is currently only supported in a client configuration.\n"429        return430    cmd = os.popen('/usr/sbin/sshd -V 2>&1 |grep OpenSSH')431    cmd = (cmd.read()).strip()432    is_openssh_installed = (len(cmd) > 0)433    if (is_openssh_installed == False):434        print_summary(bcolors.FAIL, "Task ssh-server...", "\n\n")435        results = results + INDENT + "Task ssh-server:\n        You must have OpenSSH installed before attempting this test.\n"436    else:437        openssh = (((cmd.split()[0]).split('_')[1]).split('p')[0]).split('.')438        needs_privsepoff = (int(openssh[0]) < 6) or (int(openssh[0]) == 6 and int(openssh[1]) < 6)439#GSSAPIAuthentication enabled440        num = 0441        cmd = os.popen("%s %s" % (bin_grep, " GSSAPIAuthentication /etc/ssh/sshd_config |grep -v \#"))442        cmd = (cmd.read()).strip()443        if len(cmd) > 0:444            lines = cmd.split('\n')445            for line in lines:446                if (line.lower() == 'gssapiauthentication yes'):447                    num = num + 1448            if num > 0:449                print_summary(bcolors.OKGREEN, "GSSAPIAuthentication enabled...", "")450            else:451                print_summary(bcolors.FAIL, "GSSAPIAuthentication enabled...", "")452                results = results + INDENT + "GSSAPIAuthentication enabled:\n        GSSAPIAuthentication must be enabled for Moonshot to function when using SSH.\n"453        else:454            print_summary(bcolors.FAIL, "GSSAPIAuthentication enabled...", "")455            results = results + INDENT + "GSSAPIAuthentication enabled:\n        GSSAPIAuthentication must be enabled for Moonshot to function when using SSH.\n"456#GSSAPIKeyExchange enabled457        num = 0458        cmd = os.popen("%s %s" % (bin_grep, " GSSAPIKeyExchange /etc/ssh/sshd_config |grep -v \#"))459        cmd = (cmd.read()).strip()460        if len(cmd) > 0:461            lines = cmd.split('\n')462            for line in lines:463                if (line.lower() == 'gssapikeyexchange yes'):464                    num = num + 1465            if num > 0:466                print_summary(bcolors.OKGREEN, "GSSAPIKeyExchange enabled...", "")467            else:468                print_summary(bcolors.FAIL, "GSSAPIKeyExchange enabled...", "")469                results = results + INDENT + " enabled:\n        GSSAPIKeyExchange must be enabled for Moonshot to function correctly when using SSH.\n"470        else:471            print_summary(bcolors.FAIL, "GSSAPIKeyExchange enabled...", "")472            results = results + INDENT + "GSSAPIKeyExchange enabled:\n        GSSAPIKeyExchange must be enabled for Moonshot to function correctly when using SSH.\n"473#UsePrivilegeSeparation disabled474        if (needs_privsepoff == True):475            num = 0476            cmd = os.popen("%s %s" % (bin_grep, " UsePrivilegeSeparation /etc/ssh/sshd_config |grep -v \#"))477            cmd = (cmd.read()).strip()478            if len(cmd) > 0:479                lines = cmd.split('\n')480                for line in lines:481                    if (line.lower() == 'useprivilegeseparation no'):482                        num = num + 1483                if num > 0:484                    print_summary(bcolors.OKGREEN, "Privilege separation...", "\n\n")485                else:486                    print_summary(bcolors.FAIL, "Privilege separation...", "\n\n")487                    results = results + INDENT + "Privilege separation:\n        Moonshot currently requires that OpenSSH server has privilege separation disabled.\n\n"488            else:489                print_summary(bcolors.FAIL, "Privilege separation...", "")490                results = results + INDENT + "Privilege separation:\n        Moonshot currently requires that OpenSSH server has privilege separation disabled.\n\n"491        else:492            print("\n")493#=================================  MAIN  ===========================================494size = len(sys.argv)495if size < 2 :496    print("\n\nUsage: moonshot-readiness [task] [task]...\n\n  Available tasks:\n    help\n    minimal (default)\n    client\n    rp\n    rp-proxy\n    idp\n    ssh-client\n    ssh-server\n\n")497else:498    i = 1499    while i < size:500        if (sys.argv[i]).strip() == 'help':501            print "\n\nUsage: moonshot-readiness [task] [task]...\n\n  Available tasks:\n    help\n    minimal (default)\n    client\n    rp\n    rp-proxy\n    idp-proxy\n    ssh-client\n    ssh-server\n\n  ¦---------------------------------------------------------------------------------------------------------------¦\n  ¦ TASK            ¦  DEPENDENCY  ¦  DESCRIPTION                                                                 ¦\n  ¦-----------------¦--------------¦------------------------------------------------------------------------------¦\n  ¦ basic           ¦  none        ¦  Basic set of test, required for Moonshot to function at all in any capacity ¦\n  ¦ client          ¦  basic       ¦  Fundamental tests required for Moonshot to function as a client             ¦\n  ¦ rp              ¦  basic       ¦  Fundamental tests required for Moonshot to function as an RP                ¦\n  ¦ rp-proxy        ¦  rp          ¦  Tests required for Moonshot to function as a RadSec RP                      ¦\n  ¦ idp             ¦  rp          ¦  Tests to verify if FreeRADIUS is correctly configured                       ¦\n  ¦ openssh-client  ¦  client      ¦  Tests to verify if the openssh-client is correctly configured               ¦\n  ¦ openssh-rp      ¦  rp          ¦  Tests to verify if the openssh-server is correctly configured               ¦\n  ¦ httpd-client    ¦  client      ¦  Tests to verify if mod-auth-gssapi is correctly configured                  ¦\n  ¦ httpd-rp        ¦  rp          ¦  Tests to verify if mod-auth-gssapi is correctly configured                  ¦\n  ¦-----------------¦--------------¦------------------------------------------------------------------------------¦\n\n\nSome tests require root privileges to be made.\nSome tests require the following tools:\n  augeas     (installing with apt-get install augeas-tools)\n  dig        (installing with apt-get install dnsutils)\n  hostname   (installing with apt-get install hostname)\n\n"502            sys.exit()503        elif (sys.argv[i]).strip() == 'minimal':...summaries.py
Source:summaries.py  
1# Copyright 2017 The TensorFlow Authors. All Rights Reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7#     http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14# ==============================================================================15"""Contains helper functions for creating summaries.16This module contains various helper functions for quickly and easily adding17tensorflow summaries. These allow users to print summary values18automatically as they are computed and add prefixes to collections of summaries.19Example usage:20  import tensorflow as tf21  slim = tf.contrib.slim22  slim.summaries.add_histogram_summaries(slim.variables.get_model_variables())23  slim.summaries.add_scalar_summary(total_loss, 'Total Loss')24  slim.summaries.add_scalar_summary(learning_rate, 'Learning Rate')25  slim.summaries.add_histogram_summaries(my_tensors)26  slim.summaries.add_zero_fraction_summaries(my_tensors)27"""28from __future__ import absolute_import29from __future__ import division30from __future__ import print_function31from tensorflow.python.framework import ops32from tensorflow.python.ops import logging_ops33from tensorflow.python.ops import nn_impl as nn34from tensorflow.python.summary import summary35def _get_summary_name(tensor, name=None, prefix=None, postfix=None):36  """Produces the summary name given.37  Args:38    tensor: A variable or op `Tensor`.39    name: The optional name for the summary.40    prefix: An optional prefix for the summary name.41    postfix: An optional postfix for the summary name.42  Returns:43    a summary name.44  """45  if not name:46    name = tensor.op.name47  if prefix:48    name = prefix + '/' + name49  if postfix:50    name = name + '/' + postfix51  return name52def add_histogram_summary(tensor, name=None, prefix=None):53  """Adds a histogram summary for the given tensor.54  Args:55    tensor: A variable or op tensor.56    name: The optional name for the summary.57    prefix: An optional prefix for the summary names.58  Returns:59    A scalar `Tensor` of type `string` whose contents are the serialized60    `Summary` protocol buffer.61  """62  return summary.histogram(63      _get_summary_name(tensor, name, prefix), tensor)64def add_image_summary(tensor, name=None, prefix=None, print_summary=False):65  """Adds an image summary for the given tensor.66  Args:67    tensor: a variable or op tensor with shape [batch,height,width,channels]68    name: the optional name for the summary.69    prefix: An optional prefix for the summary names.70    print_summary: If `True`, the summary is printed to stdout when the summary71      is computed.72  Returns:73    An image `Tensor` of type `string` whose contents are the serialized74    `Summary` protocol buffer.75  """76  summary_name = _get_summary_name(tensor, name, prefix)77  # If print_summary, then we need to make sure that this call doesn't add the78  # non-printing op to the collection. We'll add it to the collection later.79  collections = [] if print_summary else None80  op = summary.image(81      name=summary_name, tensor=tensor, collections=collections)82  if print_summary:83    op = logging_ops.Print(op, [tensor], summary_name)84    ops.add_to_collection(ops.GraphKeys.SUMMARIES, op)85  return op86def add_scalar_summary(tensor, name=None, prefix=None, print_summary=False):87  """Adds a scalar summary for the given tensor.88  Args:89    tensor: a variable or op tensor.90    name: the optional name for the summary.91    prefix: An optional prefix for the summary names.92    print_summary: If `True`, the summary is printed to stdout when the summary93      is computed.94  Returns:95    A scalar `Tensor` of type `string` whose contents are the serialized96    `Summary` protocol buffer.97  """98  collections = [] if print_summary else None99  summary_name = _get_summary_name(tensor, name, prefix)100  # If print_summary, then we need to make sure that this call doesn't add the101  # non-printing op to the collection. We'll add it to the collection later.102  op = summary.scalar(103      name=summary_name, tensor=tensor, collections=collections)104  if print_summary:105    op = logging_ops.Print(op, [tensor], summary_name)106    ops.add_to_collection(ops.GraphKeys.SUMMARIES, op)107  return op108def add_zero_fraction_summary(tensor, name=None, prefix=None,109                              print_summary=False):110  """Adds a summary for the percentage of zero values in the given tensor.111  Args:112    tensor: a variable or op tensor.113    name: the optional name for the summary.114    prefix: An optional prefix for the summary names.115    print_summary: If `True`, the summary is printed to stdout when the summary116      is computed.117  Returns:118    A scalar `Tensor` of type `string` whose contents are the serialized119    `Summary` protocol buffer.120  """121  name = _get_summary_name(tensor, name, prefix, 'Fraction_of_Zero_Values')122  tensor = nn.zero_fraction(tensor)123  return add_scalar_summary(tensor, name, print_summary=print_summary)124def add_histogram_summaries(tensors, prefix=None):125  """Adds a histogram summary for each of the given tensors.126  Args:127    tensors: A list of variable or op tensors.128    prefix: An optional prefix for the summary names.129  Returns:130    A list of scalar `Tensors` of type `string` whose contents are the131    serialized `Summary` protocol buffer.132  """133  summary_ops = []134  for tensor in tensors:135    summary_ops.append(add_histogram_summary(tensor, prefix=prefix))136  return summary_ops137def add_image_summaries(tensors, prefix=None):138  """Adds an image summary for each of the given tensors.139  Args:140    tensors: A list of variable or op tensors.141    prefix: An optional prefix for the summary names.142  Returns:143    A list of scalar `Tensors` of type `string` whose contents are the144    serialized `Summary` protocol buffer.145  """146  summary_ops = []147  for tensor in tensors:148    summary_ops.append(add_image_summary(tensor, prefix=prefix))149  return summary_ops150def add_scalar_summaries(tensors, prefix=None, print_summary=False):151  """Adds a scalar summary for each of the given tensors.152  Args:153    tensors: a list of variable or op tensors.154    prefix: An optional prefix for the summary names.155    print_summary: If `True`, the summary is printed to stdout when the summary156      is computed.157  Returns:158    A list of scalar `Tensors` of type `string` whose contents are the159    serialized `Summary` protocol buffer.160  """161  summary_ops = []162  for tensor in tensors:163    summary_ops.append(add_scalar_summary(tensor, prefix=prefix,164                                          print_summary=print_summary))165  return summary_ops166def add_zero_fraction_summaries(tensors, prefix=None):167  """Adds a scalar zero-fraction summary for each of the given tensors.168  Args:169    tensors: a list of variable or op tensors.170    prefix: An optional prefix for the summary names.171  Returns:172    A list of scalar `Tensors` of type `string` whose contents are the173    serialized `Summary` protocol buffer.174  """175  summary_ops = []176  for tensor in tensors:177    summary_ops.append(add_zero_fraction_summary(tensor, prefix=prefix))...ModelLoader.py
Source:ModelLoader.py  
1import json2from GanModelBase import GanModelBase3from VaeModelBase import VaeModelBase4from AeModelBase import AeModelBase5from LabeledGan import LabeledGan6from Encoder import Encoder7from Decoder import Decoder8from KLEncoder import KLEncoder9from DualEncoder import DualEncoder10from ImgToImgDecoder import ImgToImgDecoder11from Img2ImgGan import Img2ImgGan12from Database import numpy_dataloader, supervised_numpy_dataloader13def load_model(file, cuda):14    with open(file) as f:15        config = json.load(f)16    model_type = config['model_type']17    if model_type == 'wgan-gp':18        model = build_standard_wgan_gp(config, cuda)19    elif model_type == 'ae':20        model = build_standard_ae(config, cuda)21    elif model_type == 'vae':22        model = build_standard_vae(config, cuda)23    elif model_type == 'labeled_gan':24        model = build_labeled_gan(config, cuda)25    elif model_type == 'encoder':26        model = Encoder(config)27    elif model_type == 'kl-encoder':28        model = KLEncoder(config)29    elif model_type == 'decoder':30        model = Decoder(config)31    elif model_type == 'dual-encoder':32        model = DualEncoder(config)33    elif model_type == 'img2img-decoder':34        model = ImgToImgDecoder(config)35    elif model_type == 'img2img-gan':36        model = build_img2img_gan(config, cuda)37    else:38        assert False, f"Unknown model type '{model_type}'!"39    if cuda:40        model.cuda()41    return model42def build_standard_wgan_gp(config, cuda):43    generator = load_model(config['generator'], cuda)44    discriminator = load_model(config['discriminator'], cuda)45    batch_size = config['batch_size']46    dataset = config['dataset']47    dataloader = numpy_dataloader(dataset, batch_size, cuda)48    print_summary = config['print_summary'] if 'print_summary' in config else False49    gan = GanModelBase(dataloader, generator, discriminator, summary=print_summary)50    gan.batch_size = batch_size51    gan.gradient_updates = config['gradient_updates'] if 'gradient_updates' in config else 152    gan.save_snapshot_rate = config['save_snapshot_rate'] if 'save_snapshot_rate' in config else 10053    gan.save_model_rate = config['save_model_rate'] if 'save_model_rate' in config else 100054    gan.swap_buffer = config['swap_buffer'] if 'swap_buffer' in config else 51255    gan.swap_chance = config['swap_chance'] if 'swap_chance' in config else 0.556    gan.critic_updates = config['critic_updates'] if 'critic_updates' in config else 557    gan.gradient_penalty_lambda = config['gradient_penalty_lambda'] if 'gradient_penalty_lambda' in config else 1058    return gan59def build_img2img_gan(config, cuda):60    generator = load_model(config['generator'], cuda)61    discriminator = load_model(config['discriminator'], cuda)62    batch_size = config['batch_size']63    dataloader_input = numpy_dataloader(config['dataset_inputs'], batch_size, cuda)64    dataloader_output = numpy_dataloader(config['dataset_outputs'], batch_size, cuda)65    print_summary = config['print_summary'] if 'print_summary' in config else False66    gan = Img2ImgGan(dataloader_input, dataloader_output, generator, discriminator, summary=print_summary)67    gan.batch_size = batch_size68    gan.gradient_updates = config['gradient_updates'] if 'gradient_updates' in config else 169    gan.save_snapshot_rate = config['save_snapshot_rate'] if 'save_snapshot_rate' in config else 10070    gan.save_model_rate = config['save_model_rate'] if 'save_model_rate' in config else 100071    gan.swap_buffer = config['swap_buffer'] if 'swap_buffer' in config else 51272    gan.swap_chance = config['swap_chance'] if 'swap_chance' in config else 0.573    gan.critic_updates = config['critic_updates'] if 'critic_updates' in config else 574    gan.gradient_penalty_lambda = config['gradient_penalty_lambda'] if 'gradient_penalty_lambda' in config else 1075    return gan76def build_standard_ae(config, cuda):77    encoder = load_model(config['encoder'], cuda)78    decoder = load_model(config['decoder'], cuda)79    batch_size = config['batch_size']80    dataset = config['dataset']81    dataloader = numpy_dataloader(dataset, batch_size, cuda)82    print_summary = config['print_summary'] if 'print_summary' in config else False83    ae = AeModelBase(dataloader, encoder, decoder, summary=print_summary)84    ae.batch_size = batch_size85    ae.gradient_updates = config['gradient_updates'] if 'gradient_updates' in config else 186    ae.save_snapshot_rate = config['save_snapshot_rate'] if 'save_snapshot_rate' in config else 10087    ae.save_model_rate = config['save_model_rate'] if 'save_model_rate' in config else 100088    return ae89def build_labeled_gan(config, cuda):90    generator = load_model(config['generator'], cuda)91    discriminator = load_model(config['discriminator'], cuda)92    batch_size = config['batch_size']93    dataset_samples = config['dataset_samples']94    dataset_labels = config['dataset_labels']95    dataloader = supervised_numpy_dataloader(dataset_samples, dataset_labels, batch_size, cuda)96    print_summary = config['print_summary'] if 'print_summary' in config else False97    gan = LabeledGan(dataloader, generator, discriminator, summary=print_summary)98    gan.batch_size = batch_size99    gan.gradient_updates = config['gradient_updates'] if 'gradient_updates' in config else 1100    gan.save_snapshot_rate = config['save_snapshot_rate'] if 'save_snapshot_rate' in config else 100101    gan.save_model_rate = config['save_model_rate'] if 'save_model_rate' in config else 1000102    gan.swap_buffer = config['swap_buffer'] if 'swap_buffer' in config else 512103    gan.swap_chance = config['swap_chance'] if 'swap_chance' in config else 0.5104    gan.critic_updates = config['critic_updates'] if 'critic_updates' in config else 5105    gan.gradient_penalty_lambda = config['gradient_penalty_lambda'] if 'gradient_penalty_lambda' in config else 10106    return gan107def build_standard_vae(config, cuda):108    encoder = load_model(config['encoder'], cuda)109    decoder = load_model(config['decoder'], cuda)110    batch_size = config['batch_size']111    dataset = config['dataset']112    dataloader = numpy_dataloader(dataset, batch_size, cuda)113    print_summary = config['print_summary'] if 'print_summary' in config else False114    vae = VaeModelBase(dataloader, encoder, decoder, summary=print_summary)115    vae.batch_size = batch_size116    vae.gradient_updates = config['gradient_updates'] if 'gradient_updates' in config else 1117    vae.save_snapshot_rate = config['save_snapshot_rate'] if 'save_snapshot_rate' in config else 100118    vae.save_model_rate = config['save_model_rate'] if 'save_model_rate' in config else 1000119    vae.kld_weight = config['kld_weight'] if 'kld_weight' in config else 1120    vae.logcosh_alpha = config['logcosh_alpha'] if 'logcosh_alpha' in config else 10...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
