Automatically create SSH key file if it doesn't exists

Change-Id: I5c62dc86804a0e53b693767cbc7d15c4899c3e72
This commit is contained in:
Federico Ressi 2019-07-19 17:36:50 +02:00
parent 1d92307e6b
commit e14d962f31
3 changed files with 47 additions and 3 deletions

View File

@ -120,9 +120,6 @@ function configure_tobiko_nova {
# Write key_file # Write key_file
local key_file=${TOBIKO_NOVA_KEY_FILE:-} local key_file=${TOBIKO_NOVA_KEY_FILE:-}
if ! [ -r "${key_file}" ]; then
ssh-keygen -t rsa -q -P "" -f "${key_file}"
fi
iniset "${tobiko_config}" nova key_file "${key_file}" iniset "${tobiko_config}" nova key_file "${key_file}"
} }

View File

@ -26,6 +26,7 @@ from tobiko.openstack import neutron
from tobiko.openstack.stacks import _hot from tobiko.openstack.stacks import _hot
from tobiko.openstack.stacks import _neutron from tobiko.openstack.stacks import _neutron
from tobiko.shell import ssh from tobiko.shell import ssh
from tobiko.shell import sh
CONF = config.CONF CONF = config.CONF
@ -38,6 +39,7 @@ class KeyPairStackFixture(heat.HeatStackFixture):
private_key = None private_key = None
def setup_fixture(self): def setup_fixture(self):
self.create_key_file()
self.read_keys() self.read_keys()
super(KeyPairStackFixture, self).setup_fixture() super(KeyPairStackFixture, self).setup_fixture()
@ -47,6 +49,19 @@ class KeyPairStackFixture(heat.HeatStackFixture):
with open(self.key_file + '.pub', 'r') as fd: with open(self.key_file + '.pub', 'r') as fd:
self.public_key = as_str(fd.read()) self.public_key = as_str(fd.read())
def create_key_file(self):
key_file = os.path.realpath(self.key_file)
if not os.path.isfile(key_file):
key_dir = os.path.dirname(key_file)
if not os.path.isdir(key_dir):
os.makedirs(key_dir)
assert os.path.isdir(key_dir)
command = sh.shell_command(['ssh-keygen',
'-f', key_file,
'-P', ''])
sh.local_execute(command)
assert os.path.isfile(key_file)
class FlavorStackFixture(heat.HeatStackFixture): class FlavorStackFixture(heat.HeatStackFixture):
template = _hot.heat_template_file('nova/flavor.yaml') template = _hot.heat_template_file('nova/flavor.yaml')

View File

@ -0,0 +1,32 @@
# Copyright (c) 2019 Red Hat, Inc.
#
# All Rights Reserved.
#
# 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.
from __future__ import absolute_import
import os
import testtools
import tobiko
from tobiko.openstack import stacks
class KeyPairTest(testtools.TestCase):
stack = tobiko.required_setup_fixture(stacks.KeyPairStackFixture)
def test_key_files(self):
self.assertTrue(os.path.isfile(self.stack.key_file))
self.assertTrue(os.path.isfile(self.stack.key_file + '.pub'))