void deployZip(CloudBlockBlob blob, string name)
        {
            //set directory
            var app      = name.Substring(0, name.Length - 4);
            var rootPath = getRootPath(app + '/');
            var appPath  = Path.Combine(rootPath, app.Replace('/', '\\'));
            var path     = Path.GetDirectoryName(appPath);

            _logger.Info("deploying " + name + " to " + path);
            Directory.CreateDirectory(appPath);

            //deploy files
            using (var mem = new MemoryStream())
            {
                blob.DownloadToStream(mem);
                mem.Position = 0;
                using (var zip = ZipFile.Read(mem))
                {
                    zip.ExtractAll(path, ExtractExistingFileAction.OverwriteSilently);
                }
            }

            //deploy to approot, siteroot
            if (rootPath == _rootPath)
            {
                return;
            }
            if (!isWebPath(appPath))
            {
                return;
            }

            var       configFile = Path.Combine(appPath, "deploy.json");
            IisConfig config     = null;

            if (File.Exists(configFile))
            {
                var configJson = File.ReadAllText(configFile);
                config = JsonConvert.DeserializeObject <IisConfig>(configJson);
            }

            var appName = Path.GetFileName(appPath);

            IisHelper.CreateApplication(appName, appPath, _appRootPath + '/' + app, config);

            var wildcards = blob.Container
                            .ListBlobs("*", true)
                            .OfType <CloudBlockBlob>()
                            .Select(item => item.Name);

            foreach (var wildcard in wildcards)
            {
                var wBlob = blob.Container.GetBlockBlobReference(wildcard);
                var wPath = wildcard.Substring(1);
                deploy(wBlob, app + wPath);
            }
        }
Exemple #2
0
        public static void CreateApplication(string appName, string appPath, string virtualAppPath, IisConfig config = null, int siteIndex = 0)
        {
            //create app
            using (var mgr = new ServerManager())
            {
                var iisAppPool = mgr.ApplicationPools[appName];
                var hasChange  = false;
                if (iisAppPool == null)
                {
                    //directory permission
                    var dirInfo  = new DirectoryInfo(appPath);
                    var security = dirInfo.GetAccessControl(AccessControlSections.Access);
                    const FileSystemRights right = FileSystemRights.FullControl ^ FileSystemRights.ReadAndExecute;
                    var acl = new FileSystemAccessRule("NETWORK SERVICE", right, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Deny);
                    security.AddAccessRule(acl);
                    dirInfo.SetAccessControl(security);

                    //apppool permission
                    iisAppPool = mgr.ApplicationPools.Add(appName);
                    iisAppPool.ManagedRuntimeVersion     = "v4.0";
                    iisAppPool.ProcessModel.IdentityType = ProcessModelIdentityType.NetworkService;
                    hasChange = true;
                }
                config = config ?? IisConfig.Default;
                var startMode = iisAppPool["startMode"] as string;
                var target    = config.AutoStart ? "AlwaysRunning" : "OnDemand";
                if (startMode != target)
                {
                    iisAppPool["startMode"] = target;
                    hasChange = true;
                }
                var target2 = TimeSpan.FromMinutes(config.IdleTimeout);
                if (iisAppPool.ProcessModel.IdleTimeout != target2)
                {
                    iisAppPool.ProcessModel.IdleTimeout = target2;
                    hasChange = true;
                }
                if (iisAppPool.Enable32BitAppOnWin64 != config.Enable32Bit)
                {
                    iisAppPool.Enable32BitAppOnWin64 = config.Enable32Bit;
                    hasChange = true;
                }
                if (hasChange)
                {
                    mgr.CommitChanges();
                }

                hasChange = false;
                var iisApp = mgr.Sites[siteIndex].Applications[virtualAppPath];
                if (iisApp == null)
                {
                    iisApp = mgr.Sites[siteIndex].Applications.Add(virtualAppPath, appPath);
                    iisApp.ApplicationPoolName = appName;
                    iisApp.EnabledProtocols    = "https,http";
                    hasChange = true;
                }
                var preloadEnabled = Equals(iisApp["preloadEnabled"], true);
                if (preloadEnabled != config.AutoStart)
                {
                    iisApp["preloadEnabled"] = config.AutoStart;
                    hasChange = true;
                }
                if (hasChange)
                {
                    mgr.CommitChanges();
                }
            }
        }