Example #1
0
    def get_cluster(self, name):
        cluster = self.registry.get(name)
        name = cluster['name']
        env = cluster['env']

        config = yay.load_uri(cluster["cached-sidekick-file"])
        cluster.update(config)

        return Cluster(name, self.get_environment(env), cluster)
Example #2
0
    def get_cluster(self, name):
        cluster = self.registry.get(name)
        name = cluster['name']
        env = cluster['env']

        config = yay.load_uri(cluster["cached-sidekick-file"])
        cluster.update(config)

        return Cluster(
            name, self.get_environment(env), cluster)
Example #3
0
    def createOrGetApplication(self):
        application = service.Application("Yaybu Server")

        boiler = Boiler()
        boiler.setServiceParent(application)

        config = yay.load_uri(self.config.parent["config"])

        for subservice in ServiceType.create_all(config.get("services", [])):
            subservice.setServiceParent(boiler)

        return application
Example #4
0
    def __init__(self, defaults_fn=lambda: {}):
        datadir = os.environ.get("XDG_DATA_HOME", os.path.expanduser("~/.local/share"))
        self.dotdir = os.path.join(datadir, "sidekick")
        self.registry = os.path.join(self.dotdir, "registry")
        self.index_file = os.path.join(self.registry, self.file)

        if not os.path.exists(self.registry):
            os.makedirs(self.registry)

        if os.path.exists(self.index_file):
            self.index = yay.load_uri(self.index_file)
        else:
            self.index = defaults_fn()
            self.save()
Example #5
0
    def register(self, name, env, details):
        if self.contains(name):
            raise SidekickError("'%s' is already defined" % name)

        cached_path = os.path.join(self.registry, "%s.yay" % name)
        cached = yay.dump(yay.load_uri(details))
        open(cached_path, "w").write(cached)

        self.index[name] = {
            "name": name,
            "env": env,
            "sidekick-file": details,
            "cached-sidekick-file": cached_path,
            }

        self.save()
Example #6
0
    def __init__(self, configfile, opts=None):
        self.path = []
        self.ypath = []
        self.options = {}
        self._config = None

        self.resume = opts.resume
        self.no_resume = opts.no_resume
        self.user = opts.user

        self.host = opts.host
        self.connect_user = None
        self.port = None

        if self.host:
            if "@" in self.host:
                self.connect_user, self.host = self.host.split("@", 1)
            if ":" in self.host:
                self.host, self.port = self.host.split(":", 1)

        if os.path.exists("/etc/yaybu"):
            self.options = yay.load_uri("/etc/yaybu")

        if opts is not None:
            logger.debug("Invoked with ypath: %r" % opts.ypath)
            logger.debug("Environment YAYBUPATH: %r" %
                         os.environ.get("YAYBUPATH", ""))
            self.simulate = opts.simulate
            self.ypath = opts.ypath
            self.verbose = opts.verbose

        if "PATH" in os.environ:
            for term in os.environ["PATH"].split(":"):
                self.path.append(term)

        if "YAYBUPATH" in os.environ:
            for term in os.environ["YAYBUPATH"].split(":"):
                self.ypath.append(term)

        if not len(self.ypath):
            self.ypath.append(os.getcwd())

        self.configfile = configfile

        self.setup_shell(opts.env_passthrough)
        self.setup_changelog()
