Beispiel #1
0
        /// <summary>
        /// Create a new document store
        /// </summary>
        /// <param name="serverName">The name of the server which you want to create a document store for.</param>
        /// <returns></returns>
        private Lazy <IDocumentStore> CreateDocumentStore(
            string serverName)
        {
            ThrowIfDisposed();

            if (!_servers.ContainsKey(serverName))
            {
                throw new UnknownServerException("Unable to find specified server: {0}.", serverName);
            }

            var server = _servers[serverName];

            return(new Lazy <IDocumentStore>(() =>
            {
                var store = RavenHelpers.CreateDocumentStore(_host, server);
                store.Initialize();
                return store;
            }));
        }
Beispiel #2
0
        public static RavenStoreBuilder AddRavenStore(
            this IServiceCollection services)
        {
            services.AddSingleton(provider =>
            {
                var options = provider
                              .GetService <IOptions <RavenStoreOptions> >()?.Value;

                var host = provider
                           .GetService <IHostingEnvironment>();

                var store = RavenHelpers.CreateDocumentStore(host, options);
                store.Initialize();

                return(store);
            });

            return(new RavenStoreBuilder(services));
        }
Beispiel #3
0
        public static void Install(int port = 0, string installPath = null)
        {
            if (string.IsNullOrEmpty(installPath))
            {
                installPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles).Replace(" (x86)", String.Empty), DefaultDirectoryName);
            }

            if (Directory.Exists(installPath))
            {
                Console.Out.WriteLine("Path '{0}' already exists please update RavenDB manually if needed.", installPath);
                return;
            }

            var serviceName = "RavenDB";

            var service = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == serviceName);

            if (service != null)
            {
                Console.Out.WriteLine("There is already a RavenDB service installed on this computer, the RavenDB service status is {0}.", service.Status);

                if (IsRavenDBv2RunningOn(8080)) //todo: we can improve this in the future by looking into the config file to try to figure out the port
                {
                    Console.Out.WriteLine("Existing Raven is v2, NServiceBus will be configured to use it");

                    SavePortToBeUsedForRavenInRegistry(8080);

                    return;
                }

                serviceName = serviceName + "-v2";
            }

            int availablePort;

            //Check if the port is available, if so let the installer setup raven if its being run
            if (port > 0)
            {
                availablePort = PortUtils.FindAvailablePort(port);
                if (availablePort != port)
                {
                    if (IsRavenDBv2RunningOn(port))
                    {
                        Console.Out.WriteLine("A compatible(v2) version of Raven has been found on port {0} and will be used", port);

                        SavePortToBeUsedForRavenInRegistry(port);
                    }
                    else
                    {
                        Console.Out.WriteLine("Port '{0}' isn't available, please specify a different port and rerun the command.", port);
                    }

                    return;
                }
            }
            else
            {
                availablePort = PortUtils.FindAvailablePort(DefaultPort);

                //is there already a Raven2 on the default port?
                if (availablePort != DefaultPort && IsRavenDBv2RunningOn(DefaultPort))
                {
                    Console.Out.WriteLine("A compatible(v2) version of Raven has been found on port {0} and will be used", DefaultPort);

                    SavePortToBeUsedForRavenInRegistry(DefaultPort);
                    return;
                }
            }

            if (!RavenHelpers.EnsureCanListenToWhenInNonAdminContext(availablePort))
            {
                Console.WriteLine("Failed to grant rights for listening to http on port {0}, please specify a different port and rerun the command.", availablePort);
                return;
            }

            if (!Directory.Exists(installPath))
            {
                Directory.CreateDirectory(installPath);
            }

            Console.WriteLine("Unpacking resources...");

            ExportRavenResources(installPath);

            var ravenConfigPath = Path.Combine(installPath, "Raven.Server.exe.config");
            var ravenConfig     = new XmlDocument();

            ravenConfig.Load(ravenConfigPath);

            var key = (XmlElement)ravenConfig.DocumentElement.SelectSingleNode("//add[@key='Raven/Port']");

            key.SetAttribute("value", availablePort.ToString(CultureInfo.InvariantCulture));

            ravenConfig.Save(ravenConfigPath);
            Console.Out.WriteLine("Updated Raven configuration to use port {0}.", availablePort);

            SavePortToBeUsedForRavenInRegistry(availablePort);

            Console.WriteLine("Installing RavenDB as a windows service.");

            var startInfo = new ProcessStartInfo
            {
                CreateNoWindow         = true,
                UseShellExecute        = false,
                RedirectStandardOutput = true,
                WorkingDirectory       = installPath,
                Arguments = "--install --service-name=" + serviceName,
                FileName  = Path.Combine(installPath, "Raven.Server.exe")
            };

            using (var process = Process.Start(startInfo))
            {
                process.WaitForExit(20000);

                var line = process.StandardOutput.ReadToEnd();
                Console.WriteLine(line);

                if (process.ExitCode != 0)
                {
                    Console.WriteLine("The RavenDB service failed to start, Raven.Server.exe exit code was {0}.", process.ExitCode);
                    return;
                }
            }

            Console.WriteLine("{0} service started, listening on port: {1}", serviceName, availablePort);
        }
