Beispiel #1
0
        public Instance(string appDir, Droplet droplet)
        {
            if (null != droplet)
            {
                DropletID     = droplet.ID;
                InstanceID    = Guid.NewGuid();
                InstanceIndex = droplet.InstanceIndex;
                Name          = droplet.Name;
                Uris          = droplet.Uris;
                Users         = droplet.Users;
                Version       = droplet.Version;
                MemQuota      = droplet.Limits.Mem * (1024 * 1024);
                DiskQuota     = droplet.Limits.Disk * (1024 * 1024);
                FdsQuota      = droplet.Limits.FDs;
                Runtime       = droplet.Runtime;
                Framework     = droplet.Framework;
                Staged        = droplet.Name;
                Sha1          = droplet.Sha1;
                logID         = String.Format("(name={0} app_id={1} instance={2:N} index={3})", Name, DropletID, InstanceID, InstanceIndex);
                Staged        = String.Format("{0}-{1}-{2:N}", Name, InstanceIndex, InstanceID);
                Dir           = Path.Combine(appDir, Staged);
            }

            State          = VcapStates.STARTING;
            Start          = DateTime.Now.ToString(Constants.JsonDateFormat);
            StateTimestamp = Utility.GetEpochTimestamp();
        }
Beispiel #2
0
        public Instance(string appDir, Droplet droplet)
        {
            if (null != droplet)
            {
                DropletID     = droplet.ID;
                InstanceID    = Guid.NewGuid();
                InstanceIndex = droplet.InstanceIndex;
                Name          = droplet.Name;
                Uris          = droplet.Uris;
                Users         = droplet.Users;
                Version       = droplet.Version;
                MemQuota      = droplet.Limits.Mem * (1024 * 1024);
                DiskQuota     = droplet.Limits.Disk * (1024 * 1024);
                FdsQuota      = droplet.Limits.FDs;
                Runtime       = droplet.Runtime;
                Framework     = droplet.Framework;
                Staged        = droplet.Name;
                Sha1          = droplet.Sha1;
                logID         = String.Format("(name={0} app_id={1} instance={2:N} index={3})", Name, DropletID, InstanceID, InstanceIndex);
                Staged        = String.Format("{0}-{1}-{2:N}", Name, InstanceIndex, InstanceID);
                Dir           = Path.Combine(appDir, Staged);
            }

            State          = VcapStates.STARTING;
            Start          = DateTime.Now.ToString(Constants.JsonDateFormat);
            StateTimestamp = Utility.GetEpochTimestamp();
        }
Beispiel #3
0
        public Instance(string appDir, Droplet droplet)
            : this()
        {
            if (null != droplet)
            {
                DropletID      = droplet.ID;
                InstanceID     = Guid.NewGuid();
                InstanceIndex  = droplet.InstanceIndex;
                Name           = droplet.Name;
                Uris           = droplet.Uris;
                Users          = droplet.Users;
                Version        = droplet.Version;
                MemQuotaBytes  = droplet.Limits.MemoryMB * (1024*1024);
                DiskQuotaBytes = droplet.Limits.DiskMB * (1024*1024);
                FdsQuota       = droplet.Limits.FDs;
                Runtime        = droplet.Runtime;
                Framework      = droplet.Framework;
                Staged         = droplet.Name;
                Sha1           = droplet.Sha1;
                logID          = String.Format("(name={0} app_id={1} instance={2:N} index={3})", Name, DropletID, InstanceID, InstanceIndex);
                Staged         = String.Format("{0}-{1}-{2:N}", Name, InstanceIndex, InstanceID);
                Dir            = Path.Combine(appDir, Staged);
            }

            State             = VcapStates.STARTING;
            instanceStartDate = DateTime.Now;
            Start             = instanceStartDate.ToJsonString();
            StateTimestamp    = Utility.GetEpochTimestamp();

            if (MemQuotaBytes > 0)
            {
                /*
                 * TODO: if the user pushes an ASP.NET app with 64MB allocation, this will almost
                 * always kill it due to the fact that the default working set size is 64MB or maybe a bit more
                 * when a worker process is spun up.
                jobObject.JobMemoryLimit = MemQuotaBytes;
                 */
            }
        }
