diff options
Diffstat (limited to 'yarnhelper.py')
-rw-r--r-- | yarnhelper.py | 71 |
1 files changed, 63 insertions, 8 deletions
diff --git a/yarnhelper.py b/yarnhelper.py index 67b0a88..a3c2ddd 100644 --- a/yarnhelper.py +++ b/yarnhelper.py @@ -47,12 +47,10 @@ class YarnHelper(object): self._next_match += 1 return self._env[name] - def get_variable(self, name): + def get_variable(self, name, default=None): if self._variables is None: self._variables = self._load_variables() - if name not in self._variables: - raise Error('no variable {}'.format(name)) - return self._variables[name] + return self._variables.get(name, default) def _load_variables(self): if os.path.exists(variables_filename): @@ -137,22 +135,79 @@ class YarnHelper(object): m.close() m.logout() - def gitano(self, args): # pragma: no cover + def ssh_keygen(self, user): # pragma: no cover + filename = self.ssh_key_file_for_user(user) + cliapp.runcmd(['ssh-keygen', '-f', filename, '-N', '', '-C', user]) + with open(filename + '.pub') as f: + return f.read() + + def ssh_key_file_for_user(self, user): # pragma: no cover + return '{}.key'.format(user) + + def repo_ssh_url(self, repo): # pragma: no cover + return 'ssh://git@{}/{}'.format(os.environ['GITANO_SERVER'], repo) + + def git_as(self, user, args): # pragma: no cover + server = os.environ['GITANO_SERVER'] + env = dict(os.environ) + env['GIT_SSH_COMMAND'] = self.env_ssh_command(user) + print 'g_s_c:', env['GIT_SSH_COMMAND'] + return cliapp.runcmd( + ['git'] + args, + stderr=subprocess.STDOUT, + env=env) + + def env_ssh_command(self, user): # pragma: no cover + argv = [ + 'ssh', + '-o', 'PasswordAuthentication=no', + '-o', 'IdentitiesOnly=yes', + ] + if user is not None: + key = self.ssh_key_file_for_user(user) + argv.extend(['-i', key]) + return ' '.join(argv) + + def get_admin_ssh_key(self): # pragma: no cover + return os.environ['ADMIN_SSH_KEY'] + + def get_user_ssh_key(self, user): # pragma: no cover + if user is None: + return self.get_admin_ssh_key() + else: + return self.ssh_key_file_for_user(user) + + def gitano(self, user, args, stdin=None): # pragma: no cover server = os.environ['GITANO_SERVER'] + print '=' * 77 + print 'tgt:', 'git@{}'.format(server) + print 'args:', repr(args.split()) + print 'listdir:', repr(os.listdir('.')) + kwargs = { + 'stderr': subprocess.STDOUT, + 'ssh_options': [ + '-oPasswordAuthentication=no', + '-oIdentitiesOnly=yes', + '-i', self.get_user_ssh_key(user), + ], + } + if stdin is not None: + kwargs['feed_stdin'] = stdin + print 'kwargs:', repr(kwargs) return cliapp.ssh_runcmd( 'git@{}'.format(server), args.split(), - stderr=subprocess.STDOUT) + **kwargs) def gitano_confirm_with_token(self, prefix, which): # pragma: no cover try: - output = self.gitano('{} {}'.format(prefix, which)) + output = self.gitano(None, '{} {}'.format(prefix, which)) except cliapp.AppException: pass else: last_line = output.splitlines()[-1] token = last_line.split()[-1] - self.gitano('{} {}'.format(prefix, token)) + self.gitano(None, '{} {} {}'.format(prefix, which, token)) class Error(Exception): |