Example #7
0
    def __init__(self, configfile, opts=None):
        self.path = []
        self.ypath = []
        self.options = {}
        self._config = None

        self.resume = opts.resume
        self.no_resume = opts.no_resume
        self.user = opts.user

        self.host = opts.host
        self.connect_user = None
        self.port = None

        if self.host:
            if "@" in self.host:
                self.connect_user, self.host = self.host.split("@", 1)
            if ":" in self.host:
                self.host, self.port = self.host.split(":", 1)

        if os.path.exists("/etc/yaybu"):
            self.options = yay.load_uri("/etc/yaybu")

        if opts is not None:
            logger.debug("Invoked with ypath: %r" % opts.ypath)
            logger.debug("Environment YAYBUPATH: %r" % os.environ.get("YAYBUPATH", ""))
            self.simulate = opts.simulate
            self.ypath = opts.ypath
            self.verbose = opts.verbose

        if "PATH" in os.environ:
            for term in os.environ["PATH"].split(":"):
                self.path.append(term)

        if "YAYBUPATH" in os.environ:
            for term in os.environ["YAYBUPATH"].split(":"):
                self.ypath.append(term)

        if not len(self.ypath):
            self.ypath.append(os.getcwd())

        self.configfile = configfile

        self.setup_shell(opts.env_passthrough)
        self.setup_changelog()
Example #8
0
File: part.py Project: yaybu/yaybu
    def apply(self):
        if self.root.readonly:
            return

        hostname = self.params.server.fqdn.as_string()

        logger.info("Updating node %r" % hostname)

        self.host = hostname
        self.user = self.params.server.user.as_string(
            default=getpass.getuser())
        self.port = self.params.server.port.as_int(default=22)
        self.password = self.params.server.password.as_string(default="")
        self.private_key = self.params.server.private_key.as_string(default="")

        root = self.root
        self.ypath = root.ypath
        self.resume = root.resume
        self.no_resume = root.no_resume
        self.simulate = root.simulate
        self.verbose = root.verbose

        self.options = {}
        if os.path.exists("/etc/yaybu"):
            self.options = yay.load_uri("/etc/yaybu")

        if "://" in hostname:
            parsed = urlparse.urlparse(hostname)
            Transport = self.transports[parsed.scheme]
        else:
            Transport = self.transports["ssh"]

        self.transport = Transport(
            context=self,
            verbose=root.verbose,
            simulate=root.simulate,
        )

        with root.ui.throbber("Connect to '%s'" % hostname):
            self.transport.connect()

        if not self.simulate and not self.transport.exists(self.get_data_path()):
            self.transport.makedirs(self.get_data_path())

        # This makes me a little sad inside, but the whole
        # context thing needs a little thought before jumping in
        self.state = event.EventState()
        self.state.save_file = self.get_data_path("events.saved")
        self.state.simulate = self.simulate
        self.state.transport = self.transport

        if not self.simulate:
            save_parent = os.path.realpath(
                os.path.join(self.state.save_file, os.path.pardir))
            if not self.transport.exists(save_parent):
                self.transport.makedirs(save_parent)

        if self.transport.exists(self.state.save_file):
            if self.resume:
                self.state.loaded = False
            elif self.no_resume:
                if not self.simulate:
                    self.transport.unlink(self.state.save_file)
                self.state.loaded = True
            else:
                raise error.SavedEventsAndNoInstruction(
                    "There is a saved events file - you need to specify --resume or --no-resume")

        # Actually apply the configuration
        bundle = resource.ResourceBundle.create_from_yay_expression(
            self.params.resources, verbose_errors=self.verbose > 2)
        bundle.bind()

        with self.root.ui.throbber("Provision %s" % self.host) as throbber:
            changed = bundle.apply(self, throbber)
        self.root.changed(changed)

        if not self.simulate and self.transport.exists(self.state.save_file):
            self.transport.unlink(self.state.save_file)