Beispiel #4
0
        public Instance(string appDir, Droplet droplet) : this()
        {
            if (null != droplet)
            {
                DropletID      = droplet.ID;
                InstanceID     = Guid.NewGuid();
                InstanceIndex  = droplet.InstanceIndex;
                Name           = droplet.Name;
                Uris           = droplet.Uris;
                Users          = droplet.Users;
                Version        = droplet.Version;
                MemQuotaBytes  = droplet.Limits.MemoryMB * (1024 * 1024);
                DiskQuotaBytes = droplet.Limits.DiskMB * (1024 * 1024);
                FdsQuota       = droplet.Limits.FDs;
                Runtime        = droplet.Runtime;
                Framework      = droplet.Framework;
                Staged         = droplet.Name;
                Sha1           = droplet.Sha1;
                logID          = String.Format("(name={0} app_id={1} instance={2:N} index={3})", Name, DropletID, InstanceID, InstanceIndex);
                Staged         = String.Format("{0}-{1}-{2:N}", Name, InstanceIndex, InstanceID);
                Dir            = Path.Combine(appDir, Staged);
            }

            State             = VcapStates.STARTING;
            instanceStartDate = DateTime.Now;
            Start             = instanceStartDate.ToJsonString();
            StateTimestamp    = Utility.GetEpochTimestamp();

            if (MemQuotaBytes > 0)
            {
                /*
                 * TODO: if the user pushes an ASP.NET app with 64MB allocation, this will almost
                 * always kill it due to the fact that the default working set size is 64MB or maybe a bit more
                 * when a worker process is spun up.
                 * jobObject.JobMemoryLimit = MemQuotaBytes;
                 */
            }
        }
Beispiel #5
0
        public void SetupEnvironment(Droplet droplet, Instance instance)
        {
            Configuration c = getConfiguration(instance);
            if (null != c)
            {
                AppSettingsSection appSettingsSection = c.GetSection("appSettings") as AppSettingsSection;
                if (null != appSettingsSection)
                {
                    var appSettings = appSettingsSection.Settings;
                    replaceSetting(appSettings, "HOME", Path.Combine(config.AppDir, instance.Staged));

                    var applicationDict = new Dictionary<string, object>
                    {
                        { "instance_id", droplet.ID },
                        { "instance_index", droplet.InstanceIndex },
                        { "name", droplet.Name },
                        { "uris", droplet.Uris },
                        { "users", droplet.Users },
                        { "version", droplet.Version },
                        { "runtime", droplet.Runtime },
                    };
                    string vcapApplicationJson = JsonConvert.SerializeObject(applicationDict);
                    replaceSetting(appSettings, "VCAP_APPLICATION", vcapApplicationJson);

                    if (false == droplet.Services.IsNullOrEmpty())
                    {
                        string vcapServicesJson = JsonConvert.SerializeObject(droplet.Services.ToDictionary(s => s.Label));
                        replaceSetting(appSettings, "VCAP_SERVICES", vcapServicesJson);
                    }

                    replaceSetting(appSettings, "VCAP_APP_HOST", config.LocalIPAddress.ToString());
                    // TODO appSettingsSection.Settings["VCAP_APP_PORT"].Value

                    if (false == droplet.Env.IsNullOrEmpty())
                    {
                        foreach (string env in droplet.Env)
                        {
                            string[] envSplit = env.Split(new[] { '=' });
                            if (false == envSplit.IsNullOrEmpty() && envSplit.Length == 2)
                            {
                                string key = envSplit[0];
                                string value = envSplit[1];
                                replaceSetting(appSettings, key, value);
                            }
                        }
                    }
                }
                c.Save();
            }
        }
Beispiel #6
0
        public void BindServices(Droplet droplet, Instance instance)
        {
            string appPath = instance.Staged;

            if (false == droplet.Services.IsNullOrEmpty())
            {
                Configuration c = getConfiguration(instance);
                if (null != c)
                {
                    ConnectionStringsSection connectionStringsSection = c.GetSection("connectionStrings") as ConnectionStringsSection;
                    if (null != connectionStringsSection)
                    {
                        foreach (Service svc in droplet.Services.Where(s => s.IsMSSqlServer))
                        {
                            if (null != svc.Credentials)
                            {
                                SqlConnectionStringBuilder builder;
                                ConnectionStringSettings defaultConnectionStringSettings = connectionStringsSection.ConnectionStrings["Default"];
                                if (null == defaultConnectionStringSettings)
                                {
                                    builder = new SqlConnectionStringBuilder();
                                }
                                else
                                {
                                    builder = new SqlConnectionStringBuilder(defaultConnectionStringSettings.ConnectionString);
                                }

                                builder.DataSource = svc.Credentials.Host;
                                builder.ConnectTimeout = 30;

                                if (svc.Credentials.Password.IsNullOrWhiteSpace() || svc.Credentials.Username.IsNullOrWhiteSpace())
                                {
                                    builder.IntegratedSecurity = true;
                                }
                                else
                                {
                                    builder.IntegratedSecurity = false;
                                    builder.UserID = svc.Credentials.Username;
                                    builder.Password = svc.Credentials.Password;
                                }

                                if (false == svc.Credentials.Name.IsNullOrWhiteSpace())
                                {
                                    builder.InitialCatalog = svc.Credentials.Name;
                                }

                                if (null == defaultConnectionStringSettings)
                                {
                                    connectionStringsSection.ConnectionStrings.Add(new ConnectionStringSettings("Default", builder.ConnectionString));
                                }
                                else
                                {
                                    defaultConnectionStringSettings.ConnectionString = builder.ConnectionString;
                                }
                                break;
                            }
                        }
                    }
                    c.Save();
                }
            }
        }
Beispiel #7
0
        public bool Stage(Droplet droplet, Instance instance)
        {
            bool rv = false;

            using (FileData file = GetStagedApplicationFile(droplet.ExecutableUri))
            {
                if (null != file)
                {
                    InstancePaths paths = GetInstancePaths(instance);
                    Directory.CreateDirectory(paths.DropletsPath);
                    Directory.CreateDirectory(paths.BaseAppPath);

                    using (var gzipStream = new GZipInputStream(file.FileStream))
                    {
                        var tarArchive = TarArchive.CreateInputTarArchive(gzipStream);
                        tarArchive.ExtractContents(paths.DropletsPath);
                        tarArchive.Close();
                    }

                    var instanceApplicationDirInfo =  new DirectoryInfo(paths.BaseAppPath);
                    Utility.CopyDirectory(new DirectoryInfo(paths.DropletsPath), instanceApplicationDirInfo);

                    rv = true;
                }
            }

            return rv;
        }
Beispiel #8
0
        public bool Stage(Droplet droplet, Instance instance)
        {
            bool rv = false;

            DirectoryInfo instanceApplicationDirInfo = null;

            using (FileData file = GetStagedApplicationFile(droplet.ExecutableUri))
            {
                if (null != file)
                {
                    InstancePaths paths = GetInstancePaths(instance);
                    Directory.CreateDirectory(paths.DropletsPath);
                    Directory.CreateDirectory(paths.BaseAppPath);

                    using (var gzipStream = new GZipInputStream(file.FileStream))
                    {
                        var tarArchive = TarArchive.CreateInputTarArchive(gzipStream);
                        tarArchive.ExtractContents(paths.DropletsPath);
                        tarArchive.Close();
                    }

                    instanceApplicationDirInfo = new DirectoryInfo(paths.BaseAppPath);
                    Utility.CopyDirectory(new DirectoryInfo(paths.DropletsPath), instanceApplicationDirInfo);

                    rv = true;
                }
            }

            if (rv && null != instanceApplicationDirInfo)
            {
                DirectorySecurity appDirSecurity = instanceApplicationDirInfo.GetAccessControl();
                appDirSecurity.AddAccessRule(
                    new FileSystemAccessRule(
                        IIS_IUSRS,
                        FileSystemRights.Write | FileSystemRights.Read | FileSystemRights.Delete | FileSystemRights.Modify,
                        InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                        PropagationFlags.None,
                        AccessControlType.Allow));
                instanceApplicationDirInfo.SetAccessControl(appDirSecurity);
            }

            return rv;
        }