Esempio n. 1
0
def install(component, hosts, identifier, tags="package-install", extra_vars=None):
    """
    ``component``: What component is it going to get installed: mon, rgw, osd, calamari
    ``hosts``: A list of hosts to install, these host must be resolvable
    ``tags``: The tags as a comma-delimeted string that represents all the tags
              this ansible call should follow. For example "package-install, other-tag"
    ``identifier``: The UUID identifer for the task object so this function can capture process
                    metadata and persist it to the database
    """
    if not extra_vars:
        extra_vars = dict()
    component_title = "%s%s" % (component, '' if component.endswith('s') else 's')
    hosts_file = util.generate_inventory_file(component_title, hosts, identifier)
    command = process.make_ansible_command(hosts_file, identifier, tags=tags, extra_vars=extra_vars)
    task = models.Task.query.filter_by(identifier=identifier).first()
    task.command = ' '.join(command)
    task.started = datetime.now()
    # force a commit here so we can reference this command later if it fails
    models.commit()
    out, err, exit_code = process.run(command)
    task.succeeded = not exit_code
    task.exit_code = exit_code
    task.stdout = out
    task.stderr = err
    task.ended = datetime.now()
    models.commit()
Esempio n. 2
0
 def test_multiple_hosts(self, tmpdir):
     hosts = ['google.com', 'redhat.com']
     result = util.generate_inventory_file([("mons", hosts)], "uuid", tmp_dir=str(tmpdir))
     with open(result, 'r') as f:
         data = [line.strip() for line in f.readlines()]
         assert "[mons]" in data
         assert "google.com" in data
         assert "redhat.com" in data
Esempio n. 3
0
 def test_single_host(self, tmpdir):
     result = util.generate_inventory_file([("mons", "google.com")],
                                           "uuid",
                                           tmp_dir=str(tmpdir))
     with open(result, 'r') as f:
         data = [line.strip() for line in f.readlines()]
         assert "[mons]" in data
         assert "google.com" in data
Esempio n. 4
0
def call_ansible(inventory,
                 identifier,
                 tags="",
                 skip_tags="",
                 extra_vars=None,
                 playbook="site.yml.sample",
                 **kw):
    """
    This task builds an ansible-playbook command and runs it.

    ``inventory``: A list of tuples that details an ansible inventory. For example:
                   [('mons', ['mon1.host', 'mon2.host']), ('osds', ['osd1.host'])]
    ``tags``: The tags as a comma-delimeted string that represents all the tags
              this ansible call should follow. For example "package-install, other-tag"
    ``skip_tags``: The tags as a comma-delimeted string that represents all the tags
                   this ansible call should skip. For example "package-install, other-tag"
    ``identifier``: The UUID identifer for the task object so this function can capture process
                    metadata and persist it to the database
    ``verbose``: Optional keyword argument, to flag the need for increased verbosity
                 when running ansible
    """
    verbose = kw.get('verbose', False)
    if not extra_vars:
        extra_vars = dict()
    hosts_file = util.generate_inventory_file(inventory, identifier)
    command = process.make_ansible_command(hosts_file,
                                           identifier,
                                           tags=tags,
                                           extra_vars=extra_vars,
                                           skip_tags=skip_tags,
                                           playbook=playbook,
                                           verbose=verbose)
    task = models.Task.query.filter_by(identifier=identifier).first()
    task.command = ' '.join(command)
    task.started = datetime.now()
    # force a commit here so we can reference this command later if it fails
    models.commit()
    working_dir = util.get_ceph_ansible_path()
    # ansible depends on relative pathing to figure out how to load
    # plugins, among other things. Setting the current working directory
    # for this subprocess call to the directory where the playbook resides
    # allows ansible to properly find action plugins defined in ceph-ansible.
    kwargs = dict(cwd=working_dir)
    try:
        out, err, exit_code = process.run(command, **kwargs)
    except Exception as error:
        task.succeeded = False
        task.exit_code = -1
        task.stderr = str(error)
        logger.exception('failed to run command')
    else:
        task.succeeded = not exit_code
        task.exit_code = exit_code
        task.stdout = out
        task.stderr = err

    task.ended = datetime.now()
    models.commit()
