def setup_dns_records(provider_id):
    status = {
        "command": "setup_dns_records",
        "status": False,
        "message": "FALSE"
    }

    # Now check to make sure provider id is valid, die quickly otherwise.
    db_conn = wgm_db.connect()
    #print("DB Connection Open")
    cur = db_conn.cursor(cursor_factory=psycopg2.extras.DictCursor)

    #print("Provider id: %s" % provider_id)
    get_provider_sql = "SELECT * FROM dns_providers WHERE id=%s;" % provider_id

    # query db for provider
    cur.execute(get_provider_sql)
    get_provider_row = cur.fetchone()
    # make sure we have a legit provider
    if get_provider_row is None:
        status['message'] = "Provider Does Not Exist"
        return status

    provider_type = get_provider_row['type']
    #print("Provider Type: "+provider_type)

    if provider_type == "1":
        #print("Cloudflare DNS Provider")
        # get auth_token
        get_auth_sql = "SELECT auth_key0 FROM dns_auth_configs WHERE provider=" + str(
            provider_id)
        cur.execute(get_auth_sql)
        get_auth_row = cur.fetchone()
        if get_auth_row is None:
            status['message'] = "Can't get Auth Token for Provider"
            return status

        # Auth Acquired
        auth_token = get_auth_row['auth_key0']

        # foreach provider zone, update A records
        get_zones_sql = "SELECT id, value FROM dns_zones WHERE provider=" + str(
            provider_id)
        cur.execute(get_zones_sql)
        #print("Getting Records")
        get_zones_result = cur.fetchall()
        # Foreach zone, get records
        for res in get_zones_result:
            #print("Zone: "+res['value'])
            #print("ID: "+str(res['id']))
            zone_id = str(res['id'])

            # Delete all records for current zone, the unique ids from the provider should remain the same so this doesn't impact current configured servers
            delete_zone_records_sql = "DELETE FROM dns_zone_records WHERE zone=" + zone_id
            cur.execute(delete_zone_records_sql)
            db_conn.commit()

            zone_records = cf.get_dns_records(res['value'], auth_token)
            # foreach record, make entry in DB
            for record in zone_records:
                uid = wgm_db.get_unique_id(64, 'dns_zone_records', db_conn)
                insert_records_sql = "INSERT INTO dns_zone_records (name, type, content, zone, ttl, unique_id, provider_uid) VALUES (%s, %s, %s, %s, %s, %s, %s)"
                cur.execute(insert_records_sql,
                            (record['name'], record['type'], record['content'],
                             res['id'], record['ttl'], uid, record['id']))
                db_conn.commit()

    status['status'] = True
    status['message'] = "Success"
    return status
