~hudson-openstack/nova/trunk

« back to all changes in this revision

Viewing changes to bin/nova-import-canonical-imagestore

  • Committer: Tarmac
  • Author(s): Matthew Hooker
  • Date: 2011-08-10 14:53:53 UTC
  • mfrom: (1355.2.24 pylint-fixes)
  • Revision ID: tarmac-20110810145353-rmc0lkjirgunxe08
These fixes are the result of trolling the pylint violations here

https://jenkins.openstack.org/job/nova-pylint-errors/violations/

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
 
 
4
 
# Copyright 2010 United States Government as represented by the
5
 
# Administrator of the National Aeronautics and Space Administration.
6
 
# All Rights Reserved.
7
 
#
8
 
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
9
 
#    not use this file except in compliance with the License. You may obtain
10
 
#    a copy of the License at
11
 
#
12
 
#         http://www.apache.org/licenses/LICENSE-2.0
13
 
#
14
 
#    Unless required by applicable law or agreed to in writing, software
15
 
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16
 
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17
 
#    License for the specific language governing permissions and limitations
18
 
#    under the License.
19
 
 
20
 
"""
21
 
  Download images from Canonical Image Store
22
 
"""
23
 
 
24
 
import gettext
25
 
import json
26
 
import os
27
 
import tempfile
28
 
import shutil
29
 
import subprocess
30
 
import sys
31
 
import urllib2
32
 
 
33
 
# If ../nova/__init__.py exists, add ../ to Python search path, so that
34
 
# it will override what happens to be installed in /usr/(local/)lib/python...
35
 
possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
36
 
                                   os.pardir,
37
 
                                   os.pardir))
38
 
if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
39
 
    sys.path.insert(0, possible_topdir)
40
 
 
41
 
gettext.install('nova', unicode=1)
42
 
 
43
 
from nova import flags
44
 
from nova import log as logging
45
 
from nova import utils
46
 
from nova.objectstore import image
47
 
 
48
 
FLAGS = flags.FLAGS
49
 
 
50
 
API_URL = 'https://imagestore.canonical.com/api/dashboard'
51
 
 
52
 
 
53
 
def get_images():
54
 
    """Get a list of the images from the imagestore URL."""
55
 
    images = json.load(urllib2.urlopen(API_URL))['images']
56
 
    images = [img for img in images if img['title'].find('amd64') > -1]
57
 
    return images
58
 
 
59
 
 
60
 
def download(img):
61
 
    """Download an image to the local filesystem."""
62
 
    # FIXME(ja): add checksum/signature checks
63
 
    tempdir = tempfile.mkdtemp(prefix='cis-')
64
 
 
65
 
    kernel_id = None
66
 
    ramdisk_id = None
67
 
 
68
 
    for f in img['files']:
69
 
        if f['kind'] == 'kernel':
70
 
            dest = os.path.join(tempdir, 'kernel')
71
 
            subprocess.call(['curl', '--fail', f['url'], '-o', dest])
72
 
            kernel_id = image.Image.add(dest,
73
 
                description='kernel/' + img['title'], kernel=True)
74
 
 
75
 
    for f in img['files']:
76
 
        if f['kind'] == 'ramdisk':
77
 
            dest = os.path.join(tempdir, 'ramdisk')
78
 
            subprocess.call(['curl', '--fail', f['url'], '-o', dest])
79
 
            ramdisk_id = image.Image.add(dest,
80
 
                description='ramdisk/' + img['title'], ramdisk=True)
81
 
 
82
 
    for f in img['files']:
83
 
        if f['kind'] == 'image':
84
 
            dest = os.path.join(tempdir, 'image')
85
 
            subprocess.call(['curl', '--fail', f['url'], '-o', dest])
86
 
            ramdisk_id = image.Image.add(dest,
87
 
                description=img['title'], kernel=kernel_id, ramdisk=ramdisk_id)
88
 
 
89
 
    shutil.rmtree(tempdir)
90
 
 
91
 
 
92
 
def main():
93
 
    """Main entry point."""
94
 
    utils.default_flagfile()
95
 
    argv = FLAGS(sys.argv)
96
 
    logging.setup()
97
 
    images = get_images()
98
 
 
99
 
    if len(argv) == 2:
100
 
        for img in images:
101
 
            if argv[1] == 'all' or argv[1] == img['title']:
102
 
                download(img)
103
 
    else:
104
 
        print 'usage: %s (title|all)'
105
 
        print 'available images:'
106
 
        for img in images:
107
 
            print img['title']
108
 
 
109
 
if __name__ == '__main__':
110
 
    main()