Beispiel #4
0
        public bool Install(WindowsIdentity identity, int port = 0, string installPath = null, bool allowInstall = false)
        {
            if (string.IsNullOrEmpty(installPath))
            {
                installPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), DefaultDirectoryName);
            }

            if (Directory.Exists(installPath))
            {
                Console.Out.WriteLine("Path {0} already exists please update RavenDB manually if needed", installPath);
                return(true);
            }

            var service = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == "RavenDB");

            if (service != null)
            {
                Console.Out.WriteLine("There is already a RavenDB service installed on this computer, current status:{0}", service.Status);
                return(true);
            }

            if (!allowInstall)
            {
                return(false);
            }

            //Check if the port is available, if so let the installer setup raven if its beeing run
            if (port > 0 && !RavenHelpers.EnsureCanListenToWhenInNonAdminContext(port))
            {
                Console.Out.WriteLine("Port {0} isn't available please choose a different port an rerun the command", port);
                return(false);
            }

            if (!Directory.Exists(installPath))
            {
                Directory.CreateDirectory(installPath);
            }

            Console.WriteLine("Unpacking resources");
            string ravenConfigData = null;

            foreach (DictionaryEntry entry in RavenServer.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true))
            {
                var fileName = entry.Key.ToString().Replace("_", ".");

                var data = entry.Value as byte[];

                switch (fileName)
                {
                case "Raven.Studio":
                    fileName += ".xap";
                    break;

                case "Raven.Server":
                    fileName += ".exe";
                    break;

                case "Raven.Server.exe":
                    fileName       += ".config";
                    ravenConfigData = (string)entry.Value;
                    data            = null;
                    break;

                default:
                    fileName += ".dll";
                    break;
                }

                var destinationPath = Path.Combine(installPath, fileName);

                if (data == null)
                {
                    continue;
                }

                Console.WriteLine("Unpacking {0} to {1}", entry.Key, destinationPath);


                File.WriteAllBytes(destinationPath, data);
            }

            if (port > 0)
            {
                var ravenConfigPath = Path.Combine(installPath, "Raven.Server.exe.config");
                var ravenConfig     = new XmlDocument();

                ravenConfig.LoadXml(ravenConfigData);

                var key = (XmlElement)ravenConfig.DocumentElement.SelectSingleNode("//add[@key='Raven/Port']");

                key.SetAttribute("value", port.ToString());

                ravenConfig.Save(ravenConfigPath);
                Console.Out.WriteLine("Updated Raven configuration to use port: {0}", port);
            }

            Console.WriteLine("Installing RavenDB as a windows service");

            var startInfo = new ProcessStartInfo
            {
                CreateNoWindow         = true,
                UseShellExecute        = false,
                RedirectStandardOutput = true,
                WorkingDirectory       = installPath,
                Arguments = "/install",
                FileName  = Path.Combine(installPath, "Raven.Server.exe")
            };

            using (var process = Process.Start(startInfo))
            {
                process.WaitForExit(20000);

                var line = process.StandardOutput.ReadToEnd();
                Console.WriteLine(line);

                if (process.ExitCode != 0)
                {
                    Console.WriteLine("The RavenDB service failed to start service, exit code: {0}", process.ExitCode);
                    return(false);
                }
            }

            Console.WriteLine("RavenDB service started");
            return(true);
        }