Пример #1
0
def mount_device(device):
    key = rk()
    mount_path = '/mnt/%s' % key
    mount_cmd = 'mount %s %s' % (device, mount_path)
    logger.info(mount_cmd)
    os.makedirs(mount_path)
    subprocess.call(mount_cmd, shell=True)
    return mount_path
Пример #2
0
    def perform_batch_destroy(self, queryset):
        check_superuser_permission(self.request.user)

        for instance in queryset:
            instance.name = rk()
            instance.status = Network.Status.DELETE
            instance.save()
        if queryset:
            return True
        return False
Пример #3
0
    def __init__(self, request_file):
        self.request_file = request_file

        if isinstance(request_file, InMemoryUploadedFile):
            self.in_memory = True
            self.tmp_dir = os.path.join('/tmp', rk())
            self.file_path = os.path.join(self.tmp_dir, self.request_file.name)
        elif isinstance(request_file, TemporaryUploadedFile):
            self.in_memory = False
            self.file_path = self.request_file.file.name
        else:
            raise Exception('invalid request file')
Пример #4
0
def generate_default_custom_install_script(file_path, system_sub_type,
                                           install_script):
    system_type = StandardDevice.SystemSubTypeMap[system_sub_type]
    # 0不执行 1直接执行 2解压执行
    mode = 0
    file_path_lower = file_path.lower()
    if system_type == StandardDevice.SystemType.WINDOWS:
        template = window_template
        base_dir = app_settings.WINDOWS_INSTALLER_DIR
        if install_script.find('{this}') >= 0:
            file_name = file_path.split('\\')[-1]
            install_script = install_script.replace('{this}', file_name)
            mode = 1
        else:
            if file_path_lower.endswith('.zip') or file_path_lower.endswith(
                    '.rar'):
                mode = 2
    else:
        template = linux_template
        base_dir = app_settings.LINUX_INSTALLER_DIR
        if install_script.find('{this}') >= 0:
            file_name = file_path.split('/')[-1]
            install_script = install_script.replace('{this}', file_name)
            mode = 1
        else:
            if file_path.lower().endswith('.zip'):
                mode = 2

    if mode == 1:
        script = template.CUSTOM_NORMAL_INSTALL.format(
            base_dir=base_dir,
            install_script=install_script,
        )
    elif mode == 2:
        script = template.CUSTOM_INSTALL.format(
            file_path=file_path,
            base_dir=base_dir,
            installer_folder=rk(),
            install_script=install_script,
        )
    else:
        script = ''
    return script
Пример #5
0
    def image(self, request, pk=None):
        device = self.get_object()
        check_operate_permission(request.user, device)

        if request.method == 'POST':
            if not device.image_scene:
                raise exceptions.ValidationError(error.DEVICE_NO_SCENE)

            if device.image_status == StandardDevice.ImageStatus.CREATING:
                raise exceptions.ValidationError(error.IMAGE_SAVING)

            device.image_status = StandardDevice.ImageStatus.CREATING
            device.save()
            self.clear_cache()
            mconsumers.StandardDeviceWebsocket.image_status_update(
                request.user, device.pk)

            name = self.shift_data.get('name')
            desc = self.shift_data.get('desc')
            name = '{}__$__{}'.format(name, rk())

            def created(image):
                StandardDeviceSnapshot.objects.create(standard_device=device,
                                                      name=name,
                                                      desc=desc)
                self.clear_cache()
                mconsumers.StandardDeviceWebsocket.image_status_update(
                    request.user, device.pk)

            def failed(error):
                self.clear_cache()
                mconsumers.StandardDeviceWebsocket.image_status_update(
                    request.user, device.pk)

            handler = DeviceHandler(request.user, device=device)
            async_exe(handler.create_image, (name, created, failed))

            return Response(status=status.HTTP_201_CREATED)
Пример #6
0
def generate_install_script(standard_device, installers):
    system_sub_type = standard_device.system_sub_type
    system_type = StandardDevice.SystemSubTypeMap[system_sub_type]
    spliter = get_spliter(system_sub_type)
    # 生成安装目录
    script = spliter + generate_base_dir_script(system_sub_type) + spliter

    # sub_script = spliter + generate_installer_lnk_script(spliter, system_sub_type) + spliter
    sub_script = spliter
    resources = []
    has_default_installer = False
    for installer in installers:
        resource = installer.resources.filter(
            platforms__icontains=system_sub_type).first()
        resources.append(resource)
        if resource.file and not resource.install_script:
            has_default_installer = True

    if has_default_installer and standard_device.system_sub_type in (
            StandardDevice.SystemSubType.WINDOWS_10,
            StandardDevice.SystemSubType.WINDOWS_7,
            StandardDevice.SystemSubType.WINDOWS_SERVER_2012,
            StandardDevice.SystemSubType.WINDOWS_SERVER_2008,
    ):
        installer_tool_urls = [
            settings.SERVER +
            static('base_scene/common/tools/windows_installer/1.dll'),
            settings.SERVER +
            static('base_scene/common/tools/windows_installer/start.exe'),
        ]
        for installer_tool_url in installer_tool_urls:
            file_path, download_script = generate_file_download_script(
                installer_tool_url, system_sub_type)
            sub_script = sub_script + download_script + spliter

    for resource in resources:
        if resource.file:
            file_name = os.path.basename(resource.file.name)
            file_url = settings.SERVER + settings.MEDIA_URL + resource.file.name

            is_win_file_contains_zh = system_type == StandardDevice.SystemType.WINDOWS and contain_zh(
                file_name)
            if is_win_file_contains_zh:
                download_file_name = (rk() + os.path.splitext(file_name)[-1])
            else:
                download_file_name = file_name
            file_path, download_script = generate_file_download_script(
                file_url, system_sub_type, download_file_name)
            sub_script = sub_script + download_script + spliter

            resource_script = ''
            if is_win_file_contains_zh:
                resource_script = resource_script + 'ren {} {}'.format(
                    file_path, file_name) + spliter
                file_path = file_path.replace(download_file_name, file_name)

            if resource.install_script:
                resource_install_script = generate_default_custom_install_script(
                    file_path, system_sub_type, resource.install_script)
                is_win_script_contains_zh = system_type == StandardDevice.SystemType.WINDOWS and contain_zh(
                    resource_install_script)
                if not is_win_file_contains_zh and is_win_script_contains_zh:
                    resource_install_script = fix_win_zh_script(
                        resource_install_script, spliter)
                resource_script = resource_script + resource_install_script + spliter
            else:
                resource_script = resource_script + generate_default_install_script(
                    file_path, system_sub_type) + spliter

            if is_win_file_contains_zh:
                resource_script = fix_win_zh_script(resource_script, spliter)

            sub_script = sub_script + resource_script + spliter
        else:
            if resource.install_script:
                resource_install_script = resource.install_script
                is_win_script_contains_zh = system_type == StandardDevice.SystemType.WINDOWS and contain_zh(
                    resource_install_script)
                if is_win_script_contains_zh:
                    resource_install_script = fix_win_zh_script(
                        resource_install_script, spliter)
                sub_script = sub_script + resource_install_script + spliter

    if has_default_installer and standard_device.system_sub_type in (
            StandardDevice.SystemSubType.WINDOWS_10,
            StandardDevice.SystemSubType.WINDOWS_7,
            StandardDevice.SystemSubType.WINDOWS_SERVER_2012,
            StandardDevice.SystemSubType.WINDOWS_SERVER_2008,
    ):
        sub_script = sub_script + r'start /b {base_dir}\start.exe'.format(
            base_dir=app_settings.WINDOWS_INSTALLER_DIR) + spliter

    # windows作成脚本文件安装
    if system_type == StandardDevice.SystemType.WINDOWS:
        made_script_filename = '%s.bat' % rk()
        made_script_path = os.path.join(settings.MEDIA_ROOT, 'tmp_script',
                                        made_script_filename)
        with open(made_script_path, 'w') as made_script_file:
            made_script_file.write(sub_script)

        script = script + window_template.MADE_SCRIPT_INSTALL.format(
            base_dir=app_settings.WINDOWS_INSTALLER_DIR,
            file_url=settings.SERVER + settings.MEDIA_URL + 'tmp_script/' +
            made_script_filename,
        ) + spliter
    else:
        script = script + sub_script + spliter

    return script