Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
timezone = FieldBase(field_type="STRING", default="utc", choices=pytz.all_timezones)
jitter = FieldBase(field_type="INT")
class Job:
brewtils_model = brewtils.models.Job
TRIGGER_MODEL_MAPPING = {
"date": brewtils.models.DateTrigger,
"cron": brewtils.models.CronTrigger,
"interval": brewtils.models.IntervalTrigger,
}
name = FieldBase(field_type="STRING", required=True)
trigger_type = FieldBase(field_type="STRING", required=True, choices=BrewtilsJob.TRIGGER_TYPES)
request_template = FieldBase(field_type=TRIGGER_MODEL_MAPPING, required=True, choices=list(TRIGGER_MODEL_MAPPING.values()))
request_template = FieldBase(field_type=brewtils.models.RequestTemplate, required=True)
misfire_grace_time = FieldBase(field_type="INT")
coalesce = FieldBase(field_type="BOOLEAN",default=True)
next_run_time = FieldBase(field_type="DATE")
success_count = FieldBase(field_type="INT", required=True, default=0, min_value=0)
error_count = FieldBase(field_type="INT", required=True, default=0, min_value=0)
status = FieldBase(field_type="STRING",
required=True, choices=BrewtilsJob.STATUS_TYPES, default="RUNNING"
)
max_instances = FieldBase(field_type="INT", default=3, min_value=1)
def clean(self):
"""Validate before saving to the database"""
if self.trigger_type not in self.TRIGGER_MODEL_MAPPING:
raise ModelValidationError(
f"Cannot save job. No mongo model for trigger type {self.trigger_type}"
"""This is the Plugin State Manager"""
import logging
from datetime import datetime
from brewtils.models import Events, Instance, Request, RequestTemplate, System
import beer_garden.db.api as db
import beer_garden.queue.api as queue
from beer_garden.events import publish_event
import beer_garden.requests as requests
logger = logging.getLogger(__name__)
start_request = RequestTemplate(command="_start", command_type="EPHEMERAL")
stop_request = RequestTemplate(command="_stop", command_type="EPHEMERAL")
read_logs_request = RequestTemplate(command="_read_log")
@publish_event(Events.INSTANCE_INITIALIZED)
def initialize(instance_id: str, runner_id: str = None) -> Instance:
"""Initializes an instance.
Args:
instance_id: The Instance ID
runner_id: The runner id to associate with this plugin, if any
Returns:
The updated Instance
"""
instance = db.query_unique(Instance, id=instance_id)
__tablename__ = brewtils.models.Role.schema
class RefreshToken(Base):
id = Column(sqlalchemy.Integer, primary_key=True)
__tablename__ = brewtils.models.RefreshToken.schema
class Job(Base):
id = Column(sqlalchemy.Integer, primary_key=True)
__tablename__ = brewtils.models.Job.schema
class RequestTemplate(Base):
id = Column(sqlalchemy.Integer, primary_key=True)
__tablename__ = brewtils.models.RequestTemplate.schema
class DateTrigger(Base):
id = Column(sqlalchemy.Integer, primary_key=True)
__tablename__ = brewtils.models.DateTrigger.schema
class CronTrigger(Base):
id = Column(sqlalchemy.Integer, primary_key=True)
__tablename__ = brewtils.models.CronTrigger.schema
class IntervalTrigger(Base):
id = Column(sqlalchemy.Integer, primary_key=True)
__tablename__ = brewtils.models.IntervalTrigger.schema
from beer_garden.__version__ import __version__
__all__ = [
"__version__",
"application",
"logger",
"start_request",
"stop_request",
"load_config",
]
# COMPONENTS #
application = None
logger = None
start_request = RequestTemplate(command="_start", command_type="EPHEMERAL")
stop_request = RequestTemplate(command="_stop", command_type="EPHEMERAL")
def signal_handler(signal_number, stack_frame):
beer_garden.logger.info("Last call! Looks like we gotta close up.")
beer_garden.application.stop()
def load_config(cli_args):
global logger
beer_garden.config.load(cli_args)
beer_garden.log.load(beer_garden.config.get("log"))
logger = logging.getLogger(__name__)
logger.debug("Successfully loaded configuration")
meta = {"indexes": [{"fields": ["expires"], "expireAfterSeconds": 0}]}
def get_principal(self):
principal_id = self.payload.get("sub")
if not principal_id:
return None
try:
return Principal.objects.get(id=principal_id)
except DoesNotExist:
return None
class RequestTemplate(MongoModel, EmbeddedDocument):
brewtils_model = brewtils.models.RequestTemplate
for field_name, field_info in Request.TEMPLATE_FIELDS.items():
locals()[field_name] = field_info["field"](**field_info["kwargs"])
class DateTrigger(MongoModel, EmbeddedDocument):
brewtils_model = brewtils.models.DateTrigger
run_date = DateTimeField(required=True)
timezone = StringField(required=False, default="utc", chocies=pytz.all_timezones)
class IntervalTrigger(MongoModel, EmbeddedDocument):
brewtils_model = brewtils.models.IntervalTrigger
weeks = IntField(default=0)
"""Validates and publishes a Request.
Args:
new_request: The Request
wait_timeout: Float describing amount of time to wait for request to complete
<0: Wait forever
0: Don't wait at all
>0: Wait this long
Returns:
The processed Request
"""
if type(new_request) == Request:
request = new_request
elif type(new_request) == RequestTemplate:
request = Request.from_template(new_request)
else:
raise TypeError(
f"new_request type is {type(new_request)}, expected "
f"brewtils.models.Request or brewtils.models.RequestTemplate,"
)
# Validates the request based on what is in the database.
# This includes the validation of the request parameters,
# systems are there, commands are there etc.
request = RequestValidator.instance().validate_request(request)
# Once validated we need to save since validate can modify the request
request = create_request(request)
if wait_timeout != 0:
ModelType = Union[
Type[brewtils.models.Command],
Type[brewtils.models.Instance],
Type[brewtils.models.Job],
Type[brewtils.models.Request],
Type[brewtils.models.RequestTemplate],
Type[brewtils.models.System],
Type[brewtils.models.Garden],
]
ModelItem = Union[
brewtils.models.Command,
brewtils.models.Instance,
brewtils.models.Job,
brewtils.models.Request,
brewtils.models.RequestTemplate,
brewtils.models.System,
brewtils.models.Garden,
]
_model_map = beer_garden.db.sql.models.schema_mapping
engine = None
Session = None
def from_brewtils(obj: ModelItem) -> SqlModel:
"""Convert an item from its Brewtils model to its one
Args:
obj: The Brewtils model item
"""This is the Plugin State Manager"""
import logging
from datetime import datetime
from brewtils.models import Events, Instance, Request, RequestTemplate, System
import beer_garden.db.api as db
import beer_garden.queue.api as queue
from beer_garden.events import publish_event
import beer_garden.requests as requests
logger = logging.getLogger(__name__)
start_request = RequestTemplate(command="_start", command_type="EPHEMERAL")
stop_request = RequestTemplate(command="_stop", command_type="EPHEMERAL")
read_logs_request = RequestTemplate(command="_read_log")
@publish_event(Events.INSTANCE_INITIALIZED)
def initialize(instance_id: str, runner_id: str = None) -> Instance:
"""Initializes an instance.
Args:
instance_id: The Instance ID
runner_id: The runner id to associate with this plugin, if any
Returns:
The updated Instance
"""
instance = db.query_unique(Instance, id=instance_id)
system = db.query_unique(System, instances__contains=instance)
queue_type = FieldBase(field_type="STRING")
queue_info = FieldBase(field_type="JSON")
icon_name = FieldBase(field_type="STRING")
metadata = FieldBase(field_type="JSON")
def clean(self):
"""Validate before saving to the database"""
if self.status not in BrewtilsInstance.INSTANCE_STATUSES:
raise ModelValidationError(
f"Can not save Instance {self}: Invalid status '{self.status}'"
)
class RequestTemplate:
brewtils_model = brewtils.models.RequestTemplate
system = FieldBase(field_type="STRING", required=True)
system_version = FieldBase(field_type="STRING", required=True)
instance_name = FieldBase(field_type="STRING", required=True)
namespace = FieldBase(field_type="STRING", required=True)
command = FieldBase(field_type="STRING", required=True)
command_type = FieldBase(field_type="STRING")
parameters = FieldBase(field_type="DICT")
comment = FieldBase(field_type="STRING")
metadata = FieldBase(field_type="DICT")
output_type = FieldBase(field_type="STRING")
class Request(RequestTemplate):
brewtils_model = brewtils.models.Request
#import beer_garden.db.mongo.models as db_models
import beer_garden.db.mongo.new_models as db_models
from beer_garden.db.mongo.models import MongoModel
from beer_garden.db.mongo.parser import MongoParser
from beer_garden.db.mongo.pruner import MongoPruner
from beer_garden.db.mongo.util import check_indexes, ensure_roles, ensure_users
logger = logging.getLogger(__name__)
ModelType = Union[
Type[brewtils.models.Command],
Type[brewtils.models.Instance],
Type[brewtils.models.Job],
Type[brewtils.models.Request],
Type[brewtils.models.RequestTemplate],
Type[brewtils.models.System],
Type[brewtils.models.Garden],
]
ModelItem = Union[
brewtils.models.Command,
brewtils.models.Instance,
brewtils.models.Job,
brewtils.models.Request,
brewtils.models.RequestTemplate,
brewtils.models.System,
brewtils.models.Garden,
]
# _model_map = {}
# for model_name in db_models.__all__: