heat-translator/translator/common/images.py
Mathieu Velten e60d85ed7a Improve and fix images & flavors handling
Fix images matching algo (exact match favored compared to partial match)
Cache images & flavors list
Fix OpenStack session init logic (was called too late and only on deploy)

Change-Id: I6c723f03925a8b2c4b3408fcfb6c78d5af47328d
2016-08-23 16:09:00 +02:00

92 lines
3.5 KiB
Python

# 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',
'type': 'Linux',
'distribution': 'Ubuntu',
'version': '14.04'},
'ubuntu-12.04-software-config-os-init': {'architecture': 'x86_64',
'type': 'Linux',
'distribution': 'Ubuntu',
'version': '12.04'},
'fedora-amd64-heat-config': {'architecture': 'x86_64',
'type': 'Linux',
'distribution': 'Fedora',
'version': '18.0'},
'F18-x86_64-cfntools': {'architecture': 'x86_64',
'type': 'Linux',
'distribution': 'Fedora',
'version': '19'},
'Fedora-x86_64-20-20131211.1-sda': {'architecture': 'x86_64',
'type': 'Linux',
'distribution': 'Fedora',
'version': '20'},
'cirros-0.3.1-x86_64-uec': {'architecture': 'x86_64',
'type': 'Linux',
'distribution': 'CirrOS',
'version': '0.3.1'},
'cirros-0.3.2-x86_64-uec': {'architecture': 'x86_64',
'type': 'Linux',
'distribution': 'CirrOS',
'version': '0.3.2'},
'rhel-6.5-test-image': {'architecture': 'x86_64',
'type': 'Linux',
'distribution': 'RHEL',
'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"]
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