Revision 8792bb97 snf-tools/synnefo_tools/burnin.py

b/snf-tools/synnefo_tools/burnin.py
1607 1607
    return cls
1608 1608

  
1609 1609

  
1610
# --------------------------------------------------------------------
1611
# Clean up servers/networks functions
1610 1612
def cleanup_servers(timeout, query_interval, delete_stale=False):
1611 1613

  
1612 1614
    c = ComputeClient(API, TOKEN)
......
1617 1619
    if len(stale) == 0:
1618 1620
        return
1619 1621

  
1620
    print >> sys.stderr, yellow + "Found these stale servers from previous runs:" + normal
1621
    print "    " + \
1622
    # Show staled servers
1623
    print >>sys.stderr, yellow + \
1624
            "Found these stale servers from previous runs:" + \
1625
            normal
1626
    print >>sys.stderr, "    " + \
1622 1627
          "\n    ".join(["%d: %s" % (s["id"], s["name"]) for s in stale])
1623 1628

  
1629
    # Delete staled servers
1624 1630
    if delete_stale:
1625 1631
        print >> sys.stderr, "Deleting %d stale servers:" % len(stale)
1626

  
1627 1632
        fail_tmout = time.time() + timeout
1628

  
1629

  
1630 1633
        for s in stale:
1631 1634
            c.delete_server(s["id"])
1632

  
1633
        
1635
        # Wait for all servers to be deleted
1634 1636
        while True:
1635 1637
            servers = c.list_servers()
1636 1638
            stale = [s for s in servers if s["name"].startswith(SNF_TEST_PREFIX)]
1637

  
1638 1639
            if len(stale)==0:
1639 1640
                print >> sys.stderr, green + "    ...done" + normal
1640 1641
                break
1641

  
1642 1642
            elif time.time() > fail_tmout:
1643
                print >> sys.stderr, red + "Not all stale servers deleted. Action timed out." + normal
1644
                return 
1643
                print >> sys.stderr, red + \
1644
                        "Not all stale servers deleted. Action timed out." + \
1645
                        normal
1646
                return
1645 1647
            else:
1646 1648
                time.sleep(query_interval)
1647
                
1648 1649
    else:
1649 1650
        print >> sys.stderr, "Use --delete-stale to delete them."
1650 1651

  
......
1659 1660
    if len(stale) == 0:
1660 1661
        return
1661 1662

  
1662
    fail_tmout = time.time() + action_timeout
1663
    # Wait for deleting servers only if `delete_stale' is True
1664
    if delete_stale:
1665
        while True:
1666
            servers = c.list_servers()
1667
            staleServers = [s for s in servers if s["name"].startswith(SNF_TEST_PREFIX)]
1668
            if len(staleServers) == 0:
1669
                break
1670
            elif time.time() > fail_tmout:
1671
                log.error("Stale servers not deleted from previous run")
1672
                sys.exit()
1673
            else:
1674
                time.sleep(query_interval)
1675

  
1676
    print >> sys.stderr, yellow + "Found these stale networks from previous runs:" + normal
1663
    # Show staled networks
1664
    print >> sys.stderr, yellow + \
1665
            "Found these stale networks from previous runs:" + \
1666
            normal
1677 1667
    print "    " + \
1678 1668
          "\n    ".join(["%s: %s" % (str(n["id"]), n["name"]) for n in stale])
1679 1669

  
1670
    # Delete staled networks
1680 1671
    if delete_stale:
1681 1672
        print >> sys.stderr, "Deleting %d stale networks:" % len(stale)
1682

  
1683 1673
        fail_tmout = time.time() + action_timeout
1684
        
1685 1674
        for n in stale:
1686 1675
            c.delete_network(n["id"])
1687

  
1688

  
1676
        # Wait for all networks to be deleted
1689 1677
        while True:
1690 1678
            networks = c.list_networks()
1691 1679
            stale = [n for n in networks if n["name"].startswith(SNF_TEST_PREFIX)]
1692

  
1693 1680
            if len(stale)==0:
1694 1681
                print >> sys.stderr, green + "    ...done" + normal
1695 1682
                break
1696

  
1697 1683
            elif time.time() > fail_tmout:
1698
                print >> sys.stderr, red + "Not all stale networks deleted. Action timed out." + normal
1684
                print >> sys.stderr, red + \
1685
                        "Not all stale networks deleted. Action timed out." + \
1686
                        normal
1699 1687
                return 
1700 1688
            else:
1701 1689
                time.sleep(query_interval)
