def get_all_commands(): session = db.session() with session.begin(): commands = [] for c in session.query(ExecCommand): commands.append(_command_to_dict(c, include_result=False)) return commands
def _read_config(session=None): if not session: session = db.session() config = {} for c in session.query(Config).filter(Config.category == 'i2c'): config[c.key] = c return config
def find_all(): session = db.session() with session.begin(): users = session.query(User) for user in users: session.expunge(user) return users
def get_blade_commands(id): session = db.session() with session.begin(): commands = [] for c in session.query(ExecCommand).filter(ExecCommand.blade_id == id): commands.append(_command_to_dict(c, include_result=False)) return commands
def update(updated_user): session = db.session() with session.begin(): user = session.query(User).filter(User.id == updated_user.id).one() user.firstname = updated_user.firstname user.lastname = updated_user.lastname if updated_user.password is not None: user.password = security.hash_password(updated_user.password)
def get_blade_command(blade_id, id): session = db.session() with session.begin(): command = session.query(ExecCommand).filter( ExecCommand.id == id).first() if command is None or command.blade_id != blade_id: raise exception.NotFound() return _command_to_dict(command)
def findOne(filter): session = db.session() try: with session.begin(): user = session.query(User).filter(filter).one() session.expunge(user) return user except NoResultFound as e: raise exception.NotFound()
def exec_command(id, data): if 'method' not in data: raise exception.BadRequest(reason="Attribute 'method' is missing") if data['method'] != 'ssh': raise exception.Conflict(reason="Method '%s' is not supported" % data['method']) if 'command' not in data: raise exception.BadRequest(reason="Attribute 'command' is missing") if 'username' not in data: raise exception.BadRequest(reason="Attribute 'username' is missing") if 'private_key' not in data: raise exception.BadRequest(reason="Attribute 'private_key' is missing") username = data['username'] command = data['command'] private_key = data['private_key'] session = db.session() with session.begin(): blade = _get_blade(id, session) ip = network.read_ip_address(blade.mac_address) if not ip: raise exception.Conflict( reason="Did not manage to resolve IP address for blade %s" % id) exec_command = ExecCommand(blade.id, ip, command, private_key, username) session.add(exec_command) commandObject = _command_to_dict(exec_command, include_result=False) session = db.session() with session.begin(): session.add(exec_command) exec_command.start() return commandObject
def delete_single_device(bus_id, address): for device in read_single_bus(bus_id)['devices']: if device['address'] == address: session = db.session() with session.begin(): i2cConfig = _read_config(session) i2cConfig['i2c_power_read_bus'].value = '' i2cConfig['i2c_power_read_address'].value = '' return raise exception.NotFound()
def update_config(config_dict): session = db.session() with session.begin(): config = {} for c in session.query(Config).filter(Config.category == 'dhcp'): config[c.key] = c for k in config_dict: if k in config: config[k].value = config_dict[k] if config['dhcp_mode'].value not in ['proxy', 'dhcp']: raise exception.Conflict(reason="Invalid DHCP mode. Must be either 'proxy' or 'dhcp'") config['dhcp_network'].value = _normalize_cidr(config['dhcp_network'].value) if config['dhcp_mode'].value == 'dhcp': try: start = int(IPAddress(config['dhcp_range_start'].value)) except AddrFormatError: raise exception.Conflict(reason="Invalid format for DHCP start range IP address") try: end = int(IPAddress(config['dhcp_range_end'].value)) except AddrFormatError: raise exception.Conflict(reason="Invalid format for DHCP end range IP address") network = IPNetwork(config['dhcp_network'].value) network_ip = int(network.ip) netmask = int(network.netmask) network_checksum = network_ip & netmask start_checksum = start & netmask if start_checksum != network_checksum: raise exception.Conflict( reason="Invalid DHCP start range IP address. Must be in network %s" % str(network)) end_checksum = end & netmask if end_checksum != network_checksum: raise exception.Conflict( reason="Invalid DHCP end range IP address. Must be in network %s" % str(network)) if start > end: raise exception.Conflict(reason="DHCP end range must be greater than start range") if config['dhcp_lease_duration'].value != 'infinite': try: time = int(config['dhcp_lease_duration'].value) if time < 300: raise exception.Conflict(reason="DHCP lease duration must be at least 300 seconds") except ValueError: raise exception.Conflict( reason="DHCP lease duration must be either a count in seconds or the word 'infinite'") refresh()
def get_blade(id): session = db.session() with session.begin(): blade = _get_blade(id, session) mac_address = network.format_mac_address_standard(blade.mac_address) return { 'id': blade.id, 'name': blade.name, 'description': blade.description, 'building': blade.building, 'mac_address': mac_address, 'ip_address': network.read_ip_address(mac_address), 'consumption': i2c.read_power_consumption(str(blade.id)) }
def refresh(): config = read_config(db.session()) network = IPNetwork(config['dhcp_network']) config['dhcp_range_netmask'] = str(network.netmask) if config['dhcp_mode'] == 'proxy': config['dhcp_range_start'] = str(IPAddress(int(network.ip) + 1)) with open("/etc/dnsmasq.conf", "w") as text_file: text_file.write(tpl.get_template('dnsmasq.conf').render(**config)) rc, stdout, stderr = utils.cmd('service dnsmasq restart') if (rc != 0): raise exception.RuggedpodException()
def setup_device(bus_id, address, data): if 'purpose' not in data: raise exception.BadRequest(reason="field 'purpose' is missing in request body") if data['purpose'] != 'power_read': raise exception.BadRequest(reason="Currently, 'purpose' accept only the value 'power_read'") read_single_bus(bus_id) # To ensure this bus id is valid session = db.session() with session.begin(): i2cConfig = _read_config(session) i2cConfig['i2c_power_read_bus'].value = bus_id i2cConfig['i2c_power_read_address'].value = address return read_single_device(bus_id, address)
def get_blades(): session = db.session() with session.begin(): blades = [] for b in session.query(Blade): mac_address = network.format_mac_address_standard(b.mac_address) blades.append({ 'id': b.id, 'name': b.name, 'description': b.description, 'building': b.building, 'mac_address': mac_address, 'ip_address': network.read_ip_address(mac_address), 'consumption': i2c.read_power_consumption(str(b.id)) }) return blades
def cancel_build_blade(id): session = db.session() with session.begin(): blade = _get_blade(id, session) if not blade.building: raise exception.Conflict( reason="The server is not in the building state") pxe_tftp_dir = "/tftp/pxe/pxelinux.cfg" pwe_www_dir = "/var/www/pxe" pxe_mac_address = network.format_mac_address_pxe(blade.mac_address) os.remove("%s/%s" % (pxe_tftp_dir, pxe_mac_address)) os.remove("%s/%s.seed" % (pwe_www_dir, pxe_mac_address)) os.remove("%s/%s.sh" % (pwe_www_dir, pxe_mac_address)) blade.building = False
def update_blade(id, data): session = db.session() with session.begin(): blade = _get_blade(id, session) if 'id' in data and int(data['id']) != int(id): raise exception.Conflict(reason="Id mismatch") if 'name' in data: blade.name = data['name'] if 'description' in data: blade.description = data['description'] if 'mac_address' in data: blade.mac_address = network.normalize_mac_address( data['mac_address']) return get_blade(id)
def reset_blade(id): _get_blade(id, db.session()) gpio.set_blade_reset(id)
def save(user): user.password = security.hash_password(user.password) session = db.session() with session.begin(): created_used = session.add(user) return user.id
def delete(id): session = db.session() with session.begin(): user = findOne(User.id == id) session.delete(user)
def build_blade(id, data): session = db.session() with session.begin(): blade = _get_blade(id, session) if blade.building: raise exception.Conflict( reason="A building operation is already in progress") if not blade.mac_address: raise exception.Conflict( reason="Blade must have is MAC address set in order to be built" ) pxe_tftp_dir = "/tftp/pxe/pxelinux.cfg" pwe_www_dir = "/var/www/pxe" pxe_mac_address = network.format_mac_address_pxe(blade.mac_address) ip = network.get_iface_ip_address('eth0') # Default values os = 'ubuntu1404' hostname = 'blade' + str(blade.id) username = '******' ssh_pub_key = '' password = '' if 'os' in data and data['os']: os = data['os'] if 'username' in data and data['username']: username = data['username'] if 'hostname' in data and data['hostname']: hostname = data['hostname'] if 'ssh_pub_key' not in data or not data['ssh_pub_key']: if 'password' not in data: raise exception.BadRequest( reason= "A password or a SSH public key must be provided for user '%s'" % username) if not data['password']: raise exception.BadRequest( reason= "A password or a SSH public key must be provided for user '%s'" % username) password = data['password'] else: ssh_pub_key = data['ssh_pub_key'] if 'password' in data and data['password']: password = data['password'] _write_template( "ubuntu-1404/pxemenu", "%s/%s" % (pxe_tftp_dir, pxe_mac_address), { 'pxe_server': ip, 'preseed_filename': "%s.seed" % pxe_mac_address, 'serial_baudrate': 115200 }) _write_template( "ubuntu-1404/preseed", "%s/%s.seed" % (pwe_www_dir, pxe_mac_address), { 'http_proxy': "http://%s:3128" % ip, 'ubuntu_mirror': "archive.ubuntu.com", 'root_password': "******", 'ntp_server_host': ip, 'pxe_server': ip, 'post_install_script_name': "%s.sh" % pxe_mac_address }) post_install_model = { 'blade_number': blade.id, 'serial_baudrate': 115200, 'api_base_url': "http://%s:5000" % ip, 'username': username, 'hostname': hostname } if password: post_install_model['password'] = password if ssh_pub_key: post_install_model['ssh_pub_key'] = ssh_pub_key _write_template("ubuntu-1404/post-install.sh", "%s/%s.sh" % (pwe_www_dir, pxe_mac_address), post_install_model) blade.building = True
def serial_blade(id): _get_blade(id, db.session()) gpio.start_blade_serial_session(id)
def power_blade(id, hard=False): _get_blade(id, db.session()) if hard: gpio.set_blade_long_onoff(id) else: gpio.set_blade_short_onoff(id)