root / snf-cyclades-app / synnefo / tools / update_to_floating_ips @ d7fa55f9
History | View | Annotate | Download (2.5 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 |
subnet__ipversion=4, |
51 |
floating_ip=False) |
52 |
if options.network_id is not None: |
53 |
network = get_network(options.network_id) |
54 |
logger.debug("Converting IPs only of network %s" % network.id) |
55 |
public_ips = public_ips.filter(network=network) |
56 |
|
57 |
logger.info("Converting %d public IPv4 addresses to floating IPs.", |
58 |
public_ips.count()) |
59 |
|
60 |
if not public_ips: |
61 |
logger.info("No public IPs to convert.") |
62 |
return |
63 |
|
64 |
if options.debug: |
65 |
addresses = public_ips.values_list("address", flat=True) |
66 |
logger.debug("Converting the following public IPs:\n%s", |
67 |
"\n".join(addresses)) |
68 |
|
69 |
if not options.dry_run: |
70 |
public_ips.update(floating_ip=True) |
71 |
|
72 |
logger.info("Successfully updated public addresses to floating IPs.") |
73 |
return |
74 |
|
75 |
|
76 |
if __name__ == "__main__": |
77 |
main() |
78 |
sys.exit(0) |