Remove the marathon command line
This commit is contained in:
@@ -1,218 +0,0 @@
|
||||
"""
|
||||
Usage:
|
||||
dcos marathon describe [--json] <app_id>
|
||||
dcos marathon info
|
||||
dcos marathon list
|
||||
dcos marathon remove [--force] <app_id>
|
||||
dcos marathon scale [--force] <app_id> <instances>
|
||||
dcos marathon start <app_resource>
|
||||
dcos marathon suspend [--force] <app_id>
|
||||
|
||||
Options:
|
||||
-h, --help Show this screen
|
||||
--version Show version
|
||||
--force This flag disable checks in Marathon during update
|
||||
operations.
|
||||
--json Outputs JSON format instead of default (TOML) format
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
|
||||
import docopt
|
||||
import toml
|
||||
from dcos.api import config, constants, marathon, options, util
|
||||
|
||||
|
||||
def main():
|
||||
error = util.configure_logger_from_environ()
|
||||
if error is not None:
|
||||
print(error.error())
|
||||
return 1
|
||||
|
||||
config_path = os.environ[constants.DCOS_CONFIG_ENV]
|
||||
args = docopt.docopt(
|
||||
__doc__,
|
||||
version='dcos-marathon version {}'.format(constants.version))
|
||||
|
||||
if args['marathon'] and args['info']:
|
||||
return _info()
|
||||
elif args['marathon'] and args['list']:
|
||||
toml_config = config.load_from_path(config_path)
|
||||
return _list(toml_config)
|
||||
elif args['marathon'] and args['describe']:
|
||||
toml_config = config.load_from_path(config_path)
|
||||
return _describe(args['<app_id>'], args['--json'], toml_config)
|
||||
elif args['marathon'] and args['start']:
|
||||
toml_config = config.load_from_path(config_path)
|
||||
return _start(args['<app_resource>'], toml_config)
|
||||
elif args['marathon'] and args['scale']:
|
||||
toml_config = config.load_from_path(config_path)
|
||||
return _scale(args['<app_id>'],
|
||||
args['<instances>'],
|
||||
args['--force'],
|
||||
toml_config)
|
||||
elif args['marathon'] and args['suspend']:
|
||||
toml_config = config.load_from_path(config_path)
|
||||
return _suspend(args['<app_id>'], args['--force'], toml_config)
|
||||
elif args['marathon'] and args['remove']:
|
||||
toml_config = config.load_from_path(config_path)
|
||||
return _remove(args['<app_id>'], args['--force'], toml_config)
|
||||
else:
|
||||
print(options.make_generic_usage_error(__doc__))
|
||||
return 1
|
||||
|
||||
|
||||
def _info():
|
||||
"""Print marathon cli information.
|
||||
|
||||
:returns: Process status
|
||||
:rtype: int
|
||||
"""
|
||||
|
||||
print('Deploy and manage applications on Apache Mesos')
|
||||
return 0
|
||||
|
||||
|
||||
def _list(config):
|
||||
"""Lists known Marathon applications.
|
||||
|
||||
:param config: Configuration dictionary
|
||||
:type config: config.Toml
|
||||
:returns: Process status
|
||||
:rtype: int
|
||||
"""
|
||||
client = marathon.create_client(config)
|
||||
|
||||
apps, err = client.get_apps()
|
||||
if err is not None:
|
||||
print(err.error())
|
||||
return 1
|
||||
|
||||
if not apps:
|
||||
print("No applications to list.")
|
||||
|
||||
for app in apps:
|
||||
print(app['id'])
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def _describe(app_id, is_json, config):
|
||||
"""Show details of a Marathon application.
|
||||
|
||||
:param app_id: ID of the app to suspend
|
||||
:type app_id: str
|
||||
:param is_json: Whether to print in JSON format or TOML
|
||||
:type is_json: bool
|
||||
:param config: Configuration dictionary
|
||||
:type config: config.Toml
|
||||
:returns: Process status
|
||||
:rtype: int
|
||||
"""
|
||||
client = marathon.create_client(config)
|
||||
|
||||
app, err = client.get_app(app_id)
|
||||
if err is not None:
|
||||
print(err.error())
|
||||
return 1
|
||||
|
||||
if is_json:
|
||||
print(json.dumps(app, sort_keys=True, indent=2))
|
||||
else:
|
||||
print(toml.dumps(app))
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def _start(app_resource_path, config):
|
||||
"""Starts an application with Marathon
|
||||
|
||||
:param app_resource_path: Path to the application resource
|
||||
:type app_resource_path: str
|
||||
:param config: Configuration dictionary
|
||||
:type config: config.Toml
|
||||
:returns: Process status
|
||||
:rtype: int
|
||||
"""
|
||||
client = marathon.create_client(config)
|
||||
|
||||
with open(app_resource_path) as app_resource_file:
|
||||
_, err = client.add_app(app_resource_file)
|
||||
if err is not None:
|
||||
print(err.error())
|
||||
return 1
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def _scale(app_id, instances, force, config):
|
||||
"""Suspends a running Marathon application.
|
||||
|
||||
:param app_id: ID of the app to suspend
|
||||
:type app_id: str
|
||||
:param instances: The requested number of instances.
|
||||
:type instances: int
|
||||
:param force: Whether to override running deployments.
|
||||
:type force: bool
|
||||
:param config: Configuration dictionary
|
||||
:type config: config.Toml
|
||||
:returns: Process status
|
||||
:rtype: int
|
||||
"""
|
||||
client = marathon.create_client(config)
|
||||
|
||||
deployment, err = client.scale_app(app_id, instances, force)
|
||||
if err is not None:
|
||||
print(err.error())
|
||||
return 1
|
||||
|
||||
print('Created deployment {}'.format(deployment))
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def _suspend(app_id, force, config):
|
||||
"""Suspends a running Marathon application.
|
||||
|
||||
:param app_id: ID of the app to suspend
|
||||
:type app_id: str
|
||||
:param force: Whether to override running deployments.
|
||||
:type force: bool
|
||||
:param config: Configuration dictionary
|
||||
:type config: config.Toml
|
||||
:returns: Process status
|
||||
:rtype: int
|
||||
"""
|
||||
client = marathon.create_client(config)
|
||||
|
||||
deployment, err = client.stop_app(app_id, force)
|
||||
if err is not None:
|
||||
print(err.error())
|
||||
return 1
|
||||
|
||||
print('Created deployment {}'.format(deployment))
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def _remove(app_id, force, config):
|
||||
"""Remove a Marathon application.
|
||||
|
||||
:param app_id: ID of the app to remove
|
||||
:type app_id: str
|
||||
:param force: Whether to override running deployments.
|
||||
:type force: bool
|
||||
:param config: Configuration dictionary
|
||||
:type config: config.Toml
|
||||
:returns: Process status
|
||||
:rtype: int
|
||||
"""
|
||||
client = marathon.create_client(config)
|
||||
|
||||
err = client.remove_app(app_id, force)
|
||||
if err is not None:
|
||||
print(err.error())
|
||||
return 1
|
||||
|
||||
return 0
|
||||
@@ -49,7 +49,6 @@ Available DCOS commands in '{}':
|
||||
\tapp \tDeploy and manage applications on the DCOS
|
||||
\tconfig \tGet and set DCOS command line options
|
||||
\thelp \tDisplay usage information
|
||||
\tmarathon \tDeploy and manage applications on Apache Mesos
|
||||
\tpackage \tInstall and manage DCOS software packages
|
||||
|
||||
Get detailed command description with 'dcos <command> --help'.
|
||||
|
||||
@@ -1,168 +0,0 @@
|
||||
import json
|
||||
from common import exec_command
|
||||
|
||||
|
||||
def test_help():
|
||||
returncode, stdout, stderr = exec_command(['dcos', 'marathon', '--help'])
|
||||
|
||||
assert returncode == 0
|
||||
assert stdout == b"""Usage:
|
||||
dcos marathon describe [--json] <app_id>
|
||||
dcos marathon info
|
||||
dcos marathon list
|
||||
dcos marathon remove [--force] <app_id>
|
||||
dcos marathon scale [--force] <app_id> <instances>
|
||||
dcos marathon start <app_resource>
|
||||
dcos marathon suspend [--force] <app_id>
|
||||
|
||||
Options:
|
||||
-h, --help Show this screen
|
||||
--version Show version
|
||||
--force This flag disable checks in Marathon during update
|
||||
operations.
|
||||
--json Outputs JSON format instead of default (TOML) format
|
||||
"""
|
||||
|
||||
|
||||
def test_version():
|
||||
returncode, stdout, stderr = exec_command(
|
||||
['dcos', 'marathon', '--version'])
|
||||
|
||||
assert returncode == 0
|
||||
assert stdout == b'dcos-marathon version 0.1.0\n'
|
||||
assert stderr == b''
|
||||
|
||||
|
||||
def test_info():
|
||||
returncode, stdout, stderr = exec_command(['dcos', 'marathon', 'info'])
|
||||
|
||||
assert returncode == 0
|
||||
assert stdout == b'Deploy and manage applications on Apache Mesos\n'
|
||||
assert stderr == b''
|
||||
|
||||
|
||||
def test_empty_list():
|
||||
_list_apps()
|
||||
|
||||
|
||||
def test_start_app():
|
||||
_start_app('tests/data/marathon/sleep.json')
|
||||
_list_apps('test-app')
|
||||
_remove_app('test-app')
|
||||
|
||||
|
||||
def test_remove_app():
|
||||
_start_app('tests/data/marathon/sleep.json')
|
||||
_remove_app('test-app')
|
||||
_list_apps()
|
||||
|
||||
|
||||
# TODO: Let's improve this once we have a fixed version of toml
|
||||
def test_describe_app():
|
||||
_start_app('tests/data/marathon/sleep.json')
|
||||
|
||||
returncode, stdout, stderr = exec_command(
|
||||
['dcos', 'marathon', 'describe', 'test-app'])
|
||||
|
||||
assert returncode == 0
|
||||
assert stdout != b''
|
||||
assert stderr == b''
|
||||
|
||||
_remove_app('test-app')
|
||||
|
||||
|
||||
def test_describe_app_in_json():
|
||||
_start_app('tests/data/marathon/sleep.json')
|
||||
|
||||
returncode, stdout, stderr = exec_command(
|
||||
['dcos', 'marathon', 'describe', '--json', 'test-app'])
|
||||
|
||||
result = json.loads(stdout.decode('utf-8'))
|
||||
|
||||
assert returncode == 0
|
||||
assert isinstance(result, dict)
|
||||
assert result['id'] == '/test-app'
|
||||
assert stderr == b''
|
||||
|
||||
_remove_app('test-app')
|
||||
|
||||
|
||||
def test_scale_app():
|
||||
_start_app('tests/data/marathon/zero_instance_sleep.json')
|
||||
|
||||
returncode, stdout, stderr = exec_command(
|
||||
['dcos', 'marathon', 'scale', 'zero-instance-app', '2'])
|
||||
|
||||
assert returncode == 0
|
||||
assert stdout.decode('utf-8').startswith('Created deployment ')
|
||||
assert stderr == b''
|
||||
|
||||
_remove_app('zero-instance-app')
|
||||
|
||||
|
||||
def test_force_scale_appp():
|
||||
_start_app('tests/data/marathon/sleep.json')
|
||||
|
||||
returncode, stdout, stderr = exec_command(
|
||||
['dcos', 'marathon', 'scale', '--force', 'test-app', '2'])
|
||||
|
||||
assert returncode == 0
|
||||
assert stdout.decode('utf-8').startswith('Created deployment ')
|
||||
assert stderr == b''
|
||||
|
||||
_remove_app('test-app')
|
||||
|
||||
|
||||
def test_suspend_app():
|
||||
_start_app('tests/data/marathon/zero_instance_sleep.json')
|
||||
|
||||
returncode, stdout, stderr = exec_command(
|
||||
['dcos', 'marathon', 'suspend', 'zero-instance-app'])
|
||||
|
||||
assert returncode == 0
|
||||
assert stdout.decode('utf-8').startswith('Created deployment ')
|
||||
assert stderr == b''
|
||||
|
||||
_remove_app('zero-instance-app')
|
||||
|
||||
|
||||
def test_remove_missing_app():
|
||||
returncode, stdout, stderr = exec_command(
|
||||
['dcos', 'marathon', 'remove', 'missing-id'])
|
||||
|
||||
assert returncode == 1
|
||||
assert stdout == b"Error: App '/missing-id' does not exist\n"
|
||||
assert stderr == b''
|
||||
|
||||
|
||||
def _start_app(file_path):
|
||||
returncode, stdout, stderr = exec_command(
|
||||
['dcos', 'marathon', 'start', file_path])
|
||||
|
||||
assert returncode == 0
|
||||
assert stdout == b''
|
||||
assert stderr == b''
|
||||
|
||||
|
||||
def _list_apps(app_id=None):
|
||||
returncode, stdout, stderr = exec_command(['dcos', 'marathon', 'list'])
|
||||
|
||||
if app_id is None:
|
||||
result = b'No applications to list.\n'
|
||||
elif isinstance(app_id, str):
|
||||
result = '/{}\n'.format(app_id).encode('utf-8')
|
||||
else:
|
||||
assert False
|
||||
|
||||
assert returncode == 0
|
||||
assert stdout == result
|
||||
assert stderr == b''
|
||||
|
||||
|
||||
def _remove_app(app_id):
|
||||
returncode, stdout, stderr = exec_command(
|
||||
['dcos', 'marathon', 'remove', app_id])
|
||||
|
||||
assert returncode == 0
|
||||
assert stdout == b''
|
||||
assert stderr == b''
|
||||
@@ -141,7 +141,7 @@ def test_search():
|
||||
|
||||
def test_cleanup():
|
||||
returncode, stdout, stderr = exec_command(
|
||||
['dcos', 'marathon', 'remove', 'mesos-dns'])
|
||||
['dcos', 'app', 'remove', 'mesos-dns'])
|
||||
|
||||
assert returncode == 0
|
||||
assert stdout == b''
|
||||
|
||||
Reference in New Issue
Block a user