Removed print's, added logging
This commit is contained in:
parent
9c7857bc3b
commit
a0450818fd
2
.gitignore
vendored
2
.gitignore
vendored
@ -14,3 +14,5 @@ tmp/
|
||||
state/
|
||||
clients.json
|
||||
rs/
|
||||
|
||||
solar.log
|
||||
|
@ -168,8 +168,6 @@ def deploy():
|
||||
signals.connect(keystone_config1, glance_api_endpoint, {'admin_token': 'admin_token'})
|
||||
signals.connect(keystone_service1, glance_api_endpoint, {'ip': 'keystone_host', 'admin_port': 'keystone_port'})
|
||||
|
||||
signals.Connections.flush()
|
||||
|
||||
|
||||
has_errors = False
|
||||
for r in locals().values():
|
||||
|
@ -31,7 +31,6 @@ from solar import state
|
||||
from solar.core import actions
|
||||
from solar.core import resource as sresource
|
||||
from solar.core.resource import assign_resources_to_nodes
|
||||
from solar.core.resource import connect_resources
|
||||
from solar.core import signals
|
||||
from solar.core.tags_set_parser import Expression
|
||||
from solar.interfaces.db import get_db
|
||||
@ -71,7 +70,9 @@ def assign(resources, nodes):
|
||||
lambda r: Expression(resources, r.get('tags', [])).evaluate(),
|
||||
_get_resources_list())
|
||||
|
||||
print("For {0} nodes assign {1} resources".format(len(nodes), len(resources)))
|
||||
click.echo(
|
||||
"For {0} nodes assign {1} resources".format(len(nodes), len(resources))
|
||||
)
|
||||
assign_resources_to_nodes(resources, nodes)
|
||||
|
||||
|
||||
@ -129,7 +130,7 @@ def init_changes():
|
||||
@changes.command()
|
||||
def stage():
|
||||
log = operations.stage_changes()
|
||||
print log.show()
|
||||
click.echo(log.show())
|
||||
|
||||
@changes.command()
|
||||
@click.option('--one', is_flag=True, default=False)
|
||||
@ -142,7 +143,7 @@ def init_changes():
|
||||
@changes.command()
|
||||
@click.option('--limit', default=5)
|
||||
def history(limit):
|
||||
print state.CL().show()
|
||||
click.echo(state.CL().show())
|
||||
|
||||
@changes.command()
|
||||
@click.option('--last', is_flag=True, default=False)
|
||||
@ -150,11 +151,11 @@ def init_changes():
|
||||
@click.option('--uid', default=None)
|
||||
def rollback(last, all, uid):
|
||||
if last:
|
||||
print operations.rollback_last()
|
||||
click.echo(operations.rollback_last())
|
||||
elif all:
|
||||
print operations.rollback_all()
|
||||
click.echo(operations.rollback_all())
|
||||
elif uid:
|
||||
print operations.rollback_uid(uid)
|
||||
click.echo(operations.rollback_uid(uid))
|
||||
|
||||
|
||||
def init_cli_connect():
|
||||
@ -163,11 +164,11 @@ def init_cli_connect():
|
||||
@click.argument('receiver')
|
||||
@click.option('--mapping', default=None)
|
||||
def connect(mapping, receiver, emitter):
|
||||
print 'Connect', emitter, receiver
|
||||
click.echo('Connect {} to {}'.format(emitter, receiver))
|
||||
emitter = sresource.load(emitter)
|
||||
receiver = sresource.load(receiver)
|
||||
print emitter
|
||||
print receiver
|
||||
click.echo(emitter)
|
||||
click.echo(receiver)
|
||||
if mapping is not None:
|
||||
mapping = json.loads(mapping)
|
||||
signals.connect(emitter, receiver, mapping=mapping)
|
||||
@ -176,11 +177,11 @@ def init_cli_connect():
|
||||
@click.argument('emitter')
|
||||
@click.argument('receiver')
|
||||
def disconnect(receiver, emitter):
|
||||
print 'Disconnect', emitter, receiver
|
||||
click.echo('Disconnect {} from {}'.format(emitter, receiver))
|
||||
emitter = sresource.load(emitter)
|
||||
receiver = sresource.load(receiver)
|
||||
print emitter
|
||||
print receiver
|
||||
click.echo(emitter)
|
||||
click.echo(receiver)
|
||||
signals.disconnect(emitter, receiver)
|
||||
|
||||
|
||||
@ -208,14 +209,14 @@ def init_cli_connections():
|
||||
|
||||
@connections.command()
|
||||
def show():
|
||||
print json.dumps(signals.Connections.read_clients(), indent=2)
|
||||
click.echo(json.dumps(signals.Connections.read_clients(), indent=2))
|
||||
|
||||
|
||||
def init_cli_deployment_config():
|
||||
@main.command()
|
||||
@click.argument('filepath')
|
||||
def deploy(filepath):
|
||||
print 'Deploying from file {}'.format(filepath)
|
||||
click.echo('Deploying from file {}'.format(filepath))
|
||||
xd.deploy(filepath)
|
||||
|
||||
|
||||
@ -228,7 +229,9 @@ def init_cli_resource():
|
||||
@click.argument('resource_path')
|
||||
@click.argument('action_name')
|
||||
def action(action_name, resource_path):
|
||||
print 'action', resource_path, action_name
|
||||
click.echo(
|
||||
'action {} for resource {}'.format(action_name, resource_path)
|
||||
)
|
||||
r = sresource.load(resource_path)
|
||||
actions.resource_action(r, action_name)
|
||||
|
||||
@ -237,7 +240,7 @@ def init_cli_resource():
|
||||
@click.argument('base_path')
|
||||
@click.argument('args')
|
||||
def create(args, base_path, name):
|
||||
print 'create', name, base_path, args
|
||||
click.echo('create {} {} {}'.format(name, base_path, args))
|
||||
args = json.loads(args)
|
||||
sresource.create(name, base_path, args)
|
||||
|
||||
@ -275,7 +278,7 @@ def init_cli_resource():
|
||||
@click.argument('tag_name')
|
||||
@click.option('--add/--delete', default=True)
|
||||
def tag(add, tag_name, resource_path):
|
||||
print 'Tag', resource_path, tag_name, add
|
||||
click.echo('Tag {} with {} {}'.format(resource_path, tag_name, add))
|
||||
r = sresource.load(resource_path)
|
||||
if add:
|
||||
r.add_tag(tag_name)
|
||||
|
@ -5,6 +5,7 @@ import shutil
|
||||
import yaml
|
||||
|
||||
from solar.core import db
|
||||
from solar.core.log import log
|
||||
from solar.core import resource as xr
|
||||
from solar.core import signals as xs
|
||||
|
||||
@ -27,7 +28,7 @@ def deploy(filename):
|
||||
name = resource_definition['name']
|
||||
model = os.path.join(workdir, resource_definition['model'])
|
||||
args = resource_definition.get('args', {})
|
||||
print 'Creating ', name, model, resource_save_path, args
|
||||
log.debug('Creating %s %s %s %s', name, model, resource_save_path, args)
|
||||
xr.create(name, model, resource_save_path, args=args)
|
||||
|
||||
# Create resource connections
|
||||
@ -35,11 +36,11 @@ def deploy(filename):
|
||||
emitter = db.get_resource(connection['emitter'])
|
||||
receiver = db.get_resource(connection['receiver'])
|
||||
mapping = connection.get('mapping')
|
||||
print 'Connecting ', emitter.name, receiver.name, mapping
|
||||
log.debug('Connecting %s %s %s', emitter.name, receiver.name, mapping)
|
||||
xs.connect(emitter, receiver, mapping=mapping)
|
||||
|
||||
# Run all tests
|
||||
if 'test-suite' in config:
|
||||
print 'Running tests from {}'.format(config['test-suite'])
|
||||
log.debug('Running tests from %s', config['test-suite'])
|
||||
test_suite = __import__(config['test-suite'], {}, {}, ['main'])
|
||||
test_suite.main()
|
||||
|
@ -2,23 +2,24 @@
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
from solar.core.log import log
|
||||
from solar.core.handlers.base import BaseHandler
|
||||
from solar.state import STATES
|
||||
|
||||
|
||||
class Ansible(BaseHandler):
|
||||
def action(self, resource, action_name):
|
||||
inventory_file = self._create_inventory(resource)
|
||||
playbook_file = self._create_playbook(resource, action_name)
|
||||
print 'inventory_file', inventory_file
|
||||
print 'playbook_file', playbook_file
|
||||
log.debug('inventory_file: %s', inventory_file)
|
||||
log.debug('playbook_file: %s', playbook_file)
|
||||
call_args = ['ansible-playbook', '--module-path', '/vagrant/library', '-i', inventory_file, playbook_file]
|
||||
print 'EXECUTING: ', ' '.join(call_args)
|
||||
log.debug('EXECUTING: %s', ' '.join(call_args))
|
||||
|
||||
try:
|
||||
subprocess.check_output(call_args)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print e.output
|
||||
log.error(e.output)
|
||||
log.exception(e)
|
||||
raise
|
||||
|
||||
def _create_inventory(self, r):
|
||||
@ -32,7 +33,7 @@ class Ansible(BaseHandler):
|
||||
inventory = '{0} ansible_ssh_host={1} ansible_connection=ssh ansible_ssh_user={2} ansible_ssh_private_key_file={3}'
|
||||
host, user, ssh_key = r.args['ip'].value, r.args['ssh_user'].value, r.args['ssh_key'].value
|
||||
inventory = inventory.format(host, host, user, ssh_key)
|
||||
print inventory
|
||||
log.debug(inventory)
|
||||
return inventory
|
||||
|
||||
def _create_playbook(self, resource, action):
|
||||
|
@ -5,6 +5,8 @@ import tempfile
|
||||
|
||||
from jinja2 import Template
|
||||
|
||||
from solar.core.log import log
|
||||
|
||||
|
||||
class BaseHandler(object):
|
||||
def __init__(self, resources):
|
||||
@ -19,7 +21,7 @@ class BaseHandler(object):
|
||||
return self
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
print self.dst
|
||||
log.debug(self.dst)
|
||||
return
|
||||
shutil.rmtree(self.dst)
|
||||
|
||||
@ -33,11 +35,11 @@ class BaseHandler(object):
|
||||
return dest_file
|
||||
|
||||
def _render_action(self, resource, action):
|
||||
print 'Rendering %s %s' % (resource.name, action)
|
||||
log.debug('Rendering %s %s', resource.name, action)
|
||||
|
||||
action_file = resource.metadata['actions'][action]
|
||||
action_file = os.path.join(resource.metadata['actions_path'], action_file)
|
||||
print 'action file: ', action_file
|
||||
log.debug('action file: %s', action_file)
|
||||
args = self._make_args(resource)
|
||||
|
||||
with open(action_file) as f:
|
||||
|
22
solar/solar/core/log.py
Normal file
22
solar/solar/core/log.py
Normal file
@ -0,0 +1,22 @@
|
||||
import logging
|
||||
import sys
|
||||
|
||||
|
||||
log = logging.getLogger('solar')
|
||||
|
||||
|
||||
def setup_logger():
|
||||
handler = logging.FileHandler('solar.log')
|
||||
handler.setLevel(logging.DEBUG)
|
||||
formatter = logging.Formatter('%(asctime)s %(levelname)s %(funcName)s (%(filename)s::%(lineno)s)::%(message)s')
|
||||
handler.setFormatter(formatter)
|
||||
log.addHandler(handler)
|
||||
|
||||
print_formatter = logging.Formatter('%(levelname)s (%(filename)s::%(lineno)s)::%(message)s')
|
||||
print_handler = logging.StreamHandler(stream=sys.stdout)
|
||||
print_handler.setFormatter(print_formatter)
|
||||
log.addHandler(print_handler)
|
||||
|
||||
log.setLevel(logging.DEBUG)
|
||||
|
||||
setup_logger()
|
@ -1,3 +1,4 @@
|
||||
from solar.core.log import log
|
||||
from solar.core import signals
|
||||
from solar.interfaces.db import get_db
|
||||
|
||||
@ -28,16 +29,12 @@ class BaseObserver(object):
|
||||
def receivers(self):
|
||||
from solar.core import resource
|
||||
|
||||
#signals.CLIENTS = signals.Connections.read_clients()
|
||||
for receiver_name, receiver_input in signals.Connections.receivers(
|
||||
self._attached_to_name,
|
||||
self.name
|
||||
):
|
||||
yield resource.load(receiver_name).args[receiver_input]
|
||||
|
||||
def log(self, msg):
|
||||
print '{} {}'.format(self, msg)
|
||||
|
||||
def __repr__(self):
|
||||
return '[{}:{}] {}'.format(self._attached_to_name, self.name, self.value)
|
||||
|
||||
@ -76,10 +73,10 @@ class BaseObserver(object):
|
||||
:param receiver: Observer
|
||||
:return:
|
||||
"""
|
||||
self.log('Subscribe {}'.format(receiver))
|
||||
log.debug('Subscribe %s', receiver)
|
||||
# No multiple subscriptions
|
||||
if self.find_receiver(receiver):
|
||||
self.log('No multiple subscriptions from {}'.format(receiver))
|
||||
log.error('No multiple subscriptions from %s', receiver)
|
||||
return
|
||||
receiver.subscribed(self)
|
||||
|
||||
@ -93,14 +90,14 @@ class BaseObserver(object):
|
||||
receiver.notify(self)
|
||||
|
||||
def subscribed(self, emitter):
|
||||
self.log('Subscribed {}'.format(emitter))
|
||||
log.debug('Subscribed %s', emitter)
|
||||
|
||||
def unsubscribe(self, receiver):
|
||||
"""
|
||||
:param receiver: Observer
|
||||
:return:
|
||||
"""
|
||||
self.log('Unsubscribe {}'.format(receiver))
|
||||
log.debug('Unsubscribe %s', receiver)
|
||||
if self.find_receiver(receiver):
|
||||
receiver.unsubscribed(self)
|
||||
|
||||
@ -115,7 +112,7 @@ class BaseObserver(object):
|
||||
#receiver.notify(self)
|
||||
|
||||
def unsubscribed(self, emitter):
|
||||
self.log('Unsubscribed {}'.format(emitter))
|
||||
log.debug('Unsubscribed %s', emitter)
|
||||
|
||||
|
||||
class Observer(BaseObserver):
|
||||
@ -132,7 +129,7 @@ class Observer(BaseObserver):
|
||||
return resource.load(emitter_name).args[emitter_input_name]
|
||||
|
||||
def notify(self, emitter):
|
||||
self.log('Notify from {} value {}'.format(emitter, emitter.value))
|
||||
log.debug('Notify from %s value %s', emitter, emitter.value)
|
||||
# Copy emitter's values to receiver
|
||||
self.value = emitter.value
|
||||
for receiver in self.receivers:
|
||||
@ -140,7 +137,7 @@ class Observer(BaseObserver):
|
||||
self.attached_to.set_args_from_dict({self.name: self.value})
|
||||
|
||||
def update(self, value):
|
||||
self.log('Updating to value {}'.format(value))
|
||||
log.debug('Updating to value %s', value)
|
||||
self.value = value
|
||||
for receiver in self.receivers:
|
||||
receiver.notify(self)
|
||||
@ -168,7 +165,7 @@ class ListObserver(BaseObserver):
|
||||
}
|
||||
|
||||
def notify(self, emitter):
|
||||
self.log('Notify from {} value {}'.format(emitter, emitter.value))
|
||||
log.debug('Notify from %s value %s', emitter, emitter.value)
|
||||
# Copy emitter's values to receiver
|
||||
idx = self._emitter_idx(emitter)
|
||||
self.value[idx] = self._format_value(emitter)
|
||||
@ -188,7 +185,7 @@ class ListObserver(BaseObserver):
|
||||
:param receiver: Observer
|
||||
:return:
|
||||
"""
|
||||
self.log('Unsubscribed emitter {}'.format(emitter))
|
||||
log.debug('Unsubscribed emitter %s', emitter)
|
||||
idx = self._emitter_idx(emitter)
|
||||
self.value.pop(idx)
|
||||
self.attached_to.set_args_from_dict({self.name: self.value})
|
||||
|
@ -3,6 +3,7 @@ from collections import defaultdict
|
||||
import itertools
|
||||
import networkx as nx
|
||||
|
||||
from solar.core.log import log
|
||||
from solar.interfaces.db import get_db
|
||||
|
||||
db = get_db()
|
||||
@ -140,7 +141,9 @@ def disconnect(emitter, receiver):
|
||||
receiver_input = destination[1]
|
||||
if receiver_input in receiver.args:
|
||||
if receiver.args[receiver_input].type_ != 'list':
|
||||
print 'Removing input {} from {}'.format(receiver_input, receiver.name)
|
||||
log.debug(
|
||||
'Removing input %s from %s', receiver_input, receiver.name
|
||||
)
|
||||
emitter.args[src].unsubscribe(receiver.args[receiver_input])
|
||||
|
||||
disconnect_by_src(emitter.name, src, receiver)
|
||||
@ -179,15 +182,15 @@ def notify(source, key, value):
|
||||
clients.setdefault(source.name, {})
|
||||
Connections.save_clients(clients)
|
||||
|
||||
print 'Notify', source.name, key, value, clients[source.name]
|
||||
log.debug('Notify %s %s %s %s', source.name, key, value, clients[source.name])
|
||||
if key in clients[source.name]:
|
||||
for client, r_key in clients[source.name][key]:
|
||||
resource = load(client)
|
||||
print 'Resource found', client
|
||||
log.debug('Resource found: %s', client)
|
||||
if resource:
|
||||
resource.update({r_key: value}, emitter=source)
|
||||
else:
|
||||
print 'Resource {} deleted?'.format(client)
|
||||
log.debug('Resource %s deleted?', client)
|
||||
pass
|
||||
|
||||
|
||||
|
@ -1,4 +1,6 @@
|
||||
from jsonschema import validate, ValidationError, SchemaError
|
||||
from jsonschema import validate, ValidationError
|
||||
|
||||
from solar.core.log import log
|
||||
|
||||
|
||||
def schema_input_type(schema):
|
||||
@ -86,9 +88,10 @@ def validate_input(value, jsonschema=None, schema=None):
|
||||
validate(value, jsonschema)
|
||||
except ValidationError as e:
|
||||
return [e.message]
|
||||
except:
|
||||
print 'jsonschema', jsonschema
|
||||
print 'value', value
|
||||
except Exception as e:
|
||||
log.error('jsonschema: %s', jsonschema)
|
||||
log.error('value: %s', value)
|
||||
log.exception(e)
|
||||
raise
|
||||
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
|
||||
|
||||
from solar import state
|
||||
from solar.core.log import log
|
||||
from solar.core import signals
|
||||
from solar.core import resource
|
||||
from solar import utils
|
||||
@ -60,7 +61,7 @@ def stage_changes():
|
||||
srt = nx.topological_sort(conn_graph)
|
||||
except:
|
||||
for cycle in nx.simple_cycles(conn_graph):
|
||||
print 'CYCLE: %s' % cycle
|
||||
log.debug('CYCLE: %s', cycle)
|
||||
raise
|
||||
|
||||
for res_uid in srt:
|
||||
|
Loading…
x
Reference in New Issue
Block a user