Revision 997ac76a

b/image_creator/kamaki_wrapper.py
34 34
from os.path import basename
35 35

  
36 36
from kamaki.config import Config
37
from kamaki.clients import ClientError
37 38
from kamaki.clients.image import ImageClient
39
from kamaki.clients.pithos import PithosClient
38 40

  
39 41
from image_creator.util import FatalError
40 42

  
......
42 44

  
43 45

  
44 46
class Kamaki:
45
    __init__(self, account, token):
46
        self.username = username
47
    def __init__(self, account, token):
48
        self.account = account
47 49
        self.token = token
48 50

  
49 51
        config = Config()
50 52

  
51 53
        pithos_url = config.get('storage', 'url')
52
        self.account = config.get('storage', 'account')
53 54
        self.container = CONTAINER
54 55
        self.pithos_client = PithosClient(pithos_url, token, self.account,
55 56
                                                                self.container)
......
59 60

  
60 61
        self.uploaded_object = None
61 62

  
62
    set_container(self, container):
63
        self.pithos_client.container = container
64

  
65
    upload(self, filename, size=None, remote_path=None):
63
    def upload(self, filename, size=None, remote_path=None):
66 64

  
67 65
        if remote_path is None:
68 66
            remote_path = basename(filename)
69 67

  
70 68
        with open(filename) as f:
71
            # TODO: create container if necessary
72
            self.pithos_client.create_object(remote_path, f, size)
73
            self.uploaded_object = "pithos://%s/%s/%s" % \
74
                                    (self.account, self.container, remote_path)
75

  
76
    register(self, metadata):
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
                self.pithos_client.create_object(remote_path, f, size)
77
                self.uploaded_object = "pithos://%s/%s/%s" % \
78
                                (self.account, self.container, remote_path)
79
            except ClientError as e:
80
                raise FatalError("Pithos client: %d %s" % \
81
                                                        (e.status, e.message))
82

  
83
    def register(self, metadata):
77 84
        pass
78 85

  
79 86
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai
b/image_creator/main.py
35 35

  
36 36
from image_creator import get_os_class
37 37
from image_creator import __version__ as version
38
from image_creator import util
38 39
from image_creator.disk import Disk
39 40
from image_creator.util import get_command, error, success, output, FatalError
40
from image_creator import util
41
from image_creator.kamaki_wrapper import Kamaki
41 42
import sys
42 43
import os
43 44
import optparse
......
61 62
    usage = "Usage: %prog [options] <input_media>"
62 63
    parser = optparse.OptionParser(version=version, usage=usage)
63 64

  
64
    parser.add_option("-f", "--force", dest="force", default=False,
65
        action="store_true", help="overwrite output files if they exist")
66

  
67
    parser.add_option("--no-sysprep", dest="sysprep", default=True,
68
        help="don't perform system preperation", action="store_false")
69

  
70
    parser.add_option("--no-shrink", dest="shrink", default=True,
71
        help="don't shrink any partition", action="store_false")
65
    account = os.environ["OKEANOS_USER"] if "OKEANOS_USER" in os.environ \
66
        else None
67
    token = os.environ["OKEANOS_TOKEN"] if "OKEANOS_TOKEN" in os.environ \
68
        else None
72 69

  
73 70
    parser.add_option("-o", "--outfile", type="string", dest="outfile",
74 71
        default=None, action="callback", callback=check_writable_dir,
75 72
        help="dump image to FILE", metavar="FILE")
76 73

  
77
    parser.add_option("--enable-sysprep", dest="enabled_syspreps", default=[],
78
        help="run SYSPREP operation on the input media",
79
        action="append", metavar="SYSPREP")
80

  
81
    parser.add_option("--disable-sysprep", dest="disabled_syspreps",
82
        help="prevent SYSPREP operation from running on the input media",
83
        default=[], action="append", metavar="SYSPREP")
84

  
85
    parser.add_option("--print-sysprep", dest="print_sysprep", default=False,
86
        help="print the enabled and disabled sysprep operations for this "
87
        "input media", action="store_true")
74
    parser.add_option("-f", "--force", dest="force", default=False,
75
        action="store_true", help="overwrite output files if they exist")
88 76

  
89 77
    parser.add_option("-s", "--silent", dest="silent", default=False,
90 78
        help="silent mode, only output errors", action="store_true")
......
97 85
        default=False, help="register the image to ~okeanos as IMAGENAME",
98 86
        metavar="IMAGENAME")
99 87

  
88
    parser.add_option("-a", "--account", dest="account", type="string",
89
        default=account,
90
        help="Use this ACCOUNT when uploading/registring images [Default: %s]"\
91
        % account)
92

  
93
    parser.add_option("-t", "--token", dest="token", type="string",
94
        default=token,
95
        help="Use this token when uploading/registring images [Default: %s]"\
96
        % token)
97

  
98
    parser.add_option("--print-sysprep", dest="print_sysprep", default=False,
99
        help="print the enabled and disabled system preparation operations "
100
        "for this input media", action="store_true")
101

  
102
    parser.add_option("--enable-sysprep", dest="enabled_syspreps", default=[],
103
        help="run SYSPREP operation on the input media",
104
        action="append", metavar="SYSPREP")
105

  
106
    parser.add_option("--disable-sysprep", dest="disabled_syspreps",
107
        help="prevent SYSPREP operation from running on the input media",
108
        default=[], action="append", metavar="SYSPREP")
109

  
110
    parser.add_option("--no-sysprep", dest="sysprep", default=True,
111
        help="don't perform system preperation", action="store_false")
112

  
113
    parser.add_option("--no-shrink", dest="shrink", default=True,
114
        help="don't shrink any partition", action="store_false")
115

  
100 116
    options, args = parser.parse_args(input_args)
101 117

  
102 118
    if len(args) != 1:
......
105 121
    if not os.path.exists(options.source):
106 122
        raise FatalError("Input media `%s' is not accessible" % options.source)
107 123

  
108
    if options.register and options.upload == False: 
124
    if options.register and options.upload == False:
109 125
        raise FatalError("You also need to set -u when -r option is set")
110 126

  
127
    if options.upload and options.account is None:
128
        raise FatalError("Image uploading cannot be performed. No ~okeanos "
129
        "account name is specified. Use -a to set an account name.")
130

  
131
    if options.upload and options.token is None:
132
        raise FatalError("Image uploading cannot be performed. No ~okeanos "
133
        "token is specified. User -t to set a token.")
134

  
111 135
    return options
112 136

  
113 137

  
......
176 200
                f.close()
177 201

  
178 202
            dev.dump(options.outfile)
203

  
204
        if options.upload:
205
            output("Uploading image to pithos...", False)
206
            kamaki = Kamaki(options.account, options.token)
207
            kamaki.upload(dev.device, size, options.upload)
208
            output("done")
209

  
179 210
    finally:
180 211
        output('cleaning up...')
181 212
        disk.cleanup()

Also available in: Unified diff