2012-07-16 12 views
1

我为Cruise Control .net创建了以下自定义任务,但找不到如何将自定义任务添加到项目配置的单个引用。有没有人有如何在cc.net中添加对自定义任务的引用的有用示例?在CC.net配置中使用自定义任务

(下面是我的新任务)

public class RestoreDb : TaskBase 
{ 
    #region Parameters 

    [Required] 
    public string ServerName { get; set; } 
    [Required] 
    public string DbName { get; set; } 

    public string BackupFileName { get; set; } 

    #endregion 

    protected override bool Execute(ThoughtWorks.CruiseControl.Core.IIntegrationResult result) 
    { 
     bool returnResult = false; 
     try 
     { 
      SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder(); 
      connectionStringBuilder.IntegratedSecurity = true; 
      connectionStringBuilder.DataSource = ServerName; 
      SqlConnection connection = new SqlConnection(connectionStringBuilder.ToString()); 
      connection.Open(); 

      Server server = new Server(new ServerConnection(connection)); 

      if (server.Databases[DbName] != null) 
      { 
       Log.Info("Dropping existing " + DbName + " on " + ServerName); 
       server.Databases[DbName].Drop(); 
      } 
      else 
      { 
       Log.Info(DbName + " on " + ServerName + " doesn't exist."); 
      } 

      Log.Info("Restoring " + DbName + " on " + ServerName); 
      Database newDb = new Database(server, DbName); 

      Restore rs = new Restore(); 
      rs.NoRecovery = false; 

      FileInfo fi = new FileInfo(server.Settings.BackupDirectory + "\\" + BackupFileName); 
      rs.Devices.AddDevice(fi.FullName, DeviceType.File); 
      rs.Database = DbName; 
      rs.Action = RestoreActionType.Database; 
      rs.ReplaceDatabase = true; 

      DataTable fileContents = rs.ReadFileList(server); 
      string originalDbName = fileContents.Rows[0][0].ToString(); 
      string originalLogFileName = fileContents.Rows[1][0].ToString(); 
      rs.RelocateFiles.Add(new RelocateFile(originalDbName, 
      string.Format(@"C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\{0}.mdf", DbName))); 

      rs.RelocateFiles.Add(new RelocateFile(originalLogFileName, 
      string.Format(@"C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\{0}_1.ldf", DbName))); 

      rs.SqlRestore(server); 
      Log.Info("Restoring done."); 
      returnResult = true; // success! 
     } 
     catch (Exception ex) 
     { 
      Log.Error(ex); 
      returnResult = false; 
     } 
     return returnResult; 
    } 
} 

回答

0

所以我基本上扔这是因为没有实例如何将自定义任务添加到cc.net配置的任何地方。下面是我写的powershell脚本,它完成了完全相同的步骤。

$ServerName = 'servernamehere'; 
$DatabaseName = 'newDbNameHere'; 
$BackupFile = 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Backup\backupFileHere.bak'; 
if($args[0]) 
{ 
    $ServerName = $args[0]; 
} 
if($args[1]) 
{ 
    $DatabaseName = $args[1]; 
} 
if($args[2]) 
{ 
    $BackupFile = $args[2]; 
} 

