summaryrefslogtreecommitdiffstats
path: root/tosca2heat/heat-translator/translator/common/images.py
blob: d9b8818a4ebbbeb71479392ee52e527ed7696a02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import logging

try:
    import glanceclient.client
    client_available = True
except ImportError:
    client_available = False
    pass

log = logging.getLogger('heat-translator')


PREDEF_IMAGES = {
    'ubuntu-software-config-os-init': {'architecture': 'x86_64',
                                       'os_type': 'linux',
                                       'os_distro': 'ubuntu',
                                       'os_version': '14.04'
                                       },

    'ubuntu-12.04-software-config-os-init': {'architecture': 'x86_64',
                                             'os_type': 'linux',
                                             'os_distro': 'ubuntu',
                                             'os_version': '12.04'
                                             },
    'fedora-amd64-heat-config': {'architecture': 'x86_64',
                                 'os_type': 'linux',
                                 'os_distro': 'fedora',
                                 'os_version': '18.0'
                                 },
    'F18-x86_64-cfntools': {'architecture': 'x86_64',
                            'os_type': 'linux',
                            'os_distro': 'fedora',
                            'os_version': '19'
                            },
    'Fedora-x86_64-20-20131211.1-sda': {'architecture': 'x86_64',
                                        'os_type': 'linux',
                                        'os_distro': 'fedora',
                                        'os_version': '20'
                                        },
    'cirros-0.3.1-x86_64-uec': {'architecture': 'x86_64',
                                'os_type': 'linux',
                                'os_distro': 'cirros',
                                'os_version': '0.3.1'
                                },
    'cirros-0.3.2-x86_64-uec': {'architecture': 'x86_64',
                                'os_type': 'linux',
                                'os_distro': 'cirros',
                                'os_version': '0.3.2'
                                },
    'rhel-6.5-test-image': {'architecture': 'x86_64',
                            'os_type': 'linux',
                            'os_distro': 'rhel',
                            'os_version': '6.5'
                            }
}

SESSION = None

IMAGES = {}


def get_images():
    global IMAGES

    if IMAGES:
        return IMAGES

    if SESSION is not None and client_available:
        try:
            client = glanceclient.client.Client("2", session=SESSION)
        except Exception as e:
            # Handles any exception coming from openstack
            log.warn(_('Choosing predefined images since received '
                       'Openstack Exception: %s') % str(e))
        else:
            for image in client.images.list():
                image_name = image.name.encode('ascii', 'ignore')
                metadata = ["architecture", "type", "distribution", "version",
                            "os_distro", "os_type", "os_version"]
                if any(key in image.keys() for key in metadata):
                    IMAGES[image_name] = {}
                    for key in metadata:
                        if key in image.keys():
                            IMAGES[image_name][key] = image[key]

    if not IMAGES:
        IMAGES = PREDEF_IMAGES

    return IMAGES