private CommandResult Uninstall() { try { serviceProvider.UninstallService(serviceInstanceId); // If uninstall is successful, update the config. if (minerConfig.UpdatedInstanceType != null) { minerConfig.InstanceType = minerConfig.UpdatedInstanceType.Value; minerConfig.InstanceId = minerConfig.UpdatedInstanceId ?? 0; minerConfig.UpdatedInstanceType = null; minerConfig.UpdatedInstanceId = null; minerConfig.SaveToFile(); } return(CommandResult.OKResult()); } catch (TargetExecutionException ex) { throw ex; } catch (Exception ex) { throw new TargetExecutionException(DaemonErrorCode.UNKNOWN_ERROR, ex); } }
public override CommandResult Execute(string parameter) { // Update the configuration ConfigureParameter configParameters = null; isInstanceUpdated = false; try { configParameters = JsonConvert.DeserializeObject <ConfigureParameter>(parameter); } catch (FormatException) { throw new TargetExecutionException(DaemonErrorCode.COMMAND_PARAM_ERROR, "The format of the Configuration content is not valid Json."); } MinerConfig config = MinerConfig.ReadFromFile(); if (!string.IsNullOrEmpty(configParameters.DeviceId) || !string.IsNullOrEmpty(configParameters.DeviceName)) { MinerManager minerManager = new MinerManager(); List <MinerDevice> deviceList = minerManager.GetAllMinerDevices(); bool deviceFound = false; foreach (MinerDevice device in deviceList) { if (device.IsMatchId(configParameters.DeviceId) || string.Equals(device.GetDisplayName(), configParameters.DeviceName)) { config.Device = new MinerConfigDevice(device.GetDeviceId(), device.GetDisplayName(), device.GetDeviceVersion(), device.GetDriverVersion()); deviceFound = true; break; } } if (!deviceFound) { throw new TargetExecutionException(DaemonErrorCode.CONFIG_DEVICE_NOT_FOUND, string.Format("Did not find the device matches DeviceId=[{0}] or DeviceName=[{1}]", configParameters.DeviceId, configParameters.DeviceName)); } } if ((!string.IsNullOrEmpty(configParameters.XDaggerWallet) || !string.IsNullOrEmpty(configParameters.XDaggerPoolAddress)) && !string.IsNullOrEmpty(configParameters.EthPoolAddress)) { throw new TargetExecutionException(DaemonErrorCode.CONFIG_ONLY_ONE_INSTANCE_TYPE_ALLOWED, "Only one type of miner instance is allowed."); } if (configParameters.InstanceId.HasValue && configParameters.AutoDecideInstanceId) { throw new TargetExecutionException(DaemonErrorCode.COMMAND_PARAM_ERROR, "Cannot specify InstanceId while AutoDecideInstanceId is used."); } InstanceTypes?proposedInstanceType = null; if (!string.IsNullOrEmpty(configParameters.XDaggerWallet)) { string wallet = configParameters.XDaggerWallet.Trim(); // TODO: Should validate the Wallet address first if (false) { throw new TargetExecutionException(DaemonErrorCode.CONFIG_WALLET_FORMET_ERROR, string.Format("Wallet format is not correct. Wallet=[{0}]", configParameters.XDaggerWallet)); } if (false) { throw new TargetExecutionException(DaemonErrorCode.CONFIG_WALLET_NOT_FOUND, string.Format("Wallet cannot be found. Wallet=[{0}]", configParameters.XDaggerWallet)); } if (config.XDaggerMiner == null) { config.XDaggerMiner = new XDaggerMinerConfig(); } proposedInstanceType = MinerConfig.InstanceTypes.XDagger; config.XDaggerMiner.WalletAddress = wallet; } if (!string.IsNullOrEmpty(configParameters.XDaggerPoolAddress)) { string poolAddress = configParameters.XDaggerPoolAddress.Trim(); // TODO: Should validate the Wallet address first if (false) { throw new TargetExecutionException(DaemonErrorCode.CONFIG_WALLET_FORMET_ERROR, string.Format("Wallet format is not correct. Wallet=[{0}]", configParameters.XDaggerWallet)); } if (false) { throw new TargetExecutionException(DaemonErrorCode.CONFIG_WALLET_NOT_FOUND, string.Format("Wallet cannot be found. Wallet=[{0}]", configParameters.XDaggerWallet)); } if (config.XDaggerMiner == null) { config.XDaggerMiner = new XDaggerMinerConfig(); } proposedInstanceType = MinerConfig.InstanceTypes.XDagger; config.XDaggerMiner.PoolAddress = poolAddress; } if (!string.IsNullOrEmpty(configParameters.EthPoolAddress)) { string ethPoolAddress = configParameters.EthPoolAddress.Trim(); // TODO: Should validate the Wallet address first if (false) { throw new TargetExecutionException(DaemonErrorCode.CONFIG_WALLET_FORMET_ERROR, string.Format("Wallet format is not correct. Wallet=[{0}]", configParameters.XDaggerWallet)); } if (false) { throw new TargetExecutionException(DaemonErrorCode.CONFIG_WALLET_NOT_FOUND, string.Format("Wallet cannot be found. Wallet=[{0}]", configParameters.XDaggerWallet)); } if (config.EthMiner == null) { config.EthMiner = new EthMinerConfig(); } proposedInstanceType = MinerConfig.InstanceTypes.Eth; config.EthMiner.PoolAddress = ethPoolAddress; } ServiceProvider currentServiceProvider = ServiceProvider.GetServiceProvider(config.InstanceType); ServiceInstance currentServiceInstance = currentServiceProvider?.AquaireInstance(config.InstanceId); // Check the change of instance Type, if the service is already installed, put the new instance type into updated_instance_type, and detect new updated_instance_id if (currentServiceInstance != null && currentServiceInstance.IsServiceExist()) { // Put the type into updated_instance_type if (proposedInstanceType == null) { if (config.UpdatedInstanceType != null && configParameters.InstanceId != null) { config.UpdatedInstanceId = configParameters.InstanceId; } } else if (proposedInstanceType != config.InstanceType) { isInstanceUpdated = true; config.UpdatedInstanceType = proposedInstanceType; config.UpdatedInstanceId = DecideInstanceId(configParameters, proposedInstanceType.Value, true); } } else if (proposedInstanceType != null) { if (configParameters.AutoDecideInstanceId || (config.InstanceType != InstanceTypes.Unset && proposedInstanceType != config.InstanceType)) { isInstanceUpdated = true; } config.InstanceType = proposedInstanceType.Value; config.InstanceId = DecideInstanceId(configParameters, proposedInstanceType.Value, false) ?? config.InstanceId; // Clear the updated ones config.UpdatedInstanceId = null; config.UpdatedInstanceType = null; } // Save all of the changes into config file int retryTimes = 0; while (true) { try { config.SaveToFile(); if (isInstanceUpdated) { return(CommandResult.CreateResult(ConfigureOutput.Create(config.UpdatedInstanceId ?? config.InstanceId))); } else { return(CommandResult.OKResult()); } } catch (Exception ex) { if (retryTimes++ < IntermediateFailureRetryTimes) { Thread.Sleep(IntermediateFailureRetryPeriod); continue; } throw new TargetExecutionException(DaemonErrorCode.UNKNOWN_ERROR, ex.Message); } } }