Merge "Rework nova_cell_v2_discover_host.py to use nova.conf and python novaclient"
This commit is contained in:
commit
9213525d70
@ -14,82 +14,96 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from optparse import OptionParser
|
||||||
import os
|
import os
|
||||||
import pwd
|
|
||||||
import socket
|
import socket
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
# Delete this immediataly as it contains auth info
|
|
||||||
os.unlink(__file__)
|
|
||||||
|
|
||||||
# Only need root to read this script, drop to nova user
|
from keystoneauth1 import loading
|
||||||
nova_uid, nova_gid = pwd.getpwnam('nova')[2:4]
|
from keystoneauth1 import session
|
||||||
os.setgid(nova_gid)
|
|
||||||
os.setuid(nova_uid)
|
|
||||||
|
|
||||||
|
from novaclient import client
|
||||||
|
|
||||||
os.environ.update(
|
from six.moves.configparser import SafeConfigParser
|
||||||
OS_PROJECT_DOMAIN_NAME='__OS_PROJECT_DOMAIN_NAME',
|
|
||||||
OS_USER_DOMAIN_NAME='__OS_PROJECT_USER_NAME',
|
|
||||||
OS_PROJECT_NAME='__OS_PROJECT_NAME',
|
|
||||||
OS_USERNAME='__OS_USERNAME',
|
|
||||||
OS_PASSWORD='__OS_PASSWORD',
|
|
||||||
OS_AUTH_URL='__OS_AUTH_URL',
|
|
||||||
OS_AUTH_TYPE='password',
|
|
||||||
OS_IDENTITY_API_VERSION='3'
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
|
||||||
my_host = subprocess.check_output([
|
LOG = logging.getLogger('nova_cell_v2_discover_host')
|
||||||
'crudini',
|
|
||||||
'--get',
|
|
||||||
'/etc/nova/nova.conf',
|
|
||||||
'DEFAULT',
|
|
||||||
'host'
|
|
||||||
], universal_newlines=True).rstrip()
|
|
||||||
except subprocess.CalledProcessError:
|
|
||||||
# If host isn't set nova defaults to this
|
|
||||||
my_host = socket.gethostname()
|
|
||||||
|
|
||||||
# Wait until this host is listed in the service list then
|
nova_cfg = '/etc/nova/nova.conf'
|
||||||
# run cellv2 host discovery
|
|
||||||
retries = 10
|
if __name__ == '__main__':
|
||||||
for i in range(retries):
|
parser = OptionParser(usage="usage: %prog [options]")
|
||||||
try:
|
parser.add_option('-k', '--insecure',
|
||||||
service_output = subprocess.check_output([
|
action="store_false",
|
||||||
'openstack',
|
dest='insecure',
|
||||||
'-q',
|
default=True,
|
||||||
'--os-interface',
|
help='Allow insecure connection when using SSL')
|
||||||
'internal',
|
|
||||||
'compute',
|
(options, args) = parser.parse_args()
|
||||||
'service',
|
LOG.debug('Running with parameter insecure = %s',
|
||||||
'list',
|
options.insecure)
|
||||||
'-c',
|
|
||||||
'Host',
|
if os.path.isfile(nova_cfg):
|
||||||
'-c',
|
config = SafeConfigParser()
|
||||||
'Zone',
|
config.read(nova_cfg)
|
||||||
'-f',
|
else:
|
||||||
'value'
|
LOG.error('Nova configuration file %s does not exist', nova_cfg)
|
||||||
], universal_newlines=True)
|
sys.exit(1)
|
||||||
service_list = service_output.split('\n')
|
|
||||||
for entry in service_list:
|
my_host = config.get('DEFAULT', 'host')
|
||||||
# skip any empty lines
|
if not my_host:
|
||||||
if not entry:
|
# If host isn't set nova defaults to this
|
||||||
continue
|
my_host = socket.gethostname()
|
||||||
host, zone = entry.split()
|
|
||||||
if host == my_host and zone != 'internal':
|
loader = loading.get_plugin_loader('password')
|
||||||
print('(cellv2) Service registered, running discovery')
|
auth = loader.load_from_options(
|
||||||
sys.exit(subprocess.call([
|
auth_url=config.get('neutron',
|
||||||
'/usr/bin/nova-manage',
|
'auth_url'),
|
||||||
'cell_v2',
|
username=config.get('neutron',
|
||||||
'discover_hosts',
|
'username'),
|
||||||
'--by-service',
|
password=config.get('neutron',
|
||||||
'--verbose'
|
'password'),
|
||||||
]))
|
project_name=config.get('neutron',
|
||||||
print('(cellv2) Waiting for service to register')
|
'project_name'),
|
||||||
except subprocess.CalledProcessError:
|
project_domain_name=config.get('neutron',
|
||||||
print('(cellv2) Retrying')
|
'project_domain_name'),
|
||||||
time.sleep(30)
|
user_domain_name=config.get('neutron',
|
||||||
|
'user_domain_name'))
|
||||||
|
sess = session.Session(auth=auth, verify=options.insecure)
|
||||||
|
nova = client.Client('2.11', session=sess, endpoint_type='internal')
|
||||||
|
|
||||||
|
# Wait until this host is listed in the service list then
|
||||||
|
# run cellv2 host discovery
|
||||||
|
retries = 10
|
||||||
|
for i in range(retries):
|
||||||
|
try:
|
||||||
|
service_list = nova.services.list(binary='nova-compute')
|
||||||
|
for entry in service_list:
|
||||||
|
host = getattr(entry, 'host', '')
|
||||||
|
zone = getattr(entry, 'zone', '')
|
||||||
|
if host == my_host and zone != 'internal':
|
||||||
|
LOG.info('(cellv2) Service registered, running discovery')
|
||||||
|
sys.exit(subprocess.call([
|
||||||
|
'/usr/bin/nova-manage',
|
||||||
|
'cell_v2',
|
||||||
|
'discover_hosts',
|
||||||
|
'--by-service',
|
||||||
|
'--verbose'
|
||||||
|
]))
|
||||||
|
if len(service_list) == 0:
|
||||||
|
LOG.warning('(cellv2) no nova-compute service registered' +
|
||||||
|
' after %i checks', i)
|
||||||
|
LOG.info('(cellv2) Waiting for service to register')
|
||||||
|
except subprocess.CalledProcessError:
|
||||||
|
LOG.info('(cellv2) Retrying')
|
||||||
|
except Exception as e:
|
||||||
|
LOG.exception('Error during host discovery:')
|
||||||
|
time.sleep(30)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
# vim: set et ts=4 sw=4 :
|
||||||
|
Loading…
Reference in New Issue
Block a user