public PublishOperation(Site localSite, string filename, ContentAndDbMigrationControl control)
 {
     _localSite = localSite;
     _publishSettings = localSite.PublishProfile;
     _writer = new StreamWriter(filename);
     _writer.AutoFlush = true;
     _control = control;
 }
 public PublishContentOperation(Site localSite, ContentAndDbMigrationControl control)
     : base(localSite, localSite.PublishProfile.ContentTraceFile, control)
 {
 }
 public void Add(Site site)
 {
     Sites.Add(site);
 }
        protected static void GetDatabaseInfo(ref Site site, string physicalPath, string remoteComputerName)
        {
            try
            {
                string virtualSite = "/website";
                var map = new WebConfigurationFileMap();
                map.VirtualDirectories.Add(virtualSite, new VirtualDirectoryMapping(Environment.ExpandEnvironmentVariables(physicalPath), true));

                var configuration = WebConfigurationManager.OpenMappedWebConfiguration(map, virtualSite);
                if (configuration != null)
                {
                    ConfigurationSection configSection = configuration.GetSection("connectionStrings");
                    if (configSection != null)
                    {
                        foreach (ConnectionStringSettings connectionStringSetting in configuration.ConnectionStrings.ConnectionStrings)
                        {
                            string name = connectionStringSetting.Name;
                            string providerName = connectionStringSetting.ProviderName;
                            string dbConnectionString = connectionStringSetting.ConnectionString;
                            if (string.IsNullOrEmpty(dbConnectionString))
                            {
                                continue;
                            }

                            if (dbConnectionString.Contains("metadata"))
                            {
                                // TODO: check other EF scenarios
                                // this is an entity framework connection string, so we can't migrate it.
                                // We use this to validate later on however
                                // we assume the site has a normal connection string as well
                                site.Add(new Database(providerName, name, dbConnectionString) { ParentSite = site });
                                continue;
                            }

                            //var builder = new SqlConnectionStringBuilder(dbConnectionString);
                            //if (!string.IsNullOrEmpty(builder.AttachDBFilename) && name == "LocalSqlServer")
                            //{
                            //     we ignore this since it is MOST LIKELY the default values from the machine.config connection string from .NET framework
                            //    continue;
                            //}

                            try
                            {
                                var dbConn = new DbConnectionStringBuilder { ConnectionString = dbConnectionString };
                                // dbConn.ConnectionString = dbConnectionString;
                                if (dbConn.ContainsKey("Provider") && (dbConn["Provider"].ToString() == "SQLOLEDB" || dbConn["Provider"].ToString().Contains("SQLNCLI")))
                                {
                                    dbConn.Remove("Provider");
                                }

                                var sqlConn = new SqlConnectionStringBuilder(dbConn.ConnectionString);

                                //sqlConn.ConnectionString = dbConnectionString;

                                if (!string.IsNullOrEmpty(sqlConn.AttachDBFilename) && name == "LocalSqlServer")
                                {
                                    // we ignore this since it is MOST LIKELY the default values from the machine.config connection string from .NET framework
                                    continue;
                                }

                                if (!string.IsNullOrEmpty(remoteComputerName))
                                {
                                    sqlConn.DataSource = SetAppropriateServerName(sqlConn.DataSource, remoteComputerName);
                                }

                                site.Add(new Database(providerName, name, sqlConn.ConnectionString) { ParentSite = site });
                            }
                            catch (System.ArgumentException e)
                            {
                                MessageBox.Show(e.ToString());
                                TraceHelper.Tracer.WriteTrace(e.ToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                site.Errors.Add(ex.Message);
                TraceHelper.Tracer.WriteTrace(ex.ToString());
            }
        }
        public MetabaseReader(string migrationId, double iisVersion, RemoteSystemInfo remoteSystemInfo)
            : base(migrationId, iisVersion, remoteSystemInfo != null ? remoteSystemInfo.OSVersion : Environment.OSVersion.Version.ToString(), remoteSystemInfo != null ? remoteSystemInfo.ComputerName : Environment.MachineName)
        {
            _isLocal = remoteSystemInfo == null;
            var document = new XmlDocument();
            //#if DEBUG
            //            document.Load(@"E:\vsprojects\metabase.xml");
            //#else
            string metabasePath = _isLocal
                ? Path.Combine(Environment.SystemDirectory, @"inetsrv\metabase.xml")
                : remoteSystemInfo.RemoteMetabasePath;
            document.Load(metabasePath);
            //#endif

            var enable32Biton64 = false;
            var defaultAppPoolNode = document.SelectSingleNode(string.Format(NameSpaceAgnosticXPathFormat, "IIsApplicationPools"));
            var enable32bitAttribute = defaultAppPoolNode.Attributes["Enable32BitAppOnWin64"];
            if (enable32bitAttribute != null && !string.IsNullOrEmpty(enable32bitAttribute.Value))
            {
                enable32Biton64 = Convert.ToBoolean(enable32bitAttribute.Value);
            }

            XmlNode defaultServiceNode =
                document.SelectSingleNode(string.Format(NameSpaceAgnosticXPathFormat, "IIsWebService"));
            var netFxVersion =
                GetNetFxVersion(defaultServiceNode, null);
            var defaultAuthenticationType =
                GetAuthenticationType(
                    defaultServiceNode, EnabledAuthenticationType.Anonymous);
            var defaultDocList = GetDefaultDocList(defaultServiceNode, null);
            var iis5IsolationMode = false;
            var isolationModeAttribute = defaultServiceNode.Attributes["IIs5IsolationModeEnabled"];
            if (isolationModeAttribute != null && !string.IsNullOrEmpty(isolationModeAttribute.Value))
            {
                iis5IsolationMode = Convert.ToBoolean(isolationModeAttribute.Value);
            }

            var appPoolMap = new Dictionary<string, ApplicationPool>(StringComparer.OrdinalIgnoreCase);
            var appPoolNodes = document.SelectNodes(string.Format(NameSpaceAgnosticXPathFormat, "IIsApplicationPool"));
            foreach (XmlNode appPoolNode in appPoolNodes)
            {
                string name = string.Empty;
                var attribute = appPoolNode.Attributes["Location"];
                if (attribute != null && !string.IsNullOrEmpty(attribute.Value))
                {
                    name = attribute.Value.Replace("/LM/W3SVC/AppPools/", string.Empty);
                    appPoolMap[name] = new ApplicationPool(true, netFxVersion, enable32Biton64, name);
                }
            }

            var siteNodes = document.SelectNodes(string.Format(NameSpaceAgnosticXPathFormat, "IIsWebServer"));
            foreach (XmlNode siteNode in siteNodes)
            {
                var attribute = siteNode.Attributes["Location"];
                if (attribute != null)
                {
                    var m = SiteLocationRegex.Match(attribute.Value);
                    if (m.Success)
                    {
                        long siteId = Convert.ToInt64(m.Groups[1].Value);
                        string siteName = siteNode.Attributes["ServerComment"].Value;
                        var site = new Site(siteName, siteId);
                        string siteRootPath = attribute.Value + "/ROOT";
                        string lowerCaseSiteRootPath = attribute.Value + "/root";
                        var localAttribute = siteNode.Attributes["ServerBindings"];
                        if (localAttribute != null && !string.IsNullOrEmpty(localAttribute.Value))
                        {
                            AddBinding(localAttribute.Value, "http", ref site);
                        }

                        localAttribute = siteNode.Attributes["SecureBindings"];
                        if (localAttribute != null && !string.IsNullOrEmpty(localAttribute.Value))
                        {
                            AddBinding(localAttribute.Value, "https", ref site);
                        }

                        XmlNode currentSiteNode =
                            document.SelectSingleNode(string.Format(ExactAttributeXPath, "Location",
                                siteRootPath));
                        if (currentSiteNode == null)
                        {
                            currentSiteNode =
                            document.SelectSingleNode(string.Format(ExactAttributeXPath, "Location",
                                lowerCaseSiteRootPath));
                        }

                        if (currentSiteNode != null)
                        {
                            string physicalPath = currentSiteNode.Attributes["Path"].Value;
                            if (!_isLocal)
                            {
                                physicalPath = remoteSystemInfo.GetRemotePath(physicalPath);
                            }
                            var d = new GacAssemblyDetector(physicalPath);
                            site.Add(d.GetGacedAssemblies());
                            var tempAttribute = currentSiteNode.Attributes["AppPoolId"];
                            site.PhysicalPath = physicalPath;
                            if(tempAttribute == null || string.IsNullOrEmpty(tempAttribute.Value))
                            {
                                site.AppPoolName = "DefaultAppPool";
                            }
                            else
                            {
                                site.AppPoolName = tempAttribute.Value;
                            }

                            site.DefaultDocuments = GetDefaultDocList(currentSiteNode, defaultDocList);

                            site.EnabledAuthType = GetAuthenticationType(currentSiteNode, defaultAuthenticationType);
                            ApplicationPool appPool;
                            if (appPoolMap.TryGetValue(site.AppPoolName, out appPool))
                            {
                                appPool.NetFxVersion = GetNetFxVersion(currentSiteNode, netFxVersion);
                            }

                            GetDatabaseInfo(ref site, physicalPath, remoteSystemInfo != null ? remoteSystemInfo.ComputerName : string.Empty);
                        }

                        var appNodes = document.SelectNodes(string.Format(ContainsAttributeXPath, "Location", siteRootPath));
                        foreach (XmlNode appNode in appNodes)
                        {
                            var tempAttribute = appNode.Attributes["Path"];
                            if (tempAttribute == null)
                            {
                                // its just a directory
                                continue;
                            }

                            var physicalPath = tempAttribute.Value;
                            tempAttribute = appNode.Attributes["AppFriendlyName"];
                            if (tempAttribute == null)
                            {
                                // its just a vdir and not an app. No AppPool Settings
                                continue;
                            }

                            if (!_isLocal)
                            {
                                physicalPath = remoteSystemInfo.GetRemotePath(physicalPath);
                            }
                            var g = new GacAssemblyDetector(physicalPath);
                            site.Add(g.GetGacedAssemblies());
                            GetDatabaseInfo(ref site, physicalPath, remoteSystemInfo != null ? remoteSystemInfo.ComputerName : string.Empty);

                            string appName = tempAttribute.Value;
                            string appAppPoolName = string.Empty;
                            if (string.IsNullOrEmpty(appName))
                            {
                                appName = Path.GetFileName(physicalPath);
                            }

                            tempAttribute = appNode.Attributes["AppPoolId"];
                            if (tempAttribute != null)
                            {
                                appAppPoolName = tempAttribute.Value;
                            }

                            if (string.IsNullOrEmpty(appAppPoolName))
                            {
                                appAppPoolName = "DefaultAppPool";
                            }

                            ApplicationPool appPool;
                            if (appPoolMap.TryGetValue(appAppPoolName, out appPool))
                            {
                                appPool.NetFxVersion = GetNetFxVersion(appNode, netFxVersion);
                            }

                            var app = new Application(appName, physicalPath)
                            {
                                AppPoolName = appAppPoolName,
                            };

                            site.Add(app);
                        }

                        site.IIS5CompatMode = iis5IsolationMode;
                        Server.Sites.Add(site);
                    }
                }
            }

            foreach (var keyValue in appPoolMap)
            {
                Server.AppPools.Add(keyValue.Value);
            }
        }
 private static void AddBinding(string bindings, string protocol, ref Site site)
 {
     if (!string.IsNullOrEmpty(bindings))
     {
         var bindingArray = bindings.Split(new char[] {'\n', '\r', '\t'}, StringSplitOptions.RemoveEmptyEntries);
         foreach (var binding in bindingArray)
         {
             site.Add(new Binding(binding.Trim(), protocol));
         }
     }
 }