From 06036fd6dba3ba4b9a13052ea95a08ebcc97501e Mon Sep 17 00:00:00 2001 From: Steve Baker Date: Fri, 31 Mar 2017 13:32:34 +1300 Subject: [PATCH] Implement apply, cleanup commands --- paunch/__init__.py | 40 +++++++++++++++++++--- paunch/cmd.py | 84 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 117 insertions(+), 7 deletions(-) diff --git a/paunch/__init__.py b/paunch/__init__.py index e008d6f..0e87569 100644 --- a/paunch/__init__.py +++ b/paunch/__init__.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at @@ -12,8 +10,42 @@ # License for the specific language governing permissions and limitations # under the License. +'''Stable library interface to managing containers with paunch.''' + +import logging + import pbr.version +from paunch.builder import compose1 +from paunch import runner -__version__ = pbr.version.VersionInfo( - 'paunch').version_string() +__version__ = pbr.version.VersionInfo('paunch').version_string() + +LOG = logging.getLogger(__name__) + + +def apply(config_id, config, managed_by, labels=None): + builder = compose1.ComposeV1Builder( + config_id=config_id, + config=config, + managed_by=managed_by, + labels=labels + ) + return builder.apply() + + +def cleanup(config_ids, managed_by): + runner.delete_missing_configs(config_ids) + runner.rename_containers() + + +def list(): + raise NotImplementedError() + + +def show(config_id): + raise NotImplementedError() + + +def delete(config_id): + raise NotImplementedError() diff --git a/paunch/cmd.py b/paunch/cmd.py index 0a262cb..8b57722 100644 --- a/paunch/cmd.py +++ b/paunch/cmd.py @@ -11,25 +11,95 @@ # under the License. # +import collections import logging from cliff.command import Command +import yaml + +import paunch class Apply(Command): log = logging.getLogger(__name__) + def get_parser(self, prog_name): + parser = super(Apply, self).get_parser(prog_name) + parser.add_argument( + '--file', + metavar='', + required=True, + help=('YAML or JSON file containing configuration data'), + ) + parser.add_argument( + '--label', + metavar='', + dest='labels', + default=[], + help=('Extra labels to apply to containers in this config, in the ' + 'form label=value.'), + ) + parser.add_argument( + '--managed-by', + metavar='', + dest='managed_by', + default='paunch', + help=('Override the name of the tool managing the containers'), + ) + parser.add_argument( + 'config_id', + metavar='', + help=('Identifier for the config, unique to the service managing ' + 'the containers'), + ) + return parser + def take_action(self, parsed_args): - pass + + labels = collections.OrderedDict() + for l in parsed_args.labels: + k, v = l.split(('='), 1) + labels[k] = v + + with open(parsed_args.file, 'r') as f: + config = yaml.safe_load(f) + + paunch.apply( + parsed_args.config_id, + config, + managed_by='paunch', + labels=labels + ) class Cleanup(Command): log = logging.getLogger(__name__) + def get_parser(self, prog_name): + parser = super(Cleanup, self).get_parser(prog_name) + parser.add_argument( + 'config_id', + metavar='', + dest='config_ids', + help=('Identifiers for the configs which still apply, all others ' + 'will be deleted.'), + ) + parser.add_argument( + '--managed-by', + metavar='', + dest='managed_by', + default='paunch', + help=('Override the name of the tool managing the containers'), + ) + return parser + def take_action(self, parsed_args): - pass + paunch.cleanup( + parsed_args.config_ids, + managed_by=parsed_args.managed_by + ) class Delete(Command): @@ -45,4 +115,12 @@ class List(Command): log = logging.getLogger(__name__) def take_action(self, parsed_args): - pass + paunch.list() + + +class Show(Command): + + log = logging.getLogger(__name__) + + def take_action(self, parsed_args): + paunch.list()