Beispiel #1
0
 def HandleDotnetApiDbContext(self, dotnet_service, api_copy_folder):
     dbcontext_path = os.path.join(api_copy_folder, 'src', 'Data',
                                   'NameContext.cs')
     CamelCaseDbName = to_camelcase(dotnet_service['name']).replace(
         '.', '') + 'Context'
     if 'database' in dotnet_service:
         if os.path.exists(dbcontext_path):
             dbcontext_rename_path = os.path.join(api_copy_folder, 'src',
                                                  'Data',
                                                  CamelCaseDbName + '.cs')
             shutil.copy(dbcontext_path, dbcontext_rename_path)
             os.remove(dbcontext_path)
             replaceDict = {'NameContext': CamelCaseDbName}
             replace_template_file(dbcontext_rename_path, replaceDict)
     else:
         remove_data_folder_path = os.path.join(api_copy_folder, 'src',
                                                'Data')
         shutil.rmtree(remove_data_folder_path)
         rm_files = [
             'migrations.sh', 'updatedb.sh', 'dropdb.sh',
             'migrations.dev.sh', 'updatedb.dev.sh', 'dropdb.dev.sh'
         ]
         for rm_file in rm_files:
             rm_path = os.path.join(api_copy_folder, rm_file)
             os.remove(rm_path)
         docker_file = os.path.join(api_copy_folder, 'Dockerfile')
         filter_region_with_tag(docker_file, 'database')
Beispiel #2
0
 def HandleNodeJsDockerOptions(self,api_service_options,api_copy_folder):
     """
     ⚝ Configuring docker-compose options
         ① set port and replace in dockerfile
         ② if docker_compose_override is set, override default config
     """
     dockerfile_path = os.path.join(api_copy_folder,'src','Dockerfile')
     CamelCaseName = to_camelcase(api_service_options['name'])
     replace_dict = {}
     if 'port' in api_service_options:
         replace_dict['{{PORT}}'] =  str(api_service_options['port'])
     replace_template_file(dockerfile_path,replace_dict)
     docker_options = {
         'image': api_service_options['name'].lower(),
         'build': {
             'context': 'src/ApiServices/'+CamelCaseName+'/src',
             'dockerfile': 'Dockerfile'
         },
         'networks': ['localnet'],
         'ports': []
         
     }
     if 'port' in api_service_options:
         docker_options['ports'].append(str(api_service_options['port'])+':'+str(api_service_options['port']))
     if 'docker_compose_override' in api_service_options:
         docker_options.update(api_service_options['docker_compose_override'])  
     return docker_options
Beispiel #3
0
 def HandleEventBusForIs4(self, i_srv, is4_copy_folder):
     eventbus_srv = FindEventBusWithName(self.projectOptions,
                                         i_srv['eventbus']['provider'])
     startup_file_path = os.path.join(is4_copy_folder, 'src', 'Host',
                                      'Startup.cs')
     repleceDict = {
         '{{rabbitmq:host}}': 'rabbitmq://' + eventbus_srv['name'],
         '{{rabbitmq:host-dev}}': 'rabbitmq://localhost'
     }
     if 'docker_compose_override' in eventbus_srv:
         if 'environment' in eventbus_srv['docker_compose_override']:
             if 'RABBITMQ_DEFAULT_USER' in eventbus_srv[
                     'docker_compose_override']['environment']:
                 repleceDict['{{rabbitmq:user:username}}'] = eventbus_srv[
                     'docker_compose_override']['environment'][
                         'RABBITMQ_DEFAULT_USER']
             else:
                 repleceDict['{{rabbitmq:user:username}}'] = 'doom'
             if 'RABBITMQ_DEFAULT_PASSWORD' in eventbus_srv[
                     'docker_compose_override']['environment']:
                 repleceDict['{{rabbitmq:user:password}}'] = eventbus_srv[
                     'docker_compose_override']['environment'][
                         'RABBITMQ_DEFAULT_PASSWORD']
             else:
                 repleceDict['{{rabbitmq:user:password}}'] = 'machine'
         else:
             repleceDict['{{rabbitmq:user:username}}'] = 'doom'
             repleceDict['{{rabbitmq:user:password}}'] = 'machine'
     else:
         repleceDict['{{rabbitmq:user:username}}'] = 'doom'
         repleceDict['{{rabbitmq:user:password}}'] = 'machine'
     replace_template_file(startup_file_path, repleceDict)
