//--------------------------------------------------------------------------------------------------------------------- /// <summary>Executes the scheduler-related part of the background agent action <b>%Task and scheduler cleanup</b>.</summary> /* * This method is called periodically if the action is active and the background agent is running according to the action's execution interval specified in the portal configuration. * * The background agent action <b>%Task cleanup</b> deletes the tasks that have been marked for deletion and no longer consume Grid engine resources, together with all data related to these tasks (jobs, node information etc.), from the database. * This is the case for tasks that have the value <i>Deleted</i> in the database field <i>task.status</i>. * %Tasks deletions are pending if they were requested while the portal configuration variable <i>SynchronousTaskOperations</i> was set to <i>false</i>. * * /// <param name="context">The execution environment context.</param> * /// \xrefitem rmodp "RM-ODP" "RM-ODP Documentation" */ public static void ExecuteCleanup(IfyContext context) { IDataReader reader; List <int> schedulerIds = new List <int>(); // Abort tasks (next_status = Deleted) reader = context.GetQueryResult(String.Format("SELECT t.id FROM scheduler AS t WHERE status={0} ORDER BY t.id;", ProcessingStatus.Deleted)); while (reader.Read()) { schedulerIds.Add(reader.GetInt32(0)); } reader.Close(); int count = 0, taskCount = 0; foreach (int schedulerId in schedulerIds) { Scheduler scheduler = FromId(context, schedulerId); taskCount += scheduler.DoAbortAllTasks(); context.Execute(String.Format("DELETE FROM scheduler WHERE id={0};", schedulerId)); count++; } schedulerIds.Clear(); if (count > 0) { context.WriteInfo(String.Format("Deleted schedulers {0}: (aborted tasks: {1})", count, taskCount)); } }
//--------------------------------------------------------------------------------------------------------------------- /// <summary>Calculates whether a user has the specified privilege for the specified domain.</summary> /// <param name="context">The execution environment context.</param> /// <param name="userId">The database ID of the user in question.</param> /// <param name="domainId">The database ID of the domain in question. If the domain is <em>null<em>, the check is.</param> /// <param name="identifier">The unique identifier of the privilege.</param> /// <returns><i>true</i> if the user has the privilege.</returns> private static bool DoesUserHavePrivilegeInternal(IfyContext context, int userId, int domainId, string condition) { StringBuilder sql = new StringBuilder(); sql.Append("SELECT "); sql.Append(PrivilegeValueSelectSql); sql.Append(" FROM "); sql.Append(PrivilegeBaseJoinSql); sql.Replace("{0}", userId.ToString()); sql.Append(" WHERE "); sql.Append(String.Format("(rg.id_usr={0} OR ug.id_usr={0}) AND ", userId)); sql.Append(domainId == 0 ? "rg.id_domain IS NULL" : String.Format("(rg.id_domain IS NULL OR rg.id_domain={0})", domainId)); if (condition != null) { sql.Append(String.Format(" AND {0};", condition)); } IDbConnection dbConnection = context.GetDbConnection(); IDataReader reader = context.GetQueryResult(sql.ToString(), dbConnection); bool result = reader.Read(); context.CloseQueryResult(reader, dbConnection); return(result); }
//--------------------------------------------------------------------------------------------------------------------- /// <summary>Executes the background agent action <b>Computing Element status refresh</b>.</summary> /// <param name="context">The execution environment context.</param> /// <remarks> /// <p>This method is called periodically if the action is active and the background agent is running according to the action's execution interval specified in the portal configuration.</p> /// <p>The background agent action <b>Computing Element status refresh</b> refreshes task, job and node status information for active tasks.</p> /// </remarks> public static void ExecuteComputingResourceStatusRefresh(IfyContext context) { IDataReader reader; ComputingResource computingResource; List <int> computingResourceIds = new List <int>(); int unknownCount = 0; // Add computing resources into status table if not yet there context.Execute("INSERT INTO crstate (id_cr) SELECT id FROM cr AS t LEFT JOIN crstate AS t1 ON t.id=t1.id_cr WHERE t1.id_cr IS NULL;"); // Get computing resources with actual status Active (10) reader = null; string sql = "SELECT t.id FROM ce AS t WHERE status_method IS NOT NULL ORDER BY t.id;"; // WHERE status=" + ProcessingStatus.Active + " AND remote_id IS NOT NULL ORDER BY t.id;"; IDbConnection dbConnection = context.GetDbConnection(); reader = context.GetQueryResult(sql, dbConnection); while (reader.Read()) { computingResourceIds.Add(reader.GetInt32(0)); } context.CloseQueryResult(reader, dbConnection); //context.WriteInfo(String.Format("Active tasks: {0}{1}", computingResourceIds.Count, computingResourceIds.Count == 0 ? String.Empty : " - status display format: created/active/failed/incomplete/completed")); // Get available computing resource information for (int i = 0; i < computingResourceIds.Count; i++) { computingResource = ComputingResource.FromId(context, computingResourceIds[i]); //computingResource.Restricted = false; computingResource.GetStatus(); if (computingResource.Refreshed) { context.WriteInfo( String.Format("{0}: total: {1}, free: {2}", computingResource.Name, computingResource.TotalCapacity, computingResource.FreeCapacity ) ); } else { unknownCount++; } } if (unknownCount != 0) { context.WriteWarning(String.Format("Computing resources with unavailable status: {0} (out of {1})", unknownCount, computingResourceIds.Count)); } computingResourceIds.Clear(); }
//--------------------------------------------------------------------------------------------------------------------- public List <int> GetTaskIdList(IfyContext context) { List <int> taskIds = new List <int>(); string sql = String.Format("SELECT t.id FROM task AS t WHERE id_scheduler={0};", this.Id); IDbConnection dbConnection = context.GetDbConnection(); IDataReader reader = context.GetQueryResult(sql, dbConnection); while (reader.Read()) { taskIds.Add(reader.GetInt32(0)); } context.CloseQueryResult(reader, dbConnection); return(taskIds); }
//--------------------------------------------------------------------------------------------------------------------- /// <summary>Determines the scope (global or domain-restricted) for which the specified user or any of the specified groups has been granted at least one of the specified roles.</summary> /// <returns> /// <para>The database IDs of the domains for which the user has at least one of the roles. The code using this method for privilege-based authorisation checks, has to distinguish the following cases:</para> /// <list type="bullet"> /// <item>An empty array means that the user is not authorised.</item> /// <item>An array containing one or more IDs means that the user is authorised for items that belong to the domains with these database IDs.</item> /// <item>If the array is <c>null</c>, the user is authorised globally.</item> /// </list> /// </returns> /// <param name="context">The execution environment context.</param> /// <param name="userId">The database ID of the user for which the domain restriction check is performed. This can be used in combination with <em>groupIds</em>. If no specific user is to be taken into account, the value <em>0</em> can be provided.</param> /// <param name="groupIds">The database IDs of the groups for which the domain restriction check is performed. This can be used in combination with <em>userId</em>. If no group is to be taken into account, <em>null</em> can be used.</param> /// <param name="roleIds">An array of database IDs for the roles that are to be checked in relation to the user. If the array is <c>null</c>, the method returns all domains on which the user or groups have any role. If the array is empty, the grant is empty (return value is an empty array).</param> public static int[] GetGrantScope(IfyContext context, int userId, int[] groupIds, int[] roleIds) { if (roleIds != null && roleIds.Length == 0) { return new int[] {} } ; if (groupIds == null || groupIds.Length == 0) { groupIds = new int[] { 0 } } ; string roleCondition = roleIds == null ? String.Empty : String.Format("rg.id_role IN ({0})", String.Join(",", roleIds)); List <int> domainIds = new List <int>(); string sql = String.Format("SELECT DISTINCT rg.id_domain FROM rolegrant AS rg LEFT JOIN usr_grp AS ug ON rg.id_grp=ug.id_grp WHERE {2}{3}(rg.id_usr={0} OR ug.id_usr={0} OR rg.id_grp IN ({1})) ORDER BY rg.id_domain IS NULL, rg.id_domain;", userId, String.Join(",", groupIds), roleCondition, String.IsNullOrEmpty(roleCondition) ? String.Empty : " AND " ); //Console.WriteLine("DOMAINS: {0}", sql); IDbConnection dbConnection = context.GetDbConnection(); IDataReader reader = context.GetQueryResult(sql, dbConnection); bool globallyAuthorized = false; while (reader.Read()) { // The domain ID NULL means that the user has the privilege globally and other any additional domains do not matter // This applies only if a role was specifically queried, but not if all domains on which a user has any role are queried. if (roleIds != null && reader.GetValue(0) == DBNull.Value) { globallyAuthorized = true; break; } domainIds.Add(reader.GetInt32(0)); } context.CloseQueryResult(reader, dbConnection); if (globallyAuthorized) { return(null); } return(domainIds.ToArray()); }
//--------------------------------------------------------------------------------------------------------------------- public static RemoteResourceEntityCollection GetResources(IfyContext context, RemoteResourceSet resourceSet) { RemoteResourceEntityCollection result = new RemoteResourceEntityCollection(context); result.Template.ResourceSet = resourceSet; IDbConnection dbConnection = context.GetDbConnection(); IDataReader reader = context.GetQueryResult(String.Format("SELECT t.id, t.location, t.name FROM resource AS t WHERE id_set={0}", resourceSet.Id), dbConnection); while (reader.Read()) { RemoteResource resource = new RemoteResource(context); resource.Id = reader.GetInt32(0); resource.ResourceSet = resourceSet; resource.Location = reader.GetString(1); resource.Name = reader.GetString(2); } context.CloseQueryResult(reader, dbConnection); return(result); }
/*****************************************************************************************************************/ /// <summary>Creates a new DBCookie instance representing the cookie with the specified session ID and unique identifier.</summary> /// <param name="context">The execution environment context.</param> /// <param name="session">The related session ID.</param> /// <param name="identifier">The unique identifier of the cookie.</param> /// <returns>The created Group object.</returns> public static DBCookie FromSessionAndIdentifier(IfyContext context, string session, string identifier) { DBCookie cookie = new DBCookie(context); string sql = String.Format("SELECT value, expire, creation_date FROM cookie WHERE session={0} AND identifier={1};", StringUtils.EscapeSql(session), StringUtils.EscapeSql(identifier)); IDbConnection dbConnection = context.GetDbConnection(); IDataReader reader = context.GetQueryResult(sql, dbConnection); Console.WriteLine(sql); if (reader.Read()) { cookie.Session = session; cookie.Identifier = identifier; cookie.Value = reader.GetString(0); cookie.Expire = reader.GetDateTime(1); cookie.CreationDate = reader.GetDateTime(2); } context.CloseQueryResult(reader, dbConnection); return(cookie); }
//--------------------------------------------------------------------------------------------------------------------- /// <summary> /// Gets the task list. /// </summary> /// <returns>The task list.</returns> /// <param name="context">Context.</param> /// \xrefitem rmodp "RM-ODP" "RM-ODP Documentation" public List <Task> GetTaskList(IfyContext context) { List <int> taskIds = new List <int>(); IDbConnection dbConnection = context.GetDbConnection(); string sql = String.Format("SELECT t.id FROM task AS t WHERE id_scheduler={0};", this.Id); IDataReader reader = context.GetQueryResult(sql, dbConnection); while (reader.Read()) { taskIds.Add(reader.GetInt32(0)); } context.CloseQueryResult(reader, dbConnection); List <Task> result = new List <Task>(); foreach (int taskId in taskIds) { result.Add(Task.FromId(context, taskId, null, true)); } return(result); }
//--------------------------------------------------------------------------------------------------------------------- /// <summary> /// Gets the user roles for a specific domain. /// </summary> /// <returns>The user roles for domain.</returns> /// <param name="context">Context.</param> /// <param name="userId">User identifier.</param> /// <param name="domainId">Domain identifier.</param> public static Role[] GetUserRolesForDomain(IfyContext context, int userId, int domainId) { List <Role> result = new List <Role> (); string sql = String.Format("SELECT id_role FROM rolegrant WHERE id_usr={0} AND id_domain={1};", userId, domainId); List <int> rolesId = new List <int> (); IDbConnection dbConnection = context.GetDbConnection(); IDataReader reader = context.GetQueryResult(sql, dbConnection); while (reader.Read()) { rolesId.Add(reader.GetInt32(0)); } context.CloseQueryResult(reader, dbConnection); foreach (var id in rolesId) { result.Add(Role.FromId(context, id)); } return(result.ToArray()); }
//--------------------------------------------------------------------------------------------------------------------- /// <summary>Returns the privileges of a user for the specified entity item.</summary> /// <param name="context">The execution environment context.</param> /// <param name="userId">The database ID of the user in question.</param> /// <param name="item">The entity item for which the privileges are calculated.</param> /// <returns>An array of the privileges related to the entity type that contains only those privileges granted to the user for the item.</returns> public static Privilege[] GetUserPrivileges(IfyContext context, int userId, Entity item) { StringBuilder sql = new StringBuilder(); sql.Append("SELECT DISTINCT p.id FROM "); sql.Append(PrivilegeBaseJoinSql); sql.Replace("{0}", userId.ToString()); sql.Append(String.Format(" WHERE p.id_type={0} AND ", item.EntityType.TopTypeId)); sql.Append(String.Format("(rg.id_usr={0} OR ug.id_usr={0}) AND ", userId)); sql.Append(item.DomainId == 0 ? "rg.id_domain IS NULL" : String.Format("(rg.id_domain IS NULL OR rg.id_domain={0});", item.DomainId)); List <Privilege> result = new List <Privilege>(); IDbConnection dbConnection = context.GetDbConnection(); IDataReader reader = context.GetQueryResult(sql.ToString(), dbConnection); while (reader.Read()) { result.Add(Privilege.Get(reader.GetInt32(0))); } context.CloseQueryResult(reader, dbConnection); return(result.ToArray()); }
//--------------------------------------------------------------------------------------------------------------------- public static void LoadPrivileges(IfyContext context) { privileges.Clear(); IDataReader reader = context.GetQueryResult("SELECT t.id, t.identifier, t.name, t.id_type, t.operation, t.enable_log FROM priv AS t ORDER BY t.pos;"); while (reader.Read()) { int id = context.GetIntegerValue(reader, 0); int entityTypeId = context.GetIntegerValue(reader, 3); EntityType entityType = entityTypeId == 0 ? null : EntityType.GetEntityTypeFromId(entityTypeId); EntityOperationType operation = GetOperationType(context.GetValue(reader, 4)); Privilege privilege = new Privilege( id, context.GetValue(reader, 1), context.GetValue(reader, 2), entityType, operation, context.GetBooleanValue(reader, 5) ); privileges[id] = privilege; } reader.Close(); }
//--------------------------------------------------------------------------------------------------------------------- /// <summary>.</summary> public static void ExecuteCatalogueSeriesRefresh(IfyContext context) { string sql = "SELECT t.id, t.identifier, t.cat_description, t1.base_url FROM series AS t LEFT JOIN catalogue AS t1 ON t.id_catalogue=t1.id WHERE t.auto_refresh ORDER BY t.identifier;"; int count = 0; IDataReader reader = context.GetQueryResult(sql); while (reader.Read()) { int id = reader.GetInt32(0); string name = reader.GetString(1); string catalogueDescriptionUrl = reader.GetString(2); string catalogueBaseUrl = context.GetValue(reader, 3); if (catalogueDescriptionUrl == null || catalogueDescriptionUrl == String.Empty) { context.AddError("No catalogue description URL defined for \"" + name + "\""); continue; } bool usePlaceholder = (catalogueDescriptionUrl.Contains("$(CATALOGUE)") && catalogueBaseUrl != null); if (usePlaceholder) { catalogueDescriptionUrl = catalogueDescriptionUrl.Replace("$(CATALOGUE)", catalogueBaseUrl); } string catalogueUrlTemplate = null; try { catalogueUrlTemplate = Terradue.Metadata.OpenSearch.OpenSearchDescription.GetUrlTemplate(catalogueDescriptionUrl, new string[] { "application/rdf+xml", "application/xhtml+xml" }); } catch (XmlException) { context.AddError("Catalogue description URL for \"" + name + "\" returns invalid description"); } catch (Exception) { context.AddError("No catalogue URL template found for series \"" + name + "\""); } if (catalogueUrlTemplate == null) { continue; } if (usePlaceholder) { catalogueUrlTemplate = catalogueUrlTemplate.Replace(catalogueBaseUrl, "$(CATALOGUE)" + (catalogueBaseUrl.EndsWith("/") ? "/" : String.Empty)); } context.Execute(String.Format("UPDATE series SET cat_template={1} WHERE id={0};", id, StringUtils.EscapeSql(catalogueUrlTemplate))); count++; } context.AddInfo(String.Format("Updated series: {0}", count)); count = context.GetQueryIntegerValue("SELECT COUNT(*) FROM series WHERE NOT auto_refresh;"); if (count != 0) { context.AddInfo(String.Format("Ignored series: {0}", count)); } sql = "SELECT t.id, t.identifier, t.cat_description, t1.base_url FROM producttype AS t LEFT JOIN catalogue AS t1 ON t.id_catalogue=t1.id ORDER BY t.identifier;"; reader = context.GetQueryResult(sql); while (reader.Read()) { int id = reader.GetInt32(0); string name = reader.GetString(1); string catalogueDescriptionUrl = reader.GetString(2); string catalogueBaseUrl = context.GetValue(reader, 3); if (catalogueDescriptionUrl == null || catalogueDescriptionUrl == String.Empty) { context.AddError("No catalogue description URL defined for product type \"" + name + "\""); continue; } bool usePlaceholder = (catalogueDescriptionUrl.Contains("$(CATALOGUE)") && catalogueBaseUrl != null); if (usePlaceholder) { catalogueDescriptionUrl = catalogueDescriptionUrl.Replace("$(CATALOGUE)", catalogueBaseUrl); } string catalogueUrlTemplate = null; try { catalogueUrlTemplate = Terradue.Metadata.OpenSearch.OpenSearchDescription.GetUrlTemplate(catalogueDescriptionUrl, new string[] { "application/rdf+xml", "application/xhtml+xml" }); } catch (XmlException) { context.AddError("Catalogue description URL for product type \"" + name + "\" returns invalid description"); } catch (Exception) { context.AddError("No catalogue URL template found for product type \"" + name + "\""); } if (catalogueUrlTemplate == null) { continue; } if (usePlaceholder) { catalogueUrlTemplate = catalogueUrlTemplate.Replace(catalogueBaseUrl, "$(CATALOGUE)" + (catalogueBaseUrl.EndsWith("/") ? "/" : String.Empty)); } context.Execute(String.Format("UPDATE producttype SET cat_template={1} WHERE id={0};", id, StringUtils.EscapeSql(catalogueUrlTemplate))); } reader.Close(); }