def callback(caller=caller): new = True with self.smart.new_session() as session: machine = session.query(Machine) \ .filter(Machine.uuid == discovery_data["boot-info"]["uuid"]) \ .first() if machine: new = False machine.updated_date = now self._delete_all_attached(session, machine) else: machine = Machine(uuid=discovery_data["boot-info"]["uuid"], created_date=now, updated_date=now) session.add(machine) session.flush() for d in discovery_data["disks"]: session.add( MachineDisk(path=d["path"], size=d["size-bytes"], machine_id=machine.id)) self._insert_network(session, machine, discovery_data) session.commit() return new
def test_machine_exists_no_state(self): mac = "00:00:00:00:00:00" state = MachineStates.booting with self.smart.new_session() as session: uuid = "b7f5f93a-b029-475f-b3a4-479ba198cb8a" machine = Machine(uuid=uuid) session.add(machine) machine_id = session.query(Machine).filter( Machine.uuid == uuid).first().id session.add( MachineInterface(machine_id=machine_id, mac=mac, netmask=1, ipv4="10.10.10.10", cidrv4="127.0.0.1/8", as_boot=True, gateway="1.1.1.1", name="lol")) session.commit() msr = machine_state_repo.MachineStateRepository(self.smart) msr.update(mac, state) with self.smart.new_session() as session: res = session.query(MachineCurrentState).filter( MachineCurrentState.machine_mac == mac).first() self.assertEqual(mac, res.machine_mac) self.assertEqual(state, res.state_name) self.assertEqual(machine_id, res.machine_id)
def test_one_machine_with_only_interfaces(self): mac = "00:00:00:00:00:00" with self.smart.new_session() as session: uuid = "b7f5f93a-b029-475f-b3a4-479ba198cb8a" machine = Machine(uuid=uuid) session.add(machine) machine_id = session.query(Machine).filter( Machine.uuid == uuid).first().id session.add( MachineInterface(machine_id=machine_id, mac=mac, netmask=1, ipv4="10.10.10.10", cidrv4="127.0.0.1/8", as_boot=True, gateway="1.1.1.1", name="lol")) session.commit() expect = list() expect.append({ 'CIDR': '127.0.0.1/8', 'LastReport': None, 'UpdateStrategy': 'Disable', 'LastChange': None, 'MAC': '00:00:00:00:00:00', 'UpToDate': None, 'FQDN': None, 'DiskProfile': 'inMemory', 'LastState': None, 'Roles': '' }) ui = user_interface_repo.UserInterfaceRepository(self.smart) self.assertCountEqual(expect, ui.get_machines_overview())
def reset(self): NUM_MACHINES = self.config['NUM_MACHINES'] NUM_JOBS = self.config['NUM_JOBS'] self.machines = [Machine(i) for i in range(NUM_MACHINES)] self.jobs = [Jobs(i) for i in range(NUM_JOBS)] self.NOW = 0 self.jobs_processed = np.zeros( (self.config['NUM_MACHINES'], self.config['NUM_JOBS']), dtype=np.int8) return self.returnObs()
def test_one_machine_full_scheduled_with_strategy_disable(self): mac = "00:00:00:00:00:00" with self.smart.new_session() as session: uuid = "b7f5f93a-b029-475f-b3a4-479ba198cb8a" machine = Machine(uuid=uuid) session.add(machine) machine_id = session.query(Machine).filter( Machine.uuid == uuid).first().id session.add( MachineInterface(machine_id=machine_id, mac=mac, netmask=1, ipv4="10.10.10.10", cidrv4="127.0.0.1/8", as_boot=True, gateway="1.1.1.1", name="lol")) session.add( MachineDisk(path="/dev/sda", size=1024 * 1024 * 1024, machine_id=machine_id)) session.add( MachineCurrentState(machine_id=machine_id, machine_mac=mac, state_name=MachineStates.discovery)) session.add( Schedule(machine_id=machine_id, role=ScheduleRoles.kubernetes_control_plane)) session.add( LifecycleRolling(machine_id=machine_id, strategy="kexec", enable=False)) session.commit() expect = list() expect.append({ 'CIDR': '127.0.0.1/8', 'LastReport': None, 'UpdateStrategy': 'Disable', 'LastChange': None, 'MAC': '00:00:00:00:00:00', 'UpToDate': None, 'FQDN': None, 'DiskProfile': 'S', 'LastState': MachineStates.discovery, 'Roles': ScheduleRoles.kubernetes_control_plane }) ui = user_interface_repo.UserInterfaceRepository(self.smart) data = ui.get_machines_overview() self.assertCountEqual(expect, data)
def test_one_machine_scheduled_cp(self): mac = "00:00:00:00:00:00" with self.smart.new_session() as session: uuid = "b7f5f93a-b029-475f-b3a4-479ba198cb8a" machine = Machine(uuid=uuid) session.add(machine) machine_id = session.query(Machine).filter( Machine.uuid == uuid).first().id session.add( MachineInterface(machine_id=machine_id, mac=mac, netmask=1, ipv4="10.10.10.10", cidrv4="127.0.0.1/8", as_boot=True, gateway="1.1.1.1", name="lol")) session.add( Schedule(machine_id=machine_id, role=ScheduleRoles.etcd_member)) session.add( Schedule(machine_id=machine_id, role=ScheduleRoles.kubernetes_control_plane)) session.commit() ms = ScheduleRepository(self.smart) ret = ms.get_available_machines() self.assertEqual(0, len(ret)) ret = ms.get_roles_by_mac_selector(mac) self.assertEqual([ ScheduleRoles.etcd_member, ScheduleRoles.kubernetes_control_plane ], ret) ret = ms.get_machines_by_roles(ScheduleRoles.etcd_member, ScheduleRoles.kubernetes_control_plane) self.assertEqual(1, len(ret)) ret = ms.get_machines_by_roles(ScheduleRoles.kubernetes_node) self.assertEqual(0, len(ret)) ret = ms.get_machines_by_roles(ScheduleRoles.etcd_member) self.assertEqual(1, len(ret)) ret = ms.get_machines_by_roles(ScheduleRoles.kubernetes_control_plane) self.assertEqual(1, len(ret))
def test_one_machine_to_schedule(self): mac = "00:00:00:00:00:00" with self.smart.new_session() as session: uuid = "b7f5f93a-b029-475f-b3a4-479ba198cb8a" machine = Machine(uuid=uuid) session.add(machine) machine_id = session.query(Machine).filter( Machine.uuid == uuid).first().id session.add( MachineInterface(machine_id=machine_id, mac=mac, netmask=1, ipv4="10.10.10.10", cidrv4="127.0.0.1/8", as_boot=True, gateway="1.1.1.1", name="lol")) session.commit() ms = ScheduleRepository(self.smart) data = { "roles": ["kubernetes-control-plane", "etcd-member"], "selector": { "mac": mac } } s = ms.get_all_schedules() self.assertEqual(0, len(s)) ms.create_schedule(data) ms.create_schedule(data) s = ms.get_all_schedules() self.assertEqual(1, len(s)) s = ms.get_machines_by_roles( *["kubernetes-control-plane", "etcd-member"]) self.assertEqual(1, len(s)) s = ms.get_machines_by_roles("kubernetes-control-plane") self.assertEqual(1, len(s))
def machine_create(): print(request.form) model = Machine() model.name = request.form['name'] model.ip = request.form['ip'] model.user = request.form['user'] model.password = request.form['password'] if model.ip.strip() == '': return jsonify({"status": False, "data": "请输入IP"}) db.session.add(model) db.session.commit() return jsonify({"status": True, "data": ""})
def test_one_machine(self): mac = "00:00:00:00:00:00" with self.smart.new_session() as session: uuid = "b7f5f93a-b029-475f-b3a4-479ba198cb8a" machine = Machine(uuid=uuid) session.add(machine) machine_id = session.query(Machine).filter( Machine.uuid == uuid).first().id session.add( MachineInterface(machine_id=machine_id, mac=mac, netmask=1, ipv4="10.10.10.10", cidrv4="127.0.0.1/8", as_boot=True, gateway="1.1.1.1", name="lol")) session.commit() ms = ScheduleRepository(self.smart) ret = ms.get_available_machines() self.assertEqual(1, len(ret))