[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") 
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") 
try 
{ 
    $Server = new-object ("Microsoft.SqlServer.Management.Smo.Server") $ServerName 

    If ($Server.Databases.Contains($DatabaseName)) 
    { 
     write-host "Dropping existing " $DatabaseName " on " $ServerName; 
     #$Server.Databases[$DatabaseName].SetOffline(); 
     $Server.Databases[$DatabaseName].Drop(); 
    } 
    else 
    { 
     write-host $DatabaseName " on " $ServerName " doesn't exist."; 
    } 
    $Database = new-object ("Microsoft.SqlServer.Management.Smo.Database") ($Server, $DatabaseName); 

    $restore = new-object ('Microsoft.SqlServer.Management.Smo.Restore'); 
    $restore.NoRecovery = $false; 

    $fil=new-object "Microsoft.SqlServer.Management.Smo.BackupDeviceItem"; 
    $fil.DeviceType='File'; 
    $fil.Name= $BackupFile; 
    $restore.Action="Database"; 
    $restore.Devices.Add($fil); 
    $restore.Database=$DatabaseName; 
    $restore.ReplaceDatabase = $true; 

    #retrieving the backup file's original database values so we can replace them 
    $FileContents = $restore.ReadFileList($Server); 
    $originalDbName = $FileContents.Rows[0][0].ToString(); 
    $originalLogFileName = $FileContents.Rows[1][0].ToString(); 

    #replacing the bak files original file names with the new file names 
    $mdf = new-object "Microsoft.SqlServer.Management.Smo.RelocateFile" ($originalDbName, 
     "C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\$DatabaseName.mdf"); 
    $restore.RelocateFiles.Add($mdf); 
    $ldf = new-object "Microsoft.SqlServer.Management.Smo.RelocateFile" ($originalLogFileName, 
     "C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\$DatabaseName_Log.mdf"); 
    $restore.RelocateFiles.Add($ldf); 


    write-host "Restoring database $DatabaseName on $ServerName from file $BackupFile"; 

     $restore.SqlRestore($Server); 
     write-host "Restore Complete"; 
} 
catch [System.Exception] { 
    # $_ is set to the ErrorRecord of the exception 
    $currentException = $_.Exception; 
    write-host "Root Exception: " $currentException.Message; 
    While($currentException.InnerException) 
    { 
    $currentException = $_.Exception.InnerException; 
    write-host "Inner Exception: " $currentException.Message; 
    } 
    write-host "Restore Failed."; 
    exit 0; #failure 
}  
exit 1; 
+0

这真的不是你发布的问题的答案 – 2014-10-16 14:49:33

1

你没有提到你正在使用的版本,所以我回答这个假设,用户将使用更新的版本。我已经建立了这个基于开发定制任务 1.8.3.0

经验你可以找到一些用于创建和消费在这里http://www.cruisecontrolnet.org/projects/ccnet/wiki/DevInfo_MakingPlugins

编辑:我想请注意,所提供链接上的CruiseControl.NET文档已更新,现在包含此处包含的大部分信息。

然而,这份文档的重量相当轻,并且不能真正涵盖您最终会对何时尝试创建自己的任务有疑问的主题范围。

这个过程,就像我可以说的那样;

  • 您的自定义任务项目需要引用以下程序集,该程序集可以在根CruiseControl.NET服务器文件夹中找到。

    • NetReflector.dll
    • ThoughtWorks.CruiseControl.Core.Dll
    • 任何其他大会在引用这不是在.NET 3.5框架
  • 的一部分。当构建自定义任务你将需要使用反射器属性标记您希望公开的任何类或属性,以使它们在ccnet.config中可用

  • 为了让Cruis eControl.NET使用您的自定义库,您需要使用以下约定命名程序集:ccnet.WhateverYouWantHere.plugin.dll

  • 您需要将此程序集放置在CruiseControl.NET服务器文件夹中。

    • 如果您已经通过在ccnet.exe.config中为PluginLocation提供了一个值来设置插件目录,那么您需要将您的程序集放在那里。
    • 您在项目中引用的任何程序集尚不在CruiseControl中。NET服务器文件夹将需要添加到服务器或插件文件夹以及
namespace MyTasks 
{ 
    [ReflectorType("MyCustomTask")] 
    public class MyCustomTaskClass : ITask 
    { 
     [ReflectorProperty("MyCustomTaskProperty")] 
     public string CustomProperty { get; set; } 

     public string MyInternalPublicProperty { get; set; } 

     public void Run(IIntegrationResult result){} 
    } 
} 

注:的属性和名称,你暴露给的ccnet.config名称不必比赛。

为了引用创建的任务,标签等,您只需使用Reflector属性中提供的字符串。

<project> 
    <tasks> 
     <MyCustomTask MyCustomTaskProperty="This is an example"/> 
    </tasks> 
</project> 

如果您尝试使用此代码,并在你的标签添加引用MyInternalPublicProperty它不会工作:

<project> 
    <tasks> 
     <MyCustomTask MyInternalPublicProperty="This is will throw an exception in the service and not reload the ccnet config until the invalid content is removed from the config"/> 
    </tasks> 
</project> 

这是因为你没有装饰,它有自己的属性[ReflectorProperty()]属性。

相关问题