/// <summary>
		/// Upload version of module package.
		/// </summary>
		/// <param name="module">Module ideintifier.</param>
		/// <param name="major">Major version.</param>
		/// <param name="minor">Minor version.</param>
		/// <param name="build">Build version.</param>
		/// <param name="revision">Revision.</param>
		/// <param name="packageStream">Stream with module package.</param>
		public void UploadModuleVersion(string module, string major, string minor, string build, string revision, Stream packageStream)
		{
			int moduleId;
			int majorNumber;
			int minorNumber = 0;
			int buildNumber = 0;
			int revisionNumber = 0;

			if (string.IsNullOrEmpty(module))
				return;

			if (!int.TryParse(module, out moduleId))
				return;

			if (string.IsNullOrEmpty(major))
				return;

			if (!int.TryParse(major, out majorNumber))
				return;

			if (!string.IsNullOrEmpty(minor))
			{
				if (!int.TryParse(minor, out minorNumber))
					return;
			}

			if (!string.IsNullOrEmpty(build))
			{
				if (!int.TryParse(build, out buildNumber))
					return;
			}

			if (!string.IsNullOrEmpty(revision))
			{
				if (!int.TryParse(revision, out revisionNumber))
					return;
			}

			ModuleVersion moduleVersion = new ModuleVersion
			{
				ModuleId = moduleId,
				Major = majorNumber,
				Minor = minorNumber,
				Build = buildNumber,
				Revision = revisionNumber
			};

			_orationiEngine.SaveModule(moduleVersion, packageStream);

			using (MasterContext masterDb = new MasterContext())
			{
				masterDb.ModuleVersions.Add(moduleVersion);
				masterDb.SaveChanges();
			}
		}
		/// <summary>
		/// Unassign module from slave.
		/// </summary>
		/// <param name="slave">Slave identifier.</param>
		/// <param name="module">Mpdule identifier.</param>
		public void UnassignModule(string slave, string module)
		{
			Guid slaveId;
			int moduleId;

			if (string.IsNullOrEmpty(slave))
				return;

			if (!Guid.TryParse(slave, out slaveId))
				return;

			if (string.IsNullOrEmpty(module))
				return;

			if (!int.TryParse(module, out moduleId))
				return;

			using (MasterContext masterDb = new MasterContext())
			{
				SlaveModule slaveModule = masterDb.SlaveModules.FirstOrDefault(s => s.SlaveId == slaveId && s.ModuleId == moduleId);
				if (slaveModule == null)
					return;

				masterDb.SlaveModules.Remove(slaveModule);
				masterDb.SaveChanges();

				_orationiEngine.UndeployModule(slaveId, moduleId);
			}
		}
		/// <summary>
		/// Register module in system.
		/// </summary>
		/// <param name="moduleName">Module name.</param>
		/// <returns><c>ModuleItem</c></returns>
		public ModuleItem RegisterModule(string moduleName)
		{
			using (MasterContext masterDb = new MasterContext())
			{
				ModuleDescription module = masterDb.Modules.FirstOrDefault(m => string.Equals(m.Name, moduleName, StringComparison.CurrentCultureIgnoreCase));
				ModuleItem result = new ModuleItem();

				if (module == null)
				{
					module = new ModuleDescription();
					module.Name = moduleName;
					masterDb.Modules.Add(module);
					masterDb.SaveChanges();
				}

				result.Id = module.Id;
				result.Name = module.Name;

				return result;
			}
		}
		/// <summary>
		/// Assugn module to slave.
		/// </summary>
		/// <param name="slave">Slave identifier.</param>
		/// <param name="module">Module identifier.</param>
		/// <returns></returns>
		public AssignedModule AssignModule(string slave, string module)
		{
			Guid slaveId;
			int moduleId;

			if (string.IsNullOrEmpty(slave))
				return null;

			if (!Guid.TryParse(slave, out slaveId))
				return null;

			if (string.IsNullOrEmpty(module))
				return null;

			if (!int.TryParse(module, out moduleId))
				return null;

			using (MasterContext masterDb = new MasterContext())
			{
				SlaveModule slaveModule = masterDb.SlaveModules.FirstOrDefault(s => s.SlaveId == slaveId && s.ModuleId == moduleId);
				if (slaveModule == null)
				{
					slaveModule = new SlaveModule()
					{
						ModuleId = moduleId,
						SlaveId = slaveId
					};
					masterDb.SlaveModules.Add(slaveModule);
					masterDb.SaveChanges();
				}
				ModuleVersionItem moduleVersionItem = new ModuleVersionItem { ModuleId = moduleId };

				ModuleVersion moduleVersion = masterDb.ModuleVersions.Where(mv => mv.ModuleId == moduleId)
					.OrderByDescending(mv => mv.Major)
					.ThenByDescending(mv => mv.Minor)
					.ThenByDescending(mv => mv.Build)
					.ThenByDescending(mv => mv.Revision)
					.FirstOrDefault();

				if (moduleVersion != null)
				{
					moduleVersionItem.Major = moduleVersion.Major;
					moduleVersionItem.Minor = moduleVersion.Minor;
					moduleVersionItem.Build = moduleVersion.Build;
					moduleVersionItem.Revision = moduleVersion.Revision;
				};

				_orationiEngine.PushModule(slaveId, moduleVersion);

				AssignedModule assignedModule = new AssignedModule
				{
					SlaveId = slaveId,
					ModuleVersion = moduleVersionItem
				};

				return assignedModule;
			}
		}
Example #5
0
		public SlaveProcessWorker(string sessionId, IOrationiSlaveCallback callback, int inactiveTimeout = 30000)
		{
			SessionId = sessionId;
			Ip = NetworkUtility.GetClientIpFromOperationContext();
			Callback = callback;
			LastActivity = DateTime.Now;
			InactiveTimeout = inactiveTimeout;

			//Save information in Db
			using (MasterContext masterDb = new MasterContext())
			{
				//If slave exist in db - update some information.
				SlaveDescription slave = masterDb.Slaves.FirstOrDefault(s => s.Address == Ip);
				if (slave != null)
				{
					slave.LastConnectionOn = DateTime.Now;
					masterDb.SaveChanges();
					SlaveId = slave.Id;
					return;
				}

				//If slave not exist in db - create new record
				string hostName = NetworkUtility.GetHostName(Ip);
				slave = new SlaveDescription
				{
					Address = Ip,
					//Id = Guid.NewGuid(),
					RegistredOn = DateTime.Now,
					LastConnectionOn = DateTime.Now,
					Name = $"Unknown slave {hostName} ({Ip})"
				};

				masterDb.Slaves.Add(slave);
				masterDb.SaveChanges();

				SlaveId = slave.Id;
			}

#if DEBUG
			Console.WriteLine("Connection from {0} {1}", SessionId, DateTime.Now.ToString("O"));
#endif
		}