Пример #1
0
        private static bool AutoInstallService(string path)
        {
            if (!File.Exists(path))
            {
                return(false);
            }

            var root = PackageManagerSettings.CoAppRootDirectory;
            var coappBinDirectory = Path.Combine(root, "bin");

            if (!Directory.Exists(coappBinDirectory))
            {
                Directory.CreateDirectory(coappBinDirectory);
            }
            var canonicalServiceExePath = Path.Combine(coappBinDirectory, "coapp.service.exe");

            if (Symlink.IsSymlink(path))
            {
                // we found a symlink,
                if (!File.Exists(Symlink.GetActualPath(path)))
                {
                    // it is invalid anyway. trash it, try again.
                    Symlink.DeleteSymlink(path);
                }
                return(false);
            }

            try {
                Symlink.MakeFileLink(canonicalServiceExePath, path);

                // hey we found one where it's supposed to be!
                var processStartInfo = new ProcessStartInfo(canonicalServiceExePath)
                {
                    Arguments       = "--start",
                    CreateNoWindow  = true,
                    WindowStyle     = ProcessWindowStyle.Hidden,
                    UseShellExecute = false,
                };

                var process = Process.Start(processStartInfo);
                process.WaitForExit();

                // after it exits, lets see if we've got an installed service
                if (EngineServiceManager.IsServiceInstalled)
                {
                    // YAY!. We're outta here!
                    EngineServiceManager.TryToStartService();
                    return(true);
                }
            } catch {
                // hmm. not working...
            }
            return(false);
        }
Пример #2
0
        /// <summary>
        ///   Mains this instance.
        /// </summary>
        /// <remarks>
        /// </remarks>
        private Task Main()
        {
            if (IsRunning)
            {
                return(_engineTask);
            }
            Signals.EngineStartupStatus = 0;

            if (!IsInteractive)
            {
                EngineServiceManager.EnsureServiceAclsCorrect();
            }

            var npmi = PackageManagerImpl.Instance;

            _cancellationTokenSource = new CancellationTokenSource();
            _isRunning = true;

            Signals.StartingUp = true;
            // make sure coapp is properly set up.
            Task.Factory.StartNew(() => {
                try {
                    Logger.Warning("CoApp Startup Beginning------------------------------------------");

                    // this ensures that composition rules are run for toolkit.
                    Signals.EngineStartupStatus = 5;
                    EnsureCanonicalFoldersArePresent();
                    Signals.EngineStartupStatus = 10;
                    var v = Package.GetCurrentPackageVersion(CanonicalName.CoAppItself);
                    if (v > 0)
                    {
                        var p = InstalledPackageFeed.Instance.FindPackages(CanonicalName.CoAppItself).HighestPackages().FirstOrDefault();
                        if (p != null)
                        {
                            p.DoPackageComposition();
                            // put the coapp.exe symlink right into the OS directory. Yeah, evil, .... what are ya gonna do about that?
                            var coappexe = Path.Combine(p.PackageDirectory, "coapp.exe");
                            if (File.Exists(coappexe))
                            {
                                Symlink.MakeFileLink(Path.Combine(Environment.GetEnvironmentVariable("SYSTEMROOT") ?? "c:\\windows", "coapp.exe"), coappexe);
                            }
                        }
                    }
                    Signals.EngineStartupStatus = 95;
                    Logger.Warning("CoApp Version : " + v);

                    /*
                     * what can we do if the right version isn't here?
                     *
                     * FourPartVersion thisVersion = Assembly.GetExecutingAssembly().Version();
                     * if( thisVersion > v ) {
                     *
                     * }
                     * */

                    // Completes startup.

                    Signals.EngineStartupStatus = 100;
                    Signals.Available           = true;
                    Logger.Warning("CoApp Startup Finished------------------------------------------");
                } catch (Exception e) {
                    Logger.Error(e);
                    RequestStop();
                }
            });

            _engineTask = Task.Factory.StartNew(() => {
                _pipeSecurity = new PipeSecurity();
                _pipeSecurity.AddAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), PipeAccessRights.ReadWrite, AccessControlType.Allow));
                _pipeSecurity.AddAccessRule(new PipeAccessRule(WindowsIdentity.GetCurrent().Owner, PipeAccessRights.FullControl, AccessControlType.Allow));

                // start a few listeners by default--each listener will also spawn a new empty one.
                StartListener();
                StartListener();
            }, _cancellationTokenSource.Token).AutoManage();

            _engineTask = _engineTask.ContinueWith(antecedent => {
                RequestStop();
                // ensure the sessions are all getting closed.
                Session.CancelAll();
                _engineTask = null;
            }, TaskContinuationOptions.AttachedToParent).AutoManage();
            return(_engineTask);
        }
