public Heartbeat(Instance instance) { Droplet = instance.DropletID; Version = instance.Version; InstanceID = instance.InstanceID; Index = instance.InstanceIndex; State = instance.State; StateTimestamp = instance.StateTimestamp; }
public FindDropletResponse(Guid argID, Instance argInstance, TimeSpan argUptime) { Dea = argID; Version = argInstance.Version; Droplet = argInstance.DropletID; InstanceID = argInstance.InstanceID; Index = argInstance.InstanceIndex; State = argInstance.State; StateTimestamp = argInstance.StateTimestamp; Staged = argInstance.Staged; Stats = new Stats(argInstance, argUptime); }
public InstanceExited(Instance instance) { ID = instance.DropletID; Version = instance.Version; InstanceID = instance.InstanceID; InstanceIndex = instance.InstanceIndex; Reason = instance.ExitReason; if (instance.IsCrashed) { CrashTimestamp = instance.StateTimestamp; } }
public Stats(Instance instance) { TimeSpan uptimeSpan = DateTime.Now - instance.StartDate; Name = instance.Name; Host = instance.Host; Port = instance.Port; Uptime = uptimeSpan.TotalSeconds; Uris = instance.Uris; MemQuotaBytes = instance.MemQuotaBytes; DiskQuotaBytes = instance.DiskQuotaBytes; FdsQuota = instance.FdsQuota; Cores = (uint)Environment.ProcessorCount; // TODO Usage = instance.MostRecentUsage; }
public FindDropletResponse(Guid argID, Instance argInstance) { Dea = argID; Version = argInstance.Version; Droplet = argInstance.DropletID; InstanceID = argInstance.InstanceID; Index = argInstance.InstanceIndex; State = argInstance.State; StateTimestamp = argInstance.StateTimestamp; Staged = argInstance.Staged; if (this.State != VcapStates.RUNNING) { this.Stats = null; } }
public void Add(uint dropletID, Instance instance) { lock (droplets) { IDictionary<Guid, Instance> instances; if (droplets.TryGetValue(dropletID, out instances)) { instances.Add(instance.InstanceID, instance); } else { instances = new Dictionary<Guid, Instance> { { instance.InstanceID, instance } }; droplets.Add(dropletID, instances); } } }
public void CleanupInstanceDirectory(Instance instance, bool force = false) { if (force || (false == disableDirCleanup)) { InstancePaths paths = GetInstancePaths(instance); try { if (Directory.Exists(paths.DropletsPath)) { Directory.Delete(paths.DropletsPath, true); } if (Directory.Exists(paths.BaseAppPath)) { Directory.Delete(paths.BaseAppPath, true); } } catch (Exception ex) { log.Error(ex); } } }
private void SendExitedMessage(Instance instance) { if (instance.IsNotified) { return; } UnregisterWithRouter(instance); if (false == instance.HasExitReason) { instance.Crashed(); } SendExitedNotification(instance); instance.IsNotified = true; }
private InstancePaths GetInstancePaths(Instance instance) { return new InstancePaths( dropletsPath: Path.Combine(dropletsPath, instance.Staged), baseAppPath: instance.Dir, fullAppPath: Path.Combine(instance.Dir, "app")); }
public void CleanupInstanceDirectory(Instance instance) { CleanupInstanceDirectory(instance, false); }
private void UnregisterWithRouter(Instance instance, string[] uris) { if (instance.Uris.IsNullOrEmpty()) { return; } var routerUnregister = new RouterUnregister { Dea = natsClient.UniqueIdentifier, Host = instance.Host, Port = instance.Port, Uris = uris ?? instance.Uris, }; natsClient.Publish(routerUnregister); }
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; }
public void InstanceStopped(Instance instance) { uint dropletID = instance.DropletID; lock (droplets) { if (droplets.ContainsKey(dropletID)) { IDictionary<Guid, Instance> instanceDict = droplets[dropletID]; if (null != instanceDict) { instanceDict.Remove(instance.InstanceID); if (instanceDict.IsNullOrEmpty()) { droplets.Remove(dropletID); } } } } }
private void ProcessDeaStart(string message, string reply) { if (shutting_down) { return; } log.Debug(Resources.Agent_ProcessDeaStart_Fmt, message); Droplet droplet = Message.FromJson<Droplet>(message); if (false == droplet.FrameworkSupported) { log.Info(Resources.Agent_NonAspDotNet_Message); return; } var instance = new Instance(config.AppDir, droplet); if (filesManager.Stage(droplet, instance)) { WebServerAdministrationBinding binding = webServerProvider.InstallWebApp( filesManager.GetApplicationPathFor(instance), instance.Staged, instance.ManagedRuntimeVersion); if (null == binding) { log.Error(Resources.Agent_ProcessDeaStartNoBindingAvailable, instance.Staged); filesManager.CleanupInstanceDirectory(instance, true); } else { instance.Host = binding.Host; instance.Port = binding.Port; configManager.BindServices(droplet, instance); configManager.SetupEnvironment(droplet, instance); instance.OnDeaStart(); if (false == shutting_down) { SendSingleHeartbeat(new Heartbeat(instance)); RegisterWithRouter(instance, instance.Uris); dropletManager.Add(droplet.ID, instance); TakeSnapshot(); reservedMemoryMB += instance.MemQuotaMB; } } } }
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(); } } }
public string GetApplicationPathFor(Instance instance) { InstancePaths paths = GetInstancePaths(instance); return paths.FullAppPath; }
private void RegisterWithRouter(Instance instance, string[] uris) { if (shutting_down || instance.Uris.IsNullOrEmpty()) { return; } var routerRegister = new RouterRegister { Dea = natsClient.UniqueIdentifier, Host = instance.Host, Port = instance.Port, Uris = uris ?? instance.Uris, Tag = new Tag { Framework = instance.Framework, Runtime = instance.Runtime } }; natsClient.Publish(routerRegister); }
private Configuration getConfiguration(Instance instance) { var webConfigPath = Path.Combine(config.AppDir, instance.Staged, "app", "Web.config"); var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = webConfigPath }; return ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); }
private void SendExitedNotification(Instance instance) { if (false == instance.IsEvacuated) { natsClient.Publish(new InstanceExited(instance)); log.Debug(Resources.Agent_InstanceExited_Fmt, instance.LogID); } }
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(); } }
private void StopDroplet(Instance instance) { if (instance.StopProcessed) { return; } log.Info(Resources.Agent_StoppingInstance_Fmt, instance.LogID); SendExitedMessage(instance); webServerProvider.UninstallWebApp(instance.Staged); dropletManager.InstanceStopped(instance); instance.DeaStopComplete(); filesManager.CleanupInstanceDirectory(instance); log.Info(Resources.Agent_StoppedInstance_Fmt, instance.LogID); }
private void UnregisterWithRouter(Instance instance) { UnregisterWithRouter(instance, instance.Uris); }
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; }