Esempio n. 5
0
 def test_multiple_hosts(self, tmpdir):
     hosts = ['google.com', 'redhat.com']
     result = util.generate_inventory_file([("mons", hosts)],
                                           "uuid",
                                           tmp_dir=str(tmpdir))
     with open(result, 'r') as f:
         data = [line.strip() for line in f.readlines()]
         assert "[mons]" in data
         assert "google.com" in data
         assert "redhat.com" in data
Esempio n. 6
0
 def test_multiple_groups(self, tmpdir):
     hosts = ['google.com', 'redhat.com']
     inventory = [('mons', hosts), ('osds', 'osd1.host')]
     result = util.generate_inventory_file(inventory, "uuid", tmp_dir=str(tmpdir))
     with open(result, 'r') as f:
         data = [line.strip() for line in f.readlines()]
         assert "[mons]" in data
         assert "google.com" in data
         assert "redhat.com" in data
         assert "[osds]" in data
         assert "osd1.host" in data
Esempio n. 7
0
 def test_multiple_groups(self, tmpdir):
     hosts = ['google.com', 'redhat.com']
     inventory = [('mons', hosts), ('osds', 'osd1.host')]
     result = util.generate_inventory_file(inventory,
                                           "uuid",
                                           tmp_dir=str(tmpdir))
     with open(result, 'r') as f:
         data = [line.strip() for line in f.readlines()]
         assert "[mons]" in data
         assert "google.com" in data
         assert "redhat.com" in data
         assert "[osds]" in data
         assert "osd1.host" in data
Esempio n. 8
0
def call_ansible(inventory, identifier, tags="", skip_tags="", extra_vars=None, playbook="site.yml.sample", **kw):
    """
    This task builds an ansible-playbook command and runs it.

    ``inventory``: A list of tuples that details an ansible inventory. For example:
                   [('mons', ['mon1.host', 'mon2.host']), ('osds', ['osd1.host'])]
    ``tags``: The tags as a comma-delimeted string that represents all the tags
              this ansible call should follow. For example "package-install, other-tag"
    ``skip_tags``: The tags as a comma-delimeted string that represents all the tags
                   this ansible call should skip. For example "package-install, other-tag"
    ``identifier``: The UUID identifer for the task object so this function can capture process
                    metadata and persist it to the database
    ``verbose``: Optional keyword argument, to flag the need for increased verbosity
                 when running ansible
    """
    verbose = kw.get('verbose', False)
    if not extra_vars:
        extra_vars = dict()
    hosts_file = util.generate_inventory_file(inventory, identifier)
    command = process.make_ansible_command(
        hosts_file, identifier, tags=tags, extra_vars=extra_vars,
        skip_tags=skip_tags, playbook=playbook, verbose=verbose
    )
    task = models.Task.query.filter_by(identifier=identifier).first()
    task.command = ' '.join(command)
    task.started = datetime.now()
    # force a commit here so we can reference this command later if it fails
    models.commit()
    working_dir = util.get_ceph_ansible_path()
    # ansible depends on relative pathing to figure out how to load
    # plugins, among other things. Setting the current working directory
    # for this subprocess call to the directory where the playbook resides
    # allows ansible to properly find action plugins defined in ceph-ansible.
    kwargs = dict(cwd=working_dir)
    try:
        out, err, exit_code = process.run(command, **kwargs)
    except Exception as error:
        task.succeeded = False
        task.exit_code = -1
        task.stderr = str(error)
        logger.exception('failed to run command')
    else:
        task.succeeded = not exit_code
        task.exit_code = exit_code
        task.stdout = out
        task.stderr = err

    task.ended = datetime.now()
    models.commit()