Beispiel #4
0
 def HandleConnectionStringForIs4(self, identity_options, is4_copy_folder):
     conn_strings = self.BuildConnStringForIs4(identity_options)
     startup_file_path = os.path.join(is4_copy_folder, 'src', 'Host',
                                      'Startup.cs')
     replace_dict = {
         '{{database:usersconnectionstring-dev}}':
         conn_strings['user_connection_string_dev'],
         '{{database:usersconnectionstring}}':
         conn_strings['user_connection_string'],
         '{{database:configsConnectionString-dev}}':
         conn_strings['config_connection_string_dev'],
         '{{database:configsConnectionString}}':
         conn_strings['config_connection_string']
     }
     replace_template_file(startup_file_path, replace_dict)
Beispiel #5
0
 def HandleNodeJsAuthorization(self,api_service_options,api_copy_folder):
     """
     ⚝ If authorization not enabled
         ① filter between //& region (authorization) in files [app.js]
         ② remove authorize middleware file
         ③ remove relevant packages from package.json
         ④ remove auth test controller
     ⚝ If authorization enabled:
         ① filter App.js For authorization:{identity_provider}
         ② replace relevant configuration in authorize middleware file
     """
     app_js_file_path = os.path.join(api_copy_folder,'src','app.js')
     env_file_path = os.path.join(api_copy_folder,'src','.env')
     authorize_middleware_file_path = os.path.join(api_copy_folder,'src','middlewares','authorize.js')
     package_json_file_path =  os.path.join(api_copy_folder,'src','package.json')
     auth_test_route_file_path =  os.path.join(api_copy_folder,'src','controllers','authtest.js')
     authorization_enabled = 'authorization' in api_service_options
     if (authorization_enabled):
         identity_instance = FindIdentityServiceWithName(self.projectOptions, api_service_options['authorization']['issuer'])
         identity_instance_type = identity_instance['type']
         if identity_instance_type == 'identityserver4':
             # Filter App.js For authorization:identityserver4
             filter_sub_region(app_js_file_path,'database',identity_instance_type)
             # Configure authorize middleware
             replace_dict = {
                 '{{issuer_host_dev}}': str.lower(identity_instance['name'])+'.localhost',
                 '{{issuer_host}}': 'http://localhost:'+str(identity_instance['port'])
             }
             if 'secrets' in api_service_options['authorization']:
                 if len(api_service_options['authorization']['secrets']) > 0:
                     replace_dict['{{api_secret}}'] = api_service_options['authorization']['secrets'][0]
                 else:
                     replace_dict['{{api_secret}}'] = 'secret'
             else:
                 replace_dict['{{api_secret}}'] = 'secret'
             replace_template_file(authorize_middleware_file_path,replace_dict)
     else:
         # Filter App.py For Authorization
         filter_region_with_tag(app_js_file_path,'authorization')
         # Remove authorize middleware file
         if os.path.isfile(authorize_middleware_file_path):
             os.remove(authorize_middleware_file_path)
         if os.path.isfile(auth_test_route_file_path):
             os.remove(auth_test_route_file_path)
