def get_sandbox(session, logger, sandbox, query_options=None): """Allow an optional author field.""" sbx_split = sandbox.split('/') first, second = '', '' if len(sbx_split) <= 1: dbsandbox = Sandbox.get_unique(session, sandbox, compel=True, query_options=query_options) dbauthor = None return (dbsandbox, dbauthor) first = '/'.join(sbx_split[:-1]) second = sbx_split[-1] dbsandbox = Sandbox.get_unique(session, second, compel=True, query_options=query_options) dbauthor = get_user_principal(session, first) return (dbsandbox, dbauthor)
def render(self, session, logger, dbuser, sandbox, **arguments): sandbox = self.force_my_sandbox(session, logger, dbuser, sandbox) dbsandbox = Sandbox.get_unique(session, sandbox, compel=True) if not dbuser: raise AuthorizationException("Cannot get a sandbox without" " an authenticated connection.") userdir = os.path.join(self.config.get("broker", "templatesdir"), dbuser.name) sandboxdir = os.path.join(userdir, dbsandbox.name) if os.path.exists(sandboxdir): raise ArgumentError("Directory '%s' already exists. Use git " "fetch within the directory to update it." % sandboxdir) if not os.path.exists(userdir): try: logger.client_info("creating %s" % userdir) os.makedirs(userdir, mode=0775) except OSError, e: raise ArgumentError("failed to mkdir %s: %s" % (userdir, e)) args = [self.config.get("broker", "mean")] args.append("chown") args.append("-owner") args.append("%s" % dbuser.name) args.append("-path") args.append("%s" % userdir) try: run_command(args, logger=logger) except ProcessException, e: remove_dir(userdir) raise e
def render(self, session, logger, branch, sandbox, bundle, sync, rebase, **arguments): # Most of the logic here is duplicated in deploy if branch: sandbox = branch dbsandbox = Sandbox.get_unique(session, sandbox, compel=True) (handle, filename) = mkstemp() contents = b64decode(bundle) write_file(filename, contents, logger=logger) if sync and not dbsandbox.is_sync_valid and dbsandbox.trackers: # FIXME: Maybe raise an ArgumentError and request that the # command run with --nosync? Maybe provide a --validate flag? # For now, we just auto-flip anyway (below) making the point moot. pass if not dbsandbox.is_sync_valid: dbsandbox.is_sync_valid = True if rebase and dbsandbox.trackers: raise ArgumentError( "{0} has trackers, rebasing is not allowed.".format(dbsandbox)) kingdir = self.config.get("broker", "kingdir") rundir = self.config.get("broker", "rundir") tempdir = mkdtemp(prefix="publish_", suffix="_%s" % dbsandbox.name, dir=rundir) try: run_git([ "clone", "--shared", "--branch", dbsandbox.name, kingdir, dbsandbox.name ], path=tempdir, logger=logger) temprepo = os.path.join(tempdir, dbsandbox.name) run_git(["bundle", "verify", filename], path=temprepo, logger=logger) ref = "HEAD:%s" % (dbsandbox.name) command = ["pull", filename, ref] if rebase: command.append("--force") run_git(command, path=temprepo, logger=logger, loglevel=CLIENT_INFO) # FIXME: Run tests before pushing back to template-king if rebase: target_ref = "+" + dbsandbox.name else: target_ref = dbsandbox.name run_git(["push", "origin", target_ref], path=temprepo, logger=logger) except ProcessException, e: raise ArgumentError("\n%s%s" % (e.out, e.err))
def render(self, session, logger, branch, sandbox, bundle, sync, rebase, **arguments): # Most of the logic here is duplicated in deploy if branch: sandbox = branch dbsandbox = Sandbox.get_unique(session, sandbox, compel=True) (handle, filename) = mkstemp() contents = b64decode(bundle) write_file(filename, contents, logger=logger) if sync and not dbsandbox.is_sync_valid and dbsandbox.trackers: # FIXME: Maybe raise an ArgumentError and request that the # command run with --nosync? Maybe provide a --validate flag? # For now, we just auto-flip anyway (below) making the point moot. pass if not dbsandbox.is_sync_valid: dbsandbox.is_sync_valid = True if rebase and dbsandbox.trackers: raise ArgumentError("{0} has trackers, rebasing is not allowed." .format(dbsandbox)) kingdir = self.config.get("broker", "kingdir") rundir = self.config.get("broker", "rundir") tempdir = mkdtemp(prefix="publish_", suffix="_%s" % dbsandbox.name, dir=rundir) try: run_git(["clone", "--shared", "--branch", dbsandbox.name, kingdir, dbsandbox.name], path=tempdir, logger=logger) temprepo = os.path.join(tempdir, dbsandbox.name) run_git(["bundle", "verify", filename], path=temprepo, logger=logger) ref = "HEAD:%s" % (dbsandbox.name) command = ["pull", filename, ref] if rebase: command.append("--force") run_git(command, path=temprepo, logger=logger, loglevel=CLIENT_INFO) # FIXME: Run tests before pushing back to template-king if rebase: target_ref = "+" + dbsandbox.name else: target_ref = dbsandbox.name run_git(["push", "origin", target_ref], path=temprepo, logger=logger) except ProcessException, e: raise ArgumentError("\n%s%s" % (e.out, e.err))
def render(self, session, dbuser, sandbox, comments, compiler_version, autosync, **arguments): dbsandbox = Sandbox.get_unique(session, sandbox, compel=True) # FIXME: proper authorization if dbsandbox.owner != dbuser and dbuser.role.name != 'aqd_admin': raise AuthorizationException("Only the owner or an AQD admin can " "update a sandbox.") if comments: dbsandbox.comments = comments if compiler_version: dbsandbox.compiler = expand_compiler(self.config, compiler_version) if autosync is not None: dbsandbox.autosync = autosync session.flush() return
def get_branch_and_author(session, logger, domain=None, sandbox=None, branch=None, compel=False): dbbranch = None dbauthor = None if domain: dbbranch = Domain.get_unique(session, domain, compel=True) elif branch: dbbranch = Branch.get_unique(session, branch, compel=True) elif sandbox: (author, slash, name) = sandbox.partition('/') if not slash: raise ArgumentError("Expected sandbox as 'author/branch', author " "name and branch name separated by a slash.") dbbranch = Sandbox.get_unique(session, name, compel=True) dbauthor = get_user_principal(session, author) elif compel: raise ArgumentError("Please specify either sandbox or domain.") return (dbbranch, dbauthor)
def render(self, session, logger, dbuser, sandbox, start, get, comments, **arguments): if not dbuser: raise AuthorizationException("Cannot create a sandbox without an " "authenticated connection.") sandbox = self.force_my_sandbox(session, logger, dbuser, sandbox) # See `git check-ref-format --help` for naming restrictions. # We want to layer a few extra restrictions on top of that... valid = re.compile('^[a-zA-Z0-9_.-]+$') if (not valid.match(sandbox)): raise ArgumentError("sandbox name '%s' is not valid" % sandbox) Branch.get_unique(session, sandbox, preclude=True) if not start: start = self.config.get("broker", "default_domain_start") dbstart = Branch.get_unique(session, start, compel=True) kingdir = self.config.get("broker", "kingdir") base_commit = run_git(["show-ref", "--hash", "refs/heads/" + dbstart.name], logger=logger, path=kingdir) compiler = self.config.get("panc", "pan_compiler") dbsandbox = Sandbox(name=sandbox, owner=dbuser, compiler=compiler, base_commit=base_commit, comments=comments) session.add(dbsandbox) session.flush() # Currently this will fail if the branch already exists... # That seems like the right behavior. It's an internal # consistency issue that would need to be addressed explicitly. run_git(["branch", sandbox, dbstart.name], logger=logger, path=kingdir) if get == False: # The client knows to interpret an empty response as no action. return [] return CommandGet.render(self, session=session, logger=logger, dbuser=dbuser, sandbox=sandbox)