Revision b1395967 image_creator/kamaki_wrapper.py

b/image_creator/kamaki_wrapper.py
37 37
from kamaki.clients import ClientError
38 38
from kamaki.clients.image import ImageClient
39 39
from kamaki.clients.pithos import PithosClient
40
from progress.bar import Bar
40 41

  
41
from image_creator.util import FatalError, progress
42
from image_creator.util import FatalError, output, success
42 43

  
43 44
CONTAINER = "images"
44 45

  
45 46

  
47
def progress(message):
48

  
49
    MSG_LENGTH = 30
50

  
51
    def progress_gen(n):
52
        msg = "%s:" % message
53

  
54
        progressbar = Bar(msg.ljust(MSG_LENGTH))
55
        progressbar.max = n
56
        for _ in range(n):
57
            yield
58
            progressbar.next()
59
        output("\r%s...\033[K" % message, False)
60
        success("done")
61
        yield
62
    return progress_gen
63

  
46 64
class Kamaki:
47 65
    def __init__(self, account, token):
48 66
        self.account = account
......
60 78

  
61 79
        self.uploaded_object = None
62 80

  
63
    def upload(self, filename, size=None, remote_path=None):
81
    def upload(self, file_obj, size=None, remote_path=None, hp=None, up=None):
64 82

  
65 83
        if remote_path is None:
66 84
            remote_path = basename(filename)
67 85

  
68
        with open(filename) as f:
69
            try:
70
                self.pithos_client.create_container(self.container)
71
            except ClientError as e:
72
                if e.status != 202:  # Ignore container already exists errors
73
                    raise FatalError("Pithos client: %d %s" % \
74
                                                        (e.status, e.message))
75
            try:
76
                hash_progress = progress("(1/2)  Calculating block hashes:")
77
                upload_progress = progress("(2/2)  Uploading missing blocks:")
78
                self.pithos_client.create_object(remote_path, f, size,
79
                                                hash_progress, upload_progress)
80
                self.uploaded_object = "pithos://%s/%s/%s" % \
81
                                (self.account, self.container, remote_path)
82
            except ClientError as e:
86
        try:
87
            self.pithos_client.create_container(self.container)
88
        except ClientError as e:
89
            if e.status != 202:  # Ignore container already exists errors
83 90
                raise FatalError("Pithos client: %d %s" % \
84
                                                        (e.status, e.message))
91
                                                    (e.status, e.message))
92
        try:
93
            hash_cb = progress(hp) if hp is not None else None
94
            upload_cb = progress(up) if up is not None else None
95
            self.pithos_client.create_object(remote_path, file_obj, size,
96
                                                            hash_cb, upload_cb)
97
            return "pithos://%s/%s/%s" % \
98
                            (self.account, self.container, remote_path)
99
        except ClientError as e:
100
            raise FatalError("Pithos client: %d %s" % (e.status, e.message))
85 101

  
86
    def register(self, metadata):
87
        pass
102
    def register(self, name, location, metadata):
103
        params = {'is_public':'true', 'disk_format':'diskdump'}
104
        try:
105
            self.image_client.register(name, location, params, metadata)
106
        except ClientError as e:
107
            raise FatalError("Image client: %d %s" % (e.status, e.message))
88 108

  
89 109
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :

Also available in: Unified diff