Created
March 8, 2018 13:52
-
-
Save wido/14c951e2856f2a35eebd69450aa102b5 to your computer and use it in GitHub Desktop.
Add CloudStack IPv6 Security Group rules after upgrade to CloudStack 4.10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import uuid | |
import mysql.connector | |
MYSQL_PASS = 'XXXXXXXXX' | |
cnx = mysql.connector.connect(password=MYSQL_PASS, user='root', database='cloud') | |
cursor = cnx.cursor() | |
cursor.execute("SELECT id FROM security_group") | |
security_groups = [] | |
for row in cursor.fetchall(): | |
security_groups.append(row[0]) | |
cursor.close() | |
for security_group_id in security_groups: | |
add_rule_query = "INSERT INTO security_group_rule (security_group_id, uuid, type, start_port, end_port, protocol, allowed_ip_cidr) VALUES (%s, %s, 'ingress', %s, %s, %s, '::/0')" | |
protocol_query = "SELECT id FROM security_group_rule WHERE start_port = 0 AND end_port = 65535 AND protocol = %s AND allowed_ip_cidr = '::/0' AND type = 'ingress' AND security_group_id = %s" | |
for protocol in ['tcp', 'udp']: | |
cursor = cnx.cursor() | |
cursor.execute(protocol_query, (protocol, security_group_id)) | |
if len(cursor.fetchall()) == 0: | |
rule_uuid = str(uuid.uuid4()) | |
cursor.execute(add_rule_query, (security_group_id, rule_uuid, 0, 65535, protocol)) | |
cursor.close() | |
icmp_query = "SELECT id FROM security_group_rule WHERE start_port = 128 AND end_port = 0 AND protocol = 'icmp' AND allowed_ip_cidr = '::/0' AND type = 'ingress' AND security_group_id = %s" | |
cursor = cnx.cursor() | |
cursor.execute(icmp_query, (security_group_id,)) | |
if len(cursor.fetchall()) == 0: | |
rule_uuid = str(uuid.uuid4()) | |
cursor.execute(add_rule_query, (security_group_id, rule_uuid, 128, 0, 'icmp')) | |
cursor.close() | |
cnx.commit() | |
cnx.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment