Commit 5849a294 authored by Sébastien Luttringer's avatar Sébastien Luttringer
Browse files

Rework motd behaviour and GUI

parent 6e9823f1
Loading
Loading
Loading
Loading
+24 −25
Original line number Diff line number Diff line
@@ -327,11 +327,10 @@ def c_install(args):
    # select image to install
    repoman = load_repositories(args)
    image, repo = next(get_images([args.pattern], repoman, min=1, max=1))
    if repo:
        # Print repo MOTD
        arrow("Repository %s MOTD" % repo.config.name)
        repo.motd()
    # Print setup information
    if repo is not None:
        arrow("Repository message")
        out(repo.motd, endl="")
    # print setup information
    arrow(u"Installing %s v%s" % (image.name, image.version))
    # let's go
    dt = image.run(args.parser, subparser, run_setup=not args.dry_run)
@@ -354,25 +353,23 @@ def c_list(args):

def c_motd(args):
    '''
    Display (and edit) repository's MOTD
    Show and set repository's message
    '''
    repoman = load_repositories(args)
    repo = repoman[args.repository]
    arrow("Current MOTD for %s:" % repo.config.name)
    repo.motd()
    if args.edit:
        # check local repository
        if not repo.local:
            raise ISError(u"Repository must be local")
        arrow("New MOTD (end with a blank line):")
        motd = ""
        # raw_input return bytestring
        data = raw_input().decode(sys.stdin.encoding)
        motd += data
        while data:
            data = raw_input().decode(sys.stdin.encoding)
            motd += u"\n%s" % data
        repo.motd(motd[:-1])
    if args.file:
        args.set = open(args.file, "rb").read()
    elif args.remove:
        args.set = ""
    arrowlevel(1)
    for reponame in repoman.select_repositories(args.repository):
        arrow(reponame, -1)
        try:
            if args.set is not None:
                repoman[reponame].setmotd(args.set)
            else:
                out(repoman[reponame].motd)
        except IndexError as e:
            raise ISError(e)

def c_move(args):
    '''
@@ -675,9 +672,11 @@ def arg_parser_init():
    p.set_defaults(func=c_list)
    # motd command parser
    p = subparser.add_parser("motd", help=c_motd.__doc__.lower())
    p.add_argument("repository", help="repository to display/edit MOTD")
    p.add_argument("--edit", action="store_true",
                   help="edit repository's MOTD")
    g = p.add_mutually_exclusive_group()
    g.add_argument("-r", "--remove", action="store_true", help="remove the motd")
    g.add_argument("-s", "--set", help="set the motd from command line")
    g.add_argument("-f", "--file", help="set the motd from a file")
    p.add_argument("repository", nargs="*", default=[u"*"], help="image repository")
    p.set_defaults(func=c_motd)
    # move command parser
    p = subparser.add_parser("move", help=c_move.__doc__.lower())
+13 −4
Original line number Diff line number Diff line
@@ -267,11 +267,20 @@ list [-h] [-A] [-d] [-D] [-f] [-j] [-i] [-l] [-m] [-s] [-u] [<remote_image>...]
        display image url


motd [-h] [--edit] *repository*
    Display MOTD of a repository
motd [-h] [-f] [-r] [-s] *repository*
    Show or set the message of the day (MOTD) of repository.
    This message is only displayed during installation of an image from this repository.

    --edit
        edit the MOTD of the repository
    -f, --file *FILE*
        set the repository MOTD from file *FILE*.

    -s, --set *MESSAGE*
        set the repository MOTD from command line argument *MESSAGE*.

    -r, --remove
        remove the repository MOTD.

    MOTD are supported by repository version >= 2.0.


move [-h] [-f] <local_image>... *repository*
+27 −21
Original line number Diff line number Diff line
@@ -586,24 +586,23 @@ class Repository(object):
            finally:
                # Remove dummy repository
                shutil.rmtree(tmpdir)
    @property
    def motd(self):
        '''
        Return repository message of the day
        '''
        motd = self.db.ask("SELECT motd FROM repository").fetchone()[0]
        return None if len(motd) == 0 else motd

    def motd(self, new_motd=None):
    def setmotd(self, value=""):
        '''
        Display and edit repository motd
        Set repository message of the day
        '''
        if new_motd is None:
            try:
                motd = self.db.ask("SELECT motd FROM repository").fetchone()
            except:
                motd = None
            if motd is None:
                raise ISError("Unable to retrieve %s's MOTD" % self.config.name)
            out(motd[0])
        else:
        # check local repository
        if not self.local:
            raise ISError(u"Repository must be local")
            self.db.ask("UPDATE repository SET motd = ?", (new_motd,))
        arrow("Updating motd")
        self.db.ask("UPDATE repository SET motd = ?", (value,))
        self.update_last()


@@ -671,14 +670,21 @@ class Repository_v1(Repository):
            images.append(d)
        return images

    def motd(self, new_motd=None):
    @property
    def motd(self):
        '''
        Display and edit repository motd
        Return repository message of the day.
        Repository v1 don't have message of day
        '''
        out('')
        if new_motd:
            warn("[%s] repository v1, unable to edit MOTD." % self.config.name)
        return None

    def setmotd(self, value=""):
        '''
        Don't set repository message of the day. Not supported by v1.
        '''
        # check local repository
        warn(u"Repository v1 doesn't support motd. Unable to set")
        return

class RepositoryManager(object):
    '''
+1 −1
Original line number Diff line number Diff line
@@ -157,7 +157,7 @@ _is() {
         _remote_image
      ;;
      motd)
         [[ "$cur" == -* ]] && _opt '-h --help --edit' && return 0
         [[ "$cur" == -* ]] && _opt '-h --help -r --remove -f --files -s --set' && return 0
         _repo
      ;;
      move)
+4 −2
Original line number Diff line number Diff line
@@ -264,8 +264,10 @@ _is() {
                        ;;
                     (motd)
                        args+=(
                        '1:repository:_installsystems_repo'
                        "--edit[edit repository's MOTD]"
                        '(-f --files)'{-f,--files}'[set the motd from a file]: motd:_files'
                        '(-s --set)'{-s,--set}'[set the motd from command line]'
                        '(-r --remove)'{-r,--remove}'[remove the motd]'
                        '*:repository:_installsystems_local_repo'
                        )
                        ;;
                    (move)