Esempio n. 9
0
def call_ansible(component,
                 hosts,
                 identifier,
                 tags="",
                 skip_tags="",
                 extra_vars=None):
    """
    This task builds an ansible-playbook command and runs it.

    ``component``: What component we're going to work with: mon, rgw, osd, calamari
    ``hosts``: A list of hosts to run ansible against, these host must be resolvable
    ``tags``: The tags as a comma-delimeted string that represents all the tags
              this ansible call should follow. For example "package-install, other-tag"
    ``skip_tags``: The tags as a comma-delimeted string that represents all the tags
                   this ansible call should skip. For example "package-install, other-tag"
    ``identifier``: The UUID identifer for the task object so this function can capture process
                    metadata and persist it to the database
    """
    if not extra_vars:
        extra_vars = dict()
    component_title = "%s%s" % (component,
                                '' if component.endswith('s') else 's')
    hosts_file = util.generate_inventory_file(component_title, hosts,
                                              identifier)
    command = process.make_ansible_command(hosts_file,
                                           identifier,
                                           tags=tags,
                                           extra_vars=extra_vars,
                                           skip_tags=skip_tags)
    task = models.Task.query.filter_by(identifier=identifier).first()
    task.command = ' '.join(command)
    task.started = datetime.now()
    # force a commit here so we can reference this command later if it fails
    models.commit()
    working_dir = util.get_playbook_path()
    working_dir = os.path.split(working_dir)[0]
    # ansible depends on relative pathing to figure out how to load
    # plugins, among other things. Setting the current working directory
    # for this subprocess call to the directory where the playbook resides
    # allows ansible to properly find action plugins defined in ceph-ansible.
    kwargs = dict(cwd=working_dir)
    out, err, exit_code = process.run(command, **kwargs)
    task.succeeded = not exit_code
    task.exit_code = exit_code
    task.stdout = out
    task.stderr = err
    task.ended = datetime.now()
    models.commit()
Esempio n. 10
0
def call_ansible(component, hosts, identifier, tags="", skip_tags="", extra_vars=None):
    """
    This task builds an ansible-playbook command and runs it.

    ``component``: What component we're going to work with: mon, rgw, osd, calamari
    ``hosts``: A list of hosts to run ansible against, these host must be resolvable
    ``tags``: The tags as a comma-delimeted string that represents all the tags
              this ansible call should follow. For example "package-install, other-tag"
    ``skip_tags``: The tags as a comma-delimeted string that represents all the tags
                   this ansible call should skip. For example "package-install, other-tag"
    ``identifier``: The UUID identifer for the task object so this function can capture process
                    metadata and persist it to the database
    """
    if not extra_vars:
        extra_vars = dict()
    component_title = "%s%s" % (component, '' if component.endswith('s') else 's')
    hosts_file = util.generate_inventory_file(component_title, hosts, identifier)
    command = process.make_ansible_command(hosts_file, identifier, tags=tags, extra_vars=extra_vars, skip_tags=skip_tags)
    task = models.Task.query.filter_by(identifier=identifier).first()
    task.command = ' '.join(command)
    task.started = datetime.now()
    # force a commit here so we can reference this command later if it fails
    models.commit()
    working_dir = util.get_playbook_path()
    working_dir = os.path.split(working_dir)[0]
    # ansible depends on relative pathing to figure out how to load
    # plugins, among other things. Setting the current working directory
    # for this subprocess call to the directory where the playbook resides
    # allows ansible to properly find action plugins defined in ceph-ansible.
    kwargs = dict(cwd=working_dir)
    out, err, exit_code = process.run(command, **kwargs)
    task.succeeded = not exit_code
    task.exit_code = exit_code
    task.stdout = out
    task.stderr = err
    task.ended = datetime.now()
    models.commit()
Esempio n. 11
0
 def test_tmp_dir(self, tmpdir):
     result = util.generate_inventory_file("mons",
                                           "google.com",
                                           "uuid",
                                           tmp_dir=str(tmpdir))
     assert str(tmpdir) in result
Esempio n. 12
0
 def test_correct_filename(self, tmpdir):
     result = util.generate_inventory_file("mons",
                                           "google.com",
                                           "uuid",
                                           tmp_dir=str(tmpdir))
     assert "uuid_" in result
Esempio n. 13
0
 def test_single_host(self, tmpdir):
     result = util.generate_inventory_file([("mons", "google.com")], "uuid", tmp_dir=str(tmpdir))
     with open(result, 'r') as f:
         data = [line.strip() for line in f.readlines()]
         assert "[mons]" in data
         assert "google.com" in data
Esempio n. 14
0
 def test_tmp_dir(self, tmpdir):
     result = util.generate_inventory_file([("mons", "google.com")], "uuid", tmp_dir=str(tmpdir))
     assert str(tmpdir) in result
Esempio n. 15
0
 def test_correct_filename(self, tmpdir):
     result = util.generate_inventory_file([("mons", "google.com")], "uuid", tmp_dir=str(tmpdir))
     assert "uuid_" in result