Beispiel #6
0
    def HandleNodeEnvironmentForAuthConfig(self, client_options, copy_folder):

        environment_dev_path = os.path.join(copy_folder, 'src', 'environments',
                                            'environment.ts')
        environment_prod_path = os.path.join(copy_folder, 'src',
                                             'environments',
                                             'environment.prod.ts')
        # filter if auth not added
        if 'authorization' not in client_options:
            filter_region_with_tag(environment_dev_path, 'authorization')
            filter_region_with_tag(environment_prod_path, 'authorization')
        else:
            identity_instance = FindIdentityServiceWithName(
                self.projectOptions, client_options['authorization']['issuer'])
            identity_type = identity_instance['type']
            prod_replace_dict = {
                '{{auth:stsServer}}':
                'http://' + identity_instance['name'].lower() + '.localhost',
                '{{auth:clientUrl}}':
                'http://' + client_options['name'].lower() + '.localhost',
                '{{auth:client_id}}':
                client_options['name']
            }
            dev_replace_dict = {
                '{{auth:stsServer}}':
                'http://localhost:' + str(identity_instance['port']),
                '{{auth:clientUrl}}':
                'http://localhost:' + str(client_options['port']),
                '{{auth:client_id}}':
                client_options['name'] + 'dev'
            }
            if 'scopes' in client_options['authorization']:
                prod_replace_dict['{{auth:scope}}'] = " ".join(
                    client_options['authorization']['scopes'])
                dev_replace_dict['{{auth:scope}}'] = " ".join(
                    client_options['authorization']['scopes'])
            else:
                prod_replace_dict[
                    '{{auth:scope}}'] = 'openid profile email'  # default scope values
                dev_replace_dict['{{auth:scope}}'] = 'openid profile email'
            replace_template_file(environment_dev_path, dev_replace_dict)
            replace_template_file(environment_prod_path, prod_replace_dict)
Beispiel #7
0
    def HandleCSharpEventbus(self, service_options, sharp_file_path):
        eventbus_enabled = 'eventbus' in service_options
        
        if(eventbus_enabled):
            eb_replace_dict = {}
            
            eventbus_instance = FindEventBusWithName(self.projectOptions, service_options['eventbus']['provider'])

            if eventbus_instance['type'] == 'rabbitmq':
                eb_replace_dict['{{rabbitmq:host}}'] = 'rabbitmq://'+eventbus_instance['name']
                eb_replace_dict['{{rabbitmq:host-dev}}'] = 'localhost'
                eb_replace_dict['{{rabbitmq:user:username}}'] = 'doom'
                eb_replace_dict['{{rabbitmq:user:password}}'] = 'machine'
                if 'docker_compose_override' in eventbus_instance:
                    if 'envoronment' in eventbus_instance['docker_compose_override']:
                        eb_replace_dict['{{rabbitmq:user:username}}'] = eventbus_instance['docker_compose_override']['environment']['RABBITMQ_DEFAULT_USER']
                        eb_replace_dict['{{rabbitmq:user:password}}'] = eventbus_instance['docker_compose_override']['environment']['RABBITMQ_DEFAULT_PASSWORD']
            replace_template_file(sharp_file_path,eb_replace_dict)
            eventbus_type = eventbus_instance['type']
            filter_sub_region(sharp_file_path,'eventbus',eventbus_type)
        else:
            filter_region_with_tag(sharp_file_path,'eventbus')
Beispiel #8
0
 def HandleDotnetApiDockerFile(self, dotnet_service, api_copy_folder):
     docker_file_path = os.path.join(api_copy_folder, 'Dockerfile')
     docker_replace_dict = {}
     docker_replace_dict['{{port}}'] = str(dotnet_service['port'])
     docker_replace_dict['{{project_name}}'] = to_camelcase(
         dotnet_service['name'])
     replace_template_file(docker_file_path, docker_replace_dict)
     if 'database' in dotnet_service:
         ef_shell_replace_dict = {
             '{{ProjectName}}':
             to_camelcase(dotnet_service['name']),
             '{{DatabaseContextName}}':
             to_camelcase(dotnet_service['name']).replace('.', '') +
             'Context'
         }
         shell_file_paths = [
             'migrations.sh', 'updatedb.sh', 'dropdb.sh',
             'migrations.dev.sh', 'updatedb.dev.sh', 'dropdb.dev.sh'
         ]
         for path in shell_file_paths:
             f_path = os.path.join(api_copy_folder, path)
             replace_template_file(f_path, ef_shell_replace_dict)