def setup_dns_zones(provider_id):
    status = {
        "command": "setup_dns_zones",
        "status": False,
        "message": "FALSE"
    }

    # Now check to make sure provider id is valid, die quickly otherwise.
    db_conn = wgm_db.connect()
    #print("DB Connection Open")
    cur = db_conn.cursor(cursor_factory=psycopg2.extras.DictCursor)

    #print("Provider id: %s" % provider_id)
    get_provider_sql = "SELECT * FROM dns_providers WHERE id=%s;" % provider_id

    # query db for provider
    cur.execute(get_provider_sql)
    get_provider_row = cur.fetchone()
    # make sure we have a legit provider
    if get_provider_row is None:
        status['message'] = "Provider Does Not Exist"
        return status

    provider_type = get_provider_row['type']
    #print("Provider Type: "+provider_type)

    if provider_type == "1":
        #print("Cloudflare Provider")
        current_zones = list()
        # Get current zones
        get_cur_zones_sql = "SELECT * FROM dns_zones WHERE provider=" + str(
            provider_id)
        cur.execute(get_cur_zones_sql)
        get_cur_zones_ret = cur.fetchall()
        #print(len(get_cur_zones_ret))
        for cur_zone in get_cur_zones_ret:
            #print(cur_zone['name'])
            current_zones.append(cur_zone['value'])

        #print(current_zones)

        get_auth_sql = "SELECT auth_key0 FROM dns_auth_configs WHERE provider=%s" % provider_id
        cur.execute(get_auth_sql)
        get_auth_row = cur.fetchone()
        if get_auth_row is None:
            status['message'] = "Can't get Auth Token for Provider"
            return status

        # Auth Acquired
        auth_token = get_auth_row['auth_key0']
        # Get Zones from DNS Provider
        zones = cf.get_dns_zones(auth_token)

        # Update with New Zones Only
        new_zones = list()
        for zone in zones:
            new_zones.append(zone['id'])
            if zone['id'] not in current_zones:
                uid = wgm_db.get_unique_id(64, 'dns_zones', db_conn)
                #print("Found Zone: "+zone['name'])
                insert_zone_sql = "INSERT INTO dns_zones (name, value, unique_id, provider) VALUES(%s, %s, %s, %s)"
                cur.execute(insert_zone_sql,
                            (zone['name'], zone['id'], uid, provider_id))
                db_conn.commit()

        # Cleanup Old Zones
        for zone in current_zones:
            if zone not in new_zones:
                print("Old Zone")
                get_zone_id_sql = "SELECT id FROM dns_zones WHERE value='" + zone + "'"
                cur.execute(get_zone_id_sql)
                get_zone_id_ret = cur.fetchone()
                zid = get_zone_id_ret['id']
                delete_zone_records_sql = "DELETE FROM dns_zone_records WHERE zone=" + str(
                    zid)
                cur.execute(delete_zone_records_sql)
                db_conn.commit()
                cur.execute("DELETE FROM dns_zones WHERE id=" + str(zid))
                db_conn.commit()

    else:
        status['message'] = "Provider type unsupported"
        return status

    status['status'] = True
    status['message'] = "Success"

    return status
Пример #3
0
def add_dns_record(zone_id, name, rtype, content, ttl):
    record_name = name
    record_type = rtype
    record_content = content
    record_ttl = ttl
    status = {
        "command": "add_dns_record",
        "status": False,
        "message": "False",
        "record_uid": ""
    }

    db_conn = wgm_db.connect()
    #print("DB Connection Open")
    cur = db_conn.cursor(cursor_factory=psycopg2.extras.DictCursor)

    print("Zone id: %s" % zone_id)
    # Find out Zone specific information
    get_zone_sql = "SELECT * FROM dns_zones WHERE id=" + zone_id
    cur.execute(get_zone_sql)
    get_zone_row = cur.fetchone()
    if get_zone_row is None:
        #print("Zone Not Found")
        status['message'] = "Zone Not Found"
        return status

    # Setup Zone provider_uid
    zone_uid = get_zone_row['value']
    #print("Zone value: "+zone_uid)

    # Find out Provider Specific Information
    get_provider_sql = "SELECT p.type,p.id FROM dns_providers AS p JOIN dns_zones AS z ON z.provider=p.id WHERE z.id=" + zone_id
    # query db for zone
    cur.execute(get_provider_sql)
    get_provider_row = cur.fetchone()
    # make sure we have a legit digitalocean provider
    if get_provider_row is None:
        #print("Provider Does Not Exist")
        status['message'] = "Provider Does Not Exist"
        return status

    # Set up provider variables
    provider_id = str(get_provider_row['id'])
    provider_type = get_provider_row['type']

    if provider_type == "1":  ## ADD A RECORD FOR A CLOUDFLARE DNS PROVIDER TYPE
        #print("Cloudflare DNS Provider")
        #print("Unique Provider ID: "+provider_id)
        # Now we can get the auth_token
        get_auth_sql = "SELECT auth_key0 FROM dns_auth_configs WHERE provider=" + provider_id
        cur.execute(get_auth_sql)
        get_auth_row = cur.fetchone()
        if get_auth_row is None:
            #print("Auth Token Unavailable")
            status['message'] = "Auth Token Unavailable"
            return status

        # Setup Auth token
        auth_token = get_auth_row['auth_key0']

        # We should have everything we need now to create the record in Cloudflare
        #print("Creating Record in Cloudflare")
        record_uid = cf.add_dns_record(zone_uid, auth_token, record_name,
                                       record_type, record_content, record_ttl)
        if record_uid != "FALSE":
            #print("Successfully created record in Cloudflare with uid: "+record_uid)
            #print("---SUCCESS---")
            #print("Updating Database")
            new_unique_id = wgm_db.get_unique_id(64, 'dns_zone_records',
                                                 db_conn)
            sql = "INSERT INTO dns_zone_records (name, type, content, zone, ttl, unique_id, provider_uid) VALUES (%s, %s, %s, %s, %s, %s, %s)"
            cur.execute(sql, (record_name, record_type, record_content,
                              zone_id, record_ttl, new_unique_id, record_uid))
            db_conn.commit()
            #print("DB Updated")
        else:
            #print("---FAILED---")
            status['message'] = "Failed to Create DNS Record"
            return status

    else:
        #print("Unknown Provider Type, Failing")
        status['message'] = "Unknown Provider Type"
        return status

    status['message'] = "Success"
    status['status'] = True
    status['record_uid'] = record_uid
    return status
