def __PrintConfiguration(self, database): print '>> Database' print ' Name: ' + database.Name print ' Server: ' + database.Server print ' User: '******' Password: '******' Once: ' + str(database.CreateOnce) print ' Driver: ' + database.Driver print ' SQL files:' for script in database.Scripts: print ' SQL file: ' + DeployUtilities.ExpandEnvironmentVariables( script) print ' Hooks:' for hook in database.Hooks: print ' Hook: ' + hook.Executable print ' Arguments: ' + hook.Arguments print ' BeforeDrop: ' + str(hook.BeforeDrop) print ' Modules:' for uri, module in database.Modules.iteritems(): print ' Module: ' + uri + ', ' + str(module) module.PrintConfiguration(database)
def PopulateDatabase(self, configuration): print 'Populating database %s on %s' % (configuration.Name, configuration.Server) environmentVariables = {} environmentVariables['PGPASSWORD'] = configuration.Password for encodedScriptPath in configuration.Scripts: scriptPath = DeployUtilities.ExpandEnvironmentVariables( encodedScriptPath) DeployUtilities.RunExternalCommand( 'psql', '-q -d %s -h %s -U %s -f %s' % (configuration.Name, configuration.Server, configuration.UserName, encodedScriptPath), environmentVariables=environmentVariables) for hook in configuration.Hooks: if hook.BeforeDrop is False: try: arguments = '%s "%s"' % (hook.Arguments, configuration.ConnectionString) DeployUtilities.RunExternalCommand(hook.Executable, arguments) except System.ComponentModel.Win32Exception: print 'Could not open "%s".' % (hook.Executable) raise else: print 'Ran hook "%s %s %s"' % ( hook.Executable, configuration.ConnectionString, hook.Arguments) for module in configuration.Modules.values(): module.PopulateDatabase(configuration)
def DropDatabase(self, configuration): clr.AddReferenceToFileAndPath(configuration.NpgsqlLocation) import Npgsql if not self.DatabaseExists(configuration): return for hook in configuration.Hooks: if hook.BeforeDrop is True: try: arguments = '%s "%s"' % (hook.Arguments, configuration.ConnectionString) DeployUtilities.RunExternalCommand(hook.Executable, arguments) except System.ComponentModel.Win32Exception: print 'Could not open "%s".' % (hook.Executable) raise else: print 'Ran hook [%s %s "%s"]' % ( hook.Executable, hook.Arguments, configuration.ConnectionString) connection = Npgsql.NpgsqlConnection() connection.ConnectionString = "Server=%s;Port=5432;User ID=%s;Password=%s;SSL=True;Sslmode=Require;Database=postgres" % ( configuration.Server, configuration.UserName, configuration.Password) try: connection.Open() try: command = connection.CreateCommand() commandText = [] commandText.append('drop database "%s"' % (configuration.Name)) command.CommandText = ''.join(commandText) command.ExecuteNonQuery() print 'Dropping database %s on %s' % (configuration.Name, configuration.Server) except System.Exception, e: print e command.Dispose() raise else:
def DropDatabase(self, configuration): for hook in configuration.Hooks: if hook.BeforeDrop is True: try: arguments = '%s "%s"' % (hook.Arguments, configuration.ConnectionString) DeployUtilities.RunExternalCommand(hook.Executable, arguments) except System.ComponentModel.Win32Exception: print 'Could not open "%s".' % (hook.Executable) raise else: print 'Ran hook [%s %s "%s"]' % ( hook.Executable, hook.Arguments, configuration.ConnectionString) connection = System.Data.SqlClient.SqlConnection() connection.ConnectionString = "Integrated Security=SSPI;Database=master;Server=%s" % ( configuration.Server) try: connection.Open() try: command = connection.CreateCommand() commandText = [] commandText.append( "if exists (select * from sys.databases where name = N'%s') " % (configuration.Name)) commandText.append("drop database %s" % (configuration.Name)) command.CommandText = ''.join(commandText) command.ExecuteNonQuery() print 'Dropping database %s on %s' % (configuration.Name, configuration.Server) except Exception, detail: print detail command.Dispose() raise else:
def Execute(self, configuration, action): try: if not hasattr(configuration, 'Websites'): return for website in configuration.Websites: if action.Workflow & Action.OnlyMe: if action.Name != website.Name: print 'Skipping "%s" because of --only-me=%s option ...' % ( website.Name, action.Name) continue if action.Workflow & Action.DeleteWebsite: self.DeleteWebsite(website) if action.Workflow & Action.PushFiles: self.CopyFiles(website) if action.Workflow & Action.UpdateWebConfig or action.Workflow & Action.CreateWebsite or action.Workflow & Action.BuildReleaseBundle: webConfigDocument = self.CreateWebConfig(website) if webConfigDocument != None: localOnly = False if action.Workflow & Action.BuildReleaseBundle: localOnly = True self.WriteWebConfig(webConfigDocument, website, localOnly=localOnly) else: print 'Web.config is None' if action.Workflow & Action.CreateWebsite: self.CreateWebsite(website) if action.Workflow & Action.UpdateWebConfig: self.RecycleAppPool(website) if action.Workflow & Action.BuildReleaseBundle: website.ReleaseLabel = configuration.ReleaseLabel releaseLabel = configuration.ReleaseLabel releaseDirectoryName = '%s-release-%s' % ( website.SourceRoot, releaseLabel) print 'Building release bundle %s' % (releaseDirectoryName) releaseDirectoryInfo = System.IO.DirectoryInfo( releaseDirectoryName) if releaseDirectoryInfo.Exists is True: releaseDirectoryInfo.Delete(True) releaseDirectoryInfo.Create() website.ApplicationName = releaseDirectoryName website.TargetPath = '.' self.CopyFiles(website) if webConfigDocument != None: print 'Copying %s/Web.config to %s' % ( website.SourceRoot, releaseDirectoryInfo.FullName) webConfigFile = System.IO.FileInfo( '%s/Web.config' % (website.SourceRoot)) webConfigFile.CopyTo( '%s/Web.config' % (releaseDirectoryInfo.FullName), True) os.chdir(releaseDirectoryName) DeployUtilities.RunExternalCommand( 'zip', System.String.Format('-r {0}.zip *', releaseDirectoryName)) os.chdir('..') releasesDirectoryInfo = System.IO.DirectoryInfo('releases') if releasesDirectoryInfo.Exists is not True: releasesDirectoryInfo.Create() shutil.move( System.String.Format('{0}/{0}.zip', releaseDirectoryName), 'releases') for module in website.Modules.values(): module.Execute(website, action) except System.Exception, e: print e raise
def PopulateDatabase(self, configuration): connection = System.Data.SqlClient.SqlConnection() connection.ConnectionString = "Integrated Security=SSPI;Database=master;Server=%s" % ( configuration.Server) try: connection.Open() goRegexp = re.compile(r'^go[ \t]*\n', re.IGNORECASE | re.MULTILINE | re.UNICODE) for encodedScriptPath in configuration.Scripts: scriptPath = DeployUtilities.ExpandEnvironmentVariables( encodedScriptPath) print 'Running %s ...' % (scriptPath) f = open(scriptPath) try: normalizedText = goRegexp.sub('go\n', f.read()) sql = 'use %s\ngo\n%s' % (configuration.Name, normalizedText) for sqlCommand in goRegexp.split(sql): if len(sqlCommand.strip()) != 0: try: # print 'DEBUG: %s' % (sqlCommand) command = connection.CreateCommand() command.CommandText = sqlCommand command.ExecuteNonQuery() finally: command.Dispose() finally: f.close() for hook in configuration.Hooks: if hook.BeforeDrop is False: try: arguments = '%s "%s"' % ( hook.Arguments, configuration.ConnectionString) DeployUtilities.RunExternalCommand( hook.Executable, arguments) except System.ComponentModel.Win32Exception: print 'Could not open "%s".' % (hook.Executable) raise else: print 'Ran hook "%s %s %s"' % ( hook.Executable, configuration.ConnectionString, hook.Arguments) for module in configuration.Modules.values(): module.PopulateDatabase(configuration) except Exception, detail: print detail raise
def Execute(self, configuration, action): if not hasattr(configuration, 'Databases'): return for database in configuration.Databases: if action.Workflow & Action.OnlyMe: if action.Name != database.Name: print 'Skipping "%s" because of --only-me=%s option ...' % ( database.Name, action.Name) continue if hasattr(database, 'Executed'): continue else: database.Executed = True if action.Workflow & Action.SkipDatabase: print 'Skipping database deployment ...' return if action.Workflow & Action.DeleteDatabase: if self.DatabaseExists( database) == True and database.CreateOnce == True: continue self.DropDatabase(database) if action.Workflow & Action.DeployDatabase: if self.DatabaseExists( database) == True and database.CreateOnce == True: continue self.BuildDatabase(database) self.PopulateDatabase(database) if action.Workflow & Action.BuildReleaseBundle: releaseLabel = configuration.ReleaseLabel for database in configuration.Databases: releaseDirectoryName = '%s-release-%s' % (database.Name, releaseLabel) print 'Building release DB bundle %s' % ( releaseDirectoryName) releaseDirectoryInfo = System.IO.DirectoryInfo( releaseDirectoryName) if releaseDirectoryInfo.Exists is True: releaseDirectoryInfo.Delete(True) releaseDirectoryInfo.Create() for encodedScriptPath in database.Scripts: scriptPath = DeployUtilities.ExpandEnvironmentVariables( encodedScriptPath) shutil.copy(scriptPath, releaseDirectoryName) os.chdir(releaseDirectoryName) DeployUtilities.RunExternalCommand( 'zip', System.String.Format('-r {0}.zip *', releaseDirectoryName)) os.chdir('..') releasesDirectoryInfo = System.IO.DirectoryInfo('releases') if releasesDirectoryInfo.Exists is not True: releasesDirectoryInfo.Create() shutil.move( System.String.Format('{0}/{0}.zip', releaseDirectoryName), 'releases')