Files
zaza-openstack-tests/zaza/openstack/charm_tests/nova/setup.py
2022-06-29 14:06:02 +01:00

84 lines
3.1 KiB
Python

# Copyright 2018 Canonical Ltd.
#
# 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.
"""Code for configuring nova."""
import tenacity
from tenacity import Retrying, stop_after_attempt, wait_exponential
import zaza.openstack.utilities.openstack as openstack_utils
from zaza.openstack.utilities import (
cli as cli_utils,
)
import zaza.openstack.charm_tests.nova.utils as nova_utils
@tenacity.retry(stop=tenacity.stop_after_attempt(3),
wait=tenacity.wait_exponential(
multiplier=1, min=2, max=10))
def create_flavors(nova_client=None):
"""Create basic flavors.
:param nova_client: Authenticated nova client
:type nova_client: novaclient.v2.client.Client
"""
if not nova_client:
keystone_session = openstack_utils.get_overcloud_keystone_session()
nova_client = openstack_utils.get_nova_session_client(
keystone_session)
cli_utils.setup_logging()
for attempt in Retrying(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, min=2, max=10)):
with attempt:
existing_flavors = nova_client.flavors.list()
names = [flavor.name for flavor in existing_flavors]
for flavor in nova_utils.FLAVORS.keys():
if flavor not in names:
nova_flavor = nova_client.flavors.create(
name=flavor,
ram=nova_utils.FLAVORS[flavor]['ram'],
vcpus=nova_utils.FLAVORS[flavor]['vcpus'],
disk=nova_utils.FLAVORS[flavor]['disk'],
flavorid=nova_utils.FLAVORS[flavor]['flavorid'])
if 'extra-specs' in nova_utils.FLAVORS[flavor]:
nova_flavor.set_keys(nova_utils.FLAVORS[flavor]['extra-specs'])
@tenacity.retry(stop=tenacity.stop_after_attempt(3),
wait=tenacity.wait_exponential(
multiplier=1, min=2, max=10))
def manage_ssh_key(nova_client=None):
"""Create basic flavors.
:param nova_client: Authenticated nova client
:type nova_client: novaclient.v2.client.Client
"""
if not nova_client:
keystone_session = openstack_utils.get_overcloud_keystone_session()
nova_client = openstack_utils.get_nova_session_client(
keystone_session)
cli_utils.setup_logging()
if not openstack_utils.valid_key_exists(nova_client,
nova_utils.KEYPAIR_NAME):
key = openstack_utils.create_ssh_key(
nova_client,
nova_utils.KEYPAIR_NAME,
replace=True)
openstack_utils.write_private_key(
nova_utils.KEYPAIR_NAME,
key.private_key)