示例#1
0
        private void CleanupStagingInstances()
        {
            if (!File.Exists(this.stagingTaskRegistry.StagingStateFile))
            {
                return;
            }

            object[] instances = JsonConvertibleObject.DeserializeFromJsonArray(File.ReadAllText(this.stagingTaskRegistry.StagingStateFile));

            foreach (object obj in instances)
            {
                StagingInstance instance = null;
                try
                {
                    instance = new StagingInstance();
                    instance.Properties.FromJsonIntermediateObject(obj);

                    var prisonInfo = new ProcessPrisonCreateInfo();
                    prisonInfo.Id = instance.Properties.InstanceId;

                    prisonInfo.TotalPrivateMemoryLimitBytes = instance.Properties.MemoryQuotaBytes;
                    prisonInfo.WindowsPassword = instance.Properties.WindowsPassword;

                    if (this.useDiskQuota)
                    {
                        prisonInfo.DiskQuotaBytes = instance.Properties.DiskQuotaBytes;
                        prisonInfo.DiskQuotaPath = instance.Properties.Directory;
                    }

                    Logger.Info("Recovering Process Prisson: {0}", prisonInfo.Id);

                    instance.Prison.Attach(prisonInfo);
                    foreach (Process p in instance.Prison.jobObject.GetJobProcesses())
                    {
                        if (!p.HasExited)
                        {
                            p.Kill();
                        }
                    }
                    if (instance.Prison.Created)
                    {
                        instance.Prison.Destroy();
                    }
                    instance.Workspace = new StagingWorkspace(instance.Properties.Directory);
                    instance.Cleanup();
                }
                catch (Exception ex)
                {
                    Logger.Warning("Error deleting staging environment for task {0}: {1}", instance.Properties.TaskId, ex.ToString());
                }
                finally
                {
                    this.stagingTaskRegistry.RemoveStagingInstance(instance);
                    if (instance != null)
                    {
                        instance.Dispose();
                    }
                }
            }
        }
示例#2
0
        private void AfterStagingFinished(StagingInstance instance)
        {
            StagingStartMessageResponse response = new StagingStartMessageResponse();
            try
            {
                if (instance.StagingException == null)
                {
                    try
                    {
                        Logger.Info("Staging task {0}: Saving buildpackInfo", instance.Properties.TaskId);
                        StagingInfo.SaveBuildpackInfo(Path.Combine(instance.Workspace.StagedDir, StagingWorkspace.StagingInfo), instance.Buildpack.Name, instance.GetStartCommand());
                        this.stagingTaskRegistry.ScheduleSnapshotStagingState();

                        Logger.Debug("Staging task {0}: Packing droplet {1}", instance.Properties.TaskId, instance.Workspace.StagedDropletPath);
                        Directory.CreateDirectory(instance.Workspace.StagedDropletDir);
                        string tempFile = Path.ChangeExtension(instance.Workspace.StagedDropletPath, "tar");
                        DEAUtilities.TarDirectory(instance.Workspace.StagedDir, tempFile);
                        DEAUtilities.GzipFile(tempFile, instance.Workspace.StagedDropletPath);
                        File.Delete(tempFile);

                        if (File.Exists(instance.Workspace.StagedDropletPath))
                        {
                            using (Stream stream = File.OpenRead(instance.Workspace.StagedDropletPath))
                            {
                                using (SHA1 sha = SHA1.Create())
                                {
                                    response.DropletSHA = BitConverter.ToString(sha.ComputeHash(stream)).Replace("-", string.Empty);
                                }
                            }
                        }

                        this.StartStagedDropletInstance(instance, response.DropletSHA);

                        Uri uri = new Uri(instance.Properties.UploadURI);
                        Logger.Debug("Staging task {0}: Uploading droplet {1} to {2}", instance.Properties.TaskId, instance.Workspace.StagedDropletPath, instance.Properties.UploadURI);
                        DEAUtilities.HttpUploadFile(instance.Properties.UploadURI, new FileInfo(instance.Workspace.StagedDropletPath), "upload[droplet]", "application/octet-stream", uri.UserInfo);
                    }
                    catch (Exception ex)
                    {
                        instance.StagingException = ex;
                    }
                    try
                    {
                        Directory.CreateDirectory(instance.Workspace.Cache);
                        string tempFile = Path.ChangeExtension(instance.Workspace.StagedBuildpackCachePath, "tar");
                        DEAUtilities.TarDirectory(instance.Workspace.Cache, tempFile);
                        DEAUtilities.GzipFile(tempFile, instance.Workspace.StagedBuildpackCachePath);
                        Uri uri = new Uri(instance.Properties.BuildpackCacheUploadURI);
                        Logger.Debug("Staging task {0}: Uploading buildpack cache {1} to {2}", instance.Properties.TaskId, instance.Workspace.StagedBuildpackCachePath, instance.Properties.BuildpackCacheUploadURI);
                        DEAUtilities.HttpUploadFile(instance.Properties.BuildpackCacheUploadURI, new FileInfo(instance.Workspace.StagedBuildpackCachePath), "upload[droplet]", "application/octet-stream", uri.UserInfo);
                    }
                    catch
                    {
                        Logger.Debug("Staging task {0}: Cannot pack buildpack cache", instance.Properties.TaskId);
                    }
                }

                if (instance.StagingException != null)
                {
                    response.Error = instance.StagingException.ToString();
                }

                response.TaskId = instance.Properties.TaskId;

                // try to read log. don't throw exception if it fails
                try
                {
                    response.TaskLog = File.ReadAllText(instance.Properties.TaskLog);
                }
                catch { }

                if (instance.Properties.DetectedBuildpack != null)
                {
                    response.DetectedBuildpack = instance.Properties.DetectedBuildpack;
                }

                this.deaReactor.SendReply(instance.Properties.Reply, response.SerializeToJson());
                Logger.Debug("Staging task {0}: sent reply {1}", instance.Properties.TaskId, response.SerializeToJson());
            }
            finally
            {
                Logger.Debug("Cleaning up directory {0}", instance.Workspace.BaseDir);
                instance.Cleanup();
            }
        }