Remove unused code from freezer-scheduler

The freezer client has been switched to python-freezerclient,
the code related to use freezer-scheduler as a command line client
should be removed.

Change-Id: I999d0f2e72cf135fdf3b9373461648416c9e0902
Closes-Bug: #1645635
Signed-off-by: Shangzhong Zhu <zhu.shangzhong@zte.com.cn>
This commit is contained in:
Shangzhong Zhu 2016-11-29 19:40:30 +08:00
parent 9efe7e4922
commit 8101e2a7b0
4 changed files with 25 additions and 366 deletions

View File

@ -59,21 +59,6 @@ def get_common_opts():
DEFAULT_FREEZER_SCHEDULER_CONF_D)
_COMMON = [
cfg.StrOpt('job',
default=None,
dest='job_id',
short='j',
help='Name or ID of the job'),
cfg.StrOpt('session',
default=None,
dest='session_id',
short='s',
help='Name or ID of the session'),
cfg.StrOpt('file',
default=None,
dest='fname',
help='Local file that contains the resource to be '
'uploaded/downloaded'),
cfg.StrOpt('client-id',
default=None,
dest='client_id',
@ -87,11 +72,6 @@ def get_common_opts():
dest='no_api',
short='n',
help='Prevents the scheduler from using the api service'),
cfg.BoolOpt('active-only',
default=False,
dest='active_only',
short='a',
help='Filter only active jobs/session'),
cfg.StrOpt('conf',
default=scheduler_conf_d,
dest='jobs_dir',
@ -119,15 +99,6 @@ def get_common_opts():
dest='disable_exec',
help='Allow Freezer Scheduler to deny jobs that execute '
'commands for security reasons'),
cfg.BoolOpt('all',
default=False,
dest='all',
help='Used when querying the API, to retrieve any '
'document regardless of the client-id'),
cfg.BoolOpt('long',
default=False,
dest='long',
help='List additional fields in output')
]
return _COMMON

View File

