2012-07-16 38 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()]屬性。

相關問題