def works(cls): try: ClHelper.run_command('which gem') return True except exceptions.ClException: cls._debug_doesnt_work('"gem" binary not found') return False
def _github_create_ssh_key(cls, **kwargs): try: login = cls._user.login pkey_path = '{home}/.ssh/{keyname}'.format( home=os.path.expanduser('~'), keyname=settings.GITHUB_SSH_KEYNAME.format(login=login)) # create ssh keys here if not os.path.isfile( '{pkey_path}.pub'.format(pkey_path=pkey_path)): ClHelper.run_command('ssh-keygen -t rsa -f {pkey_path}\ -N \"\" -C \"DeveloperAssistant\"' .\ format(pkey_path=pkey_path)) ClHelper.run_command( 'ssh-add {pkey_path}'.format(pkey_path=pkey_path)) public_key = ClHelper.run_command( 'cat {pkey_path}.pub'.format(pkey_path=pkey_path)) # find out if this key is already registered with this user for key in cls._user.get_keys(): # don't use "==" because we have comments etc added in public_key if key._key in public_key: break else: cls._user.create_key("devassistant", public_key) # next, create ~/.ssh/config entry for the key, if system username != GH login cls._github_create_ssh_config_entry(**kwargs) except exceptions.ClException: pass # TODO: determine and log the error
def test_env(self): out = ClHelper.run_command('echo $DEVASSISTANTTESTFOO', env={'DEVASSISTANTTESTFOO': 'foo'}) assert out == 'foo' # if the variable is not specified, check that there's nothing printed out = ClHelper.run_command('echo $DEVASSISTANTTESTFOO') assert out == ''
def _github_create_and_push(cls, **kwargs): ClHelper.run_command('cd {0}'.format(os.path.abspath(os.path.expanduser(kwargs['name'])))) logger.info('Registering your project on GitHub as {0}/{1}...'.format(cls._github_username(**kwargs), cls._github_reponame(**kwargs))) cls._github_create_repo(**kwargs) logger.info('Pushing your project to the new GitHub repository...') cls._github_push(**kwargs) logger.info('GitHub repository was created and source code pushed.')
def works(cls): try: ClHelper.run_command('npm') return True except exceptions.ClException as e: msg = 'Package manager for "{0}" not operational: {1}'.format(dep_t, e) logger.error(msg) raise exceptions.PackageManagerNotOperational(msg)
def _docker_group_add(cls): username = getpass.getuser() try: logger.info("Adding {0} to group docker ...".format(username)) ClHelper.run_command('bash -c "usermod -a -G docker {0}"'.format(username), as_user="******") except exceptions.ClException as e: msg = 'Failed to add user to "docker" group: {0}'.format(e.output) raise exceptions.CommandException(msg)
def _github_store_authorization(cls, user, auth): """Store an authorization token for the given GitHub user in the git global config file. """ ClHelper.run_command("git config --global github.token.{login} {token}".format( login=user.login, token=auth.token), log_secret=True) ClHelper.run_command("git config --global github.user.{login} {login}".format( login=user.login))
def _github_add_remote_origin(cls, **kwargs): reponame = cls._github_reponame(**kwargs) # if system username != GH login, we need to use [email protected]{login}:... # else just [email protected]:... dash_login = '' if getpass.getuser() != cls._user.login: dash_login = '******' + cls._user.login ClHelper.run_command("git remote add origin [email protected]{dash_login}:{login}/{reponame}.git".\ format(dash_login=dash_login, login=cls._user.login, reponame=reponame), logging.INFO)
def works(cls): try: ClHelper.run_command('npm') return True except exceptions.ClException as e: msg = 'Package manager for "{0}" not operational: {1}'.format( dep_t, e) logger.error(msg) raise exceptions.PackageManagerNotOperational(msg)
def install(cls, *args): cmd = [cls.c_homebrew, 'install'] quoted_pkgs = ['"{0}"'.format(pkg) for pkg in args] cmd.extend(quoted_pkgs) try: ClHelper.run_command(' '.join(cmd), ignore_sigint=True) return args except exceptions.ClException: return False
def test_format_as_another_user_picks_the_right_exe( self, correct, wrong, system_exe): flexmock(os.path).should_receive('isfile').with_args( '/usr/libexec/da_auth').and_return(system_exe) flexmock(os).should_receive('access').with_args( '/usr/libexec/da_auth', 1).and_return(system_exe) assert correct in ClHelper.format_for_another_user('foo', 'root') assert wrong not in ClHelper.format_for_another_user('foo', 'root')
def _docker_group_add(cls): username = getpass.getuser() try: logger.info('Adding {0} to group docker ...'.format(username)) ClHelper.run_command('bash -c "usermod -a -G docker {0}"'.format(username), as_user='******') except exceptions.ClException as e: msg = 'Failed to add user to "docker" group: {0}'.format(e.output) raise exceptions.CommandException(msg)
def install(cls, *args): cmd = [cls.c_pacman, '-S', '--noconfirm'] quoted_pkgs = map(lambda pkg: '"{pkg}"'.format(pkg=pkg), args) cmd.extend(quoted_pkgs) try: ClHelper.run_command(' '.join(cmd), ignore_sigint=True, as_user='******') return args except exceptions.ClException: return False
def install(cls, *args): cmd = [cls.c_gem, 'install'] quoted_pkgs = map(lambda pkg: '"{pkg}"'.format(pkg=pkg), args) cmd.extend(quoted_pkgs) try: ClHelper.run_command(' '.join(cmd), ignore_sigint=True) return args except exceptions.ClException: return False
def is_group_installed(cls, group): logger.info('Checking for presence of group {0}...'.format(group)) try: ClHelper.run_command('{pacman} -Qg "{group}"'.format( pacman=cls.c_pacman, group=group)) return group except exceptions.ClException: return False
def _github_create_and_push(cls, **kwargs): ClHelper.run_command('cd {0}'.format( os.path.abspath(os.path.expanduser(kwargs['name'])))) logger.info('Registering your project on GitHub as {0}/{1}...'.format( cls._github_username(**kwargs), cls._github_reponame(**kwargs))) cls._github_create_repo(**kwargs) logger.info('Pushing your project to the new GitHub repository...') cls._github_push(**kwargs) logger.info('GitHub repository was created and source code pushed.')
def is_pkg_installed(cls, dep): logger.info('Checking for presence of {0}...'.format(dep), extra={'event_type': 'dep_check'}) try: ClHelper.run_command(' '.join([cls.c_gem, 'list', '-i', dep])) logger.info('Found {0}'.format(dep), extra={'event_type': 'dep_found'}) return True except exceptions.ClException: logger.info('Not found, will install', extra={'event_type': 'dep_not_found'}) return False
def is_pkg_installed(cls, dep): logger.info('Checking for presence of {0}...'.format(dep), extra={'event_type': 'dep_check'}) try: ClHelper.run_command(' '.join([cls.c_gem, 'list', '-i', '"{pkg}"'.format(pkg=dep)])) logger.info('Found {0}'.format(dep), extra={'event_type': 'dep_found'}) return True except exceptions.ClException: logger.info('Not found, will install', extra={'event_type': 'dep_not_found'}) return False
def is_group_installed(cls, group): logger.info('Checking for presence of group {0}...'.format(group)) try: ClHelper.run_command('{pacman} -Qg "{group}"'.\ format(pacman=cls.c_pacman, group=group)) return group except exceptions.ClException: return False
def _github_store_authorization(cls, user, auth): """Store an authorization token for the given GitHub user in the git global config file. """ ClHelper.run_command( "git config --global github.token.{login} {token}".format( login=user.login, token=auth.token), log_secret=True) ClHelper.run_command( "git config --global github.user.{login} {login}".format( login=user.login))
def _github_add_remote_origin(cls, **kwargs): """Note: the kwargs are not the global context here, but what cls.format_args returns.""" reponame = kwargs['reponame'] login = kwargs['login'] # if system username != GH login, we need to use [email protected]{login}:... # else just [email protected]:... dash_login = '' if getpass.getuser() != login: dash_login = '******' + login ClHelper.run_command("git remote add origin [email protected]{dash_login}:{login}/{reponame}.git".\ format(dash_login=dash_login, login=login, reponame=reponame), logging.INFO)
def test_output_from_process_with_closed_stdout(self): """Previously, DevAssistant occasionally failed in Travis because of race condition in ClHelper.run_command. The cause of this was that on very slow machines the subprocess would close its output (it just finished), while proc.poll() still returned None. In such cases, readline() returned empty string, which was attached to the stdout (represented as list of output lines). Stdout was then joined with "'\n'.join(stdout)" - that resulted in string with bazillion newlines because of all the appended empty strings.""" test_script = os.path.join(os.path.dirname(__file__), 'fixtures', 'proc_with_closed_stdout.py') try: ClHelper.run_command(test_script) except ClException as e: assert 'script really ran' in e.output assert '\n\n' not in e.output
def _github_create_auth(cls): """ Store token into ~/.gitconfig. If token is not defined then store it into ~/.gitconfig file """ if not cls._token: try: auth = cls._user.create_authorization(scopes=['repo', 'user'], note="DeveloperAssistant") ClHelper.run_command("git config --global github.token.{login} {token}".format( login=cls._user.login, token=auth.token)) ClHelper.run_command("git config --global github.user.{login} {login}".format( login=cls._user.login)) except cls._gh_module.GithubException as e: logger.warning('Creating authorization failed: {0}'.format(e))
def _github_create_auth(cls, **kwargs): """ Store token into ~/.gitconfig. If token is not defined then store it into ~/.gitconfig file """ if not cls._token: try: auth = cls._user.create_authorization(scopes=['repo', 'user'], note="DeveloperAssistant") ClHelper.run_command("git config --global github.token.{login} {token}".format( login=cls._user.login, token=auth.token)) ClHelper.run_command("git config --global github.user.{login} {login}".format( login=cls._user.login)) except cls._gh_module.GithubException as e: logger.warning('Creating authorization failed: {0}'.format(e))
def test_connect_quoted_both_quote_types_in_first_part(self): assert ClHelper._connect_quoted([u'-i', u'"s|\'NAME\'.', u"''|'NAME'.", u'\'asd\'|"', u'asd/asd/settings.py']) == \ [u'-i', u"\"s|'NAME'. ''|'NAME'. 'asd'|\"", u'asd/asd/settings.py']
def test_command_processors(self): def foo(cmd_str): return 'FOO=bar && ' + cmd_str ClHelper.command_processors['foo'] = foo out = ClHelper.run_command('echo $FOO') ClHelper.command_processors.pop('foo') assert out == 'bar'
def _github_create_ssh_key(cls): try: login = cls._user.login pkey_path = '{home}/.ssh/{keyname}'.format(home=os.path.expanduser('~'), keyname=settings.GITHUB_SSH_KEYNAME.format(login=login)) # TODO: handle situation where {pkey_path} exists, but it's not registered on GH # generate ssh key ClHelper.run_command('ssh-keygen -t rsa -f {pkey_path}\ -N \"\" -C \"DevAssistant\"'.\ format(pkey_path=pkey_path)) ClHelper.run_command('ssh-add {pkey_path}'.format(pkey_path=pkey_path)) public_key = ClHelper.run_command('cat {pkey_path}.pub'.format(pkey_path=pkey_path)) cls._user.create_key("devassistant", public_key) except exceptions.ClException as e: msg = 'Couldn\'t create a new ssh key: {e}'.format(e) raise exceptions.CommandException(msg)
def _github_push(cls): try: ret = ClHelper.run_command("git push -u origin master") logger.info('Source code was successfully pushed.') return (True, ret) except exceptions.ClException as e: logger.warning('Problem pushing source code: {0}'.format(e.output)) return (False, e.output)
def _github_create_ssh_key(cls): """Creates a local ssh key, if it doesn't exist already, and uploads it to Github.""" try: login = cls._user.login pkey_path = '{home}/.ssh/{keyname}'.format(home=os.path.expanduser('~'), keyname=settings.GITHUB_SSH_KEYNAME.format(login=login)) # generate ssh key only if it doesn't exist if not os.path.exists(pkey_path): ClHelper.run_command('ssh-keygen -t rsa -f {pkey_path}\ -N \"\" -C \"DevAssistant\"'.\ format(pkey_path=pkey_path)) ClHelper.run_command('ssh-add {pkey_path}'.format(pkey_path=pkey_path)) public_key = ClHelper.run_command('cat {pkey_path}.pub'.format(pkey_path=pkey_path)) cls._user.create_key("DevAssistant", public_key) except exceptions.ClException as e: msg = 'Couldn\'t create a new ssh key: {e}'.format(e) raise exceptions.CommandException(msg)
def _github_token(cls, login): if not cls._token: try: cls._token = ClHelper.run_command("git config github.token.{login}".format( login=login)) except exceptions.ClException: pass # token is not available yet return cls._token
def rpm_q(cls, rpm_name): try: # if we install by e.g. virtual provide, then rpm -q foo will fail # therefore we always use rpm -q --whatprovides foo return ClHelper.run_command(' '.join([ cls.c_rpm, '-q', '--whatprovides', '"' + rpm_name.strip() + '"' ])) except exceptions.ClException: return False
def test_run_command_cd(self): cwd = os.getcwd() try: tmpdir = tempfile.gettempdir() out = ClHelper.run_command('cd {0}'.format(tmpdir)) assert out == '' assert os.getcwd() == tmpdir finally: os.chdir(cwd)
def test_run_command_cd(self): cwd = os.getcwd() try: # On OSX, /etc is a link to /private/etc, hence the realpath call tmpdir = os.path.realpath(tempfile.gettempdir()) out = ClHelper.run_command('cd {0}'.format(tmpdir)) assert out == '' assert os.getcwd() == tmpdir finally: os.chdir(cwd)
def _github_token(cls, **kwargs): if not cls._token: try: cls._token = ClHelper.run_command("git config github.token.{login}".format( login=cls._github_login(**kwargs))) except exceptions.ClException as e: # token is not available yet pass return cls._token
def rpm_q(cls, rpm_name): try: # if we install by e.g. virtual provide, then rpm -q foo will fail # therefore we always use rpm -q --whatprovides foo return ClHelper.run_command(' '.join([cls.c_rpm, '-q', '--whatprovides', '"' + rpm_name.strip() + '"'])) except exceptions.ClException: return False
def run(cls, c): log_level = logging.DEBUG as_user = None if 'i' in c.comm_type: log_level = logging.INFO if 'r' in c.comm_type: as_user = '******' # if there is an exception, just let it bubble up result = ClHelper.run_command(c.input_res, log_level, as_user=as_user) return [True, result]
def is_group_installed(cls, group): logger.info('Checking for presence of group {0}...'.format(group)) output = ClHelper.run_command(' '.join( [cls.c_yum, 'group', 'list', '"{0}"'.format(group)])) if 'Installed Groups' in output: logger.info('Found {0}'.format(group), extra={'event_type': 'dep_found'}) return group else: logger.info('Not found, will install', extra={'event_type': 'dep_not_found'}) return False
def is_pacmanpkg_installed(cls, pkg_name): logger.info('Checking for presence of {0}...'.format(pkg_name), extra={'event_type': 'dep_check'}) try: found_pkg = ClHelper.run_command('{pacman} -Q "{pkg}"'.\ format(pacman=cls.c_pacman, pkg=pkg_name)) logger.info('Found {0}'.format(found_pkg), extra={'event_type': 'dep_found'}) return found_pkg except exceptions.ClException: logger.info('Not found, will install', extra={'event_type': 'dep_not_found'}) return False
def test_output_from_process_with_lots_of_output(self): """When a subprocess is fired, we use readline() while it's running and then read() the rest once it finishes (if there is some rest). Previously, DevAssistant didn't put a newline between these two, so it resulted in failures like: https://bugzilla.redhat.com/show_bug.cgi?id=1061207 This attempts to test this by running "cat" on very long file, hoping that this situation occurs, but it may not. TODO: make this test stable under any circumstances.""" test_file = os.path.join(os.path.dirname(__file__), 'fixtures', 'long_cat') out = ClHelper.run_command('cat {0}'.format(test_file)) assert 'ba' not in out
def _docker_group_active(cls): if cls._has_docker_group is None: logger.debug('Determining if current user has active "docker" group ...') # we have to run cl command, too see if the user has already re-logged # after being added to docker group, so that he can effectively use it if 'docker' in ClHelper.run_command('groups').split(): logger.debug('Current user is in "docker" group.') cls._has_docker_group = True else: logger.debug('Current user is not in "docker" group.') cls._has_docker_group = False return cls._has_docker_group
def resolve(cls, *args): logger.info('Resolving Homebrew dependencies ...') for pkg in args: logger.debug('Looking at {0}'.format(pkg)) logger.debug('Installing/Updating:') to_install = set() for pkg in args: query = ClHelper.run_command(' '.join([cls.c_homebrew, 'deps -n', pkg])) to_install.update(query.split('\n')) return list(to_install)