Example #1
0
def create_stack(**kwargs):
    sys.path.insert(0, '/root/scripts/')
    from core.credentials import get_keystone_creds
    creds = get_keystone_creds()
    creds.pop('project_id', None)
    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)

    with open(kwargs['yaml_file']) as infile:
        txt = infile.read()

    templateLoader = jinja2.FileSystemLoader(searchpath=kwargs['jinja_path'])
    templateEnv = jinja2.Environment(loader=templateLoader)
    template_file = kwargs['jinja_file']
    template = templateEnv.get_template(template_file)
    template_vars = kwargs['stack_template']
    data = yaml.load(template.render(template_vars))
    input_data = {
        "files": {},
        "disable_rollback": "true",
        "stack_name": kwargs['stack_name'],
        "template": txt,
        "parameters": data,
        "environment": {}
    }

    stack = heatclient.stacks.create(**input_data)
    return stack
Example #2
0
def clean(creds):
    creds.pop('project_id', None)
    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)
    for stack in heatclient.stacks.list():
        delete_stack(heatclient, stack)

    print('Done. Check "slist {}".'.format(creds.get('tenant_name', None)) +
          '                                                                  ')
Example #3
0
 def __init__(self,
              version='2.1',
              username=None,
              api_key=None,
              project_id=None,
              auth_url='',
              **kwargs):
     loader = loading.get__plugin_loader('password')
     auth = loader.load_from_options(auth_url=auth_url,
                                     username=username,
                                     user_domain_name=u'Default',
                                     password=api_key,
                                     project_domain_name=u'Default')
     session = session.Session(auth=auth)
     self.nova_client = nvclient.Client(version, session=session)
     self.cinder_client = cdclient.Client(version, session=session)
     self.heat_client = Heat_Client('1', session=session)
Example #4
0
 def __init__(self, log_file=None):
     self.session = self._get_auth_session()
     self.nova = novaclient.Client(version=NOVA_VERSION, session=self.session)
     self.neutron = neutronclient.Client(session=self.session)
     self.glance = glance_client('2', session=self.session)
     self.heat = Heat_Client('1', session=self.session)
     images = self.glance.images.list()
     self.hypervisors = self.get_hypervisors()
     self.flavors = self.get_flavors()
     self.networks = self.get_networks()
     self.zones = self.nova.availability_zones.list()
     self.services = {}
     self.sfc_queue = []
     self.finished = False
     self.log_file = log_file
     self.network_id = self.set_network('DARK-net', '192.168.0.0/16')
     if log_file is not None:
         logging.basicConfig(filename=self.log_file, level=logging.INFO)
 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'])
Example #6
0
def _run_heat(args, hot):
    try:
        from heatclient.common import utils
        from heatclient.client import Client as Heat_Client
        from keystoneclient.v2_0 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')
        ),
        '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 ubuntu
  ForwardAgent yes
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
"""

    # write out private key if using one generated by heat
    if private_key:
        ssh_config += "  IdentityFile tmp/ssh_key"
        LOG.debug("writing ssh_key to /tmp/ssh_key")
        with open("tmp/ssh_key", "w") as text_file:
            text_file.write(private_key)
        os.chmod("tmp/ssh_key", 0600)
        _ssh_add("tmp/ssh_key")

    with open("tmp/ssh_config", "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("tmp/ssh_config", "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 -o StrictHostKeyChecking=no"
                               " ubuntu@{floating_ip} nc %h %p\n\n")
        ssh_config = ssh_config_pre.format(
            server=server, ip=ip, floating_ip=floating_ip)
        with open("tmp/ssh_config", "a") as text_file:
            text_file.write(ssh_config)

    ansible_ssh_config_file = "tmp/ssh_config"
    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)
Example #7
0
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)
#!/usr/bin/python
from os import environ as env
from keystoneauth1 import loading
from keystoneauth1 import session
from keystoneclient.v2_0 import client
from heatclient.client import Client as Heat_Client
from credentials import get_nova_creds

creds = get_nova_creds()

keystone = client.Client(auth_url=env['OS_AUTH_URL'],
                         username=env['OS_USERNAME'],
                         password=env['OS_PASSWORD'],
                         tenant_name=env['OS_TENANT_NAME'])


heat_endpoint = keystone.service_catalog.url_for(service_type='orchestration',endpoint_type='publicURL')
heatclient = Heat_Client('1',heat_endpoint,token=keystone.auth_token)

def list_stacks():
    return heatclient.stacks.list()

aa = list_stacks()

stack_id = "e3610e64-3574-42b5-b77f-7efa3421b10f"

heatclient.resources.list(stack_id)
Example #9
0
def _run_heat(args, hot):
    try:
        from heatclient.client import Client as Heat_Client
        from keystoneclient.v2_0 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['OS_USERNAME'],
        'password': os.environ['OS_PASSWORD'],
        'tenant_name': os.environ['OS_TENANT_NAME'],
        'auth_url': os.environ['OS_AUTH_URL'],
    }

    stack_name = os.path.basename(args.environment)

    STACK = {'stack_name': stack_name, 'template': hot}

    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)

    if 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 creation...")
            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 created!")

    servers = {}
    floating_ip = 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']))

    LOG.debug("writing ssh_key to /tmp/ssh_key")
    with open("tmp/ssh_key", "w") as text_file:
        text_file.write(private_key)
    os.chmod("tmp/ssh_key", 0600)
    args.ursula_ssh_key = "tmp/ssh_key"

    ssh_config = """
Host *
  User ubuntu
  ForwardAgent yes
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile tmp/ssh_key
"""
    with open("tmp/ssh_config", "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("tmp/ssh_config", "a") as text_file:
            text_file.write(ssh_config)
        _ssh_add("tmp/ssh_key")

    for server, ip in servers.iteritems():
        test_ip = ip
        ssh_config_pre = """
Host {server}
  Hostname {ip}
"""
        if floating_ip:
            ssh_config_pre += ("  ProxyCommand ssh -o StrictHostKeyChecking=no"
                               " ubuntu@{floating_ip} nc %h %p\n\n")
        ssh_config = ssh_config_pre.format(server=server,
                                           ip=ip,
                                           floating_ip=floating_ip)
        with open("tmp/ssh_config", "a") as text_file:
            text_file.write(ssh_config)

    ansible_ssh_config_file = "tmp/ssh_config"
    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)