Exemple #1
0
def create_vm(vm_data):
    server = Server(name=vm_data['name'],
                    image=vm_data['image'],
                    networks=vm_data['networks'],
                    region=vm_data['region'],
                    driver=vm_data['driver'],
                    flavor=vm_data['flavor'],
                    key=vm_data['key'])
    server.create()
Exemple #2
0
def delete(cluster_id):
    cluster = db.clusters.find_one({'_id': ObjectId(cluster_id)})
    for vm in cluster['vms']:
        vm = Server(_id=vm['_id'])
        vm.swarm_leave()

    delete_cluster_id.delay(cluster_id)
    delete_rule.delay(cluster_id)
    db.clusters.remove({'_id': ObjectId(cluster_id)})
    return 'cluster removed'
Exemple #3
0
def translate_id(id):
    ips = []
    server = Server(_id=_id)
    network_interfaces = server.ips
    for nic in network_interfaces:
        ips.append(network_interfaces[nic][0]['addr'])

    return ips
Exemple #4
0
def add_to_cluster(cluster):
    cluster = db.clusters.find_one({'_id': ObjectId(cluster)})
    manager = None
    for vm in cluster['vms']:
        if vm['role'] == 'manager':
            manager = vm

    server = Server(
        name='scaled_vm',
        image='Ubuntu-16-04',
        networks=['ece1548-net'],
        region='CORE',
        driver='openstack',
        flavor='m1.small',
        key='swascale_key'
        )
    server.create()
    manager = Server(_id=manager['_id'])
    server.swarm_join_worker(manager)
    cluster['vms'].append({
        '_id': str(server.uid),
        'role': 'worker'
    })
    cluster = db.clusters.update_one({'_id': cluster['_id']}, {
        '$set': {
            'vms': cluster['vms']
        }
    })

    update_targets()
Exemple #5
0
def update_targets():
    clusters = db.clusters.find({})
    prometheusTargets = []
    with open('config/targets.json', 'w') as outfile:
        for cluster in clusters:
            targets = []
            for target in cluster['vms']:
                vm = Server(_id=target['_id'])
                targets.append(vm.ips[vm.networks[0]][0]['addr'] + ':' +
                               cfg.prometheus['PROMETHEUS_PORT'])

            prometheusTargets.append(
                {'targets': targets, 'labels': {
                    'cluster': str(cluster['_id'])
                }}
            )

        json.dump(prometheusTargets, outfile)
Exemple #6
0
def remove_from_cluster(cluster):
    cluster = db.clusters.find_one({'_id': ObjectId(cluster)})
    worker = None
    for vm in cluster['vms']:
        if vm['role'] == 'worker':
            worker = vm
            break

    if worker is not None:
        cluster['vms'].remove(worker)
        worker = Server(_id=worker['_id'])
        worker.swarm_leave()
        Server.delete(worker.uid)

        cluster = db.clusters.update_one({'_id': cluster['_id']}, {
            '$set': {
                'vms': cluster['vms']
            }
        })

        update_targets()
Exemple #7
0
def truncate():
    Server.truncate()
    return 'truncate'
Exemple #8
0
def delete(uid):
    Server.delete(uid)
    return 'deleted'
Exemple #9
0
def create():
    targets = []
    vm_manager = None
    for vm in request.json.get('vms'):
        if vm['role'] == 'manager':
            vm_manager = vm
            break
    server = Server(_id=vm_manager['_id'])
    server.swarm_init()
    targets.append(server.ips[server.networks[0]][0]['addr'] + ':' +
                   cfg.prometheus['PROMETHEUS_PORT'])

    for vm in request.json.get('vms'):
        if vm['_id'] != vm_manager['_id']:
            if vm['role'] == 'manager':
                manager = Server(_id=vm['_id'])
                manager.swarm_join_manager(server)
                targets.append(manager.ips[manager.networks[0]][0]['addr'] +
                               ':' + cfg.prometheus['PROMETHEUS_PORT'])
            elif vm['role'] == 'worker':
                worker = Server(_id=vm['_id'])
                worker.swarm_join_worker(server)
                targets.append(worker.ips[worker.networks[0]][0]['addr'] +
                               ':' + cfg.prometheus['PROMETHEUS_PORT'])
    cluster = db.clusters.insert({'vms': request.json.get('vms')})
    if 'up' in request.get_json():
        rule = request.json.get('up')
        direction = 'up'
        db.clusters.update_one({'_id': cluster}, {
            '$set': {'up': request.json.get('up')}
            })

    if 'down' in request.get_json():
        rule = request.json.get('down')
        direction = 'down'
        db.clusters.update_one({'_id': cluster}, {
            '$set': {'down': request.json.get('down')}
            })

    add_cluster_id.delay(targets, str(cluster))

    return 'created'
Exemple #10
0
def create():
    targets = []
    vm_manager = None
    for vm in request.json.get('vms'):
        if vm['role'] == 'manager':
            vm_manager = vm
            break
    server = Server(_id=vm_manager['_id'])
    server.swarm_init()
    targets.append(server.ips[server.networks[0]][0]['addr'] + ':' +
                   cfg.prometheus['PROMETHEUS_PORT'])

    for vm in request.json.get('vms'):
        if vm['_id'] != vm_manager['_id']:
            if vm['role'] == 'manager':
                manager = Server(_id=vm['_id'])
                manager.swarm_join_manager(server)
                targets.append(manager.ips[manager.networks[0]][0]['addr'] +
                               ':' + cfg.prometheus['PROMETHEUS_PORT'])
            elif vm['role'] == 'worker':
                worker = Server(_id=vm['_id'])
                worker.swarm_join_worker(server)
                targets.append(worker.ips[worker.networks[0]][0]['addr'] +
                               ':' + cfg.prometheus['PROMETHEUS_PORT'])
    cluster = db.clusters.insert({'vms': request.json.get('vms')})
    add_cluster_id.delay(targets, str(cluster))

    return 'created'