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()
def key(self): """ Serves the public SSH key for the user that own the current service """ # look for the ssh key of the current user private_key_path = os.path.expanduser('~/.ssh/id_rsa') public_key_path = os.path.expanduser('~/.ssh/id_rsa.pub') ssh_dir = os.path.dirname(public_key_path) if not os.path.isdir(ssh_dir): logger.warning('.ssh directory not found, creating one at: %s', ssh_dir) mkdir(ssh_dir) # if there isn't one create it if not os.path.exists(public_key_path): logger.warning('expected public key not found: %s', public_key_path) logger.warning('will create new ssh key pair') # create one command = [ 'ssh-keygen', '-q', '-t', 'rsa', '-N', '', '-f', private_key_path, ] out, err, code = process.run(command, send_input='y\n') if code != 0: error(500, err) # define the file to download response.headers['Content-Disposition'] = 'attachment; filename=id_rsa.pub' with open(public_key_path) as key_contents: key = StringIO() key.write(key_contents.read()) key.seek(0) response.app_iter = FileIter(key)
def ensure_ssh_keys(): """ Generate ssh keys as early as possible so that they are available to all web server workers immediately when serving ``/setup/key/``. This helper does not use logging because it is too early in running the application and no logging has been configured yet. """ # look for the ssh key of the current user private_key_path = os.path.expanduser('~/.ssh/id_rsa') public_key_path = os.path.expanduser('~/.ssh/id_rsa.pub') ssh_dir = os.path.dirname(public_key_path) if not os.path.isdir(ssh_dir): mkdir(ssh_dir) # if there isn't one create it if not os.path.exists(public_key_path): # create one command = [ 'ssh-keygen', '-q', '-t', 'rsa', '-N', '', '-f', private_key_path, ] out, err, code = process.run(command, send_input='y\n') if code != 0: raise RuntimeError('ssh-keygen failed: %s %s' % (out, err))
def test_decode_unicode_on_the_fly_for_stderr(self, monkeypatch): monkeypatch.setattr( process.subprocess, 'Popen', lambda *a, **kw: FakePopen('stdout', '™') ) stdout, stderr, code = process.run('ls') assert stderr == u'\u2122'
def main(self): parser = Transport(self.arguments, options=self.options, check_help=True) parser.catch_help = self._help parser.parse_args() parser.catches_help() branch = parser.get('--branch', 'master') user = parser.get('--user', 'vagrant') high_verbosity = '-vvvv' if parser.has('-vvvv') else '-v' if not parser.unknown_commands: log.error( "it is required to pass a host to deploy to, but none was provided" ) raise SystemExit(1) command = [ "ansible-playbook", "-i", "%s," % parser.unknown_commands[-1], high_verbosity, "-u", user, "--extra-vars", 'branch=%s' % branch, "deploy.yml", ] log.debug("Running command: %s" % ' '.join(command)) out, err, code = process.run(command, cwd=playbook_path) log.error(err) log.debug(out)
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()
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()
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()
def key(self): """ Serves the public SSH key for the user that own the current service """ # look for the ssh key of the current user private_key_path = os.path.expanduser('~/.ssh/id_rsa') public_key_path = os.path.expanduser('~/.ssh/id_rsa.pub') ssh_dir = os.path.dirname(public_key_path) if not os.path.isdir(ssh_dir): logger.warning('.ssh directory not found, creating one at: %s', ssh_dir) mkdir(ssh_dir) # if there isn't one create it if not os.path.exists(public_key_path): logger.warning('expected public key not found: %s', public_key_path) logger.warning('will create new ssh key pair') # create one command = [ 'ssh-keygen', '-q', '-t', 'rsa', '-N', '', '-f', private_key_path, ] out, err, code = process.run(command, send_input='y\n') if code != 0: error(500, err) # define the file to download response.headers[ 'Content-Disposition'] = 'attachment; filename=id_rsa.pub' with open(public_key_path) as key_contents: key = StringIO() key.write(key_contents.read()) key.seek(0) response.app_iter = FileIter(key)
def main(self): parser = Transport(self.arguments, options=self.options, check_help=True) parser.catch_help = self._help parser.parse_args() parser.catches_help() branch = parser.get('--branch', 'master') user = parser.get('--user', 'vagrant') high_verbosity = '-vvvv' if parser.has('-vvvv') else '-v' if not parser.unknown_commands: log.error("it is required to pass a host to deploy to, but none was provided") raise SystemExit(1) command = [ "ansible-playbook", "-i", "%s," % parser.unknown_commands[-1], high_verbosity, "-u", user, "--extra-vars", 'branch=%s' % branch, "deploy.yml", ] log.debug("Running command: %s" % ' '.join(command)) out, err, code = process.run(command, cwd=playbook_path) log.error(err) log.debug(out)