Saturday, July 25, 2009

Batch solution deployment between farms

Last week, we have mounted a new two-servers farm for our development environment. Among the task I had to do was to copy and deploy about 50 solutions (wsps) from the old farm to the new one. To do this, I wrote a console application to help me extract all the solutions installed and create at the same time a command file with the necessary StsAdm commands to install them in the new farm.

Here is what the console applications has to do :

- Create a folder "SolutionsToDeploy" where the WSPs will be extracted and the command file created.
- Iterate through the solutions in the solutions store, extract the wsp file and generate the Stsadm command needed to install it.

Here is the code :


Imports System.Text
Imports System.IO
Imports System
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Administration
Imports System.Xml
Imports System.Web

Module ExtractSolutions
    Sub Main(ByVal args() As String)
        Try
            Dim solutions As SPSolutionCollection = SPFarm.Local.Solutions
            Dim Path As String = "SolutionsToDeploy/"
            Dim AllowGac As Boolean = False
            Dim AllowCas As Boolean = False

            ' Create the folder if it does not exist
            If Not System.IO.Directory.Exists(Path) Then
                System.IO.Directory.CreateDirectory(Path)
            End If

            'Create a file stream where StsAdm command will be added
            Dim wrt As System.IO.TextWriter = New StreamWriter(Path + "DeploySolutions.cmd")
            For Each sol As SPSolution In solutions
                'See if the parameters -AllowGacDeployment and -AllowCasPolicies are needed
                AllowGac = IIf(sol.ContainsGlobalAssembly, True, False)
                AllowCas = IIf(sol.ContainsCasPolicy, True, False)
                Dim strline As String = String.Empty

                'Extract the solution into a file
                Dim wsp As SPPersistedFile = sol.SolutionFile
                wsp.SaveAs(Path + sol.Name)

                'For every solution we need two commands : AddSolution and DeploySolution
                strline = "Stsadm -o AddSolution -FileName " + sol.Name
                wrt.WriteLine(strline)
                If sol.DeploymentState = SPSolutionDeploymentState.GlobalAndWebApplicationDeployed Or sol.DeploymentState = SPSolutionDeploymentState.WebApplicationDeployed Then
                    For Each wapp As SPWebApplication In sol.DeployedWebApplications
                        strline = "Stsadm -o DeploySolution -name " + sol.Name + " -url " + wapp.GetResponseUri(SPUrlZone.Default).ToString() + IIf(AllowGac, " -AllowGacDeployment", "") + IIf(AllowCas, " -AllowCasPolicies", "") + " -Immediate"
                    Next
                Else
                    strline = "Stsadm -o DeploySolution -name " + sol.Name + IIf(AllowGac, " -AllowGacDeployment", "") + IIf(AllowCas, " -AllowCasPolicies", "") + " -Immediate"
                End If
                wrt.WriteLine(strline)

                ' Execute the timer job
                strline = "Stsadm -o ExecAdmSvcJobs"
                wrt.WriteLine(strline)
                wrt.WriteLine()
            Next
            'Write the command file to disk and close the stream
            wrt.Flush()
            wrt.Close()
            wrt = Nothing
        Catch ex As Exception            
            Console.WriteLine(ex.Message)
        End Try
    End Sub
End Module



I executed the console application, copied the new folder created (SolutionsToDeploy) to the new farm and ran the command file (DeploySolutions.cmd). With one shot, all my 50 solutions were added and deployed to the new farm.

Hope this helps.

No comments:

Post a Comment