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 get_templates(server_group): ucs_templates = [] db = YamlDB() sg = db.get_server_group(Const.KUBAM_CFG, server_group) if sg['type'] == "ucsc": handle = UCSCUtil.ucsc_login(sg) ucs_templates = UCSCTemplate.list_templates(handle) UCSCUtil.ucsc_logout(handle) elif sg['type'] == "ucsm": handle = UCSUtil.ucs_login(sg) ucs_templates = UCSTemplate.list_templates(handle) UCSUtil.ucs_logout(handle) return ucs_templates
def delete_ucsm(handle, wanted): try: all_servers = UCSServer.list_servers(handle) ucs_servers = UCSUtil.servers_to_objects(all_servers, wanted) except KubamError as e: UCSUtil.ucs_logout(handle) return {"error": str(e)}, Const.HTTP_BAD_REQUEST return {"status":"ok"}, 201 from ucsmsdk.mometa.storage.StorageLocalDisk import StorageLocalDisk for i in ucs_servers: try: UCSServer.reset_disks(handle, i) except KubamError as e: UCSUtil.ucs_logout(handle) return {"error": str(e)}, Cont.HTTP_BAD_REQUEST return {"status" : "ok"}, 201
def powerstat_ucsm(sg, wanted_servers): """ Get the power stat of the UCS Manager servers. """ try: handle = UCSUtil.ucs_login(sg) except KubamError as e: return jsonify({"error": str(e)}), Const.HTTP_UNAUTHORIZED try: powerstat = UCSServer.list_servers(handle) if not wanted_servers == "all": powerstat = UCSUtil.servers_to_objects(powerstat, wanted_servers) powerstat = UCSUtil.objects_to_servers(powerstat, ["oper_power"]) except KubamError as e: UCSUtil.ucs_logout(handle) return jsonify({"error": str(e)}), Const.HTTP_UNAUTHORIZED UCSUtil.ucs_logout(handle) return powerstat
def get_servers(server_group): """ List all the servers in the server group or in this case the domain. 1. Make call to UCS to grab the servers. 2. Make call to database to see which ones are selected. 3. Call servers_to_api which merges the two adding 'selected: true' to the servers that are selected. """ db = YamlDB() try: sg = db.get_server_group(Const.KUBAM_CFG, server_group) except KubamError as e: return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST if sg['type'] == "ucsm": try: handle = UCSUtil.ucs_login(sg) except KubamError as e: return jsonify({"error": str(e)}), Const.HTTP_UNAUTHORIZED ucs_servers = UCSServer.list_servers(handle) 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_UNAUTHORIZED ucs_servers = UCSCServer.list_servers(handle) UCSCUtil.ucsc_logout(handle) # Gets a hash of severs of form: # {blades: ["1/1", "1/2",..], rack: ["6", "7"]} err, msg, db_servers = db.get_ucs_servers(Const.KUBAM_CFG, server_group) if err != 0: return jsonify({"error": msg}), Const.HTTP_BAD_REQUEST if db_servers is None: return jsonify({"servers": ucs_servers}) ucs_servers = UCSUtil.servers_to_api(ucs_servers, db_servers) if err != 0: return jsonify({"error": msg}), Const.HTTP_BAD_REQUEST return jsonify({"servers": ucs_servers}), Const.HTTP_OK
def power_server_ucsm(sg, servers, action): """ perform power operations (hardreset, off, on..) for UCS Manager. """ # we expect servers to be a hash of like: # {blades: ["1/1", "1/2",..], rack: ["6", "7"]} try: handle = UCSUtil.ucs_login(sg) except KubamError as e: return jsonify({"error": str(e)}), Const.HTTP_UNAUTHORIZED try: ucs_servers = servers_to_objects(handle, servers) except KubamError as e: UCSUtil.ucs_logout(handle) return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST for i in ucs_servers: try: UCSServer.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 = UCSUtil.objects_to_servers(ucs_servers, ["oper_power"]) return jsonify({"status": powerstat}), Const.HTTP_CREATED
def get_server_status_ucsm(sg, wanted): """ Get the UCS Manager Server status """ try: handle = UCSUtil.ucs_login(sg) except KubamError as e: UCSUtil.ucs_logout(handle) return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST try: all_servers = UCSServer.list_servers(handle) if not wanted == "all": all_servers = UCSUtil.servers_to_objects(all_servers, wanted) # put in dn name format status = {} for i in all_servers: status[i['dn']] = i out = UCSUtil.dn_hash_to_out(status) except KubamError as e: UCSUtil.ucs_logout(handle) return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST UCSUtil.ucs_logout(handle) return jsonify({"servers": out}), Const.HTTP_OK
def deploy_ucs(sg, hosts, org): """ Deploy UCS hosts """ try: handle = UCSUtil.ucs_login(sg) except KubamError as e: return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST for h in hosts: if "service_profile_template" in h: err = 0 msg = "" err, msg = UCSServer.make_profile_from_template(handle, org, h) if err != 0: UCSUtil.ucs_logout(handle) return jsonify({"error": msg}), Const.HTTP_BAD_REQUEST if "server" in h: err, msg = UCSServer.associate_server(handle,org,h) else: # TODO: Create this part. print "This part is not implemented yet" UCSUtil.ucs_logout(handle) return jsonify({"status": hosts}), Const.HTTP_CREATED
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 ucs_fsm(sg, wanted): """ Pass in the server group to get the fsm of the servers """ try: handle = UCSUtil.ucs_login(sg) except KubamError as e: return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST try: all_servers = UCSServer.list_servers(handle) if not wanted == "all": all_servers = UCSUtil.servers_to_objects(all_servers, wanted) # put in dn name format status = {} for i in all_servers: status[i['dn']] = UCSMonitor.get_fsm(handle, i) out = UCSUtil.dn_hash_to_out(status) except KubamError as e: UCSUtil.ucs_logout(handle) return jsonify({"error": str(e)}), Const.HTTP_BAD_REQUEST UCSUtil.ucs_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