Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / tools / update_to_floating_ips @ 0136e854

History | View | Annotate | Download (2.4 kB)

1
#!/usr/bin/env python
2
"""Tool to convert public IPv4 addresses to floatign IPs, which may be needed
3
for update to Synnefo v0.15.
4

    
5
"""
6

    
7
import sys
8
import logging
9
from optparse import OptionParser, TitledHelpFormatter
10

    
11
# Configure Django env
12
from synnefo import settings
13
from django.core.management import setup_environ
14
setup_environ(settings)
15

    
16
from synnefo.management.common import get_network
17
from synnefo.db.models import IPAddress
18
from django.db import transaction
19

    
20
logger = logging.getLogger("update_floating_ips")
21
handler = logging.StreamHandler()
22

    
23
formatter = logging.Formatter("[%(levelname)s] %(message)s")
24
handler.setFormatter(formatter)
25
logger.setLevel(logging.INFO)
26
logger.addHandler(handler)
27
logger.propagate = False
28

    
29
DESCRIPTION = """Tool to convert public IPv4 addresses to floating IPs."""
30

    
31

    
32
@transaction.commit_on_success
33
def main():
34
    parser = OptionParser(description=DESCRIPTION,
35
                          formatter=TitledHelpFormatter())
36
    parser.add_option("--network-id", dest="network_id", default=None,
37
                      help="Update addresses only of this network."),
38
    parser.add_option("--dry-run", dest="dry_run", default=False,
39
                      action="store_true",
40
                      help="Dry-run mode.")
41
    parser.add_option("-d", "--debug", dest="debug", default=False,
42
                      action="store_true",
43
                      help="Display debug information.")
44
    options, args = parser.parse_args()
45

    
46
    if options.debug:
47
        logger.setLevel(logging.DEBUG)
48

    
49
    public_ips = IPAddress.objects.filter(network__public=True,
50
                                          floating_ip=False)
51
    if options.network_id is not None:
52
        network = get_network(options.network_id)
53
        logger.debug("Converting IPs only of network %s" % network.id)
54
        public_ips = public_ips.filter(network=network)
55

    
56
    logger.info("Converting %d public IPv4 addresses to floating IPs.",
57
                public_ips.count())
58

    
59
    if not public_ips:
60
        logger.info("No public IPs to convert.")
61
        return
62

    
63
    if options.debug:
64
        addresses = public_ips.values_list("address", flat=True)
65
        logger.debug("Converting the following public IPs:\n%s",
66
                     "\n".join(addresses))
67

    
68
    if not options.dry_run:
69
        public_ips.update(floating_ip=True)
70

    
71
    logger.info("Successfully updated public addresses to floating IPs.")
72
    return
73

    
74

    
75
if __name__ == "__main__":
76
    main()
77
    sys.exit(0)