Beispiel #9
0
    def HandleNodeJsData(self, api_service_options,api_copy_folder):

        """
        ⚝ If Database not enabled
            ① remove all relevant packages from packages.json
            ② filter app.js between (database) tag
            ③ remove models created for mongoose
            ④ remove data folder (sequelize)
            ⑤
            ⑥
            ⑦
            ⑧
            ⑨
            ⑩
        ⚝ If Database is enabled
            ⚝ If Database is mongodb
                ① remove all other database packages(sequelize) from packages.json
                ② filter app.js between (database) tag which is not subtagged mongodb
                ③ set connection strings in app.js
                ④ remove data folder of sequelize
                ⑤ remove controllers of sequelize data entpoints⑦
                ⑧
                ⑨
                ⑩
            ⚝ If Database is postgre or mysql or sqlite
                ① remove all other database packages(mongoose) from packages.json
                ② filter app.js between (database) tag which is not subtagged postgre,sqlite,mysql
                ③ set connection strings in .env file
                ④ remove relevant file/folders for mongodb
                ⑤ remove controllers of mongodb data entpoints
                ⑥ remove .sequelizerc file
                ⑦
                ⑧
                ⑨
                ⑩
        """

        app_js_file_path = os.path.join(api_copy_folder,'src','app.js')
        env_file_path = os.path.join(api_copy_folder,'src','.env')
        models_folder_path =  os.path.join(api_copy_folder,'src','models')
        package_json_file_path =  os.path.join(api_copy_folder,'src','package.json')
        db_entity_route_file_path =  os.path.join(api_copy_folder,'src','controllers','entity.js')
        postgre_entity_folder = os.path.join(api_copy_folder,'src','postgre')
        mongo_db_packages = ['mongoose']
        sequelize_db_packages = ['sequelize']
        database_enabled = 'database' in api_service_options
        if (database_enabled):        
            database_provider = api_service_options['database']['provider']
            database_instance = FindDatabaseWithName(self.projectOptions, database_provider)
            filter_sub_region(app_js_file_path,'database',database_instance['type'])
            if database_instance['type'] == 'mongodb':
                connection_string, connection_string_dev = GetConnectionString(api_service_options, database_instance)
                replace_dict = {
                    '{{mongoose_connection_dev}}': connection_string_dev,
                    '{{mongoose_connection}}': connection_string
                }
                replace_template_file(app_js_file_path,replace_dict)
            else:
                RemovePackagesFromJson(package_json_file_path, mongo_db_packages)
                if os.path.isfile(db_entity_route_file_path):
                    os.remove(db_entity_route_file_path)
            if database_instance['type'] == 'postgresql' or database_instance['type'] == 'mysql':
                username,password = GetDatabaseUsernameAndPassword(database_instance)
                env_replace_dict = {
                    '{{database:host}}': api_service_options['database']['database_name'],
                    '{{database:user}}': username,
                    '{{database:password}}': password
                }
                replace_template_file(env_file_path,env_replace_dict)
            else:
                RemovePackagesFromJson(package_json_file_path, sequelize_db_packages)
                sequelizerc_file_path = os.path.join(api_copy_folder,'src','.sequelizerc')
                if os.path.isfile(sequelizerc_file_path):
                    os.remove(sequelizerc_file_path)
                if os.path.isdir(postgre_entity_folder):
                    shutil.rmtree(postgre_entity_folder)
        else:        
            filter_region_with_tag(app_js_file_path,'database')
            if os.path.isdir(models_folder_path):
                shutil.rmtree(models_folder_path,ignore_errors=True)    
            RemovePackagesFromJson(package_json_file_path,mongo_db_packages+sequelize_db_packages)
Beispiel #10
0
 def DockerComposeFinalization(self, file):
     replace_dict = {
         'rabbitmq:healtcheck':
         '["CMD", "curl", "-f", "http://localhost:15672"]'
     }
     replace_template_file(file, replace_dict)
Beispiel #11
0
 def HandleIs4DockerFile(self, identity_service, is4_copy_folder):
     docker_file_path = os.path.join(is4_copy_folder, 'Dockerfile')
     docker_replace_dict = {}
     docker_replace_dict['{{port}}'] = str(identity_service['port'])
     replace_template_file(docker_file_path, docker_replace_dict)
