package: add dcos package repo import (#928)

It accepts the same JSON file format that is generated by `dcos package
list --json`, so that means that you can use `dcos package list --json`
to make a backup of your repository configuration and `dcos package repo
import` to restore from that backup.
This commit is contained in:
Marc Abramowitz
2017-03-15 09:48:13 -07:00
committed by tamarrow
parent b7a2c2bbf0
commit 4d6c4f622a
2 changed files with 59 additions and 0 deletions

View File

@@ -16,6 +16,7 @@ Usage:
[--yes]
dcos package list [<package-name> --json --app-id=<app-id> --cli]
dcos package repo add <repo-name> <repo-url> [--index=<index>]
dcos package repo import <repos-file>
dcos package repo list [--json]
dcos package repo remove <repo-name>
dcos package search [<query> --json]
@@ -91,3 +92,9 @@ Positional Arguments:
<repo-url>
URL of the package repository. For example,
https://universe.mesosphere.com/repo.
<repo-file>
A file containing package repositories, listed in the format
of `dcos package list --json`.
Example:
{"repositories": [{"name": "Universe", "uri": "uri-here"}]}

View File

@@ -58,6 +58,11 @@ def _cmds():
arg_keys=['<repo-name>', '<repo-url>', '--index'],
function=_add_repo),
cmds.Command(
hierarchy=['package', 'repo', 'import'],
arg_keys=['<repos-file>'],
function=_import_repos),
cmds.Command(
hierarchy=['package', 'repo', 'remove'],
arg_keys=['<repo-name>'],
@@ -180,6 +185,53 @@ def _add_repo(repo_name, repo_url, index):
return 0
def _import_repos(repos_file):
"""Add package repos from a JSON file
:param repos_file: path to JSON file containing repos.
:type repos_file: str
:rtype: int
"""
package_manager = get_package_manager()
with open(repos_file) as f:
try:
data = json.load(f)
except json.JSONDecodeError:
_raise_invalid_repos_file()
repositories = data.get('repositories')
if not repositories:
_raise_invalid_repos_file()
for index, repo in enumerate(data['repositories']):
repo_name = repo.get('name')
repo_uri = repo.get('uri')
if not repo_name or not repo_uri:
emitter.publish('Repo missing name or uri. Skipping.')
continue
try:
package_manager.add_repo(repo['name'], repo['uri'], index)
emitter.publish('Added repo "%s" (%s) at index %d'
% (repo_name, repo_uri, index))
except DCOSException as e:
emitter.publish('Error (%s) while adding repo "%s" (%s). '
'Skipping.'
% (e, repo_name, repo_uri))
continue
return 0
def _raise_invalid_repos_file():
raise DCOSException(
'No repositories found to import. '
'You should provide a file containing package '
'repositories, listed in the format of `dcos package list --json`.'
'\nExample: '
'{"repositories": [{"name": "Universe", "uri": "uri-here"}]}')
def _remove_repo(repo_name):
"""Remove package repo and update repo with new repo