def list_ucsc(handle, wanted): """ Get all the drives of the servers listed and print them out. """ try: all_servers = UCSCServer.list_servers(handle) ucs_servers = UCSCUtil.servers_to_objects(all_servers, wanted) except KubamError as e: UCSCUtil.ucsc_logout(handle) return {"error": str(e)}, Const.HTTP_BAD_REQUEST disks = {} from ucscsdk.mometa.storage.StorageLocalDisk import StorageLocalDisk for i in ucs_servers: try: server_disks = UCSCServer.list_disks(handle, i) disks[i['dn']] = [] for d in server_disks: # d.__dict__ flattens the object to a dictionary. kv = d.__dict__ kv = dict((key, value) for key, value in kv.iteritems() if not key.startswith('_') ) disks[i['dn']].append( kv) except KubamError as e: UCSUtil.ucs_logout(handle) return {"error": str(e)}, Const.HTTP_BAD_REQUEST out = UCSCUtil.dn_hash_to_out(disks) UCSCUtil.ucsc_logout(handle) return out, Const.HTTP_OK
def power_server_ucsc(sg, servers, action): """ perform power operations (hardreset, off, on..) for UCS Central """ try: handle = UCSCUtil.ucsc_login(sg) except Exception as e: return jsonify({"error": str(e)}), Const.HTTP_UNAUTHORIZED try: ucsc_servers = ucsc_servers_to_objects(handle, servers) except KubamError as e: UCSCUtil.ucsc_logout(handle) return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST for i in ucsc_servers: try: UCSCServer.power_server(handle, i, action) except KubamError as e: UCSUtil.ucs_logout(handle) return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST UCSUtil.ucs_logout(handle) powerstat = UCSCUtil.objects_to_servers(ucsc_servers, ["oper_power"]) return jsonify({"status": powerstat}), Const.HTTP_CREATED
def create_servers(req): """ Create a new UCS Domain Format of request should be JSON that looks like: {"name", "ucs01", "type" : "ucsm", "credentials": {"user": "******", "password": "******", "ip" : "172.28.225.163" }} """ err, msg = YamlDB.check_valid_server_group(req) if err != 0: return {"error": msg}, Const.HTTP_BAD_REQUEST # Make sure we can log in first. if not 'type' in req: return {"error": "No server type sent as part of request"}, Const.HTTP_UNAUTHORIZED if req['type'] == "ucsm": try: UCSUtil.check_ucs_login(req) except KubamError as e: return {"error": str(e)}, Const.HTTP_UNAUTHORIZED elif req["type"] == "ucsc": try: UCSCUtil.check_ucsc_login(req) except KubamError as e: return {"error":str(e)}, Const.HTTP_UNAUTHORIZED elif req["type"] == "imc": try: IMCUtil.check_imc_login(req) except KubamError as e: return {"error":str(e)}, Const.HTTP_UNAUTHORIZED else: return {"error": "type: {0} is not recognized".format(req["type"])}, Const.HTTP_UNAUTHORIZED db = YamlDB() err, msg = db.new_server_group(Const.KUBAM_CFG, req) if err == 1: return {"error": msg}, Const.HTTP_BAD_REQUEST return {"status": "new server group {0} created!".format(req['name'])}, Const.HTTP_CREATED
def powerstat_ucsc(sg, wanted_servers): """ Get the power status of the UCS Central servers. """ try: handle = UCSCUtil.ucsc_login(sg) except KubamError as e: return jsonify({"error": str(e)}), Const.HTTP_UNAUTHORIZED try: powerstat = UCSCServer.list_servers(handle) if not wanted_servers == "all": powerstat = UCSCUtil.servers_to_objects(powerstat, wanted_servers) powerstat = UCSCUtil.objects_to_servers(powerstat, ["oper_power"]) except KubamError as e: UCSCUtil.ucsc_logout(handle) UCSCUtil.ucsc_logout(handle) return powerstat
def disk_operation(server_group): """ Figure out the operation and do it. """ wanted = "all" try: db = YamlDB() sg = db.get_server_group(Const.KUBAM_CFG, server_group) except KubamError as e: return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST if request.json and "servers" in request.json: wanted = request.json["servers"] ## login to UCS Manager and do the action. if sg["type"] == "ucsm": try: handle = UCSUtil.ucs_login(sg) except KubamError as e: return jsonify({"error": str(e)}), Const.HTTP_UNAUTHORIZED if request.method == "DELETE": js, rc = Disks.delete_ucsm(handle, wanted) js, rc = Disks.list_ucsm(handle, wanted) return jsonify(js), rc ## login to UCS Central and do the action elif sg["type"] == "ucsc": try: handle = UCSCUtil.ucsc_login(sg) except KubamError as e: return jsonify({"error": str(e)}), Const.HTTP_UNAUTHORIZED if request.method == "DELETE": js, rc = Disks.delete_ucsc(handle, wanted) js, rc = Disks.list_ucsc(handle, wanted) return jsonify(js), rc
def get_server_status_ucsc(sg, wanted): try: handle = UCSCUtil.ucsc_login(sg) except KubamError as e: return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST try: all_servers = UCSCServer.list_servers(handle) if not wanted == "all": all_servers = UCSCUtil.servers_to_objects(all_servers, wanted) # put in dn name format status = {} for i in all_servers: status[i['dn']] = i out = UCSCUtil.dn_hash_to_out(status) except KubamError as e: UCSCUtil.ucsc_logout(handle) return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST UCSCUtil.ucsc_logout(handle) return jsonify({"servers": out}), Const.HTTP_OK
def ucsc_fsm(sg, wanted): """ Get the FSM of the servers in UCS Central """ try: handle = UCSCUtil.ucsc_login(sg) except KubamError as e: return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST try: all_servers = UCSCServer.list_servers(handle) if not wanted == "all": all_servers = UCSCUtil.servers_to_objects(all_servers, wanted) # put in dn name format status = {} for i in all_servers: status[i['dn']] = UCSCMonitor.get_fsm(handle, i) out = UCSCUtil.dn_hash_to_out(status) except KubamError as e: UCSCUtil.ucsc_logout(handle) return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST UCSCUtil.ucsc_logout(handle) return jsonify({"servers": out}), Const.HTTP_OK
def create_vmedia(server_group): """ Create the Vmedia policy for a server group """ db = YamlDB() # get server group. try: sg = db.get_server_group(Const.KUBAM_CFG, server_group) except KubamError as e: return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST org = "org-root" if "org" in sg: org = sg["org"] # go through all hosts associated with this server group err, msg, hosts = db.list_hosts(Const.KUBAM_CFG) if err == 1: return jsonify({'error': msg}), Const.HTTP_BAD_REQUEST hosts = [x for x in hosts if 'server_group' in x and x['server_group'] == server_group] if len(hosts) < 1: return jsonify({'error': 'no hosts associated with server group'}), Const.HTTP_OK # get the os image they use oses = list(set([ x["os"] for x in hosts])) # create those vmedia policies err = 0 msg = "" err, msg, kubam_ip = db.get_kubam_ip(Const.KUBAM_CFG) if kubam_ip is None: return jsonify({'error': 'Please define the KUBAM IP first.'} ), Const.HTTP_OK handle = "" if sg['type'] == 'ucsm': try: handle = UCSUtil.ucs_login(sg) except KubamError as e: return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST err, msg = UCSServer.make_vmedias(handle, org, kubam_ip, oses) UCSUtil.ucs_logout(handle) elif sg['type'] == 'ucsc': try: handle = UCSCUtil.ucsc_login(sg) except KubamError as e: return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST err, msg = UCSCServer.make_vmedias(handle, org, kubam_ip, oses) UCSCUtil.ucsc_logout(handle) elif sg['type'] == 'imc': try: handle = IMCUtil.imc_login(sg) except KubamError as e: return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST try: IMCServer.mount_media(handle, kubam_ip, hosts[0]['name'], oses[0]) except KubamError as e: IMCUtil.imc_logout(handle) return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST IMCUtil.imc_logout(handle) if err != 0: return jsonify({'error': msg}), Const.HTTP_BAD_REQUEST return jsonify({"status": oses}), Const.HTTP_CREATED