Beispiel #12
0
 def ReplaceDotnetNameSpaces(self, file_paths, namespace_name, replace_name):
     replace_dict = {}
     replace_dict[namespace_name] = replace_name
     for file in file_paths:
         if os.path.exists(file):
             replace_template_file(file,replace_dict)
Beispiel #13
0
    def HandleDotnetApiStartup(self, dotnet_service, api_copy_folder):
        print('Handle DotnetApi Startup.cs File')
        api_startup_path = os.path.join(api_copy_folder, 'src', 'Startup.cs')

        self.csharp_templater.HandleCSharpDatabase(dotnet_service,
                                                   api_startup_path)
        self.csharp_templater.HandleCSharpCache(dotnet_service,
                                                api_startup_path)
        self.csharp_templater.HandleCSharpEventbus(dotnet_service,
                                                   api_startup_path)
        self.csharp_templater.HandleCSharpLogging(dotnet_service,
                                                  api_startup_path)
        self.csharp_templater.HandleCSharpServer(dotnet_service,
                                                 api_startup_path)
        self.csharp_templater.HandleCSharpSwagger(dotnet_service,
                                                  api_startup_path)
        # Set DBContext Name
        CamelCaseName = to_camelcase(dotnet_service['name'])
        replaceDict = {
            'NameContext': CamelCaseName.replace('.', '') + 'Context'
        }
        if 'database' in dotnet_service:

            conn_string, conn_string_dev = self.BuildConnStringForDotnetApi(
                dotnet_service)
            replaceDict['{{database:connectionString}}'] = conn_string
            replaceDict['{{database:connectionString-dev}}'] = conn_string_dev

        if 'cache' in dotnet_service:
            if dotnet_service['cache']['type'] == 'redis':
                redis_instance = FindDatabaseWithName(
                    self.projectOptions,
                    dotnet_service['cache']['redis_options']['redis_server'])
                if redis_instance is None:
                    print(
                        'Warning: Redis instance could not found. Configuration left default'
                    )
                else:
                    redis_conn_string, redis_conn_string_dev = BuildRedisConnectionString(
                        redis_instance)
                    replaceDict[
                        '{{redis_options:connection}}'] = redis_conn_string
                    replaceDict[
                        '{{redis_options:connection-dev}}'] = redis_conn_string_dev
                    if 'redis_instance_name' in dotnet_service['cache'][
                            'redis_options']:
                        replaceDict[
                            '{{redis_options:instance_name}}'] = dotnet_service[
                                'cache']['redis_options'][
                                    'redis_instance_name']
        if 'authorization' in dotnet_service:
            issuer = dotnet_service['authorization']['issuer']
            if issuer is None:
                print('Error: Identity Issuer for ' + dotnet_service['name'] +
                      ' is required')
            identity_instance = FindIdentityServiceWithName(
                self.projectOptions, issuer)
            if identity_instance is None:
                print('Error: Identity Service Instance for ' +
                      dotnet_service['name'] + ' could not found')
            else:
                replaceDict['{{authorization:api_name}}'] = dotnet_service[
                    'name']
                replaceDict['{{authorization:authority}}'] = str.lower(
                    identity_instance['name']) + '.localhost'
                replaceDict[
                    '{{authorization:authority-dev}}'] = 'http://localhost:' + str(
                        identity_instance['port'])
                if 'api_secret' in dotnet_service['authorization']:
                    replaceDict[
                        '{{authorization:api_secret}}'] = dotnet_service[
                            'authorization']['secrets'][0]
                else:
                    # Set Default Secret
                    replaceDict['{{authorization:api_secret}}'] = 'secret'

        replace_template_file(api_startup_path, replaceDict)
Beispiel #14
0
 def HandleDockerfileForAngularSsr(self, client_options, copy_folder):
     dockerfile_path = os.path.join(copy_folder, 'Dockerfile')
     replace_dict = {'{{PORT}}': str(client_options['port'])}
     replace_template_file(dockerfile_path, replace_dict)