def create_dns_server(name, dns_type, provider_id, provider_image,
                      provider_zone):
    status = {"status": False, "message": "FALSE"}

    # Now check to make sure provider id is valid, die quickly otherwise.
    db_conn = wgm_db.connect()
    #print("DB Connection Open")
    cur = db_conn.cursor(cursor_factory=psycopg2.extras.DictCursor)

    #print("Provider id: %s" % provider_id)
    get_provider_sql = "SELECT * FROM iaas_providers WHERE id=" + provider_id

    # query db for provider
    cur.execute(get_provider_sql)
    get_provider_row = cur.fetchone()
    # make sure we have a legit digitalocean provider
    if get_provider_row is None:
        status['message'] = "Provider Does Not Exist"
        return status

    provider_type = get_provider_row['type']

    # Now check to make sure provider image is valid, die quickly otherwise.
    get_image_sql = "SELECT * FROM iaas_vm_images WHERE provider=%s AND id=%s"
    cur.execute(get_image_sql, (provider_id, provider_image))
    get_image_row = cur.fetchone()
    if get_image_row is None:
        status['message'] = "Image does not exist"
        return status

    image_value = get_image_row['value']

    # Now check to make sure provider zone is valid, die quickly otherwise.
    get_zone_sql = "SELECT * FROM iaas_zones WHERE id=%s AND provider=%s"
    cur.execute(get_zone_sql, (provider_zone, provider_id))
    get_zone_row = cur.fetchone()
    if get_zone_row is None:
        status['message'] = "Image does not exist"
        return status

    zone_value = get_zone_row['value']

    if provider_type == 1:
        #print("DigitalOcean IaaS Provider")
        # THERE ARE SOME DEFAULTS ASSUMED AT THE MOMENT
        ipv6 = True
        backups = False
        size = "s-1vcpu-2gb"
        tags = ["wgm", "dns"]

        # Get Auth token
        get_auth_sql = "SELECT * FROM iaas_auth_configs WHERE provider=" + provider_id
        cur.execute(get_auth_sql)
        get_auth_row = cur.fetchone()
        if get_auth_row is None:
            status['message'] = "Unable to get Auth Token"
            return status

        auth_token = get_auth_row['auth_key0']

        db_keys = [
            get_auth_row['ssh_key0'], get_auth_row['ssh_key1'],
            get_auth_row['ssh_key2']
        ]
        config_keys = []
        for key in db_keys:
            if key != "-":
                ssh = sshpubkeys.SSHKey(key)
                config_keys.append(ssh.hash_md5().replace('MD5:', ''))

        config = {
            "token": auth_token,
            "name": name,
            "region": zone_value,
            "image": image_value,
            "size": size,
            "ssh_keys": config_keys,
            "tags": tags,
            "ipv6": ipv6,
            "backups": ipv6
        }

        #print(config)
        print("Deploying DNS Server")
        result = do.create_do_droplet(config)
        server = result['droplet']
        if server is None:
            status['message'] = result['message']
            return status

    else:
        print("Unknown IaaS Provider")
        status['message'] = "Unknown IaaS Provider"
        return status

    # Update DB
    new_unique_id = wgm_db.get_unique_id(64, 'dns_servers', db_conn)
    new_dns_server_sql = "INSERT INTO dns_servers (dns_name,type,ipv4_addr,ipv6_addr,unique_id,provider,provider_uid) VALUES (%s, %s, %s, %s, %s, %s, %s)"
    cur.execute(new_dns_server_sql,
                (name, dns_type, server.ip_address, server.ip_v6_address,
                 new_unique_id, provider_id, str(server.id)))
    db_conn.commit()

    status['status'] = True
    status['message'] = "Success"
    return status