Example #9
0
def main():
    parser = optparse.OptionParser(version=version())
    parser.add_option("-s", "--simulate", default=False, action="store_true")
    parser.add_option("-p", "--ypath", default=[], action="append")
    parser.add_option("", "--log-facility", default="2", help="the syslog local facility number to which to write the audit trail")
    parser.add_option("", "--log-level", default="info", help="the minimum log level to write to the audit trail")
    parser.add_option("-d", "--debug", default=False, action="store_true", help="switch all logging to maximum, and write out to the console")
    parser.add_option("-l", "--logfile", default=None, help="The filename to write the audit log to, instead of syslog. Note: the standard console log will still be written to the console.")
    parser.add_option("-v", "--verbose", default=2, action="count", help="Write additional informational messages to the console log. repeat for even more verbosity.")
    parser.add_option("--host", default=None, action="store", help="A host to remotely run yaybu on")
    parser.add_option("-u", "--user", default="root", action="store", help="User to attempt to run as")
    parser.add_option("--remote", default=False, action="store_true", help="Run yaybu.protocol client on stdio")
    parser.add_option("--ssh-auth-sock", default=None, action="store", help="Path to SSH Agent socket")
    parser.add_option("--expand-only", default=False, action="store_true", help="Set to parse config, expand it and exit")
    parser.add_option("--resume", default=False, action="store_true", help="Resume from saved events if terminated abnormally")
    parser.add_option("--no-resume", default=False, action="store_true", help="Clobber saved event files if present and do not resume")
    parser.add_option("--env-passthrough", default=[], action="append", help="Preserve an environment variable in any processes Yaybu spawns")
    opts, args = parser.parse_args()

    if len(args) != 1:
        parser.print_help()
        return 1

    if opts.debug:
        opts.logfile = "-"
        opts.verbose = 2

    if opts.expand_only:
        ctx = runcontext.RunContext(args[0], opts)
        cfg = ctx.get_config()

        if opts.verbose <= 2:
            cfg = dict(resources=cfg.get("resources", []))

        print yay.dump(cfg)
        return 0

    if opts.ssh_auth_sock:
        os.environ["SSH_AUTH_SOCK"] = opts.ssh_auth_sock

    atexit.register(logging.shutdown)

    # Probably not the best place to put this stuff...
    if os.path.exists("/etc/yaybu"):
        config = yay.load_uri("/etc/yaybu")
        opts.env_passthrough = config.get("env-passthrough", opts.env_passthrough)

    if opts.host:
        r = remote.RemoteRunner()
        r.load_system_host_keys()
        r.set_missing_host_key_policy("ask")
    else:
        r = runner.Runner()

    if not opts.remote:
        ctx = runcontext.RunContext(args[0], opts)
    else:
         ctx = runcontext.RemoteRunContext(args[0], opts)

    rv = r.run(ctx)
    return rv
Example #10
0
def main():
    parser = optparse.OptionParser(version=version())
    parser.add_option("-s", "--simulate", default=False, action="store_true")
    parser.add_option("-p", "--ypath", default=[], action="append")
    parser.add_option(
        "",
        "--log-facility",
        default="2",
        help=
        "the syslog local facility number to which to write the audit trail")
    parser.add_option("",
                      "--log-level",
                      default="info",
                      help="the minimum log level to write to the audit trail")
    parser.add_option(
        "-d",
        "--debug",
        default=False,
        action="store_true",
        help="switch all logging to maximum, and write out to the console")
    parser.add_option(
        "-l",
        "--logfile",
        default=None,
        help=
        "The filename to write the audit log to, instead of syslog. Note: the standard console log will still be written to the console."
    )
    parser.add_option(
        "-v",
        "--verbose",
        default=2,
        action="count",
        help=
        "Write additional informational messages to the console log. repeat for even more verbosity."
    )
    parser.add_option("--host",
                      default=None,
                      action="store",
                      help="A host to remotely run yaybu on")
    parser.add_option("-u",
                      "--user",
                      default="root",
                      action="store",
                      help="User to attempt to run as")
    parser.add_option("--remote",
                      default=False,
                      action="store_true",
                      help="Run yaybu.protocol client on stdio")
    parser.add_option("--ssh-auth-sock",
                      default=None,
                      action="store",
                      help="Path to SSH Agent socket")
    parser.add_option("--expand-only",
                      default=False,
                      action="store_true",
                      help="Set to parse config, expand it and exit")
    parser.add_option("--resume",
                      default=False,
                      action="store_true",
                      help="Resume from saved events if terminated abnormally")
    parser.add_option(
        "--no-resume",
        default=False,
        action="store_true",
        help="Clobber saved event files if present and do not resume")
    parser.add_option(
        "--env-passthrough",
        default=[],
        action="append",
        help="Preserve an environment variable in any processes Yaybu spawns")
    opts, args = parser.parse_args()

    if len(args) != 1:
        parser.print_help()
        return 1

    if opts.debug:
        opts.logfile = "-"
        opts.verbose = 2

    if opts.expand_only:
        ctx = runcontext.RunContext(args[0], opts)
        cfg = ctx.get_config()

        if opts.verbose <= 2:
            cfg = dict(resources=cfg.get("resources", []))

        print yay.dump(cfg)
        return 0

    if opts.ssh_auth_sock:
        os.environ["SSH_AUTH_SOCK"] = opts.ssh_auth_sock

    atexit.register(logging.shutdown)

    # Probably not the best place to put this stuff...
    if os.path.exists("/etc/yaybu"):
        config = yay.load_uri("/etc/yaybu")
        opts.env_passthrough = config.get("env-passthrough",
                                          opts.env_passthrough)

    if opts.host:
        r = remote.RemoteRunner()
        r.load_system_host_keys()
        r.set_missing_host_key_policy("ask")
    else:
        r = runner.Runner()

    if not opts.remote:
        ctx = runcontext.RunContext(args[0], opts)
    else:
        ctx = runcontext.RemoteRunContext(args[0], opts)

    rv = r.run(ctx)
    return rv