Пример #3
0
        private bool ApplyRule(CompositionRule rule)
        {
            switch (rule.Action)
            {
            case CompositionAction.DownloadFile:
                if (!File.Exists(rule.Destination))
                {
                    PackageSessionData.DownloadFile(rule.Source, rule.Destination);
                }
                return(true);

            case CompositionAction.InstallCommand:
                return(ExecuteCommand(rule.Source, rule.Destination));

            case CompositionAction.RemoveCommand:
                // we never 'apply' remove commands. Just remove em'
                return(true);

            case CompositionAction.FileCopy:
                // file copy operations may only manipulate files in the package directory.
                if (string.IsNullOrEmpty(rule.Source))
                {
                    Logger.Error("ERROR: Illegal file copy rule. Source must be in package directory [{0}] => [{1}]", rule.Source, rule.Destination);
                    return(false);
                }

                if (string.IsNullOrEmpty(rule.Destination))
                {
                    Logger.Error("ERROR: Illegal file copy rule. Destination must be in package directory [{0}] => [{1}]", rule.Source, rule.Destination);
                    return(false);
                }

                if (!File.Exists(rule.Source))
                {
                    Logger.Error("ERROR: Illegal file copy rule. Source file does not exist [{0}] => [{1}]", rule.Source, rule.Destination);
                    return(false);
                }
                try {
                    var destParent = Path.GetDirectoryName(rule.Destination);
                    if (!string.IsNullOrEmpty(destParent))
                    {
                        if (!Directory.Exists(destParent))
                        {
                            Directory.CreateDirectory(destParent);
                        }
                        File.Copy(rule.Source, rule.Destination, true);
                    }
                }
                catch (Exception e) {
                    Logger.Error(e);
                }
                return(true);

            case CompositionAction.FileRewrite:
                // file copy operations may only manipulate files in the package directory.
                if (string.IsNullOrEmpty(rule.Source))
                {
                    Logger.Error("ERROR: Illegal file rewrite rule. Source must be in package directory [{0}] => [{1}]", rule.Source, rule.Destination);
                    return(false);
                }

                if (string.IsNullOrEmpty(rule.Destination))
                {
                    Logger.Error("ERROR: Illegal file rewrite rule. Destination must be in package directory [{0}] => [{1}]", rule.Source, rule.Destination);
                    return(false);
                }

                if (!File.Exists(rule.Source))
                {
                    Logger.Error("ERROR: Illegal file rewrite rule. Source file does not exist [{0}] => [{1}]", rule.Source, rule.Destination);
                    return(false);
                }
                File.WriteAllText(rule.Destination, ResolveVariables(File.ReadAllText(rule.Source)));
                return(true);

            case CompositionAction.SymlinkFile:
                if (string.IsNullOrEmpty(rule.Destination))
                {
                    Logger.Error("ERROR: Illegal file symlink rule. Destination location '{0}' must be a subpath of apps dir", rule.Destination);
                    return(false);
                }

                if (string.IsNullOrEmpty(rule.Source))
                {
                    Logger.Error("ERROR: Illegal file symlink rule. Source file '{0}' must be a subpath of package directory", rule.Source);
                    return(false);
                }

                if (!File.Exists(rule.Source))
                {
                    Logger.Error("ERROR: Illegal folder symlink rule. Source file '{0}' does not exist.", rule.Source);
                    return(false);
                }

                var parentDir = Path.GetDirectoryName(rule.Destination);
                if (!string.IsNullOrEmpty(parentDir))
                {
                    if (!Directory.Exists(parentDir))
                    {
                        Directory.CreateDirectory(parentDir);
                    }
                    try {
                        // Logger.Message("Creating file Symlink [{0}] => [{1}]", rule.Destination, rule.Source);
                        Symlink.MakeFileLink(rule.Destination, rule.Source);
                    }
                    catch (Exception) {
                        Logger.Error("Warning: File Symlink Link Failed. [{0}] => [{1}]", rule.Destination, rule.Source);
                    }
                }
                return(true);

            case CompositionAction.SymlinkFolder:
                if (string.IsNullOrEmpty(rule.Destination))
                {
                    Logger.Error("ERROR: Illegal folder symlink rule. Destination location '{0}' must be a subpath of appsdir", rule.Destination);
                    return(false);
                }

                if (string.IsNullOrEmpty(rule.Source))
                {
                    Logger.Error("ERROR: Illegal folder symlink rule. Source folder '{0}' must be a subpath of package directory{1}", rule.Source);
                    return(false);
                }

                if (!Directory.Exists(rule.Source))
                {
                    Logger.Error("ERROR: Illegal folder symlink rule. Source folder '{0}' does not exist.", rule.Source);
                    return(false);
                }

                try {
                    // Logger.Message("Creatign Directory Symlink [{0}] => [{1}]", rule.Destination, rule.Source);
                    Symlink.MakeDirectoryLink(rule.Destination, rule.Source);
                }
                catch (Exception) {
                    Logger.Error("Warning: Directory Symlink Link Failed. [{0}] => [{1}]", rule.Destination, rule.Source);
                    return(false);
                }
                return(true);

            case CompositionAction.Shortcut:
                if (string.IsNullOrEmpty(rule.Source))
                {
                    Logger.Error("ERROR: Illegal shortcut rule. Source file '{0}' must be a subpath of package directory", rule.Source);
                    return(false);
                }

                if (!File.Exists(rule.Source))
                {
                    Logger.Error("ERROR: Illegal shortcut rule. Source file '{0}' does not exist.", rule.Source);
                    return(false);
                }

                var pDir = Path.GetDirectoryName(rule.Destination);
                if (!string.IsNullOrEmpty(pDir))
                {
                    if (!Directory.Exists(pDir))
                    {
                        Directory.CreateDirectory(pDir);
                    }
                    // Logger.Message("Creating Shortcut [{0}] => [{1}]", rule.Destination, rule.Source);
                    ShellLink.CreateShortcut(rule.Destination, rule.Source, workingDirectory: "");
                }

                return(true);

            case CompositionAction.EnvironmentVariable:
                switch (rule.Key.ToLower())
                {
                case "path":
                case "pathext":
                case "psmodulepath":
                case "comspec":
                case "temp":
                case "tmp":
                case "username":
                case "windir":
                case "allusersprofile":
                case "appdata":
                case "commonprogramfiles":
                case "commonprogramfiles(x86)":
                case "commonprogramw6432":
                case "computername":
                case "current_cpu":
                case "FrameworkVersion":
                case "homedrive":
                case "homepath":
                case "logonserver":
                case "number_of_processors":
                case "os":
                case "processor_architecture":
                case "processor_identifier":
                case "processor_level":
                case "processor_revision":
                case "programdata":
                case "programfiles":
                case "programfiles(x86)":
                case "programw6432":
                case "prompt":
                case "public":
                case "systemdrive":
                case "systemroot":
                case "userdomain":
                case "userprofile":
                    Logger.Warning("Package may not set environment variable '{0}'", rule.Key);
                    return(true);

                default:
                    EnvironmentUtility.SetSystemEnvironmentVariable(rule.Key, rule.Value);
                    return(true);
                }


            case CompositionAction.Registry:
                if (CanonicalName.Architecture == Architecture.x64 && Environment.Is64BitOperatingSystem)
                {
                    RegistryView.System["SOFTWARE"][rule.Key].StringValue = rule.Value;
                }
                else
                {
                    RegistryView.System["SOFTWARE\\Wow6432Node"][rule.Key].StringValue = rule.Value;
                }
                return(true);
            }
            return(true);
        }