Exemple #1
0
class PivNetUploader:
    def __init__(self):
        self.database = Database()

    def upload_files(self, config, folder_path, force=False):
        file_names = []
        print(type(folder_path))
        files = os.listdir(folder_path)
        for file in files:
            if os.path.isfile(os.path.join(folder_path, file)):
                file_names.append(file)
        files_from_db = self.database.check_file_exists(file_names)
        if len(file_names) == len(files_from_db) or force:
            self.upload(config, folder_path)
        else:
            return "Found files in [pathname] that are not in product db. Either remove unknown files, or use --force to upload every file in [pathname] whether its in the db or not."

    def upload(self, config, dest_path):
        c = pycurl.Curl()
        print(config)
        for opsman in config["opsmanager"]:
            # for i in opsman:
            if "username" in opsman and "password" in opsman:
                username = opsman["username"]
                password = opsman["password"]
                c.setopt(pycurl.USERPWD, "%s:%s" % (username, password))
            if "url" in opsman:
                c.setopt(c.URL, "https://" + opsman["url"] + "/api/products")
            c.setopt(pycurl.VERBOSE, 0)
            c.setopt(c.SSL_VERIFYPEER, 0)
            c.setopt(c.SSL_VERIFYHOST, 0)

            c.setopt(c.NOPROGRESS, 0)
            c.setopt(c.PROGRESSFUNCTION, self.progress)

            files = os.listdir(dest_path)
            for filename in files:
                full_path = os.path.join(dest_path, filename)
                c.setopt(c.HTTPPOST, [("product[file]", (c.FORM_FILE, full_path))])
                c.perform()

    def progress(self, download_t, download_d, upload_t, upload_d):
        if upload_t > 0:
            print(" Uploaded so far {per}%".format(per=int(upload_d * 100 / upload_t)))
Exemple #2
0
class PivNetUploader:
    def __init__(self):
        self.database = Database()

    def upload_files(self, config, folder_path, force=False):
        file_names = []
        print('folder path: %s, force=%s' % (folder_path, force))
        files = os.listdir(folder_path)
        for file in files:
            # if not file.endswith(('.pivotal', '.tgz')):
            if not file.endswith(('.pivotal')):
                print(
                    'Skipping %s - only tiles (.pivotal) and stemcells (.tgz) are uploaded.'
                    % (file))
            elif not os.path.isfile(os.path.join(folder_path, file)):
                print('Skipping %s - is not a file.' % (file))
            elif not self.database.check_file_exists(file) and not force:
                print(
                    'Skipping %s - is not in product db. Either remove unknown file, or use --force to upload every file in %s whether its in the db or not.'
                    % (file, folder_path))
            else:
                file_names.append(file)
        self.upload(config, folder_path, file_names)

    def upload(self, config, dest_path, file_names):
        # print(config)
        for opsman in config["opsmanager"]:
            if not "access_token" in opsman:
                print('No Ops Manager access_token')
                return
            elif not "url" in opsman:
                print('No Ops Manager URL')
                return
            else:
                access_token = opsman["access_token"]
                url = opsman["url"]

            for filename in file_names:
                full_path = os.path.join(dest_path, filename)
                # print(
                #     'access_token = %s, url = %s, file = %s' %
                #     (access_token, url, full_path))
                # continue
                try:
                    c = pycurl.Curl()
                    c.setopt(
                        c.URL, "https://" + opsman["url"] +
                        "/api/v0/available_products")
                    c.setopt(pycurl.HTTPHEADER,
                             ['Authorization: bearer %s' % (access_token)])
                    c.setopt(pycurl.VERBOSE, 0)
                    c.setopt(c.SSL_VERIFYPEER, 0)
                    c.setopt(c.SSL_VERIFYHOST, 0)
                    c.setopt(c.NOPROGRESS, 0)
                    c.setopt(c.HTTPPOST, [
                        ('product[file]', (
                            c.FORM_FILE,
                            full_path,
                        )),
                    ])
                    print('Uploading %s' % (full_path))
                    result = c.perform()
                finally:
                    c.close()