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