Example #11
0
    def apply(self):
        if self.root.readonly:
            return

        hostname = self.params.server.fqdn.as_string()

        logger.info("Updating node %r" % hostname)

        self.host = hostname
        self.user = self.params.server.user.as_string(
            default=getpass.getuser())
        self.port = self.params.server.port.as_int(default=22)
        self.password = self.params.server.password.as_string(default="")
        self.private_key = self.params.server.private_key.as_string(default="")

        root = self.root
        self.ypath = root.ypath
        self.resume = root.resume
        self.no_resume = root.no_resume
        self.simulate = root.simulate
        self.verbose = root.verbose

        self.options = {}
        if os.path.exists("/etc/yaybu"):
            self.options = yay.load_uri("/etc/yaybu")

        self.transport = self.Transport(
            context=self,
            verbose=root.verbose,
            simulate=root.simulate,
        )

        with root.ui.throbber("Connect to '%s'" % hostname):
            self.transport.connect()

        if not self.simulate and not self.transport.exists(
                self.get_data_path()):
            self.transport.makedirs(self.get_data_path())

        # This makes me a little sad inside, but the whole
        # context thing needs a little thought before jumping in
        self.state = event.EventState()
        self.state.save_file = self.get_data_path("events.saved")
        self.state.simulate = self.simulate
        self.state.transport = self.transport

        if not self.simulate:
            save_parent = os.path.realpath(
                os.path.join(self.state.save_file, os.path.pardir))
            if not self.transport.exists(save_parent):
                self.transport.makedirs(save_parent)

        if self.transport.exists(self.state.save_file):
            if self.resume:
                self.state.loaded = False
            elif self.no_resume:
                if not self.simulate:
                    self.transport.unlink(self.state.save_file)
                self.state.loaded = True
            else:
                raise error.SavedEventsAndNoInstruction(
                    "There is a saved events file - you need to specify --resume or --no-resume"
                )

        # Actually apply the configuration
        bundle = resource.ResourceBundle.create_from_yay_expression(
            self.params.resources, verbose_errors=self.verbose > 2)
        bundle.bind()

        with self.root.ui.throbber("Provision %s" % self.host) as throbber:
            changed = bundle.apply(self, throbber)
        self.root.changed(changed)

        if not self.simulate and self.transport.exists(self.state.save_file):
            self.transport.unlink(self.state.save_file)