internal void Start(Business.Server server, SetStatusLabelDelegate setStatusLabelDelegate) { string userName = null; string password = null; if (server.UseImpersonation == true) { userName = server.UserName; password = Encryption.Decrypt(server.Password); } List <WmiService> services = WmiService.GetAllServices(server.ServerName, userName, password, "Name = '" + this.ServiceName + "'"); foreach (WmiService service in services) { setStatusLabelDelegate("Starting " + service.DisplayName); ReturnValue returnValue; if (service.State != State.Running && service.State != State.StartPending) { if ((returnValue = WmiService.Start(server.ServerName, userName, password, service.Name)) != ReturnValue.Success) { throw new Exception("Couldn't start service: " + service.DisplayName + ", the result was: " + returnValue); } } } setStatusLabelDelegate("Ready."); }
public void ReadInstancesFromServer() { List <WmiService> services; if (this.UseImpersonation == true) { services = WmiService.GetAllServices(this.ServerName, this.UserName, Encryption.Decrypt(this.Password), "PathName LIKE '%memcached%'"); } else { services = WmiService.GetAllServices(this.ServerName, null, null, "PathName LIKE '%memcached%'"); } Instances.Clear(); foreach (WmiService service in services) { Instance instance = new Instance(); instance.DisplayName = service.DisplayName; instance.ServiceName = service.Name; instance.ParseImagePath(service.PathName); Instances.Add(instance); } //ImpersonateUser impersonateUser = null; //try //{ // if (this.UseImpersonation == true) // { // impersonateUser = new ImpersonateUser(UserName, Encryption.Decrypt(Password)); // } // RegistryKey serviceBranchKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, this.ServerName).OpenSubKey(@"SYSTEM\CurrentControlSet\Services"); // Instances.Clear(); // foreach (string serviceName in serviceBranchKey.GetSubKeyNames()) // { // RegistryKey serviceKey = serviceBranchKey.OpenSubKey(serviceName); // string imagePath; // if ((imagePath = (string)serviceKey.GetValue("ImagePath", String.Empty)) != String.Empty) // { // if (imagePath.ToLower().Contains("memcached.exe") == true) // { // Instance instance = new Instance(); // instance.DisplayName = (string)serviceKey.GetValue("DisplayName", String.Empty); // Instances.Add(instance); // } // } // } //} //finally //{ // if(impersonateUser != null && impersonateUser.IsImpersonating == true) // impersonateUser.Undo(); //} }
internal void RemoveFromServer(Business.Server server, SetStatusLabelDelegate setStatusLabelDelegate) { string userName = null; string password = null; if (server.UseImpersonation == true) { userName = server.UserName; password = Encryption.Decrypt(server.Password); } List <WmiService> services = WmiService.GetAllServices(server.ServerName, userName, password, "Name = '" + this.ServiceName + "'"); // Saftey in case we get too many services back, we don't want to delete them all... if (services.Count > 20) { StringBuilder servicesToRemove = new StringBuilder("This action will remove the following services, are you sure?\n"); foreach (WmiService service in services) { servicesToRemove.AppendFormat("\n{0}", service.DisplayName); } if (MessageBox.Show(servicesToRemove.ToString(), "Are you sure?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel) { return; } } foreach (WmiService service in services) { setStatusLabelDelegate("Removing " + service.DisplayName); ReturnValue returnValue; if (service.State != State.Stopped) { if ((returnValue = WmiService.Stop(server.ServerName, userName, password, service.Name)) != ReturnValue.Success) { throw new Exception("Couldn't stop service: " + service.DisplayName + ", the result was: " + returnValue); } } if ((returnValue = WmiService.Delete(server.ServerName, userName, password, service.Name)) != ReturnValue.Success) { throw new Exception("Couldn't remove service: " + service.DisplayName + ", the result was: " + returnValue); } } setStatusLabelDelegate("Ready."); }
internal State GetServiceState(Server server) { string userName = null; string password = null; if (server.UseImpersonation == true) { userName = server.UserName; password = Encryption.Decrypt(server.Password); } List <WmiService> services = WmiService.GetAllServices(server.ServerName, userName, password, "Name = '" + this.ServiceName + "'"); if (services.Count > 0) { return(services[0].State); } return(State.Unknown); }
private string GetNextMemcachedInstanceName() { List <WmiService> services; if (_server.UseImpersonation == true) { services = WmiService.GetAllServices(_server.ServerName, _server.UserName, Encryption.Decrypt(_server.Password), "PathName LIKE '%memcached%'"); } else { services = WmiService.GetAllServices(_server.ServerName, null, null, "PathName LIKE '%memcached%'"); } int highestInstanceNumber = 0; Regex instanceFinder = new Regex( "\\w+_(?<instanceNumber>\\d+)", RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.Compiled ); foreach (WmiService service in services) { Match match = instanceFinder.Match(service.Name); if (match.Success == true) { int instanceNumber = 0; if (int.TryParse(match.Groups["instanceNumber"].Value, out instanceNumber) == true && instanceNumber > highestInstanceNumber) { highestInstanceNumber = instanceNumber; } } } int newInstanceNumber = highestInstanceNumber + 1; return("MemCacheD_" + newInstanceNumber); }
private void SaveChanges() { Cursor = Cursors.WaitCursor; try { SetStatusLabel("Checking server name."); if (IsUserAccountInfomationValid() == false) { return; } if (IsServerNameAcceptable() == false) { return; } if (DoesServerNameAlreadyExistInList() == true) { return; } string userName = null; string password = null; if (chkUseImpersonation.Checked == true) { userName = txtUserName.Text; password = txtPassword.Text; } SetStatusLabel("Checking connection and authorization."); // Test the credentials. try { WmiService.GetAllServices(txtServerName.Text, userName, password, "PathName LIKE '%memcached%'"); } catch (Exception ex) { MessageBox.Show("Couldn't connect to the server. Please check server connectivity and user name and password.\n" + ex.Message); return; } if (_server == null) { _server = new MemCacheDManager.Business.Server(); ServerConfiguration.Servers.Add(_server); } _server.ServerName = txtServerName.Text; _server.UserName = txtUserName.Text; _server.Password = Encryption.Encrypt(txtPassword.Text); _server.UseImpersonation = chkUseImpersonation.Checked; _server.BinaryPath = txtBinaryPath.Text; SetStatusLabel("Checking for MemCacheD instances."); _server.ReadInstancesFromServer(); SetStatusLabel("Saving."); ServerConfiguration.Save(Configuration.Default.LastConfigFile); btnApply.Enabled = false; btnCancel.Enabled = false; btnAddInstance.Enabled = true; SetStatusLabel("Updating UI."); if (Save != null) { Save(this._server); } } finally { Cursor = Cursors.Default; SetStatusLabel("Ready."); } }