def create_gw_server(name, dns_zone, provider_id, provider_image,
                     provider_zone):
    status = {
        "command": "create_gw_server",
        "status": False,
        "message": "FALSE"
    }
    vm_exists = False

    # Now check to make sure provider id is valid, die quickly otherwise.
    db_conn = wgm_db.connect()
    #print("DB Connection Open")
    cur = db_conn.cursor(cursor_factory=psycopg2.extras.DictCursor)

    #print("Provider id: %s" % provider_id)
    get_provider_sql = "SELECT * FROM iaas_providers WHERE id=" + provider_id

    # query db for provider
    cur.execute(get_provider_sql)
    get_provider_row = cur.fetchone()
    # make sure we have a legit digitalocean provider
    if get_provider_row is None:
        status['message'] = "Provider Does Not Exist"
        return status

    provider_type = get_provider_row['type']

    # Now check to make sure dns_zone is valid, die quickly otherwise
    get_dns_zone_sql = "SELECT * FROM dns_zones WHERE id=" + dns_zone
    cur.execute(get_dns_zone_sql)
    get_dns_zone_ret = cur.fetchone()
    if get_dns_zone_ret is None:
        status['message'] = "DNS Zone Does Not Exist"
        return status

    # Now check to makesure the dns_provider is valid, die quickly otherwise
    get_dns_provider_sql = "SELECT * FROM dns_providers WHERE id=" + str(
        get_dns_zone_ret['provider'])
    cur.execute(get_dns_provider_sql)
    get_dns_provider_ret = cur.fetchone()
    if get_dns_provider_ret is None:
        status['message'] = "DNS Provider Does Not Exist"
        return status

    dns_provider_id = str(get_dns_zone_ret['provider'])
    dns_provider_type = get_dns_provider_ret['type']

    # Now check to make sure provider image is valid, die quickly otherwise.
    get_image_sql = "SELECT * FROM iaas_vm_images WHERE provider=%s AND id=%s"
    cur.execute(get_image_sql, (provider_id, provider_image))
    get_image_row = cur.fetchone()
    if get_image_row is None:
        status['message'] = "Image does not exist"
        return status

    image_value = get_image_row['value']

    # Now check to make sure provider zone is valid, die quickly otherwise.
    get_zone_sql = "SELECT * FROM iaas_zones WHERE id=%s AND provider=%s"
    cur.execute(get_zone_sql, (provider_zone, provider_id))
    get_zone_row = cur.fetchone()
    if get_zone_row is None:
        status['message'] = "Image does not exist"
        return status

    zone_value = get_zone_row['value']

    if provider_type == 1:
        print("DigitalOcean IaaS Provider")
        #print("DigitalOcean IaaS Provider")
        # THERE ARE SOME DEFAULTS ASSUMED AT THE MOMENT
        ipv6 = True
        backups = False
        size = "s-1vcpu-2gb"
        tags = ["wgm", "wg", "vpn"]

        # Get Auth token
        get_auth_sql = "SELECT * FROM iaas_auth_configs WHERE provider=" + provider_id
        cur.execute(get_auth_sql)
        get_auth_row = cur.fetchone()
        if get_auth_row is None:
            status['message'] = "Unable to get Auth Token"
            return status

        auth_token = get_auth_row['auth_key0']

        db_keys = [
            get_auth_row['ssh_key0'], get_auth_row['ssh_key1'],
            get_auth_row['ssh_key2']
        ]
        config_keys = []
        for key in db_keys:
            if key != "-":
                ssh = sshpubkeys.SSHKey(key)
                config_keys.append(ssh.hash_md5().replace('MD5:', ''))

        config = {
            "token": auth_token,
            "name": name,
            "region": zone_value,
            "image": image_value,
            "size": size,
            "ssh_keys": config_keys,
            "tags": tags,
            "ipv6": ipv6,
            "backups": ipv6
        }
        #print(config)

        # Build Server
        #print("Deploying GW Server")
        result = do.create_do_droplet(config)
        server = result['droplet']
        if result['droplet'] is None:
            status['message'] = result['message']
            return status

    else:
        status['message'] = "Unsupported IaaS Provider"
        return status

    # Server is deployed, let's setup DNS
    if dns_provider_type == "1":
        print("Cloudflare DNS Provider")
        # Some Defaults are Assumed
        rtype = "A"
        ttl = "120"

        #def add_dns_record(zone_id,name,rtype,content,ttl)
        record_status = dns.add_dns_record(dns_zone, name, rtype,
                                           server.ip_address, ttl)
        if record_status['status'] == False:
            status['message'] = "Failed to Create DNS Record"
            # Failed to create the DNS Record, let's destroy the droplet
            result = do.destroy_do_droplet(str(server.id), auth_token)
            if result == False:
                status[
                    'message'] = "Failed to create the DNS Record, Also Failed to Destroy DigitalOcean Droplet"
                return status

        #print(record_status['record_uid'])

    else:
        status['message'] = "Unsupported DNS Provider"

    # Okay, server was successfully deployed and the DNS record was successfully created. Let's update the DB
    new_unique_id = wgm_db.get_unique_id(64, 'gw_servers', db_conn)
    new_gw_server_sql = "INSERT INTO gw_servers (name,pub_ipv4_addr,pub_ipv6_addr,dns_record_uid,provider,provider_uid,dns_zone, unique_id, dns_provider) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"
    cur.execute(new_gw_server_sql,
                (name, server.ip_address, server.ip_v6_address,
                 record_status['record_uid'], provider_id, server.id, dns_zone,
                 new_unique_id, dns_provider_id))
    db_conn.commit()

    status['message'] = "Success"
    status['status'] = True

    return status
