Revision e77e66a9 image_creator/main.py

b/image_creator/main.py
36 36
from image_creator import __version__ as version
37 37
from image_creator import util
38 38
from image_creator.disk import Disk
39
from image_creator.util import get_command, error, success, output, \
40
                                                    FatalError, progress, md5
39
from image_creator.util import get_command, FatalError, MD5
40
from image_creator.output import Output, Output_with_progress, Silent, error
41 41
from image_creator.os_type import get_os_class
42 42
from image_creator.kamaki_wrapper import Kamaki
43 43
import sys
......
153 153
def image_creator():
154 154
    options = parse_options(sys.argv[1:])
155 155

  
156
    if options.silent:
157
        util.silent = True
158

  
159 156
    if options.outfile is None and not options.upload \
160 157
                                            and not options.print_sysprep:
161 158
        raise FatalError("At least one of `-o', `-u' or `--print-sysprep' " \
162 159
                                                                "must be set")
163 160

  
161
    if options.silent:
162
        out = Silent()
163
    else:
164
        out = Output_with_progress()
165

  
164 166
    title = 'snf-image-creator %s' % version
165
    output(title)
166
    output('=' * len(title))
167
    out.output(title)
168
    out.output('=' * len(title))
167 169

  
168 170
    if os.geteuid() != 0:
169 171
        raise FatalError("You must run %s as root" \
......
176 178
                raise FatalError("Output file %s exists "
177 179
                    "(use --force to overwrite it)." % filename)
178 180

  
179
    disk = Disk(options.source)
181
    disk = Disk(options.source, out)
180 182
    try:
181 183
        snapshot = disk.snapshot()
182 184

  
......
184 186
        dev.mount()
185 187

  
186 188
        osclass = get_os_class(dev.distro, dev.ostype)
187
        image_os = osclass(dev.root, dev.g)
188
        output()
189
        image_os = osclass(dev.root, dev.g, out)
190
        out.output()
189 191

  
190 192
        for sysprep in options.disabled_syspreps:
191 193
            image_os.disable_sysprep(sysprep)
......
195 197

  
196 198
        if options.print_sysprep:
197 199
            image_os.print_syspreps()
198
            output()
200
            out.output()
199 201

  
200 202
        if options.outfile is None and not options.upload:
201 203
            return 0
......
206 208
        metadata = image_os.meta
207 209
        dev.umount()
208 210

  
209
        size = options.shrink and dev.shrink() or dev.size
211
        size = options.shrink and dev.shrink() or dev.meta['SIZE']
210 212
        metadata.update(dev.meta)
211 213

  
212 214
        # Add command line metadata to the collected ones...
213 215
        metadata.update(options.metadata)
214 216

  
215
        checksum = md5(snapshot, size)
217
        md5 = MD5(out)
218
        checksum = md5.compute(snapshot, size)
216 219

  
217 220
        metastring = '\n'.join(
218 221
                ['%s=%s' % (key, value) for (key, value) in metadata.items()])
......
221 224
        if options.outfile is not None:
222 225
            dev.dump(options.outfile)
223 226

  
224
            output('Dumping metadata file...', False)
227
            out.output('Dumping metadata file...', False)
225 228
            with open('%s.%s' % (options.outfile, 'meta'), 'w') as f:
226 229
                f.write(metastring)
227
            success('done')
230
            out.success('done')
228 231

  
229
            output('Dumping md5sum file...', False)
232
            out.output('Dumping md5sum file...', False)
230 233
            with open('%s.%s' % (options.outfile, 'md5sum'), 'w') as f:
231 234
                f.write('%s %s\n' % (checksum, \
232 235
                                            os.path.basename(options.outfile)))
233
            success('done')
236
            out.success('done')
234 237

  
235 238
        # Destroy the device. We only need the snapshot from now on
236 239
        disk.destroy_device(dev)
237 240

  
238
        output()
241
        out.output()
239 242

  
240 243
        uploaded_obj = ""
241 244
        if options.upload:
242
            output("Uploading image to pithos:")
243
            kamaki = Kamaki(options.account, options.token)
245
            out.output("Uploading image to pithos:")
246
            kamaki = Kamaki(options.account, options.token, out)
244 247
            with open(snapshot) as f:
245 248
                uploaded_obj = kamaki.upload(f, size, options.upload,
246 249
                                "(1/4)  Calculating block hashes",
247 250
                                "(2/4)  Uploading missing blocks")
248 251

  
249
            output("(3/4)  Uploading metadata file...", False)
252
            out.output("(3/4)  Uploading metadata file...", False)
250 253
            kamaki.upload(StringIO.StringIO(metastring), size=len(metastring),
251 254
                                remote_path="%s.%s" % (options.upload, 'meta'))
252
            success('done')
253
            output("(4/4)  Uploading md5sum file...", False)
255
            out.success('done')
256
            out.output("(4/4)  Uploading md5sum file...", False)
254 257
            md5sumstr = '%s %s\n' % (
255 258
                checksum, os.path.basename(options.upload))
256 259
            kamaki.upload(StringIO.StringIO(md5sumstr), size=len(md5sumstr),
257 260
                            remote_path="%s.%s" % (options.upload, 'md5sum'))
258
            success('done')
259
            output()
261
            out.success('done')
262
            out.output()
260 263

  
261 264
        if options.register:
262
            output('Registring image to ~okeanos...', False)
265
            out.output('Registring image to ~okeanos...', False)
263 266
            kamaki.register(options.register, uploaded_obj, metadata)
264
            success('done')
265
            output()
267
            out.success('done')
268
            out.output()
266 269

  
267 270
    finally:
268
        output('cleaning up...')
271
        out.output('cleaning up...')
269 272
        disk.cleanup()
270 273

  
271
    success("snf-image-creator exited without errors")
274
    out.success("snf-image-creator exited without errors")
272 275

  
273 276
    return 0
274 277

  

Also available in: Unified diff