def user(config): master = config.get('master') op = config.get('op') username = config.get('username') passwd = config.get('passwd') master, port = master.split(":") port = int(port) bb_username = config.get('bb_username') bb_password = config.get('bb_password') if bb_username or bb_password: bb_password = users.encrypt(bb_password) info = config.get('info') ids = config.get('ids') # find identifier if op == add if info and op == 'add': for user in info: user['identifier'] = sorted(user.values())[0] uc = usersclient.UsersClient(master, username, passwd, port) output = yield uc.send(op, bb_username, bb_password, ids, info) if output: print(output) defer.returnValue(0)
def test_usersclient_ids(self): uc = usersclient.UsersClient('localhost', "user", "userpw", 1234) yield uc.send('remove', None, None, ['x'], None) self.assertProcess('localhost', 1234, dict(op='remove', bb_username=None, bb_password=None, ids=['x'], info=None))
def test_usersclient_ids(self): uc = usersclient.UsersClient('localhost', "user", "userpw", 1234) d = uc.send('remove', None, None, ['x'], None) def check(_): self.assertProcess('localhost', 1234, dict(op='remove', bb_username=None, bb_password=None, ids=['x'], info=None)) d.addCallback(check) return d
def test_usersclient_info(self): uc = usersclient.UsersClient('localhost', "user", "userpw", 1234) yield uc.send('update', 'bb_user', 'hashed_bb_pass', None, [{'identifier': 'x', 'svn': 'x'}]) self.assertProcess('localhost', 1234, dict(op='update', bb_username='******', bb_password='******', ids=None, info=[dict(identifier='x', svn='x')]))
def users_client(config, runReactor=False): from buildbot.clients import usersclient from buildbot.process.users import users # for srcs, encrypt # accepted attr_types by `buildbot user`, in addition to users.srcs attr_types = ['identifier', 'email'] master = config.get('master') assert master, "you must provide the master location" try: master, port = master.split(":") port = int(port) except: raise AssertionError("master must have the form 'hostname:port'") op = config.get('op') assert op, "you must specify an operation: add, remove, update, get" if op not in ['add', 'remove', 'update', 'get']: raise AssertionError("bad op %r, use 'add', 'remove', 'update', " "or 'get'" % op) username = config.get('username') passwd = config.get('passwd') assert username and passwd, "A username and password pair must be given" bb_username = config.get('bb_username') bb_password = config.get('bb_password') if bb_username or bb_password: if op != 'update': raise AssertionError("bb_username and bb_password only work " "with update") if not bb_username or not bb_password: raise AssertionError("Must specify both bb_username and " "bb_password or neither.") bb_password = users.encrypt(bb_password) # check op and proper args info = config.get('info') ids = config.get('ids') # check for erroneous args if not info and not ids: raise AssertionError("must specify either --ids or --info") if info and ids: raise AssertionError("cannot use both --ids and --info, use " "--ids for 'remove' and 'get', --info " "for 'add' and 'update'") if op == 'add' or op == 'update': if ids: raise AssertionError("cannot use --ids with 'add' or 'update'") if op == 'update': for user in info: if 'identifier' not in user: raise ValueError("no ids found in update info, use: " "--info=id:type=value,type=value,..") if op == 'add': for user in info: if 'identifier' in user: raise ValueError("id found in add info, use: " "--info=type=value,type=value,..") if op == 'remove' or op == 'get': if info: raise AssertionError("cannot use --info with 'remove' or 'get'") # find identifier if op == add if info: # check for valid types for user in info: for attr_type in user: if attr_type not in users.srcs + attr_types: raise ValueError("Type not a valid attr_type, must be in: " "%r" % (users.srcs + attr_types)) if op == 'add': user['identifier'] = user.values()[0] uc = usersclient.UsersClient(master, username, passwd, port) d = uc.send(op, bb_username, bb_password, ids, info) if runReactor: from twisted.internet import reactor status = [True] def printSuccess(res): print res def failed(f): status[0] = False print "user op NOT sent - something went wrong: " + str(f) d.addCallbacks(printSuccess, failed) d.addBoth(lambda _: reactor.stop()) reactor.run() return status[0] return d