def setup_iaas_images(provider_id):
    status = {
        "command": "setup_iaas_images",
        "status": False,
        "message": "FALSE",
        'provider_id': provider_id
    }

    # Now check to make sure provider id is valid, die quickly otherwise.
    db_conn = wgm_db.connect()
    #print("DB Connection Open")
    cur = db_conn.cursor(cursor_factory=psycopg2.extras.DictCursor)

    #print("Provider id: %s" % provider_id)
    get_provider_sql = "SELECT * FROM iaas_providers WHERE id=" + provider_id

    # query db for provider
    cur.execute(get_provider_sql)
    get_provider_row = cur.fetchone()
    # make sure we have a legit digitalocean provider
    if get_provider_row is None:
        status['message'] = "Provider Does Not Exist"
        return status

    provider_type = get_provider_row['type']

    if provider_type == 1:
        #print("DigitalOcean IaaS Provider")

        # provider is digitalocean type, let's get the token
        get_auth_sql = "SELECT auth_key0 FROM iaas_auth_configs WHERE provider=%s" % provider_id
        cur.execute(get_auth_sql)
        get_auth_row = cur.fetchone()
        if get_auth_row is None:
            status['message'] = "No Auth Token Found"
            return status
        auth_token = get_auth_row['auth_key0']

        # Get current images
        current_images = dict()
        get_cur_images_sql = "SELECT * FROM iaas_vm_images"
        cur.execute(get_cur_images_sql)
        get_cur_images_ret = cur.fetchall()
        for image in get_cur_images_ret:
            current_images[image['id']] = image['value']
            #print(image['value'])

        #print(current_images)

        # Let's get the relevant available images and update the database
        new_images = do.get_do_images(provider_id, auth_token)
        new_image_values = list()
        for image in new_images:
            new_image_values.append(str(image.id))
            matching_record = False
            for key in current_images:
                if current_images[key] == str(image.id):
                    matching_record = True
                    # This is an image we already know about. Update name
                    #print("Image Name: "+image.name)
                    #print("Record ID: "+str(key))
                    update_image_sql = "UPDATE iaas_vm_images SET name = %s WHERE id=%s"
                    cur.execute(update_image_sql, (image.name, key))
                    db_conn.commit()
            if matching_record is False:
                # No matching record currently, make a new one
                new_uid = wgm_db.get_unique_id(64, 'iaas_vm_images', db_conn)
                new_image_sql = "INSERT INTO iaas_vm_images (name,value,unique_id,provider) VALUES (%s, %s, %s, %s)"
                cur.execute(new_image_sql,
                            (image.name, str(image.id), new_uid, provider_id))
                db_conn.commit()

        # Now let's cleanup the old images
        for key in current_images:
            if current_images[key] not in new_image_values:
                # Old image, delete it
                delete_image_sql = "DELETE FROM iaas_vm_images WHERE id=" + str(
                    key)
                cur.execute(delete_image_sql)
                db_conn.commit()

    status['status'] = True
    status['message'] = "Success"
    return status
