Example #1
0
    def render(self, session, vendor, comments, **arguments):
        validate_template_name("--vendor", vendor)
        Vendor.get_unique(session, vendor, preclude=True)

        dbv = Vendor(name=vendor, comments=comments)
        session.add(dbv)
        return
Example #2
0
    def render(self, session, osname, osversion, archetype, comments,
               **arguments):
        validate_nlist_key("--osname", osname)
        validate_template_name("--osversion", osversion)

        dbarchetype = Archetype.get_unique(session, archetype, compel=True)
        OperatingSystem.get_unique(session, name=osname, version=osversion,
                                   archetype=dbarchetype, preclude=True)

        dbos = OperatingSystem(name=osname, version=osversion,
                               archetype=dbarchetype, comments=comments)
        session.add(dbos)

        return
Example #3
0
    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...
        validate_template_name("--sandbox", sandbox)

        Branch.get_unique(session, sandbox, preclude=True)

        # Check that the user has cleared up a directory of the same
        # name; if this is not the case the branch may be created (in git)
        # and added to the database - however CommandGet will fail roleing
        # back the database leaving the branch created in git
        templatesdir = self.config.get("broker", "templatesdir")
        sandboxdir = os.path.join(templatesdir, dbuser.name, sandbox)
        if os.path.exists(sandboxdir):
            raise ArgumentError("Sandbox directory %s already exists; " "cannot create branch." % sandboxdir)

        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 we arrive there the above "git branch" command has succeeded;
        # therefore we should comit the changes to the database.  If this is
        # not done, and CommandGet fails (see dir check above), then the
        # git branch will be created but the database changes roled back.
        session.commit()

        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)
Example #4
0
    def render(self, session, logger, dbuser, domain, track, start,
               change_manager, comments, allow_manage, **arguments):
        if not dbuser:
            raise AuthorizationException("Cannot create a domain without "
                                         "an authenticated connection.")

        validate_template_name("--domain", domain)
        Branch.get_unique(session, domain, preclude=True)

        compiler = self.config.get("panc", "pan_compiler")
        dbtracked = None
        if track:
            dbtracked = Branch.get_unique(session, track, compel=True)
            if getattr(dbtracked, "tracked_branch", None):
                raise ArgumentError("Cannot nest tracking.  Try tracking "
                                    "{0:l} directly.".format(dbtracked.tracked_branch))
            start_point = dbtracked
            if change_manager:
                raise ArgumentError("Cannot enforce a change manager for "
                                    "tracking domains.")
        else:
            if not start:
                start = self.config.get("broker", "default_domain_start")
            start_point = Branch.get_unique(session, start, compel=True)

        dbdomain = Domain(name=domain, owner=dbuser, compiler=compiler,
                          tracked_branch=dbtracked,
                          requires_change_manager=bool(change_manager),
                          comments=comments)
        session.add(dbdomain)
        if allow_manage is not None:
            dbdomain.allow_manage = allow_manage
        session.flush()

        domainsdir = self.config.get("broker", "domainsdir")
        clonedir = os.path.join(domainsdir, dbdomain.name)
        if os.path.exists(clonedir):
            raise InternalError("Domain directory already exists")

        kingdir = self.config.get("broker", "kingdir")
        cmd = ["branch"]
        if track:
            cmd.append("--track")
        else:
            cmd.append("--no-track")
        cmd.append(dbdomain.name)
        cmd.append(start_point.name)
        run_git(cmd, path=kingdir, logger=logger)

        # If the branch command above fails the DB will roll back as normal.
        # If the command below fails we need to clean up from itself and above.
        try:
            run_git(["clone", "--branch", dbdomain.name,
                     kingdir, dbdomain.name],
                    path=domainsdir, logger=logger)
        except ProcessException, e:
            try:
                remove_dir(clonedir, logger=logger)
                run_git(["branch", "-D", dbdomain.name],
                        path=kingdir, logger=logger)
            except ProcessException, e2:
                logger.info("Exception while cleaning up: %s", e2)