Skip to content

Commit

Permalink
added --clear-resources option
Browse files Browse the repository at this point in the history
  • Loading branch information
advornic committed Jan 29, 2015
1 parent 64cb108 commit 799b798
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 15 deletions.
3 changes: 1 addition & 2 deletions test/actions/test_run_bash_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
sys.path.append('test/client')

from client_test_lib import Bootstrap, ActionFailureTest
from client_test_lib import eapi_log, get_action, random_string
from client_test_lib import get_action, random_string
from client_test_lib import startup_config_action
from client_test_lib import raise_exception

Expand Down Expand Up @@ -121,7 +121,6 @@ def test_failure(self):
bootstrap.ztps.set_action_response('test_action',
get_action('run_bash_script'))

print_string = random_string()
contents = '''#!/usr/bin/env python
assert False'''
bootstrap.ztps.set_file_response(config, contents)
Expand Down
13 changes: 8 additions & 5 deletions test/server/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
# pylint: disable=C0102,C0103,E1103,W0142,W0613,C0302
# pylint: disable=C0102,C0103,E1103,W0142,W0613,C0302,E1120
#

import json
Expand Down Expand Up @@ -538,7 +538,8 @@ def test_create(self, m_repository):
controller = ztpserver.controller.NodesController()
with patch.object(controller, 'fsm') as m_fsm:
controller.create(request)
self.assertEqual(self.identifier(node), m_fsm.call_args[1]['node_id'])
self.assertEqual(self.identifier(node),
m_fsm.call_args[1]['node_id'])

@patch('ztpserver.controller.create_repository')
def test_create_systemmac(self, m_repository):
Expand Down Expand Up @@ -871,7 +872,8 @@ def test_post_node_no_definition_in_pattern(self, m_load_neighbordb):

controller = ztpserver.controller.NodesController()
self.assertRaises(AttributeError, controller.post_node, dict(),
request=request, node=node, node_id=self.identifier(node))
request=request, node=node,
node_id=self.identifier(node))

@patch('ztpserver.controller.load_neighbordb')
def test_post_node_no_definition_in_pattern_systemmac(self,
Expand Down Expand Up @@ -974,8 +976,9 @@ def test_get_attributes_missing(self, m_repository):


controller = ztpserver.controller.NodesController()
(resp, state) = controller.get_attributes(dict(),
resource=self.identifier(node))
(resp, state) = controller.get_attributes(
dict(),
resource=self.identifier(node))

self.assertEqual(state, 'do_substitution')
self.assertIsInstance(resp, dict)
Expand Down
66 changes: 58 additions & 8 deletions ztpserver/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

from ztpserver import config, controller

from ztpserver.serializers import load
from ztpserver.serializers import load, dump
from ztpserver.validators import NeighbordbValidator
from ztpserver.constants import CONTENT_TYPE_YAML
from ztpserver.topology import neighbordb_path
Expand Down Expand Up @@ -115,7 +115,7 @@ def start_wsgiapp(config_file=None, debug=False):

try:
version = open(config.VERSION_FILE_PATH).read().split()[0].strip()
except Exception:
except Exception: # pylint: disable=W0703
version = 'N/A'

log.info('Starting ZTPServer v%s...' % version)
Expand Down Expand Up @@ -154,8 +154,7 @@ def run_server(version, config_file, debug):
except KeyboardInterrupt:
log.info('Shutdown...')

def run_validator():

def validate_neighbordb():
# Validating neighbordb
validator = NeighbordbValidator('N/A')
neighbordb = neighbordb_path()
Expand All @@ -180,6 +179,7 @@ def run_validator():
except Exception as exc: #pylint: disable=W0703
print 'ERROR: Failed to validate neighbordb\n%s' % exc

def validate_definitions():
data_root = config.runtime.default.data_root

print '\nValidating definitions...'
Expand All @@ -194,17 +194,25 @@ def run_validator():
print '\nERROR: Failed to validate %s\n%s' % \
(definition, exc)

def validate_resources(raiseException=False):
data_root = config.runtime.default.data_root

print '\nValidating resources...'
for resource in all_files(os.path.join(data_root,
'resources')):
'resources')):
print 'Validating %s...' % resource,
try:
load(resource, CONTENT_TYPE_YAML,
'validator')
print 'Ok!'
except Exception as exc: #pylint: disable=W0703
except Exception as exc: #pylint: disable=W0703
print '\nERROR: Failed to validate %s\n%s' % \
(resource, exc)
if raiseException:
raise exc

def validate_nodes():
data_root = config.runtime.default.data_root

print '\nValidating nodes...'
for filename in [x for x in all_files(os.path.join(data_root,
Expand All @@ -220,6 +228,39 @@ def run_validator():
print '\nERROR: Failed to validate %s\n%s' % \
(filename, exc)

def clear_resources(debug):
start_logging(debug)

try:
validate_resources(raiseException=True)
except Exception: #pylint: disable=W0703
sys.exit('ERROR: Unable to clear resources because of validation error')

data_root = config.runtime.default.data_root

print '\nClearing resources...'
for resource in all_files(os.path.join(data_root,
'resources')):
print 'Clearing %s...' % resource
try:
contents = load(resource, CONTENT_TYPE_YAML,
'clear_resource')
for key in contents:
contents[key] = 'None'
dump(contents, resource, CONTENT_TYPE_YAML,
'clear_resource')
except Exception as exc: #pylint: disable=W0703
print '\nERROR: Failed to clear %s\n%s' % \
(resource, exc)

def run_validator(debug):
start_logging(debug)

validate_neighbordb()
validate_definitions()
validate_resources()
validate_nodes()

def main():
''' The :py:func:`main` is the main entry point for the ztpserver if called
from the commmand line. When called from the command line, the server is
Expand Down Expand Up @@ -248,6 +289,10 @@ def main():
action='store_true',
help='Enables debug output to the STDOUT')

parser.add_argument('--clear-resources', '-r',
action='store_true',
help='Clears all resource files')


args = parser.parse_args()

Expand All @@ -259,9 +304,14 @@ def main():

if args.version:
print 'ZTPServer version %s' % version
sys.exit()

if args.validate_config:
sys.exit(run_validator())
run_validator(args.debug)

if args.clear_resources:
clear_resources(args.debug)

if args.version or args.validate_config or args.clear_resources:
sys.exit()

return run_server(version, args.conf, args.debug)

0 comments on commit 799b798

Please sign in to comment.