def run(self): crypt = Crypt() if self.create_key: path = crypt._loader.get_path_from_env() # pylint: disable=protected-access pwd = self.get_input('Type a password to generate the key file: ') generate_file = self.get_input('Do you want to generate a file in {}? [Y/n]'.format(path)) generate_file = generate_file.lower() != "n" key = crypt.generate_key(pwd, generate_file) if generate_file: self.print_ok("File {} generated OK".format(path)) else: self.print_ok("Key generated: {}".format(key)) if self.encrypt: encrypted = crypt.encrypt(self.encrypt) self.print_ok("Encrypted OK: {}".format(encrypted)) if self.startproject: check_package_exists("cookiecutter") cookiecutter = import_from("cookiecutter.main", "cookiecutter") cookiecutter('gh:python-microservices/cookiecutter-pyms', checkout=self.branch) self.print_ok("Created project OK") if self.merge_swagger: try: merge_swagger_file(main_file=self.file) self.print_ok("Swagger file generated [swagger-complete.yaml]") except FileNotFoundError as ex: self.print_error(ex.__str__()) return False return True
def init_jaeger_tracer(self): """This scaffold is configured whith `Jeager <https://github.com/jaegertracing/jaeger>`_ but you can use one of the `opentracing tracers <http://opentracing.io/documentation/pages/supported-tracers.html>`_ :param service_name: the name of your application to register in the tracer :return: opentracing.Tracer """ check_package_exists("jaeger_client") Config = import_from("jaeger_client", "Config") host = {} if self.host: host = {'local_agent': {'reporting_host': self.host}} metrics_config = get_conf(service=get_service_name(service="metrics"), empty_init=True) metrics = "" if metrics_config: service_name = self.component_name.lower().replace("-", "_").replace( " ", "_") metrics = PrometheusMetricsFactory(service_name_label=service_name) config = Config(config={ **{ 'sampler': { 'type': 'const', 'param': 1, }, 'propagation': 'b3', 'logging': True }, **host }, service_name=self.component_name, metrics_factory=metrics, validate=True) return config.initialize_tracer()
def init_app(self, config, path): """ Initialize Connexion App. See more info in [Connexion Github](https://github.com/zalando/connexion) :param config: The Flask configuration defined in the config.yaml: ```yaml pyms: services: requests: true swagger: path: "" file: "swagger.yaml" config: <!-- DEBUG: true TESTING: false APP_NAME: "Python Microservice" APPLICATION_ROOT: "" ``` :param path: The current path where is instantiated Microservice class: ``` Microservice(path=__file__) ^^^^--- This param ``` :return: Flask """ check_package_exists("connexion") specification_dir = self.path application_root = self._get_application_root(config) if not os.path.isabs(self.path): specification_dir = os.path.join(path, self.path) app = connexion.App(__name__, specification_dir=specification_dir, resolver=RestyResolver(self.project_dir)) params = { "specification": self.get_bundled_specs( Path(os.path.join(specification_dir, self.file))) if prance else self.file, "arguments": { 'title': config.APP_NAME }, "base_path": application_root, "options": { "swagger_url": self.url }, } # Fix Connexion issue https://github.com/zalando/connexion/issues/1135 if application_root == "/": del params["base_path"] app.add_api(**params) # Invert the objects, instead connexion with a Flask object, a Flask object with application = app.app application.connexion_app = app return application
def init_app(self, config, path): check_package_exists("connexion") app = connexion.App(__name__, specification_dir=os.path.join(path, self.path), resolver=RestyResolver(self.project_dir)) app.add_api(self.file, arguments={'title': config.APP_NAME}, base_path=config.APPLICATION_ROOT, options={"swagger_url": self.url}) # Invert the objects, instead connexion with a Flask object, a Flask object with application = app.app application.connexion_app = app return application
def init_app(self) -> Flask: """Set attribute in flask `swagger`. See in `pyms.flask.services.swagger` how it works. If not set, run a "normal" Flask app. :return: None """ if self.swagger: application = self.swagger.init_app(config=self.config.to_flask(), path=self.path) else: check_package_exists("flask") application = Flask(__name__, static_folder=os.path.join(self.path, 'static'), template_folder=os.path.join(self.path, 'templates')) application.root_path = self.path # Fix connexion issue https://github.com/zalando/connexion/issues/527 application.wsgi_app = ReverseProxied(application.wsgi_app) return application
def init_app(self) -> Flask: """Set attribute in flask `swagger`. See in `pyms.flask.services.swagger` how it works. If not set, run a "normal" Flask app. :return: None """ if self._exists_service("swagger"): application = self.swagger.init_app(config=self.config.to_flask(), path=self.path) else: check_package_exists("flask") application = Flask( __name__, static_folder=os.path.join(self.path, 'static'), template_folder=os.path.join(self.path, 'templates')) application.root_path = self.path return application
def run(self): crypt = Crypt() if self.create_key: path = crypt._loader.get_path_from_env() # pylint: disable=protected-access pwd = self.get_input("Type a password to generate the key file: ") # Should use yes_no_input insted of get input below # the result should be validated for Yes (Y|y) rather allowing anything other than 'n' generate_file = self.get_input( "Do you want to generate a file in {}? [Y/n]".format(path)) generate_file = generate_file.lower() != "n" key = crypt.generate_key(pwd, generate_file) if generate_file: self.print_ok("File {} generated OK".format(path)) else: self.print_ok("Key generated: {}".format(key)) if self.encrypt: # Spoted Unhandle exceptions - The encrypt function throws FileDoesNotExistException, ValueError # which are not currently handled encrypted = crypt.encrypt(self.encrypt) self.print_ok("Encrypted OK: {}".format(encrypted)) if self.startproject: check_package_exists("cookiecutter") cookiecutter = import_from("cookiecutter.main", "cookiecutter") cookiecutter("gh:python-microservices/cookiecutter-pyms", checkout=self.branch) self.print_ok("Created project OK") if self.merge_swagger: try: merge_swagger_file(main_file=self.file) self.print_ok("Swagger file generated [swagger-complete.yaml]") except FileNotFoundError as ex: self.print_error(ex.__str__()) return False if self.create_config: use_requests = self.yes_no_input("Do you want to use request") use_swagger = self.yes_no_input("Do you want to use swagger") try: conf_file_path = create_conf_file(use_requests, use_swagger) self.print_ok(f'Config file "{conf_file_path}" created') return True except Exception as ex: self.print_error(ex.__str__()) return False return True
def run(self): crypt = Crypt() if self.create_key: path = crypt._loader.get_path_from_env() # pylint: disable=protected-access pwd = self.get_input('Type a password to generate the key file: ') generate_file = self.get_input('Do you want to generate a file in {}? [Y/n]'.format(path)) generate_file = generate_file.lower() != "n" key = crypt.generate_key(pwd, generate_file) if generate_file: self.print_ok("File {} generated OK".format(path)) else: self.print_ok("Key generated: {}".format(key)) if self.encrypt: encrypted = crypt.encrypt(self.encrypt) self.print_ok("Encrypted OK: {}".format(encrypted)) if self.startproject: check_package_exists("cookiecutter") cookiecutter = import_from("cookiecutter.main", "cookiecutter") cookiecutter('gh:python-microservices/cookiecutter-pyms', checkout=self.branch) self.print_ok("Created project OK") return True
def init_lightstep_tracer(self): check_package_exists("lightstep") lightstep = import_package("lightstep") return lightstep.Tracer(component_name=self.component_name)
def test_check_package_exists_exception(self): with pytest.raises(PackageNotExists) as excinfo: check_package_exists("this-package-not-exists") assert "this-package-not-exists is not installed. try with pip install -U this-package-not-exists" \ in str(excinfo.value)
def _init_boto(self): # pragma: no cover check_package_exists("boto3") boto3 = import_package("boto3") boto3.set_stream_logger(name='botocore') self.client = boto3.client('kms')
def _init_boto(self) -> None: # pragma: no cover check_package_exists("boto3") boto3 = import_package("boto3") boto3.set_stream_logger(name="botocore") self.client = boto3.client("kms")