示例#1
0
 def __init__(self, *args, **kwargs):
     super(SettingsForm, self).__init__(*args, **kwargs)
     conf = self.read_settings()
     self.fields['settings'].initial = conf
     params = SFTP.get_params(conf.split('\n'))
     for p in params:
         setattr(settings, str(p[0]).upper(), p[1])
示例#2
0
文件: views.py 项目: nikozhangwj/Ops
def ssh_terminal(request, pk):
    server_obj = ServerAssets.objects.select_related('assets').get(id=pk)
    ssh_server_ip = server_obj.assets.asset_management_ip

    if request.method == 'GET':
        download_file = request.GET.get('download_file')
        if download_file:
            download_file_path = os.path.join(settings.MEDIA_ROOT, 'admin_files', request.user.username, 'download',
                                              ssh_server_ip)

            sftp = SFTP(ssh_server_ip, server_obj.port, server_obj.username,
                        CryptPwd().decrypt_pwd(server_obj.password))

            response = sftp.download_file(download_file, download_file_path)
            return response
        else:
            remote_ip = request.META.get('REMOTE_ADDR')
            return render(request, 'assets/ssh_terminal.html', locals())
    elif request.method == 'POST':
        try:
            upload_file = request.FILES.get('upload_file')
            upload_file_path = os.path.join(settings.MEDIA_ROOT, 'fort_files', request.user.username, 'upload',
                                            server_obj.assets.asset_management_ip)
            sftp = SFTP(ssh_server_ip, server_obj.port, server_obj.username,
                        CryptPwd().decrypt_pwd(server_obj.password))
            sftp.upload_file(upload_file, upload_file_path)

            return JsonResponse({'code': 200, 'msg': '上传成功!文件默认放在{}用户家目录下'.format(server_obj.username)})
        except Exception as e:
            return JsonResponse({'code': 500, 'msg': '上传失败!{}'.format(e)})
示例#3
0
def terminal(request, server_id, fort_user_id):
    server_obj = ServerAssets.objects.select_related('assets').get(id=server_id)
    fort_user_obj = FortServerUser.objects.get(id=fort_user_id)
    fort_username = fort_user_obj.fort_username
    server_ip = server_obj.assets.asset_management_ip

    if request.method == 'GET':
        download_file = request.GET.get('download_file')
        if download_file:

            download_file_path = os.path.join(settings.MEDIA_ROOT, 'fort_files', request.user.username, 'download',
                                              server_ip)
            sftp = SFTP(server_ip, server_obj.port, fort_username,
                        fort_user_obj.fort_password)

            response = sftp.download_file(download_file, download_file_path)
            return response
        else:
            group_name = str(uuid.uuid4())
            return render(request, 'fort/terminal.html', locals())
    elif request.method == 'POST':
        try:
            upload_file = request.FILES.get('upload_file')
            upload_file_path = os.path.join(settings.MEDIA_ROOT, 'fort_files', request.user.username, 'upload',
                                            server_obj.assets.asset_management_ip)
            sftp = SFTP(server_ip, server_obj.port, fort_username,
                        fort_user_obj.fort_password)

            sftp.upload_file(upload_file, upload_file_path)

            return JsonResponse({'code': 200, 'msg': '上传成功!文件默认放在/home/{}路径下'.format(fort_username)})
        except Exception as e:
            return JsonResponse({'code': 500, 'msg': '上传失败!{}'.format(e)})
示例#4
0
 def save(self, commit=True):
     instance = super(SettingsForm, self).save(commit=commit)
     if self.is_valid():
         conf = self.cleaned_data.get('settings')
         self.write_settings(self.cleaned_data.get('settings'))
         params = SFTP.get_params(conf.split('\n'))
         for p in params:
             setattr(settings, str(p[0]).upper(), p[1])
     return instance
示例#5
0
文件: views.py 项目: ziguicxd/Ops
def terminal(request, server_id, fort_user_id):
    server_obj = ServerAssets.objects.select_related('assets').get(
        id=server_id)
    fort_user_obj = FortServerUser.objects.get(id=fort_user_id)
    fort_username = fort_user_obj.fort_username
    sftp = SFTP(server_obj.assets.asset_management_ip, server_obj.port,
                fort_username, fort_user_obj.fort_password)
    if request.method == 'GET':
        server_ip = server_obj.assets.asset_management_ip
        download_file = request.GET.get('download_file')
        if download_file:
            download_file_path = os.path.join(settings.MEDIA_ROOT,
                                              'fort_files',
                                              request.user.username,
                                              'download', server_ip)
            local_file_name = download_file.split('/')[-1]

            if not os.path.exists(download_file_path):
                os.makedirs(download_file_path, exist_ok=True)

            local_file = '{}/{}'.format(download_file_path, local_file_name)
            download_file_size = sftp.sftp.stat(download_file).st_size

            sftp.get_file(download_file, local_file)

            local_file_size = None
            while local_file_size != download_file_size:
                local_file_size = os.path.getsize(local_file)

            response = FileResponse(open(local_file, 'rb'))
            response['Content-Type'] = 'application/octet-stream'
            response[
                'Content-Disposition'] = 'attachment;filename="{filename}"'.format(
                    filename=local_file_name)
            return response
        else:
            return render(request, 'fort/terminal.html', locals())
    elif request.method == 'POST':
        try:
            upload_file = request.FILES.get('upload_file')
            upload_file_path = os.path.join(
                settings.MEDIA_ROOT, 'fort_files', request.user.username,
                'upload', server_obj.assets.asset_management_ip)
            if not os.path.exists(upload_file_path):
                os.makedirs(upload_file_path, exist_ok=True)

            local_file = '{}/{}'.format(upload_file_path, upload_file.name)

            if not os.path.exists(local_file):
                open(local_file, 'w').close()
            local_file_size = None

            while local_file_size != upload_file.size:
                with open(local_file, 'wb') as f:
                    for chunk in upload_file.chunks():
                        f.write(chunk)
                local_file_size = os.path.getsize(local_file)

            sftp.put_file(local_file, '/home/{}'.format(fort_username))

            return JsonResponse({
                'code':
                200,
                'msg':
                '上传成功!文件默认放在/home/{}路径下'.format(fort_username)
            })
        except Exception as e:
            return JsonResponse({'code': 500, 'msg': '上传失败!{}'.format(e)})
示例#6
0
 def clean_settings(self):
     data = self.cleaned_data['settings']
     if not SFTP.validate_conf(data=data):
         raise forms.ValidationError("You conf is not valid!")
     return data
示例#7
0
import os
from dispatch_service import settings
from utils.sftp import SFTP

path = os.path.realpath(os.path.join(settings.DJANGO_PROJECT_ROOT, 'conf.ini'))

params = []
if SFTP.validate_conf(path):
    with open(path, 'r') as conf_file:
        params = SFTP.get_params(conf_file.read().split('\n'))
    for p in params:
        setattr(settings, str(p[0]).upper(), p[1])