@ -29,7 +29,6 @@ import six
from freezer.scheduler import arguments
from freezer.scheduler import scheduler_job
from freezer.scheduler import shell
from freezer.scheduler import utils
from freezer.utils import utils as freezer_utils
from freezer.utils import winutils
@ -199,33 +198,16 @@ class FreezerScheduler(object):
LOG.warning("reload not supported")
def _get_doers(module):
doers = {}
for attr in (a for a in dir(module) if a.startswith('do_')):
command = attr[3:].replace('_', '-')
callback = getattr(module, attr)
doers[command] = callback
return doers
def main():
doers = _get_doers(shell)
doers.update(_get_doers(utils))
possible_actions = doers.keys() + ['start', 'stop', 'status', 'reload']
possible_actions = ['start', 'stop', 'status', 'reload']
arguments.parse_args(possible_actions)
arguments.setup_logging()
if CONF.action is None:
if CONF.action is None or CONF.action not in possible_actions:
CONF.print_help()
return 65 # os.EX_DATAERR
if CONF.action not in ['start', 'stop', 'status', 'reload']:
sys.stderr.write("Using freezer-scheduler as a command line client is "
"deprecated. Please use the freezer command line tool"
" from python-freezerclient.\n")
apiclient = None
verify = True
if CONF.insecure:
@ -245,14 +227,6 @@ def main():
print("--no-api mode is not available on windows")
return 69 # os.EX_UNAVAILABLE
if CONF.action in doers:
try:
return doers[CONF.action](apiclient, CONF)
except Exception as e:
LOG.error(e)
print('ERROR {0}'.format(e))
return 70 # os.EX_SOFTWARE
freezer_utils.create_dir(CONF.jobs_dir, do_log=False)
freezer_scheduler = FreezerScheduler(apiclient=apiclient,
interval=int(CONF.interval),

View File

@ -1,286 +0,0 @@
#!/usr/bin/env python
"""
Copyright 2015 Hewlett-Packard
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
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import json
import os
import prettytable
import six
from freezer.utils import utils as freezer_utils
import utils
try:
from betterprint import pprint
except Exception:
def pprint(doc):
print(json.dumps(doc, indent=4))
def do_session_remove_job(client, args):
"""
remove the job from the session
"""
if not args.session_id:
raise Exception("Parameter --session required")
if not args.job_id:
raise Exception("Parameter --job required")
client.sessions.remove_job(args.session_id, args.job_id)
def do_session_add_job(client, args):
"""
adds a job to the session
"""
if not args.session_id:
raise Exception("Parameter --session required")
if not args.job_id:
raise Exception("Parameter --job required")
for job_id in args.job_id.split(','):
try:
client.sessions.add_job(args.session_id, job_id)
except Exception as e:
print("Error processin job {0}. {1}".format(job_id, e))
def do_session_list_job(client, args):
"""
prints a list of jobs assigned to the specific session
:return: None
"""
if not args.session_id:
raise Exception("Parameter --session required")
session_doc = client.sessions.get(args.session_id)
jobs = session_doc.get('jobs', {})
table = prettytable.PrettyTable(["job_id", "status", "result",
"client_id"])
for job_id, job_data in six.iteritems(jobs):
table.add_row([job_id,
job_data['status'],
job_data['result'],
job_data['client_id']
])
print(table)
def do_session_delete(client, args):
if not args.session_id:
raise Exception("Parameter --session required")
client.sessions.delete(args.session_id)
def do_session_create(client, args):
"""
creates a session object loading it from disk
:return: None
"""
if not args.fname:
raise Exception("Parameter --file required")
session_doc = utils.load_doc_from_json_file(args.fname)
session_id = client.sessions.create(session_doc)
print("Created session {0}".format(session_id))
def do_session_get(client, args):
"""
gets a specific session object and saves it to file. If file is not
specified the session obj is printed.
:return: None
"""
if not args.session_id:
raise Exception("Parameter --session required")
session_doc = client.sessions.get(args.session_id)
if args.fname:
utils.save_doc_to_json_file(session_doc, args.fname)
else:
pprint(session_doc)
def do_session_list(client, args):
"""
print a list of all jobs
:return: None
"""
table = prettytable.PrettyTable(["session_id", "tag", "status",
"description", "jobs", "last_start"])
session_docs = client.sessions.list()
offset = 0
while session_docs:
offset += len(session_docs)
for doc in session_docs:
table.add_row([doc['session_id'],
doc['session_tag'],
doc['status'],
doc.get('description', ''),
len(doc.get('jobs', [])),
doc['last_start']])
session_docs = client.sessions.list(offset=offset)
print(table)
def do_job_create(client, args):
if not args.fname:
raise Exception("Parameter --file required")
job_doc = utils.load_doc_from_json_file(args.fname)
job_id = client.jobs.create(job_doc)
print("Created job {0}".format(job_id))
def do_job_delete(client, args):
if not args.job_id:
raise Exception("Parameter --job required")
client.jobs.delete(args.job_id)
def do_job_get(client, args):
if not args.job_id:
raise Exception("Parameter --job required")
job_doc = client.jobs.get(args.job_id)
if args.fname:
args.save_doc_to_file(job_doc, args.fname)
else:
pprint(job_doc)
def do_job_start(client, args):
if not args.job_id:
raise Exception("Parameter --job required")
client.jobs.start_job(args.job_id)
print("Start request sent for job {0}".format(args.job_id))
def do_job_stop(client, args):
if not args.job_id:
raise Exception("Parameter --job required")
client.jobs.stop_job(args.job_id)
print("Stop request sent for job {0}".format(args.job_id))
def do_job_download(client, args):
freezer_utils.create_dir(args.jobs_dir, do_log=True)
for doc in _job_list(client, args):
fname = os.path.normpath('{0}/job_{1}.conf'.
format(args.jobs_dir, doc['job_id']))
try:
utils.save_doc_to_json_file(doc, fname)
except Exception:
print("Unable to write to file {0}".format(fname))
def do_job_upload(client, args):
for job_doc in utils.get_jobs_from_disk(args.jobs_dir):
job_id = client.jobs.create(job_doc)
print("Uploaded job {0}".format(job_id))
def _job_list(client, args):
list_func = client.jobs.list_all if args.all else client.jobs.list
search = {}
if args.active_only:
search = {"match_not": [{"status": "completed"}]}
client_docs = list_func(search=search)
offset = 0
while client_docs:
offset += len(client_docs)
for doc in client_docs:
yield doc
client_docs = list_func(offset=offset, search=search)
raise StopIteration
def do_job_list(client, args):
table = prettytable.PrettyTable(["job_id", "client-id", "description",
"# actions", "status", "event",
"result", "session_id"])
for doc in _job_list(client, args):
job_scheduling = doc.get('job_schedule', {})
job_status = job_scheduling.get('status', '')
job_event = job_scheduling.get('event', '')
job_result = job_scheduling.get('result', '')
table.add_row([doc['job_id'],
doc.get('client_id'),
doc.get('description', ''),
len(doc.get('job_actions', [])),
job_status,
job_event,
job_result,
doc.get('session_id', '')])
print(table)
def do_client_list(client, args):
table = prettytable.PrettyTable(["client_id", "hostname", "description"])
l = client.clients.list()
offset = 0
while l:
offset += len(l)
for doc in l:
client_doc = doc['client']
table.add_row([client_doc['client_id'],
client_doc.get('hostname', ''),
client_doc.get('description', '')])
l = client.clients.list(offset=offset)
print(table)
def do_backup_list(client, args):
list_func = client.backups.list_all if args.all else client.backups.list
if args.long:
fields = ["backup uuid", "job-id", "client-id", "container",
"hostname", "backup name", "timestamp", "level", "path"]
else:
fields = ["backup uuid", "container", "backup name", "timestamp",
"level", "path"]
table = prettytable.PrettyTable(fields)
l = list_func()
offset = 0
while l:
offset += len(l)
for doc in l:
metadata_doc = doc['backup_metadata']
timestamp = int(metadata_doc.get('time_stamp', 0))
if args.long:
row = [doc['backup_uuid'],
metadata_doc.get('job_id', ''),
metadata_doc.get('client_id', ''),
metadata_doc.get('container', ''),
metadata_doc.get('hostname', ''),
metadata_doc.get('backup_name', ''),
str(freezer_utils.DateTime(timestamp)),
metadata_doc.get('curr_backup_level', ''),
metadata_doc.get('fs_real_path', '')]
else:
row = [doc['backup_uuid'],
metadata_doc.get('container', ''),
metadata_doc.get('backup_name', ''),
str(freezer_utils.DateTime(timestamp)),
metadata_doc.get('curr_backup_level', ''),
metadata_doc.get('fs_real_path', '')]
table.add_row(row)
l = list_func(offset=offset)
print(table)
def do_job_abort(client, args):
if not args.job_id:
raise Exception("Parameter --job required")
client.jobs.abort_job(args.job_id)
print("Abort request sent for job {0}".format(args.job_id))

View File

@ -24,8 +24,7 @@ from tempest.lib.cli import output_parser
from freezer.tests.freezer_tempest_plugin.tests.api import base
JOB_TABLE_RESULT_COLUMN = 6
JOB_TABLE_STATUS_COLUMN = 4
JOB_TABLE_RESULT_COLUMN = 3
class BaseFreezerCliTest(base.BaseFreezerTest):
@ -48,8 +47,7 @@ class BaseFreezerCliTest(base.BaseFreezerTest):
cls.cli.cli_dir = ''
def delete_job(self, job_id):
self.cli.freezer_scheduler(action='job-delete',
flags='-c test_node -j {}'.format(job_id))
self.cli.freezer_client(action='job-delete', params=job_id)
def create_job(self, job_json):
@ -57,12 +55,13 @@ class BaseFreezerCliTest(base.BaseFreezerTest):
job_file.write(json.dumps(job_json))
job_file.flush()
output = self.cli.freezer_scheduler(
output = self.cli.freezer_client(
action='job-create',
flags='-c test_node --file {}'.format(job_file.name)
params='--file {}'.format(job_file.name)
)
self.assertTrue(output.startswith('Created job'))
job_id = output[len('Created job '):]
job_id = output.split()[1]
expected = 'Job {} created'.format(job_id)
self.assertEqual(expected, output.strip())
self.addCleanup(self.delete_job, job_id)
@ -70,8 +69,7 @@ class BaseFreezerCliTest(base.BaseFreezerTest):
def find_job_in_job_list(self, job_id):
job_list = output_parser.table(
self.cli.freezer_scheduler(action='job-list',
flags='-c test_node'))
self.cli.freezer_client(action='job-list', params='-C test_node'))
for row in job_list['values']:
if row[0].strip() == job_id.strip():
@ -79,21 +77,17 @@ class BaseFreezerCliTest(base.BaseFreezerTest):
self.fail('Could not find job: {}'.format(job_id))
def wait_for_job_status(self, job_id, status, timeout=720):
def wait_for_job_status(self, job_id, timeout=720):
start = time.time()
while True:
row = self.find_job_in_job_list(job_id)
if row[JOB_TABLE_STATUS_COLUMN] == status:
if row[JOB_TABLE_RESULT_COLUMN]:
return
elif time.time() - start > timeout:
self.fail(
("Status of job '{}' is '{}'. "
"Expected '{}'").format(job_id,
row[JOB_TABLE_STATUS_COLUMN],
status)
)
self.fail("Status of job '{}' is '{}'."
.format(job_id, row[JOB_TABLE_RESULT_COLUMN]))
else:
time.sleep(1)
@ -125,6 +119,12 @@ class CLIClientWithFreezer(cli_base.CLIClient):
return self.cmd_with_auth(
'freezer-scheduler', action, flags, params, fail_ok, merge_stderr)
def freezer_client(self, action, flags='', params='', fail_ok=False,
endpoint_type='publicURL', merge_stderr=True):
flags += ' --os-endpoint-type %s' % endpoint_type
return self.cmd_with_auth(
'freezer', action, flags, params, fail_ok, merge_stderr)
# This class is just copied from the freezer repo. Depending on where the
# scenario tests end up we may need to refactore this.
@ -245,6 +245,7 @@ class TestFreezerScenario(BaseFreezerCliTest):
def test_simple_backup(self):
backup_job = {
"client_id": "test_node",
"job_actions": [
{
"freezer_action": {
@ -262,6 +263,7 @@ class TestFreezerScenario(BaseFreezerCliTest):
"description": "a test backup"
}
restore_job = {
"client_id": "test_node",
"job_actions": [
{
"freezer_action": {
@ -279,15 +281,13 @@ class TestFreezerScenario(BaseFreezerCliTest):
}
backup_job_id = self.create_job(backup_job)
self.cli.freezer_scheduler(action='job-start',
flags='-c test_node -j {}'.format(
backup_job_id))
self.wait_for_job_status(backup_job_id, 'completed')
self.cli.freezer_client(action='job-start', params=backup_job_id)
self.wait_for_job_status(backup_job_id)
self.assertJobColumnEqual(backup_job_id, JOB_TABLE_RESULT_COLUMN,
'success')
restore_job_id = self.create_job(restore_job)
self.wait_for_job_status(restore_job_id, 'completed')
self.wait_for_job_status(restore_job_id)
self.assertJobColumnEqual(restore_job_id, JOB_TABLE_RESULT_COLUMN,
'success')