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()
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'
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
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()
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)
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()
def truncate(): Server.truncate() return 'truncate'
def delete(uid): Server.delete(uid) return 'deleted'
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'
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'