class Injector: def __init__(self, debug=False, config_dir=None): self.platform_config = PlatformConfig(config_dir=config_dir) if not logger.factory_instance: console = True if debug else False level = logging.DEBUG if debug else logging.INFO logger.init(level, console, join(self.platform_config.get_platform_log())) self.user_platform_config = PlatformUserConfig(self.platform_config.get_user_config()) self.log_aggregator = Aggregator(self.platform_config) self.platform_app_paths = AppPaths(PLATFORM_APP_NAME, self.platform_config) self.platform_app_paths.get_data_dir() self.versions = Versions(self.platform_config) self.redirect_service = RedirectService(self.user_platform_config, self.versions) self.port_config = PortConfig(self.platform_app_paths.get_data_dir()) self.nat_pmp_port_mapper = NatPmpPortMapper() self.upnp_port_mapper = UpnpPortMapper(UPnP()) self.port_mapper_factory = PortMapperFactory(self.nat_pmp_port_mapper, self.upnp_port_mapper) self.port_drill_factory = PortDrillFactory(self.user_platform_config, self.port_config, self.port_mapper_factory) self.device_info = DeviceInfo(self.user_platform_config, self.port_config) if self.platform_config.get_installer() == 'sam': self.sam = SamStub(self.platform_config, self.device_info) else: self.sam = Snap(self.platform_config, self.device_info) self.platform_cron = PlatformCron(self.platform_config) self.systemctl = Systemctl(self.platform_config) self.ldap_auth = LdapAuth(self.platform_config, self.systemctl) self.event_trigger = EventTrigger(self.sam, self.platform_config) self.nginx = Nginx(self.platform_config, self.systemctl, self.device_info) self.certbot_genetator = CertbotGenerator(self.platform_config, self.user_platform_config, self.device_info, self.sam) self.tls = CertificateGenerator(self.platform_config, self.user_platform_config, self.device_info, self.nginx, self.certbot_genetator) self.device = Device(self.platform_config, self.user_platform_config, self.redirect_service, self.port_drill_factory, self.sam, self.platform_cron, self.ldap_auth, self.event_trigger, self.tls, self.nginx) self.internal = Internal(self.platform_config, self.device, self.redirect_service, self.log_aggregator) self.path_checker = PathChecker(self.platform_config) self.lsblk = Lsblk(self.platform_config, self.path_checker) self.hardware = Hardware(self.platform_config, self.event_trigger, self.lsblk, self.path_checker, self.systemctl) self.network = Network() self.public = Public(self.platform_config, self.user_platform_config, self.device, self.device_info, self.sam, self.hardware, self.redirect_service, self.log_aggregator, self.certbot_genetator, self.port_mapper_factory, self.network, self.port_config) self.udev = Udev(self.platform_config) def get_app_paths(self, app_name): return AppPaths(app_name, self.platform_config) def get_app_setup(self, app_name): return AppSetup(app_name, self.get_app_paths(app_name), self.nginx, self.hardware, self.device_info, self.device, self.user_platform_config, self.systemctl)
def platform_version(): config = PlatformConfig() app_dir = config.app_dir() version_filename = '{0}/META/version'.format(app_dir) with open(version_filename) as f: content = f.readline() return content.strip() raise Exception("Can't get platform version")
def add_service(app_id, service, include_socket=False, start=True): config = PlatformConfig() app_dir = join(config.apps_root(), app_id) log = logger.get_logger('systemctl') shutil.copyfile(__app_service_file(app_dir, service), __systemd_service_file(service)) if include_socket: shutil.copyfile(__app_socket_file(app_dir, service), __systemd_socket_file(service)) log.info('enabling {0}'.format(service)) check_output('systemctl enable {0} 2>&1'.format(service), shell=True) if start: start_service(service)
def test_loop_support(): output = 'NAME="/dev/loop0" SIZE="10M" TYPE="loop" MOUNTPOINT="" PARTTYPE="" FSTYPE="" MODEL=""' platform_config = PlatformConfig(CONFIG_DIR) lsblk = Lsblk(platform_config, PathChecker(platform_config)) disks = lsblk.available_disks(output) assert len(disks) == 1
def test_proxy_server(): assert Nginx(PlatformConfig()).proxy_definition( 'test', 80, join(dir, '..', '..', '..', 'config', 'nginx'), 'app.server') == """ server { listen 80; server_name test.*; location / { proxy_set_header X-Forwarded-Proto $scheme ; proxy_set_header X-Forwarded-Host $http_host ; proxy_pass http://localhost:80 ; proxy_redirect http://localhost:80 $scheme://$http_host ; } } server { listen 443 ssl; server_name test.*; add_header Strict-Transport-Security "max-age=31536000; includeSubdomains"; location / { proxy_set_header X-Forwarded-Proto $scheme ; proxy_set_header X-Forwarded-Host $http_host ; proxy_pass http://localhost:80 ; proxy_redirect http://localhost:80 $scheme://$http_host ; } } """.strip()
def add_mount(device, fs_type, options): log = logger.get_logger('systemctl') config = PlatformConfig() mount_template_file = join(config.config_dir(), 'mount', 'mount.template') mount_definition = Template(open(mount_template_file, 'r').read()).substitute({ 'what': device, 'where': config.get_external_disk_dir(), # 'type': fs_type, 'type': 'auto', 'options': options }) config = PlatformConfig() mount_filename = __dir_to_systemd_mount_filename( config.get_external_disk_dir()) with open(__systemd_file(mount_filename), 'w') as f: f.write(mount_definition) log.info('enabling {0}'.format(mount_filename)) check_output('systemctl enable {0} 2>&1'.format(mount_filename), shell=True) __start(mount_filename)
class Injector: def __init__(self, debug=False): self.platform_config = PlatformConfig() if not logger.factory_instance: console = True if debug else False level = logging.DEBUG if debug else logging.INFO logger.init(level, console, join(self.platform_config.get_platform_log())) self.user_platform_config = PlatformUserConfig(self.platform_config.get_user_config()) self.log_aggregator = Aggregator(self.platform_config) self.platform_app_paths = AppPaths(PLATFORM_APP_NAME, self.platform_config) self.platform_app_paths.get_data_dir() self.redirect_service = RedirectService(self.user_platform_config, platform_version) self.port_config = PortConfig(self.platform_app_paths.get_data_dir()) self.nat_pmp_port_mapper = NatPmpPortMapper() self.upnp_port_mapper = UpnpPortMapper(UPnP()) self.port_drill_factory = PortDrillFactory(self.user_platform_config, self.port_config, self.nat_pmp_port_mapper, self.upnp_port_mapper) self.info = DeviceInfo(self.user_platform_config, self.port_config) self.sam = SamStub(self.platform_config, self.info) self.platform_cron = PlatformCron(self.platform_config) self.ldap_auth = LdapAuth(self.platform_config) self.event_trigger = EventTrigger(self.sam) self.nginx = Nginx(self.platform_config) self.tls = Tls(self.platform_config, self.info, self.nginx) self.device = Device(self.platform_config, self.user_platform_config, self.redirect_service, self.port_drill_factory, self.sam, self.platform_cron, self.ldap_auth, self.event_trigger, self.tls) self.internal = Internal(self.platform_config, self.device, self.redirect_service, self.log_aggregator) self.path_checker = PathChecker(self.platform_config) self.lsblk = Lsblk(self.platform_config, self.path_checker) self.hardware = Hardware(self.platform_config, self.event_trigger, self.lsblk, self.path_checker) self.public = Public(self.platform_config, self.user_platform_config, self.device, self.info, self.sam, self.hardware, self.redirect_service, self.log_aggregator) self.udev = Udev(self.platform_config)
def test_empty_disk(): output = 'NAME="/dev/sda" SIZE="3.7G" TYPE="disk" MOUNTPOINT="" PARTTYPE="" FSTYPE="" MODEL="BLANK DISK"' platform_config = PlatformConfig(CONFIG_DIR) lsblk = Lsblk(platform_config, PathChecker(platform_config)) disks = lsblk.available_disks(output) assert len(disks[0].partitions) == 0
def test_do_not_show_squashfs(): output = 'NAME="/dev/loop1" SIZE="41.1M" TYPE="loop" MOUNTPOINT="/snap/platform/180821" PARTTYPE="" FSTYPE="squashfs" MODEL=""' platform_config = PlatformConfig(CONFIG_DIR) lsblk = Lsblk(platform_config, PathChecker(platform_config)) disks = lsblk.available_disks(output) assert len(disks) == 0
def test_list(): platform_config = PlatformConfig(CONFIG_DIR) lsblk = Lsblk(platform_config, PathChecker(platform_config)) disks = lsblk.available_disks(default_output) assert len(disks) == 5 assert len(disks[0].partitions) == 4 assert disks[1].partitions[2].mount_point == '/opt/disk/external' assert len(disks[1].partitions) == 3
def test_default_empty_disk_name(): lsblk_output = 'NAME="/dev/sdb" SIZE="14.4G" TYPE="disk" MOUNTPOINT="" PARTTYPE="" FSTYPE="" MODEL=""\n' lsblk_output += 'NAME="/dev/sdb1" SIZE="14.4G" TYPE="part" MOUNTPOINT="" PARTTYPE="0x83" FSTYPE="ext4" MODEL=""\n' lsblk_output += 'NAME="/dev/sdb2" SIZE="41.8M" TYPE="part" MOUNTPOINT="" PARTTYPE="0xc" FSTYPE="vfat" MODEL=""' platform_config = PlatformConfig(CONFIG_DIR) lsblk = Lsblk(platform_config, PathChecker(platform_config)) disks = lsblk.available_disks(lsblk_output) assert disks[0].name == 'Disk'
def test_do_not_show_disks_with_root_partition(): lsblk_output = 'NAME="/dev/sdb" SIZE="14.4G" TYPE="disk" MOUNTPOINT="" PARTTYPE="" FSTYPE="" MODEL=""\n' lsblk_output += 'NAME="/dev/sdb1" SIZE="14.4G" TYPE="part" MOUNTPOINT="/" PARTTYPE="0x83" FSTYPE="ext4" MODEL=""\n' lsblk_output += 'NAME="/dev/sdb2" SIZE="41.8M" TYPE="part" MOUNTPOINT="" PARTTYPE="0xc" FSTYPE="vfat" MODEL=""' platform_config = PlatformConfig(CONFIG_DIR) lsblk = Lsblk(platform_config, PathChecker(platform_config)) disks = lsblk.available_disks(lsblk_output) assert len(disks) == 0
def add_mount(device, fs_type, options): log = logger.get_logger('systemctl') config = PlatformConfig() mount_template_file = join(config.config_dir(), 'mount', 'mount.template') mount_definition = Template(open(mount_template_file, 'r').read()).substitute({ 'what': device, 'where': config.get_external_disk_dir(), # 'type': fs_type, 'type': 'auto', 'options': options}) config = PlatformConfig() mount_filename = __dir_to_systemd_mount_filename(config.get_external_disk_dir()) with open(__systemd_file(mount_filename), 'w') as f: f.write(mount_definition) log.info('enabling {0}'.format(mount_filename)) check_output('systemctl enable {0} 2>&1'.format(mount_filename), shell=True) __start(mount_filename)
def test_list(): platform_config = PlatformConfig(CONFIG_DIR) lsblk = Lsblk(platform_config, PathChecker(platform_config)) disks = lsblk.available_disks(default_output) assert len(disks) == 5 disk = [d for d in disks if d.device == '/dev/sda'][0] assert len(disk.partitions) == 4 disk = [d for d in disks if d.device == '/dev/sdb'][0] assert disk.partitions[2].mount_point == '/opt/disk/external' assert len(disk.partitions) == 3
def test_raid(): raid_output = '''NAME="/dev/sda" SIZE="1.8T" TYPE="disk" MOUNTPOINT="" PARTTYPE="" FSTYPE="linux_raid_member" MODEL="WDC WD20EFRX-68E" NAME="/dev/sdb" SIZE="1.8T" TYPE="disk" MOUNTPOINT="" PARTTYPE="" FSTYPE="linux_raid_member" MODEL="WDC WD20EFRX-68E" NAME="/dev/sdc" SIZE="1.8T" TYPE="disk" MOUNTPOINT="" PARTTYPE="" FSTYPE="linux_raid_member" MODEL="WDC WD20EFRX-68E" NAME="/dev/sdd" SIZE="1.8T" TYPE="disk" MOUNTPOINT="" PARTTYPE="" FSTYPE="linux_raid_member" MODEL="WDC WD20EFRX-68E" NAME="/dev/md0" SIZE="3.7T" TYPE="raid10" MOUNTPOINT="/mnt/md0" PARTTYPE="" FSTYPE="ext4" MODEL="" NAME="/dev/md0" SIZE="3.7T" TYPE="raid10" MOUNTPOINT="/mnt/md0" PARTTYPE="" FSTYPE="ext4" MODEL="" NAME="/dev/md0" SIZE="3.7T" TYPE="raid10" MOUNTPOINT="/mnt/md0" PARTTYPE="" FSTYPE="ext4" MODEL="" NAME="/dev/md0" SIZE="3.7T" TYPE="raid10" MOUNTPOINT="/mnt/md0" PARTTYPE="" FSTYPE="ext4" MODEL=""''' platform_config = PlatformConfig(CONFIG_DIR) lsblk = Lsblk(platform_config, PathChecker(platform_config)) disks = lsblk.available_disks(raid_output) assert len(disks) == 1 assert len(disks[0].partitions) == 1 assert disks[0].partitions[0].mount_point == '/mnt/md0'
def test_is_external_disk_detached(): platform_config = PlatformConfig(CONFIG_DIR) lsblk = Lsblk(platform_config, PathChecker(platform_config)) assert not lsblk.is_external_disk_attached(default_output, '/opt/disk/detached')
def remove_mount(): config = PlatformConfig() __remove(__dir_to_systemd_mount_filename(config.get_external_disk_dir()))
def get_lsblk(): platform_config = PlatformConfig(CONFIG_DIR) return Lsblk(platform_config, PathChecker(platform_config))
def get_platform_config(): config = PlatformConfig(platform_config_file) return config
def get_app_paths(app_name): return AppPaths(app_name, PlatformConfig())