예제 #1
0
    def add(self, name=None, source=None, group=None):
        """
        key add [NAME] [--source=FILENAME]
        key add [NAME] [--source=git]
        key add [NAME] [--source=ssh]
        """
        keys = None
        if source == "git":
            config = Config()
            username = config["cloudmesh.profile.github"]
            keys = SSHkey().get_from_git(username)
            for key in keys:
                key['group'] = group or ["git"]

        elif source == "ssh":
            key = SSHkey(name=name)
            key['group'] = group or ["local", "ssh"]
            keys = [key]

        else:
            # source is filename

            key = SSHkey()

            if not group:
                group = os.path.basename(source)
                if "." in group:
                    group = [group.rsplit(".", 1)[0]]

            filename = path_expand(source)

            print(group)
            print(filename)

            lines = readfile(filename).splitlines()

            pprint(lines)

            keys = []
            for line in lines:

                key = SSHkey()

                key.add(key=line, group=group, filename=source)
                key["cm"]["name"] = key["name"] = line.split(' ', 2)[2]
                keys.append(key)

            pprint(keys)

            # key = SSHkey(name=name, path=path_expand(source))
            # key['group'] = group or ["local", "ssh"]
            # keys = [key]

            for key in keys:
                print(key['name'], key['fingerprint'])
        return keys
예제 #2
0
    def add(self, name=None, source=None, group=None):
        """
        key add [NAME] [--source=FILENAME]
        key add [NAME] [--source=git]
        key add [NAME] [--source=ssh]
        """

        if source == "git":
            config = Config()
            username = config["cloudmesh.profile.github"]
            keys = SSHkey().get_from_git(username)
            for key in keys:
                key['group'] = group or ["git"]

        elif source == "ssh":
            key = SSHkey(name=name)
            key['group'] = list(set([group] + ["local", "ssh"]))
            keys = [key]

        else:
            # source is filename
            def get_group_name(x):
                if x and "." in x:
                    x = [x.rsplit(".", 1)[0]]
                return x

            if not group:
                group = ["local"]
                file_group = os.path.basename(source)
                file_group = get_group_name(file_group)
                group = group + file_group

            filename = path_expand(source)

            lines = readfile(filename).splitlines()

            keys = []

            group = [get_group_name(x) for x in group]

            for line in lines:
                key = SSHkey()
                key_group = group + [line.split("-", 1)[0]]

                key.add(key=line, group=key_group, filename=source)
                key["cm"]["name"] = key["name"] = line.split(' ', 2)[2]
                keys.append(key)

        return keys