Default config is set up on server init

server ips set in default config
Fix bug in config ui
This commit is contained in:
Itay Mizeretz 2017-09-19 15:30:42 +03:00
parent 787d08db57
commit 2a1417d696
5 changed files with 68 additions and 8 deletions

View File

@ -15,6 +15,7 @@ from cc.resources.edge import Edge
from cc.resources.node import Node
from cc.resources.root import Root
from cc.services.config import ConfigService
__author__ = 'Barak'
@ -65,6 +66,9 @@ def init_app(mongo_url):
app.config['MONGO_URI'] = mongo_url
mongo.init_app(app)
with app.app_context():
ConfigService.init_config()
app.add_url_rule('/', 'serve_home', serve_home)
app.add_url_rule('/<path:path>', 'serve_static_file', serve_static_file)

View File

@ -15,7 +15,6 @@ class MonkeyConfiguration(flask_restful.Resource):
def post(self):
config_json = json.loads(request.data)
mongo.db.config.update({'name': 'newconfig'}, {"$set": config_json}, upsert=True)
return jsonify(schema=ConfigService.get_config_schema(), configuration=ConfigService.get_config())
ConfigService.update_config(config_json)
return self.get()

View File

@ -4,6 +4,7 @@ from flask import request, make_response, jsonify
import flask_restful
from cc.database import mongo
from cc.services.config import ConfigService
from cc.utils import local_ip_addresses
@ -24,6 +25,7 @@ class Root(flask_restful.Resource):
mongo.db.telemetry.drop()
mongo.db.node.drop()
mongo.db.edge.drop()
ConfigService.init_config()
return jsonify(status='OK')
elif action == "killall":
mongo.db.monkey.update({}, {'$set': {'config.alive': False, 'modifytime': datetime.now()}}, upsert=False,

View File

@ -1,4 +1,8 @@
from cc.database import mongo
from jsonschema import Draft4Validator, validators
from cc.island_config import ISLAND_PORT
from cc.utils import local_ip_addresses
__author__ = "itay.mizeretz"
@ -799,5 +803,51 @@ class ConfigService:
)
@staticmethod
def update_config():
pass
def update_config(config_json):
mongo.db.config.update({'name': 'newconfig'}, {"$set": config_json}, upsert=True)
@staticmethod
def get_default_config():
defaultValidatingDraft4Validator = ConfigService._extend_config_with_default(Draft4Validator)
config = {}
defaultValidatingDraft4Validator(SCHEMA).validate(config)
return config
@staticmethod
def init_config():
if ConfigService.get_config() != {}:
return
config = ConfigService.get_default_config()
ConfigService.set_server_ips_in_config(config)
ConfigService.update_config(config)
@staticmethod
def set_server_ips_in_config(config):
ips = local_ip_addresses()
config["cnc"]["servers"]["command_servers"] = ["%s:%d" % (ip, ISLAND_PORT) for ip in ips]
config["cnc"]["servers"]["current_server"] = "%s:%d" % (ips[0], ISLAND_PORT)
@staticmethod
def _extend_config_with_default(validator_class):
validate_properties = validator_class.VALIDATORS["properties"]
def set_defaults(validator, properties, instance, schema):
# Do it only for root.
if instance != {}:
return
for property, subschema in properties.iteritems():
main_dict = {}
for property2, subschema2 in subschema["properties"].iteritems():
sub_dict = {}
for property3, subschema3 in subschema2["properties"].iteritems():
if "default" in subschema3:
sub_dict[property3] = subschema3["default"]
main_dict[property2] = sub_dict
instance.setdefault(property, main_dict)
for error in validate_properties(validator, properties, instance, schema):
yield error
return validators.extend(
validator_class, {"properties": set_defaults},
)

View File

@ -12,7 +12,7 @@ class ConfigurePageComponent extends React.Component {
configuration: {},
saved: false,
sections: [],
selectedSection: ''
selectedSection: 'basic'
};
}
@ -55,8 +55,13 @@ class ConfigurePageComponent extends React.Component {
};
render() {
let displayedSchema = this.state.schema["properties"][this.state.selectedSection];
displayedSchema["definitions"] = this.state.schema["definitions"];
let displayedSchema = {};
if (this.state.schema.hasOwnProperty('properties')) {
displayedSchema = this.state.schema["properties"][this.state.selectedSection];
displayedSchema["definitions"] = this.state.schema["definitions"];
}
return (
<Col xs={8}>
<h1 className="page-title">Monkey Configuration</h1>