private static string PackageArtifactFromFolder(IEnvironment environment, IDeploymentSettingsManager settings, DeploymentContext context, string srcDirectory, string artifactDirectory, string artifactFilename)
        {
            context.Logger.Log("Writing the artifacts to a squashfs file");
            string file = Path.Combine(artifactDirectory, artifactFilename);
            ExternalCommandFactory commandFactory = new ExternalCommandFactory(environment, settings, context.RepositoryPath);
            Executable             exe            = commandFactory.BuildExternalCommandExecutable(srcDirectory, artifactDirectory, context.Logger);

            try
            {
                exe.ExecuteWithProgressWriter(context.Logger, context.Tracer, $"mksquashfs . {file} -noappend");
            }
            catch (Exception)
            {
                context.GlobalLogger.LogError();
                throw;
            }

            int numOfArtifacts = OryxBuildConstants.FunctionAppBuildSettings.ConsumptionBuildMaxFiles;

            DeploymentHelper.PurgeBuildArtifactsIfNecessary(artifactDirectory, BuildArtifactType.Squashfs, context.Tracer, numOfArtifacts);
            return(file);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Package every files and sub directories from a source folder
        /// </summary>
        /// <param name="context">The deployment context in current scope</param>
        /// <param name="srcDirectory">The source directory to be packed</param>
        /// <param name="artifactDirectory">The destination directory to eject the build artifact</param>
        /// <param name="artifactFilename">The filename of the build artifact</param>
        /// <param name="artifactType">The method for packing the artifact</param>
        /// <param name="numBuildArtifacts">The number of temporary artifacts should be hold in the destination directory</param>
        /// <returns></returns>
        private string PackageArtifactFromFolder(DeploymentContext context, string srcDirectory, string artifactDirectory, string artifactFilename, BuildArtifactType artifactType, int numBuildArtifacts = 0)
        {
            context.Logger.Log($"Writing the artifacts to {artifactType.ToString()} file at {artifactDirectory}");
            string file = Path.Combine(artifactDirectory, artifactFilename);
            var    exe  = ExternalCommandFactory.BuildExternalCommandExecutable(srcDirectory, artifactDirectory, context.Logger);

            try
            {
                switch (artifactType)
                {
                case BuildArtifactType.Zip:
                    exe.ExecuteWithProgressWriter(context.Logger, context.Tracer, $"zip -r -0 -q {file} .");
                    break;

                case BuildArtifactType.Squashfs:
                    exe.ExecuteWithProgressWriter(context.Logger, context.Tracer, $"mksquashfs . {file} -noappend");
                    break;

                default:
                    throw new ArgumentException($"Received unknown file extension {artifactType.ToString()}");
                }
            }
            catch (Exception)
            {
                context.GlobalLogger.LogError();
                throw;
            }

            // Just to be sure that we don't keep adding build artifacts here
            if (numBuildArtifacts > 0)
            {
                DeploymentHelper.PurgeBuildArtifactsIfNecessary(artifactDirectory, artifactType, context.Tracer, numBuildArtifacts);
            }

            return(file);
        }
Ejemplo n.º 3
0
        protected virtual IProcess CreateProcess(string connectionId, string shell)
        {
            var externalCommandFactory = new ExternalCommandFactory(_environment, _settings, _environment.RootPath);
            var exe       = externalCommandFactory.BuildExternalCommandExecutable(_environment.RootPath, _environment.WebRootPath, NullLogger.Instance);
            var startInfo = new ProcessStartInfo()
            {
                UseShellExecute        = false,
                CreateNoWindow         = true,
                RedirectStandardError  = true,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                WorkingDirectory       = _environment.RootPath
            };

            if (shell.Equals("powershell", StringComparison.OrdinalIgnoreCase))
            {
                startInfo.FileName  = System.Environment.ExpandEnvironmentVariables(@"%windir%\System32\WindowsPowerShell\v1.0\powershell.exe");
                startInfo.Arguments = "-File -";
            }
            else
            {
                startInfo.FileName  = System.Environment.ExpandEnvironmentVariables(@"%windir%\System32\cmd.exe");
                startInfo.Arguments = "/Q";
            }

            foreach (var environmentVariable in exe.EnvironmentVariables)
            {
                startInfo.EnvironmentVariables[environmentVariable.Key] = environmentVariable.Value;
            }

            // add '>' to distinguish PROMPT from other output
            startInfo.EnvironmentVariables["PROMPT"] = "$P$G";

            // dir cmd would list folders then files alpabetically
            // consistent with FileBrowser ui.
            startInfo.EnvironmentVariables["DIRCMD"] = "/OG /ON";

            var process = new Process
            {
                StartInfo           = startInfo,
                EnableRaisingEvents = true
            };

            process.Exited += delegate
            {
                SafeInvoke(() =>
                {
                    ProcessInfo temp;
                    _processes.TryRemove(connectionId, out temp);
                    Connection.Send(connectionId, new { Output = "\r\nprocess [" + process.Id + "] terminated!  Press ENTER to start a new cmd process.\r\n", RunningProcessesCount = _processes.Count }).Wait();
                });
            };

            process.Start();

            EnsureIdleTimer();

            HookProcessStreamsToConnection(process, connectionId);

            return(new ProcessWrapper(process));
        }
Ejemplo n.º 4
0
        public void ExecuteCommandAsync(string command, string relativeWorkingDirectory)
        {
            string workingDirectory;
            if (String.IsNullOrEmpty(relativeWorkingDirectory))
            {
                workingDirectory = _rootDirectory;
            }
            else
            {
                workingDirectory = Path.Combine(_rootDirectory, relativeWorkingDirectory);
            }

            Executable exe = _externalCommandFactory.BuildExternalCommandExecutable(workingDirectory, _environment.WebRootPath, NullLogger.Instance);
            _executingProcess = exe.CreateProcess(command, new object[0]);

            var commandEvent = CommandEvent;

            _executingProcess.Exited += (sender, e) =>
            {
                if (commandEvent != null)
                {
                    commandEvent(new CommandEvent(CommandEventType.Complete)
                    {
                        ExitCode = _executingProcess.ExitCode
                    });
                }
            };

            _executingProcess.OutputDataReceived += (sender, e) =>
            {
                if (e.Data == null)
                {
                    return;
                }

                if (commandEvent != null)
                {
                    commandEvent(new CommandEvent(CommandEventType.Output, e.Data));
                }
            };

            _executingProcess.ErrorDataReceived += (sender, e) =>
            {
                if (e.Data == null)
                {
                    return;
                }

                if (commandEvent != null)
                {
                    commandEvent(new CommandEvent(CommandEventType.Error, e.Data));
                }
            };

            _executingProcess.EnableRaisingEvents = true;
            _executingProcess.Start();
            _executingProcess.BeginErrorReadLine();
            _executingProcess.BeginOutputReadLine();

            _executingProcess.StandardInput.Close();
        }