def __init__(self, cloud_name): # RapidLog.log_init('CREATEStack.log', 'DEBUG', 'INFO', '2020.05.05') self.dp_ips = [] self.dp_macs = [] self.mngmt_ips = [] self.names = [] self.number_of_servers = 0 self.cloud_name = cloud_name self.heat_template = 'L6_heat_template.yaml' self.heat_param = 'params_rapid.yaml' self.cloud_config = os_client_config.OpenStackConfig().get_all_clouds() ks_client = None for cloud in self.cloud_config: if cloud.name == self.cloud_name: ks_client = Keystone_Client(**cloud.config['auth']) break if ks_client == None: sys.exit() heat_endpoint = ks_client.service_catalog.url_for( service_type='orchestration', endpoint_type='publicURL') self.heatclient = Heat_Client('1', heat_endpoint, token=ks_client.auth_token) self.nova_client = NovaClient.Client(2, **cloud.config['auth'])
from heatclient.client import Client as Heat_Client from keystoneclient.v3 import Client as Keystone_Client def get_keystone_creds(): creds = {} creds['username'] = '******' creds['password'] = '******' creds['auth_url'] = 'http://192.168.56.111:5000/v3' creds['tenant_name'] = 'demo' return creds creds = get_keystone_creds() ks_client = Keystone_Client(**creds) heat_endpoint = ks_client.service_catalog.url_for(service_type='orchestration', endpoint_type='publicURL') heatclient = Heat_Client('1', heat_endpoint, token=ks_client.auth_token) def create_stack(stack_file_path, stack_name, parameters=None): template = open(stack_file_path) if parameters: stack = heatclient.stacks.create(stack_name=stack_name, template=template.read(), parameters=parameters) else: stack = heatclient.stacks.create(stack_name=stack_name, template=template.read()) template.close() return stack def delete_stack(stack_id): heatclient.stacks.delete(stack_id)
def _run_heat(args, hot): try: from heatclient.common import utils from heatclient.client import Client as Heat_Client from keystoneclient.v3 import Client as Keystone_Client except ImportError as e: LOG.error("You must have python-heatclient in your python path") raise Exception(e) CREDS = { 'username': os.environ.get('OS_USERNAME'), 'password': os.environ.get('OS_PASSWORD'), 'tenant_name': os.environ.get('OS_TENANT_NAME', os.environ.get('OS_PROJECT_NAME')), 'project_name': os.environ.get('OS_TENANT_NAME', os.environ.get('OS_PROJECT_NAME')), 'auth_url': os.environ.get('OS_AUTH_URL'), } ex_msg = ( "%s, ensure your environment (probably the stackrc file) is properly " "configured with OpenStack credentials") # Get name of CREDS key with a value of None and raise an exception # because we're missing some obviously important creds data if not all(CREDS.values()): name = CREDS.keys()[CREDS.values().index(None)] namestr = "%s is missing" % name raise OpenStackConfigurationError(ex_msg % namestr) if args.heat_stack_name: stack_name = args.heat_stack_name else: stack_name = os.path.basename(args.environment) STACK = {'stack_name': stack_name, 'template': hot} if args.heat_parameters: STACK['parameters'] = utils.format_parameters(args.heat_parameters) LOG.debug("Logging into heat") ks_client = Keystone_Client(**CREDS) heat_endpoint = ks_client.service_catalog.url_for( service_type='orchestration', endpoint_type='publicURL') heatclient = Heat_Client('1', heat_endpoint, token=ks_client.auth_token) try: LOG.debug("Checking for existence of heat stack: %s" % stack_name) heatclient.stacks.get(stack_name) stack_exists = True LOG.debug("Already exists") except Exception as e: if e.code == 404: stack_exists = False else: raise Exception(e) stack_action = 'create' if stack_exists and args.heat_stack_update: stack_action = 'update' LOG.debug("Updating stack") heatclient.stacks.update(stack_name, **STACK) time.sleep(5) elif not stack_exists: LOG.debug("Creating stack") heatclient.stacks.create(**STACK) time.sleep(5) while heatclient.stacks.get(stack_name).status == 'IN_PROGRESS': LOG.debug("Waiting on stack...") time.sleep(5) stack = heatclient.stacks.get(stack_name) if stack.status != 'COMPLETE': raise Exception("stack %s returned an unexpected status (%s)" % (stack_name, stack.status)) LOG.debug("Stack %sd!" % stack_action) servers = {} floating_ip = None private_key = None for output in stack.outputs: if output['output_key'] == "floating_ip": floating_ip = output['output_value'] LOG.debug("floating_ip : %s" % floating_ip) elif output['output_key'] == "private_key": private_key = output['output_value'] else: servers[output['output_key']] = output['output_value'] LOG.debug("server : %s (%s) " % (output['output_key'], output['output_value'])) ssh_config = """ Host * User {user} ForwardAgent yes UserKnownHostsFile /dev/null StrictHostKeyChecking no PasswordAuthentication no """.format(user=args.ursula_user) ssh_key_path = os.path.join(args.environment, ".ssh_key") ssh_config_path = os.path.join(args.environment, ".ssh_config") # write out private key if using one generated by heat if private_key: ssh_config += " IdentityFile " + ssh_key_path LOG.debug("writing ssh_key to %s" % ssh_key_path) with open(ssh_key_path, "w") as text_file: text_file.write(private_key) os.chmod(ssh_key_path, 0600) _ssh_add(ssh_key_path) LOG.debug("writing ssh_config to %s" % ssh_config_path) with open(ssh_config_path, "w") as text_file: text_file.write(ssh_config) if floating_ip: ssh_config_pre = """ Host floating_ip Hostname {floating_ip} """ ssh_config = ssh_config_pre.format(floating_ip=floating_ip) with open(ssh_config_path, "a") as text_file: text_file.write(ssh_config) for server, ip in servers.iteritems(): test_ip = ip ssh_config_pre = """ Host {server} Hostname {ip} """ if floating_ip: ssh_config_pre += ( " ProxyCommand ssh -W %h:%p -o StrictHostKeyChecking=no" " {user}@{floating_ip}\n\n") ssh_config = ssh_config_pre.format(server=server, ip=ip, user=args.ursula_user, floating_ip=floating_ip) with open(ssh_config_path, "a") as text_file: text_file.write(ssh_config) ansible_ssh_config_file = os.path.abspath(ssh_config_path) if os.path.isfile(ansible_ssh_config_file): _append_envvar("ANSIBLE_SSH_ARGS", "-F %s" % ansible_ssh_config_file) LOG.debug("waiting for SSH connectivity...") if floating_ip: while not test_ssh(floating_ip): LOG.debug("waiting for SSH connectivity...") time.sleep(5) else: while not test_ssh(test_ip): LOG.debug("waiting for SSH connectivity...") time.sleep(5)