Skip to content

Instantly share code, notes, and snippets.

@ccamacho
Created December 13, 2017 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccamacho/354f798102710d165c1f6167eb533caf to your computer and use it in GitHub Desktop.
Save ccamacho/354f798102710d165c1f6167eb533caf to your computer and use it in GitHub Desktop.
Mistral client snippet
from __future__ import print_function
import copy
import errno
import getpass
import glob
import hashlib
import json
import logging
import os
import platform
import socket
import subprocess
import sys
import tempfile
import time
import uuid
from ironicclient import client as ir_client
from keystoneauth1 import session
from keystoneauth1 import exceptions as ks_exceptions
from keystoneclient import discover
import keystoneauth1.identity.generic as ks_auth
from mistralclient.api import client as mistralclient
from novaclient import client as novaclient
from novaclient import exceptions
import os_client_config
from oslo_config import cfg
from oslo_utils import netutils
import psutil
import pystache
import six
from swiftclient import client as swiftclient
from instack_undercloud import validator
def _extract_from_stackrc(name):
"""Extract authentication values from stackrc
:param name: The value to be extracted. For example: OS_USERNAME or
OS_AUTH_URL.
"""
with open(os.path.expanduser('~/stackrc')) as f:
for line in f:
if name in line:
parts = line.split('=')
return parts[1].rstrip()
def _get_session():
user, password, project, auth_url = _get_auth_values()
auth_kwargs = {
'auth_url': auth_url,
'username': user,
'password': password,
'project_name': project,
'project_domain_name': 'Default',
'user_domain_name': 'Default',
}
auth_plugin = ks_auth.Password(**auth_kwargs)
return session.Session(auth=auth_plugin)
def _get_auth_values():
"""Get auth values from stackrc
Returns the user, password, project and auth_url as read from stackrc,
in that order as a tuple.
"""
user = _extract_from_stackrc('OS_USERNAME')
password = _run_command(['sudo', 'hiera', 'admin_password']).rstrip()
project = _extract_from_stackrc('OS_PROJECT_NAME')
auth_url = _extract_from_stackrc('OS_AUTH_URL')
return user, password, project, auth_url
def _run_command(args, env=None, name=None):
"""Run the command defined by args and return its output
:param args: List of arguments for the command to be run.
:param env: Dict defining the environment variables. Pass None to use
the current environment.
:param name: User-friendly name for the command being run. A value of
None will cause args[0] to be used.
"""
if name is None:
name = args[0]
if env is None:
env = os.environ
env = env.copy()
# When running a localized python script, we need to tell it that we're
# using utf-8 for stdout, otherwise it can't tell because of the pipe.
env['PYTHONIOENCODING'] = 'utf8'
try:
return subprocess.check_output(args,
stderr=subprocess.STDOUT,
env=env).decode('utf-8')
except subprocess.CalledProcessError as e:
LOG.error('%s failed: %s', name, e.output)
raise
####
### Mistral snippet to call mistral client from the undercloud
####
sess = _get_session()
mistral_url = "https://192.168.24.2:13989/v2"
mistral = mistralclient.client(mistral_url=mistral_url, session=sess)
env_name = "tripleo.undercloud-config"
current_env = mistral.environments.get(env_name)
# From here you can invoke and play with Mistral from Python
print (current_env.variables)
config_data = {
"undercloud_ceilometer_snmpd_password":
"uc_pass_asdlsad",
"undercloud_db_password":
"oajsdfiu_asdfasdf"
}
#config_data = {
# "undercloud_ceilometer_snmpd_password":
# "99a4a84e40bfe5f52065c9e3ceb72a58ca1e7c4f",
# "undercloud_db_password":
# "f116d9a56723195c36ed7f9cc142f05146604129"
#}
env_data = mistral.environments.get(env_name)
if (config_data != env_data.variables):
print ("tey are different")
mistral.environments.update(
name=env_name,
description="Undercloud configuration parameters",
variables=json.dumps(config_data)
)
print ("eNV VARS UPDATED")
else:
print ("they are the same")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment