ansible / mazer

Experimental Ansible Galaxy Content Manager
GNU General Public License v3.0
114 stars 18 forks source link

Feature/publish #148

Closed chouseknecht closed 5 years ago

chouseknecht commented 5 years ago

Adds publish command to perform a multipart form file upload.

Usage: mazer publish [options] archive_path

Options:
  -h, --help            show this help message and exit
  -c, --ignore-certs    Ignore SSL certificate validation errors.
  -s SERVER_URL, --server=SERVER_URL
                        The API server destination
  -v, --verbose         verbose mode (-vvv for more, -vvvv to enable
                        connection debugging)

Tested against the following Django view:

import json
import logging
import os
import tempfile
import tarfile

from galaxy.api.views import base_views
from rest_framework.parsers import MultiPartParser, FormParser
from rest_framework.response import Response
from rest_framework import status

__all__ = [
    'FileUploadView',
]

logger = logging.getLogger(__name__)

def _handle_file(f, name, version):
    tmp_path = tempfile.mkdtemp()
    archive_path = os.path.join(tmp_path, '%s-%s.tar.gz' % (name, version))
    with open(archive_path, 'wb+') as destination:
        for chunk in f.chunks():
            destination.write(chunk)
    return archive_path

class FileUploadView(base_views.APIView):
    parser_classes = (MultiPartParser, FormParser)

    def post(self, request, *args, **kwargs):

        file_obj = request.data['file']
        checksum = request.data.get('sha256')
        name = request.data.get('name')
        version = request.data.get('version')
        logger.debug('checksum: %s  name: %s  version: %s' %
                     (checksum, name, version))

        archive_path = _handle_file(file_obj, name, version)
        archive = tarfile.open(archive_path, mode='r')
        top_dir = os.path.commonprefix(archive.getnames())
        manifest_path = os.path.join(top_dir, 'MANIFEST.json')
        manifest_file = archive.extractfile(manifest_path)
        manifest_data = json.load(manifest_file)
        logger.debug(json.dumps(manifest_data))

        return Response(status=status.HTTP_201_CREATED)

NOTE: This is just the first iteration to establish a working publish command. It does not yet handle whatever results will come back from Galaxy. Presumably it will need to wait for, and possibly long pull, to get upload/import status messages as they become available. Additionally options, like --no-wait, may also be desirable.

alikins commented 5 years ago

@chouseknecht https://github.com/alikins/mazer/tree/push_rebase has a rebase of this to master. If you don't have any changes pending for that branch, I'll update your branch and this with the rebase.