private bool CheckAndCleanupPreviousService()
        {
            if (minerConfig.UpdatedInstanceType == null)
            {
                // Nothing to be updated.
                logger.Trace("UpdatedInstanceType is null, no need to update.");
                return(false);
            }

            // Uninstall the previous service instance
            MinerConfig.InstanceTypes instanceType = minerConfig.InstanceType;
            int instanceId = minerConfig.InstanceId;

            logger.Trace($"Try to find service with InstanceType=[{instanceType}] InstanceId=[{instanceId}].");
            ServiceProvider serviceProvider = ComposeServiceProvider(minerConfig.InstanceType);
            ServiceInstance serviceInstance = serviceProvider.AquaireInstance(minerConfig.InstanceId);

            if (serviceInstance.IsServiceExist())
            {
                logger.Trace("IsServiceExist is true, try uninstalling the previous service.");

                try
                {
                    serviceProvider.UninstallService(instanceId);
                }
                catch (Exception ex)
                {
                    // Will continue when uninstalling is failed.
                    logger.Error("Got error while uninstalling: " + ex.ToString());
                }
            }

            // If the uninsallation is successful, update the config file
            minerConfig.InstanceType        = minerConfig.UpdatedInstanceType.Value;
            minerConfig.InstanceId          = minerConfig.UpdatedInstanceId ?? 0;
            minerConfig.UpdatedInstanceType = null;
            minerConfig.UpdatedInstanceId   = null;

            minerConfig.SaveToFile();
            logger.Trace("Updated miner config file.");

            return(true);
        }
示例#2
0
        public override CommandResult Execute(string parameter)
        {
            try
            {
                ServiceProvider serviceProvider = ComposeServiceProvider(MinerConfig.GetInstance().InstanceType);
                int             instanceId      = MinerConfig.GetInstance().InstanceId;

                serviceInstance = serviceProvider.AquaireInstance(instanceId);

                ReportOutput outputResult = new ReportOutput();
                outputResult.Status = ReportOutput.StatusEnum.Unknown;

                if (!serviceInstance.IsServiceExist())
                {
                    outputResult.Status = ReportOutput.StatusEnum.NotInstalled;
                }
                else if (!serviceInstance.IsServiceRunning())
                {
                    outputResult.Status = ReportOutput.StatusEnum.Stopped;
                }
                else
                {
                    // Retrieve the miner status from NamedPipe
                    QueryServiceStatusByNamedPipe(outputResult);
                }

                return(CommandResult.CreateResult(outputResult));
            }
            catch (TargetExecutionException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new TargetExecutionException(DaemonErrorCode.UNKNOWN_ERROR, ex);
            }
        }
示例#3
0
        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);
                }
            }
        }