Merge "Release D doc update"
[parser.git] / tosca2heat / heat-translator / translator / common / images.py
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import logging
14
15 try:
16     import glanceclient.client
17     client_available = True
18 except ImportError:
19     client_available = False
20     pass
21
22 log = logging.getLogger('heat-translator')
23
24
25 PREDEF_IMAGES = {
26     'ubuntu-software-config-os-init': {'architecture': 'x86_64',
27                                        'type': 'Linux',
28                                        'distribution': 'Ubuntu',
29                                        'version': '14.04'},
30     'ubuntu-12.04-software-config-os-init': {'architecture': 'x86_64',
31                                              'type': 'Linux',
32                                              'distribution': 'Ubuntu',
33                                              'version': '12.04'},
34     'fedora-amd64-heat-config': {'architecture': 'x86_64',
35                                  'type': 'Linux',
36                                  'distribution': 'Fedora',
37                                  'version': '18.0'},
38     'F18-x86_64-cfntools': {'architecture': 'x86_64',
39                             'type': 'Linux',
40                             'distribution': 'Fedora',
41                             'version': '19'},
42     'Fedora-x86_64-20-20131211.1-sda': {'architecture': 'x86_64',
43                                         'type': 'Linux',
44                                         'distribution': 'Fedora',
45                                         'version': '20'},
46     'cirros-0.3.1-x86_64-uec': {'architecture': 'x86_64',
47                                 'type': 'Linux',
48                                 'distribution': 'CirrOS',
49                                 'version': '0.3.1'},
50     'cirros-0.3.2-x86_64-uec': {'architecture': 'x86_64',
51                                 'type': 'Linux',
52                                 'distribution': 'CirrOS',
53                                 'version': '0.3.2'},
54     'rhel-6.5-test-image': {'architecture': 'x86_64',
55                             'type': 'Linux',
56                             'distribution': 'RHEL',
57                             'version': '6.5'}
58 }
59
60 SESSION = None
61
62 IMAGES = {}
63
64
65 def get_images():
66     global IMAGES
67
68     if IMAGES:
69         return IMAGES
70
71     if SESSION is not None and client_available:
72         try:
73             client = glanceclient.client.Client("2", session=SESSION)
74         except Exception as e:
75             # Handles any exception coming from openstack
76             log.warn(_('Choosing predefined images since received '
77                        'Openstack Exception: %s') % str(e))
78         else:
79             for image in client.images.list():
80                 image_name = image.name.encode('ascii', 'ignore')
81                 metadata = ["architecture", "type", "distribution", "version"]
82                 if any(key in image.keys() for key in metadata):
83                     IMAGES[image_name] = {}
84                     for key in metadata:
85                         if key in image.keys():
86                             IMAGES[image_name][key] = image[key]
87
88     if not IMAGES:
89         IMAGES = PREDEF_IMAGES
90
91     return IMAGES