Skip to content

Commit

Permalink
Add support for listing and removing remotes
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Graber <stgraber@ubuntu.com>
  • Loading branch information
stgraber committed Jul 16, 2014
1 parent 60a1bbb commit 61f4f2c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/lxccmd/lxccmd/commands/remote/__init__.py
Expand Up @@ -22,7 +22,8 @@
# Import everything we need
import gettext

from lxccmd.config import config_has_section, config_list_sections
from lxccmd.config import config_has_section, config_list_sections, \
config_remove_section, config_get, config_set
from lxccmd.exceptions import LXCError
from lxccmd.network import remote_get_fingerprint, remote_get_role, \
remote_add_trusted
Expand Down Expand Up @@ -105,21 +106,29 @@ def cli_add_remote(args):
if "success" not in res:
raise LXCError(_("Invalid password."))

# remote_add(args.name, args.url, fingerprint)
config_set("remote/%s" % args.name, "url", args.url)
config_set("remote/%s" % args.name, "fingerprint", fingerprint)
config_set("remote/%s" % args.name, "type", "lxc-rest")


def cli_list_remote(args):
for entry in config_list_sections():
if not entry.startswith("remote/"):
continue

print(entry.split("remote/", 1)[-1])
remote_name = entry.split("remote/", 1)[-1]
remote_url = config_get(entry, "url")
remote_fingerprint = config_get(entry, "fingerprint")

print(" - %s (%s, %s)" % (remote_name, remote_url, remote_fingerprint))


def cli_remove_remote(args):
if not config_has_section("remote/%s" % args.name):
raise LXCError(_("Remote '%s' doesn't exist." % args.name))

config_remove_section("remote/%s" % args.name)


# REST functions
def rest_list_remote():
Expand Down
16 changes: 16 additions & 0 deletions src/lxccmd/lxccmd/config.py
Expand Up @@ -81,6 +81,22 @@ def config_list_sections():
return config.sections()


def config_remove_section(section):
"""
Remove a section from the configuration.
"""

config_file = "%s/lxccmd.conf" % get_config_path()

config = ConfigParser()
config.read(config_file)

config.remove_section(section)

with open(config_file, "w+") as fd:
config.write(fd)


def config_set(section, key, value):
"""
Sets a key in the configuration file.
Expand Down

0 comments on commit 61f4f2c

Please sign in to comment.