def _validate_and_prepare(self, properties, arguments): prepared_args = {} host = arguments.get('host') Fail.fail_on_no_host(host) Fail.fail_on_invalid_host(host, properties) prepared_args['host'] = host environment = arguments.get('environment') default_environment = properties.get('default_environment') environment = Ora.nvl(environment, default_environment) Fail.fail_on_invalid_environment(environment, properties) prepared_args['environment'] = environment alias = arguments.get('alias') Fail.fail_on_invalid_alias(alias, properties) # if an alias is given, only this database will be installed, other databases will be # ignored. if alias: print("using alias: {}".format(alias)) prepared_args['databases'] = [alias] else: database = arguments.get('database') Fail.fail_on_invalid_database(database, properties) default_databases = properties.get('databases') databases = Ora.nvl(database, default_databases) prepared_args['databases'] = databases return prepared_args
def execute(self, properties, arguments): """ Drop a database after checking if schema and environment are valid values. Also check that host is not on the block list. :type properties: system.Properties.Properties :param properties: The project properties :type arguments: dict :param arguments: This dict contains the plugin arguments: * **host**: The hostname to drop on; * **environment**: Environment to drop the database from. """ prepared_args = self._validate_and_prepare(properties, arguments) host = prepared_args['host'] environment = prepared_args['environment'] databases = properties.get('databases') objects = properties.get('drop_objects') # retrieve the user credentials for this database project. users = properties.get('postgresql_users') # try to retrieve the users from the credentials file, when no users are configured in # myproject.json. if not users: # retrieve the name of this database project, introduced in version 1.0.12 profile = PropertyHelper.get_profile(properties) if profile: users = profile.get('postgresql_users') # fail when no users are found. This means that they are not set in myproject.json or # credentials.json Fail.fail_on_no_users(users) for database in databases: print( "dropping database '{db}' on host '{host}' using environment '{env}'" .format(db=database, host=host, env=environment)) executor = PropertyHelper.get_postgres_properties( users, host, database) connector = self.get_connector() for obj in objects: folder = File( os.path.join(properties.get('plugin.dir'), 'postgresql', 'drop', obj)) ConnectionExecutor.execute(connector, executor, properties, folder) print("database '{}' dropped".format(database))
def _validate_and_prepare(self, properties, arguments): # If no hostname was provided, we only set the version and then return prepared_args = {} version = arguments.get('version') if version: prepared_args['version'] = version host = arguments.get('host') if not host: return prepared_args project_file = properties.get('project.file') current_dir = properties.get('current.dir') # Determine the project's name and create a new directory database = arguments.get('database') Fail.fail_on_no_database(database) project = database + "-db" # First check the rest of the arguments before we continue port = arguments.get('port') Fail.fail_on_no_port(port) schema = arguments.get('schema') Fail.fail_on_no_schema(schema) username = arguments.get('username') Fail.fail_on_no_user('username') password = arguments.get('password') # Create the project directory os.mkdir(project) # Get the json project template for this plugin template_file = os.path.join(properties.get('plugin.dir'), 'mssql', 'generate', 'templates', project_file) # Parse the template and replace the parameters with open(template_file) as fd: stream = fd.read() stream = stream.replace('{project}', project) stream = stream.replace('{database}', database) stream = stream.replace('{host}', host) stream = stream.replace('{port}', str(port)) stream = stream.replace('{schema}', schema) stream = stream.replace('{username}', username) stream = stream.replace('{password}', password) stream = stream.replace('{version}', version) # Write the new configuration back to file config_file = os.path.join(current_dir, project, project_file) with open(config_file, 'w') as fd: fd.write(stream) # Update the project properties to set current.dir to the new project dir properties['current.dir'] = os.path.join(current_dir, project) # Update the project properties properties.update_config() return prepared_args
def main(args=None): properties = Properties() properties.set_property("noora.dir", NOORA_DIR) properties.set_property("current.dir", CURRENT_DIR) properties.set_property("plugin.dir", os.path.join(NOORA_DIR, 'plugins')) properties.set_property("project.file", "myproject.json") # find the project config file myproject.json, in the current folder or in the noora folder app = App() f = app.get_config_file(properties) f = open(f.get_url()) data = json.load(f) for key in data.keys(): properties.set_property(key, data[key]) # Instantiate the argument parser parser = argparse.ArgumentParser(description="mynoora, a sql deployment tool", add_help=False) parser.add_argument("commands", help="display a square of a given number", type=str, nargs='+') parser.add_argument('-r', action='store_true', help='show the revision') parser.add_argument('-v', type=str, help='version', required=False) parser.add_argument('-h', type=str, help='host', required=False) parser.add_argument('-d', type=str, help='database', required=False) parser.add_argument('-e', type=str, help='environment', required=False) parser.add_argument('-a', type=str, help='alias', required=False) parser.add_argument('-s', type=str, help='schema', required=False) parser.add_argument('-t', type=str, help='technology', required=False) args = parser.parse_args(args) # show the revision if args.r: print noora.__title__ + " version " + noora.__version__ exit(0) # execute the given commands commands = args.commands Fail.fail_on_no_command(commands) for command in commands: plugin = App.find_plugin(command, properties) Fail.fail_on_invalid_plugin(plugin) plugin.execute(args, properties)
def _validate_and_prepare(self, properties, arguments): prepared_args = {} host = arguments.get('host') Fail.fail_on_no_host(host) Fail.fail_on_invalid_host(host, properties) Fail.fail_on_blocked_hosts(host, properties) prepared_args['host'] = host environment = arguments.get('environment') default_environment = properties.get('default_environment') environment = Ora.nvl(environment, default_environment) Fail.fail_on_invalid_environment(environment, properties) prepared_args['environment'] = environment return prepared_args
def execute(self, arguments, properties): host = arguments.h Fail.fail_on_no_host(host) self.fail_on_blocked_hosts(host, properties) default_databases = properties.get_property('databases') databases = Ora.nvl(arguments.d, default_databases) Fail.fail_on_invalid_database(arguments.d, properties) default_environment = properties.get_property('default_environment') environment = Ora.nvl(arguments.e, default_environment) Fail.fail_on_invalid_environment(arguments.e, properties) objects = properties.get_property('drop_objects') alias = arguments.a Fail.fail_on_invalid_alias(arguments.a, properties) # if an alias is given, only the alias database will be installed, other databases will be ignored. if alias: print "using alias :" + alias databases = [alias] for database in databases: print "dropping database '" + database + "' on host '" + host + "' using environment '" + environment + "'" users = properties.get_property('mysql_users') username = PropertyHelper.get_mysql_user(users, host, database) password = PropertyHelper.get_mysql_passwd(users, host, database) connector = self.get_connector() executor = {} executor['host'] = host executor['database'] = database executor['username'] = username executor['password'] = password for object in objects: folder = File( os.path.join(self.get_drop_dir(properties), object)) ConnectionExecutor.execute(connector, executor, properties, folder) print "database '" + database + "' dropped."
def _validate_and_prepare(self, properties, arguments): prepared_args = {} version = arguments.get('version') Fail.fail_on_no_version(version) Fail.fail_on_unknown_version(version, properties) prepared_args['version'] = version host = arguments.get('host') Fail.fail_on_no_host(host) prepared_args['host'] = host environment = arguments.get('environment') default_environment = properties.get('default_environment') environment = Ora.nvl(environment, default_environment) Fail.fail_on_invalid_environment(environment, properties) prepared_args['environment'] = environment return prepared_args
def _validate_and_prepare(self, properties, arguments): prepared_args = {} version = arguments.get('version') Fail.fail_on_no_version(version) Fail.fail_on_unknown_version(version, properties) prepared_args['version'] = version database = arguments.get('database') default_databases = properties.get('databases') databases = Ora.nvl(database, default_databases) Fail.fail_on_invalid_database(database, properties) prepared_args['databases'] = databases return prepared_args
def execute(self, properties, arguments): """ Create a new database instance for the initial version. :type properties: system.Properties.Properties :param properties: The project properties :type arguments: dict :param arguments: This dict contains the plugin arguments: * **host**: The hostname where the database is running; * **environment**: The environment to create the database in (optional). """ prepared_args = self._validate_and_prepare(properties, arguments) host = prepared_args['host'] environment = prepared_args['environment'] databases = properties.get('databases') objects = properties.get('create_objects') # retrieve the user credentials for this database project. users = properties.get('postgresql_users') # try to retrieve the users from the credentials file, when no users are configured in # myproject.json. if not users: # retrieve the name of this database project, introduced in version 1.0.12 profile = PropertyHelper.get_profile(properties) if profile: users = profile.get('postgresql_users') # fail when no users are found. This means that they are not set in myproject.json or # credentials.json Fail.fail_on_no_users(users) connector = self.get_connector() create_dir = properties.get('create.dir') for database in databases: print("creating database '{db}' on host '{host}' using environment '{env}'".format( db=database, host=host, env=environment)) executor = PropertyHelper.get_postgres_properties(users, host, database) for obj in objects: # global ddl objects folder = File(os.path.join(create_dir, database, 'ddl', obj)) ConnectionExecutor.execute(connector, executor, properties, folder) # environment specific ddl objects folder = File(os.path.join(create_dir, database, 'ddl', obj, environment)) ConnectionExecutor.execute(connector, executor, properties, folder) # global dat objects folder = File(os.path.join(create_dir, database, 'dat')) ConnectionExecutor.execute(connector, executor, properties, folder) # environment specific dat objects folder = File(os.path.join(create_dir, database, 'dat', environment)) ConnectionExecutor.execute(connector, executor, properties, folder) print("database '{}' created.".format(database))
def execute(self, arguments, properties): properties.set_property('create.dir', os.path.join(properties.get_property('current.dir'), 'create')) properties.set_property('alter.dir', os.path.join(properties.get_property('current.dir'), 'alter')) version = arguments.v Fail.fail_on_no_version(version) Fail.fail_on_unknown_version(version, properties) default_databases = properties.get_property('databases') databases = Ora.nvl(arguments.d, default_databases) Fail.fail_on_invalid_database(arguments.d, properties) current_dir = properties.get_property('current.dir') component_name = properties.get_property('component_name') target_dir = os.path.join(current_dir, properties.get_property('component_target_folder')) objects = properties.get_property('create_objects') build_dir = App.build_dir(version, properties) # exclude the file 'version.sql', this file is excluded from the dat listing below. component_excluded_files = properties.get_property('component_excluded_files') excluded_files = properties.get_property('excluded_files') excluded_files.extend(component_excluded_files) # create the target folder, if not present. if not File(target_dir).exists(): os.makedirs(target_dir) print "building component with version '" + version + "'" zip_file = os.path.join(target_dir, component_name + '_' + version + '.zip') zip_handle = ZipFile(zip_file, 'w') for database in databases: for object in objects: if not object == 'lib': # global ddl objects folder = File(os.path.join(build_dir, database, 'ddl', object)) zip_dir = File(os.path.join(component_name + '_' + version, 'ddl', object)) files = Files.list_filtered(folder, properties) for file in files: print file.get_url() target_file = File(os.path.join(zip_dir.get_url(), file.tail())) zip_handle.write(file.get_url(), target_file.get_url(), ZIP_DEFLATED) # global dat files folder = File(os.path.join(build_dir, database, 'dat')) zip_dir = File(os.path.join(component_name + '_' + version, 'dat')) files = Files.list_filtered(folder, properties) for file in files: print file.get_url() target_file = File(os.path.join(zip_dir.get_url(), file.tail())) zip_handle.write(file.get_url(), target_file.get_url(), ZIP_DEFLATED) # create the version script in the dat folder version_statement = self.version_statement(version, properties) version_statement = version_statement.replace('<version>', version) version_statement = version_statement.replace('<name>', component_name) f = open('version.sql', 'w') f.write(version_statement) f.close() zip_dir = File(os.path.join(component_name + '_' + version, 'dat')) target_file = File(os.path.join(zip_dir.get_url(), 'version.sql')) zip_handle.write('version.sql', target_file.get_url()) # remove the version.sql file. os.remove('version.sql') # create the checkversion script in the root folder # first retrieve the previous version for the checkversion script versions = Versions() version_loader = VersionLoader(versions) version_loader.load(properties) versions.sort() previous = versions.previous(Version(version)) component_select_statement = properties.get_property("component_select_statement") component_select_statement = component_select_statement.replace('<name>', component_name) component_select_statement = component_select_statement.replace('<previous>', previous.get_value()) print component_select_statement f = open('checkversion.sql', 'w') f.write(component_select_statement) f.close() zip_dir = File(os.path.join(component_name + '_' + version)) target_file = File(os.path.join(zip_dir.get_url(), 'checkversion.sql')) zip_handle.write('checkversion.sql', target_file.get_url()) # remove the version.sql file. os.remove('checkversion.sql') zip_handle.close() print "component with version " + version + " created."
def execute(self, arguments, properties): properties.set_property( 'create.dir', os.path.join(properties.get_property('current.dir'), 'create')) properties.set_property( 'alter.dir', os.path.join(properties.get_property('current.dir'), 'alter')) host = arguments.h Fail.fail_on_no_host(host) version = arguments.v Fail.fail_on_no_version(version) default_databases = properties.get_property('databases') databases = Ora.nvl(arguments.d, default_databases) Fail.fail_on_invalid_database(arguments.d, properties) default_environment = properties.get_property('default_environment') environment = Ora.nvl(arguments.e, default_environment) Fail.fail_on_invalid_environment(arguments.e, properties) objects = properties.get_property('create_objects') version_database = properties.get_property('version_database') alter_dir = properties.get_property('alter.dir') self.fail_on_unknown_version(version, properties) alias = arguments.a database_aliases = properties.get_property('database_aliases') Fail.fail_on_invalid_alias(alias, properties) # if an alias is given, only this database will be installed, other databases will be ignored. if alias: print "using alias :" + alias databases = [alias] connector = self.get_connector() for database in databases: print "updating database '" + database + "' on host '" + host + "' using environment '" + environment + "'" users = properties.get_property('mysql_users') username = PropertyHelper.get_mysql_user(users, host, database) password = PropertyHelper.get_mysql_passwd(users, host, database) executor = {} executor['host'] = host executor['database'] = database executor['username'] = username executor['password'] = password database_folder = PropertyHelper.get_database_folder( database, database_aliases) if database == version_database: self.fail_on_invalid_environment(connector, executor, environment, properties) self.fail_on_invalid_version(connector, executor, version, properties) for object in objects: # global ddl objects folder = File( os.path.join(alter_dir, version, database_folder, 'ddl', object)) ConnectionExecutor.execute(connector, executor, properties, folder) # environment specific ddl objects folder = File( os.path.join(alter_dir, version, database_folder, 'ddl', object, environment)) ConnectionExecutor.execute(connector, executor, properties, folder) # global dat objects folder = File( os.path.join(alter_dir, version, database_folder, 'dat')) ConnectionExecutor.execute(connector, executor, properties, folder) # environment specific dat objects folder = File( os.path.join(alter_dir, version, database_folder, 'dat', environment)) ConnectionExecutor.execute(connector, executor, properties, folder) print "database '" + database + "' updated."
def execute(self, arguments, properties): properties.set_property('create.dir', os.path.join(properties.get_property('current.dir'), 'create')) host = arguments.h Fail.fail_on_no_host(host) default_schemes = properties.get_property('schemes') schemes = Ora.nvl(arguments.s, default_schemes) Fail.fail_on_invalid_schema(arguments.s, properties) default_environment = properties.get_property('default_environment') environment = Ora.nvl(arguments.e, default_environment) Fail.fail_on_invalid_environment(arguments.e, properties) database = properties.get_property('database') objects = properties.get_property('create_objects') # # alias = arguments.a # database_aliases = properties.get_property('database_aliases') # Fail.fail_on_invalid_alias(alias, properties) # # # if an alias is given, only this database will be installed, other databases will be ignored. # if alias: # print "using alias :" + alias # databases = [alias] connector = self.get_connector() create_dir = properties.get_property('create.dir') for schema in schemes: print "creating schema '" + schema + "' in database '" + database + "' on host '" + host + "' using environment '" + environment + "'" users = properties.get_property('mssql_users') username = PropertyHelper.get_mssql_user(users, host, schema) password = PropertyHelper.get_mssql_password(users, host, schema) executor = {} executor['host'] = host executor['database'] = database executor['username'] = username executor['password'] = password # database_folder = PropertyHelper.get_database_folder(database, database_aliases) for object in objects: # global ddl objects folder = File(os.path.join(create_dir, schema, 'ddl', object)) ConnectionExecutor.execute(connector, executor, properties, folder) # environment specific ddl objects folder = File(os.path.join(create_dir, schema, 'ddl', object, environment)) ConnectionExecutor.execute(connector, executor, properties, folder) # global dat objects folder = File(os.path.join(create_dir, schema, 'dat')) ConnectionExecutor.execute(connector, executor, properties, folder) # environment specific dat objects folder = File(os.path.join(create_dir, schema, 'dat', environment)) ConnectionExecutor.execute(connector, executor, properties, folder) print "schema '" + schema + "' created."