def setup_iaas_zones(provider_id):
    status = {
        "command": "setup_iaas_zones",
        "status": False,
        "message": "FALSE",
        'provider_id': provider_id
    }

    # Now check to make sure provider id is valid, die quickly otherwise.
    db_conn = wgm_db.connect()
    #print("DB Connection Open")
    cur = db_conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
    #print("Provider id: %s" % provider_id)
    get_provider_sql = "SELECT * FROM iaas_providers WHERE id=" + provider_id
    # query db for provider
    cur.execute(get_provider_sql)
    get_provider_row = cur.fetchone()
    # make sure we have a legit digitalocean provider
    if get_provider_row is None:
        status['message'] = "Provider Does Not Exist"
        return status
    provider_type = get_provider_row['type']

    if provider_type == 1:
        #print("DigitalOcean IaaS Provider")

        # provider is digitalocean type, let's get the token
        get_auth_sql = "SELECT auth_key0 FROM iaas_auth_configs WHERE provider=" + provider_id
        cur.execute(get_auth_sql)
        get_auth_row = cur.fetchone()
        if get_auth_row is None:
            status['message'] = "No Auth Token Found"
            return status
        auth_token = get_auth_row['auth_key0']

        # Get current zones(regions) to check against
        get_cur_zones = "SELECT * FROM iaas_zones WHERE provider=" + provider_id
        cur.execute(get_cur_zones, (provider_id))
        get_cur_zones_ret = cur.fetchall()

        cur_zones = list()
        for zone in get_cur_zones_ret:
            cur_zones.append(zone['value'])
        #print(cur_zones)

        # Let's get the available regions and update the database
        regions = do.get_do_regions(provider_id, auth_token)
        new_slugs = list()
        for region in regions:
            new_slugs.append(region.slug)
            if region.slug not in cur_zones:
                # This is a new slug, add it to the DB
                new_uid = wgm_db.get_unique_id(64, 'iaas_zones', db_conn)
                new_slug_sql = "INSERT INTO iaas_zones (name, value,unique_id,provider) VALUES (%s, %s, %s, %s)"
                cur.execute(new_slug_sql,
                            (region.name, region.slug, new_uid, provider_id))
                db_conn.commit()

        # Great, now let's delete zones that don't exist anymore
        for zone in cur_zones:
            if zone not in new_slugs:
                #print(zone)
                delete_zone_sql = "DELETE FROM iaas_zones WHERE value='" + zone + "' AND provider=" + provider_id
                cur.execute(delete_zone_sql)
                db_conn.commit()

    else:
        status['message'] = "Provider Type Unknown"
        return status

    status['status'] = True
    status['message'] = "Success"
    return status