1702

  
1703 1690
    else:
1704 1691
        print >> sys.stderr, "Use --delete-stale to delete them."
1705 1692

  
1706 1693

  
1694
# --------------------------------------------------------------------
1695
# Parse arguments functions
1707 1696
def parse_comma(option, opt, value, parser):
1708 1697
    tests = set(['all', 'auth', 'images', 'flavors',
1709 1698
               'servers', 'server_spawn', 'network_spawn'])
......
1816 1805
                      default="/var/log/burnin/")
1817 1806
    parser.add_option("--verbose", "-V",
1818 1807
                      action="store_true", dest="verbose",
1819
                      help="Print detailed output about multiple processes spawning",
1808
                      help="Print detailed output about multiple "\
1809
                           "processes spawning",
1820 1810
                      default=False)
1821 1811
    parser.add_option("--set-tests",
1822 1812
                      action="callback",
......
1832 1822

  
1833 1823
    (opts, args) = parser.parse_args(args)
1834 1824

  
1825
    # -----------------------
1835 1826
    # Verify arguments
1827

  
1828
    # `delete_stale' implies `show_stale'
1836 1829
    if opts.delete_stale:
1837 1830
        opts.show_stale = True
1838 1831

  
1832
    # `image-id' is mandatory
1839 1833
    if not opts.show_stale:
1840 1834
        if not opts.force_imageid:
1841
            print >>sys.stderr, red + "The --image-id argument " \
1842
                                       "is mandatory.\n" + normal
1843
            parser.print_help()
1844
            sys.exit(1)
1845

  
1846
        if not opts.token:
1847
            print >>sys.stderr, red + "The --token argument is " \
1848
                                      "mandatory.\n" + normal
1835
            print >>sys.stderr, red + \
1836
                    "The --image-id argument is mandatory.\n" + \
1837
                    normal
1849 1838
            parser.print_help()
1850 1839
            sys.exit(1)
1851

  
1852 1840
        if opts.force_imageid != 'all':
1853 1841
            try:
1854 1842
                opts.force_imageid = str(opts.force_imageid)
1855 1843
            except ValueError:
1856
                print >>sys.stderr, red + "Invalid value specified for" \
1857
                    "--image-id. Use a valid id, or `all'." + normal
1844
                print >>sys.stderr, red + \
1845
                        "Invalid value specified for" + \
1846
                        "--image-id. Use a valid id, or `all'." + \
1847
                        normal
1858 1848
                sys.exit(1)
1859 1849

  
1850
    # `token' is mandatory
1851
    if not opts.token:
1852
        print >>sys.stderr, red + \
1853
                "The --token argument is mandatory.\n" + \
1854
                normal
1855
        parser.print_help()
1856
        sys.exit(1)
1857

  
1860 1858
    return (opts, args)
1861 1859

  
1862 1860

  
1861
# --------------------------------------------------------------------
1862
# Burnin main function
1863 1863
def main():
1864 1864
    """Assemble test cases into a test suite, and run it
1865 1865

  
......
1871 1871

  
1872 1872
    """
1873 1873

  
1874
    # Parse arguments using `optparse'
1874 1875
    (opts, args) = parse_arguments(sys.argv[1:])
1875 1876

  
1877
    # Some global variables
1876 1878
    global API, TOKEN, PLANKTON, PLANKTON_USER, NO_IPV6, VERBOSE, NOFAILFAST
1877 1879
    API = opts.api
1878 1880
    TOKEN = opts.token
......
1882 1884
    VERBOSE = opts.verbose
1883 1885
    NOFAILFAST = opts.nofailfast
1884 1886

  
1885
    # Cleanup stale servers from previous runs
1887
    # If `show_stale', cleanup stale servers
1888
    # from previous runs and exit
1886 1889
    if opts.show_stale:
1887
        cleanup_servers(opts.action_timeout, opts.query_interval, delete_stale=opts.delete_stale)
1888
        cleanup_networks(opts.action_timeout, opts.query_interval, delete_stale=opts.delete_stale)
1889

  
1890
        # We must clean the servers first
1891
        cleanup_servers(opts.action_timeout, opts.query_interval,
1892
                        delete_stale=opts.delete_stale)
1893
        cleanup_networks(opts.action_timeout, opts.query_interval,
1894
                         delete_stale=opts.delete_stale)
1890 1895
        return 0
1891 1896

  
1892 1897
    # Initialize a kamaki instance, get flavors, images

Also available in: Unified diff