I, Me, Myself

My photo
Hyderabad, Hindhu/AndhraPradesh, India
CoOl,jOvial,strAight fOrward

Search

Tuesday, June 7, 2011

Deployer Service

public class Deployer : IDeployer
    {
        public Deployer()
        {
        }

        public bool Deploy(DeploymentManifest dto, BODataDeployInformation boDeployInfo, DeploymentComponentType deployComponentType)
        {
            bool status = false;
            string logFilePath = System.IO.Path.Combine(boDeployInfo.ExecutableDirectory, Constants.CDF_ERROR_LOG_FILE);
            if (logFilePath != string.Empty)
            {
                logFilePath = logFilePath.Replace(Constants.CDF_CLIENT_FOLDER_NAME, Constants.CDF_AGENT_FOLDER_NAME);
            }
            Logger.InitializeLogger(logFilePath);
            switch (deployComponentType)
            {
                case DeploymentComponentType.Solution:
                    SolutionDeployer solutionDeployer = new SolutionDeployer(dto as Solution, boDeployInfo);
                    status = solutionDeployer.Deploy();
                    break;
                case DeploymentComponentType.Site:
                    SiteDeployer siteDeployer = new SiteDeployer(dto as Site, boDeployInfo);
                    status = siteDeployer.Deploy();
                    break;
                case DeploymentComponentType.SiteTemplate:
                    SiteTemplateDeployer siteTemplateDeployer = new SiteTemplateDeployer(dto as SiteTemplate, boDeployInfo);
                    status = siteTemplateDeployer.Deploy();
                    break;
                case DeploymentComponentType.DeploymentManisfestList:
                    DeploymentManifestListDeployer listDeployer = new DeploymentManifestListDeployer(dto as DeploymentManifestList, boDeployInfo);
                    status = listDeployer.Deploy();
                    break;
                case DeploymentComponentType.ListTemplate:
                    ListTemplateDeployer listTemplateDeployer = new ListTemplateDeployer(dto as ListTemplate, boDeployInfo);
                    status = listTemplateDeployer.Deploy();
                    break;
                case DeploymentComponentType.ProfileProperty:
                    ProfilePropertyDeployer profilePropertyDeployer = new ProfilePropertyDeployer(dto as ProfileProperty);
                    status = profilePropertyDeployer.Deploy();
                    break;
                case DeploymentComponentType.ManagedProperty:
                    ManagedMetadataPropertyDeployer managedPropertyDeployer = new ManagedMetadataPropertyDeployer(dto as ManagedProperty);
                    status = managedPropertyDeployer.Deploy();
                    break;
                case DeploymentComponentType.StsAdmCommand:
                    ICommand commandStsAdm = CommandHandler.GetCommand(CommandType.StsAdmCommand, boDeployInfo);
                    status = commandStsAdm.Execute(dto as Microsoft.Spo.Cdf.BusinessObjects.StsAdmCommand);
                    break;
                case DeploymentComponentType.PowerShellCommand:
                    ICommand commandPS = CommandHandler.GetCommand(CommandType.PowerShellCommand, boDeployInfo);
                    status = commandPS.Execute(dto as Microsoft.Spo.Cdf.BusinessObjects.PowerShellCommand);
                    break;

            }
            return status;
        }


        public bool UploadDeploymentBit(BODataDeployInformation boDeployInfo, DeployComponent component)
        {
            bool uploadBitStatus = true;

            String agentPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
           
            agentPath = System.IO.Path.GetDirectoryName(agentPath);

            string logFilePath = System.IO.Path.Combine(agentPath, Constants.CDF_ERROR_LOG_FILE);
            Logger.InitializeLogger(logFilePath);

            BackupSPSite backupHandler = new BackupSPSite(boDeployInfo);
            uploadBitStatus = backupHandler.UploadDeploymentBit(new DeployComponent(Constants.CDF_AGGREGATE_MANIFEST_XML,
                                                                                      string.Empty,
                                                                                      DeploymentComponentType.AggregateOperation));
            return uploadBitStatus;
        }

        public bool UploadDeploymentBits(BODataDeployInformation boDeployInfo, DeploymentComponents components)
        {
            bool uploadBitStatus = true;

            String agentPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            agentPath = System.IO.Path.GetDirectoryName(agentPath);
            string logFilePath = System.IO.Path.Combine(agentPath, Constants.CDF_ERROR_LOG_FILE);
            Logger.InitializeLogger(logFilePath);

            BackupSPSite backupHandler = new BackupSPSite(boDeployInfo);
            uploadBitStatus = backupHandler.UploadDeploymentBits(components);
            return uploadBitStatus;
        }

        public bool DownloadDataBits(BODataDeployInformation deployInformation, DeployComponent component)
        {
            String agentPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            agentPath = System.IO.Path.GetDirectoryName(agentPath);
            string logFilePath = System.IO.Path.Combine(agentPath, Constants.CDF_ERROR_LOG_FILE);
            Logger.InitializeLogger(logFilePath);

            IDataManager dataDownload = DeployDataManager.Create(DataOperationType.FarmDataOperation);
            bool downloadBitsStatus = (bool)dataDownload.GetData(deployInformation, component);
            return downloadBitsStatus;
        }

        public bool CreateSPTimerJob(BODataDeployInformation deployInfo)
        {

            String agentPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            agentPath = System.IO.Path.GetDirectoryName(agentPath);

            string logFilePath = System.IO.Path.Combine(agentPath, Constants.CDF_ERROR_LOG_FILE);
           
            Logger.InitializeLogger(logFilePath);

            if (deployInfo.ExecutableDirectory != string.Empty)
            {
                deployInfo.ExecutableDirectory = agentPath; //setting Agent installed path
            }

            Assembly assembly = Assembly.Load(Constants.TIMERJOB_ASSEMBLY_NAME);

            if (assembly == null)
            {
                throw new Exception(string.Format("Failed to load assembly '{0}'", Constants.TIMERJOB_ASSEMBLY_NAME));
            }

            System.Type type = assembly.GetType(Constants.TIMERJOB_TIMER_FULLYQUALIFIED_CLASS);

            if (null == type)
            {
                throw new Exception(string.Format("Failed to load type '{0}'", Constants.TIMERJOB_TIMER_FULLYQUALIFIED_CLASS));
            }

            try
            {
                IDeploymentTimer deploymentTimer = Activator.CreateInstance(type) as IDeploymentTimer;
                deploymentTimer.Create(deployInfo);
            }
            catch (Exception ex)
            {
                CDFException ex1 = new CDFException(ex.Message);
                throw new FaultException(ex1);
                Logger.WriteLog(ex, CDFEventType.Error, DeploymentComponentType.TimerJob);
            }
            finally
            {

            }
            //SPTimerJob spTimerJob = new SPTimerJob();
            //return spTimerJob.Create(deployInfo);
            return true;
        }

        public void TranferCafPackageFile(CAFPackageFile packageFile)
        {
            try
            {
                String agentPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
                agentPath = System.IO.Path.GetDirectoryName(agentPath);

                string logFilePath = System.IO.Path.Combine(agentPath, Constants.CDF_ERROR_LOG_FILE);

                if (packageFile.CDFExecutableDirectory != string.Empty)
                {
                    packageFile.CDFExecutableDirectory = agentPath;
                }

                Logger.InitializeLogger(logFilePath);

                string cabFilePath = packageFile.CDFRunDirectory;
                string destinationFilePath = string.Empty;

                if (!System.IO.Directory.Exists(packageFile.CDFRunDirectory))
                    System.IO.Directory.CreateDirectory(packageFile.CDFRunDirectory);

                //constrct the CabFilePath in CDF RunDirectory and CR Number
                if (packageFile.ChangeRequestNumber != string.Empty)
                    cabFilePath = System.IO.Path.Combine(cabFilePath, packageFile.ChangeRequestNumber);

                // create output folder, if does not exist -- Change request number
                if (!System.IO.Directory.Exists(cabFilePath))
                    System.IO.Directory.CreateDirectory(cabFilePath);

                if (packageFile.CAFPackageName.Contains(Constants.CDF_AGGREGATE_MANIFEST_XML))
                {
                    // create output folder, if does not exist -- Solution Artifacts
                    cabFilePath = System.IO.Path.Combine(cabFilePath, Constants.CDF_PACKAGE_FOLDER_NAME);
                    if (!System.IO.Directory.Exists(cabFilePath))
                    {
                        System.IO.Directory.CreateDirectory(cabFilePath);
                    }
                }

                destinationFilePath = cabFilePath;

                // delete the CAB file if it is already exists
                cabFilePath = System.IO.Path.Combine(cabFilePath, packageFile.CAFPackageName);
                if (System.IO.File.Exists(cabFilePath))
                {
                    System.IO.File.Delete(cabFilePath);
                }

                int chunkSize = 2048;
                byte[] buffer = new byte[chunkSize];

                using (System.IO.FileStream writeStream = new System.IO.FileStream(cabFilePath, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write))
                {
                    try
                    {

                        do
                        {
                            // read bytes from input stream
                            int bytesRead = packageFile.FileByteStream.Read(buffer, 0, chunkSize);
                            if (bytesRead == 0) break;

                            // write bytes to output stream
                            writeStream.Write(buffer, 0, bytesRead);
                        } while (true);

                    }
                    catch (Exception ex)
                    {
                        Logger.WriteLog(ex, CDFEventType.Error, DeploymentComponentType.CABFileTransfer);
                    }
                    finally
                    {
                        if (writeStream != null)
                        {
                            writeStream.Close();
                        }
                    }
                }

                if (packageFile.CAFPackageName == Constants.CAF_PACKAGE_NAME)
                {
                    FileIO.ExtractUploadedPackagedFile(cabFilePath, destinationFilePath, packageFile.CDFExecutableDirectory);
                }
                //return true;
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex, CDFEventType.Error, DeploymentComponentType.CABFileTransfer);
                //return false;
            }
        }

    }

No comments:

Followers