Skip to content

Commit

Permalink
Merge pull request #3114 from uselagoon/task-changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tobybellwood authored Apr 21, 2022
2 parents 011d920 + 90154fa commit 56567d1
Show file tree
Hide file tree
Showing 21 changed files with 259 additions and 34 deletions.
4 changes: 4 additions & 0 deletions node-packages/commons/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export const generateBuildId = function() {
return `lagoon-build-${Math.random().toString(36).substring(7)}`;
};

export const generateTaskName = function() {
return `lagoon-task-${Math.random().toString(36).substring(7)}`;
};

export const jsonMerge = function(a, b, prop) {
var reduced = a.filter(function(aitem) {
return !b.find(function(bitem) {
Expand Down
3 changes: 2 additions & 1 deletion services/api-db/docker-entrypoint-initdb.d/00-tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,11 @@ CREATE TABLE IF NOT EXISTS environment_service (
CREATE TABLE IF NOT EXISTS task (
id int NOT NULL auto_increment PRIMARY KEY,
name varchar(100) NOT NULL,
task_name varchar(100) NULL,
environment int NOT NULL REFERENCES environment (id),
service varchar(100) NOT NULL,
command varchar(300) NOT NULL,
status ENUM('active', 'succeeded', 'failed') NOT NULL,
status ENUM('new', 'pending', 'running', 'cancelled', 'error', 'failed', 'complete', 'active', 'succeeded') NOT NULL,
created datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
started datetime NULL,
completed datetime NULL,
Expand Down
91 changes: 91 additions & 0 deletions services/api-db/docker-entrypoint-initdb.d/01-migrations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1631,6 +1631,94 @@ CREATE OR REPLACE PROCEDURE
END;
$$

CREATE OR REPLACE PROCEDURE
add_task_name_to_tasks()

BEGIN
IF NOT EXISTS (
SELECT NULL
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
table_name = 'task'
AND table_schema = 'infrastructure'
AND column_name = 'task_name'
) THEN
ALTER TABLE `task`
ADD `task_name` varchar(100) NULL;
END IF;
END;
$$

CREATE OR REPLACE PROCEDURE
add_new_task_status_types()

BEGIN
IF NOT EXISTS (
SELECT NULL
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
table_name = 'task'
AND table_schema = 'infrastructure'
AND column_name = 'status'
AND column_type like '%''cancelled%'
) THEN
ALTER TABLE `task`
MODIFY status ENUM('new', 'pending', 'running', 'cancelled', 'error', 'failed', 'complete', 'active', 'succeeded') NOT NULL;
END IF;
END;
$$

-- update any active or succeeded statuses to be running or complete
CREATE OR REPLACE PROCEDURE
update_active_succeeded_tasks()

BEGIN
UPDATE task t
SET
t.status = 'running'
WHERE
t.task_name = 'active';
UPDATE task t
SET
t.status = 'complete'
WHERE
t.task_name = 'succeeded';
END;
$$

-- generate a taskname for tasks missing one
CREATE OR REPLACE PROCEDURE
update_missing_tasknames()

BEGIN
UPDATE task t
SET
t.task_name = CONCAT('lagoon-task-', (SELECT LEFT(UUID(), 6)))
WHERE
t.task_name IS NULL;
END;
$$

-- TODO: Eventually the `active/succeeded` values should go away once `remote-controller` is updated to send the correct values
-- CREATE OR REPLACE PROCEDURE
-- remove_active_succeeded_task_types()

-- BEGIN
-- IF NOT EXISTS (
-- SELECT NULL
-- FROM INFORMATION_SCHEMA.COLUMNS
-- WHERE
-- table_name = 'task'
-- AND table_schema = 'infrastructure'
-- AND column_name = 'status'
-- AND column_type like '%''cancelled%'
-- ) THEN
-- ALTER TABLE `task`
-- MODIFY status ENUM('new', 'pending', 'running', 'cancelled', 'error', 'failed', 'complete') NOT NULL;
-- END IF;
-- END;
-- $$

DELIMITER ;

-- If adding new procedures, add them to the bottom of this list
Expand Down Expand Up @@ -1715,6 +1803,9 @@ CALL change_name_index_for_advanced_task_argument();
CALL add_confirmation_text_to_advanced_task_def();
CALL add_display_name_to_advanced_task_argument();
CALL add_ecdsa_ssh_key_types();
CALL add_new_task_status_types();
CALL update_active_succeeded_tasks();
CALL update_missing_tasknames();

-- Drop legacy SSH key procedures
DROP PROCEDURE IF EXISTS CreateProjectSshKey;
Expand Down
8 changes: 8 additions & 0 deletions services/api/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const {

const {
getTasksByEnvironmentId,
getTaskByTaskName,
getTaskByRemoteId,
getTaskById,
addTask,
Expand Down Expand Up @@ -306,6 +307,12 @@ const resolvers = {
PROBLEM: 'problem',
},
TaskStatusType: {
NEW: 'new',
PENDING: 'pending',
RUNNING: 'running',
CANCELLED: 'cancelled',
ERROR: 'error',
COMPLETE: 'complete',
ACTIVE: 'active',
SUCCEEDED: 'succeeded',
FAILED: 'failed',
Expand Down Expand Up @@ -432,6 +439,7 @@ const resolvers = {
userCanSshToEnvironment,
deploymentByRemoteId: getDeploymentByRemoteId,
deploymentsByBulkId: getDeploymentsByBulkId,
taskByTaskName: getTaskByTaskName,
taskByRemoteId: getTaskByRemoteId,
taskById: getTaskById,
advancedTaskDefinitionById,
Expand Down
6 changes: 6 additions & 0 deletions services/api/src/resources/task/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const Helpers = (sqlClientPool: Pool) => ({
addTask: async ({
id,
name,
taskName,
status,
created,
started,
Expand All @@ -26,6 +27,7 @@ export const Helpers = (sqlClientPool: Pool) => ({
}: {
id?: number;
name: string;
taskName: string;
status?: string;
created?: string;
started?: string;
Expand All @@ -41,6 +43,7 @@ export const Helpers = (sqlClientPool: Pool) => ({
Sql.insertTask({
id,
name,
taskName,
status,
created,
started,
Expand Down Expand Up @@ -99,6 +102,7 @@ export const Helpers = (sqlClientPool: Pool) => ({
{
id,
name,
taskName,
status,
created,
started,
Expand All @@ -112,6 +116,7 @@ export const Helpers = (sqlClientPool: Pool) => ({
}: {
id?: number,
name: string,
taskName: string,
status?: string,
created?: string,
started?: string,
Expand Down Expand Up @@ -142,6 +147,7 @@ export const Helpers = (sqlClientPool: Pool) => ({
Sql.insertTask({
id,
name,
taskName,
status,
created,
started,
Expand Down
42 changes: 41 additions & 1 deletion services/api/src/resources/task/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Helpers as projectHelpers } from '../project/helpers';
import { Validators as envValidators } from '../environment/validators';
import S3 from 'aws-sdk/clients/s3';
import sha1 from 'sha1';
import { generateTaskName } from '@lagoon/commons/dist/util';

const accessKeyId = process.env.S3_FILES_ACCESS_KEY_ID || 'minio'
const secretAccessKey = process.env.S3_FILES_SECRET_ACCESS_KEY || 'minio123'
Expand Down Expand Up @@ -97,7 +98,7 @@ export const getTaskLog: ResolverFn = async (

export const getTasksByEnvironmentId: ResolverFn = async (
{ id: eid },
{ id: filterId, limit },
{ id: filterId, taskName: taskName, limit },
{ sqlClientPool, hasPermission }
) => {
const environment = await environmentHelpers(
Expand All @@ -116,13 +117,41 @@ export const getTasksByEnvironmentId: ResolverFn = async (
queryBuilder = queryBuilder.andWhere('id', filterId);
}

if (taskName) {
queryBuilder = queryBuilder.andWhere('task_name', taskName);
}

if (limit) {
queryBuilder = queryBuilder.limit(limit);
}

return query(sqlClientPool, queryBuilder.toString());
};

export const getTaskByTaskName: ResolverFn = async (
root,
{ taskName },
{ sqlClientPool, hasPermission }
) => {
const queryString = knex('task')
.where('task_name', '=', taskName)
.toString();

const rows = await query(sqlClientPool, queryString);
const task = R.prop(0, rows);

if (!task) {
return null;
}

const rowsPerms = await query(sqlClientPool, Sql.selectPermsForTask(task.id));
await hasPermission('task', 'view', {
project: R.path(['0', 'pid'], rowsPerms)
});

return task;
};

export const getTaskByRemoteId: ResolverFn = async (
root,
{ id },
Expand Down Expand Up @@ -208,13 +237,16 @@ export const addTask: ResolverFn = async (
execute = true;
}

let taskName = generateTaskName()

userActivityLogger(`User added task '${name}'`, {
project: '',
event: 'api:addTask',
payload: {
input: {
id,
name,
taskName,
status,
created,
started,
Expand All @@ -231,6 +263,7 @@ export const addTask: ResolverFn = async (
const taskData = await Helpers(sqlClientPool).addTask({
id,
name,
taskName,
status,
created,
started,
Expand Down Expand Up @@ -393,6 +426,7 @@ TOKEN="$(ssh -p $TASK_SSH_PORT -t lagoon@$TASK_SSH_HOST token)" && curl -sS "$TA

const taskData = await Helpers(sqlClientPool).addTask({
name: 'Drush archive-dump',
taskName: generateTaskName(),
environment: environmentId,
service: 'cli',
command,
Expand Down Expand Up @@ -441,6 +475,7 @@ TOKEN="$(ssh -p $TASK_SSH_PORT -t lagoon@$TASK_SSH_HOST token)" && curl -sS "$TA

const taskData = await Helpers(sqlClientPool).addTask({
name: 'Drush sql-dump',
taskName: generateTaskName(),
environment: environmentId,
service: 'cli',
command,
Expand Down Expand Up @@ -491,6 +526,7 @@ export const taskDrushCacheClear: ResolverFn = async (

const taskData = await Helpers(sqlClientPool).addTask({
name: 'Drush cache-clear',
taskName: generateTaskName(),
environment: environmentId,
service: 'cli',
command,
Expand Down Expand Up @@ -530,6 +566,7 @@ export const taskDrushCron: ResolverFn = async (

const taskData = await Helpers(sqlClientPool).addTask({
name: 'Drush cron',
taskName: generateTaskName(),
environment: environmentId,
service: 'cli',
command: `drush cron`,
Expand Down Expand Up @@ -601,6 +638,7 @@ export const taskDrushSqlSync: ResolverFn = async (

const taskData = await Helpers(sqlClientPool).addTask({
name: `Sync DB ${sourceEnvironment.name} -> ${destinationEnvironment.name}`,
taskName: generateTaskName(),
environment: destinationEnvironmentId,
service: 'cli',
command: command,
Expand Down Expand Up @@ -672,6 +710,7 @@ export const taskDrushRsyncFiles: ResolverFn = async (

const taskData = await Helpers(sqlClientPool).addTask({
name: `Sync files ${sourceEnvironment.name} -> ${destinationEnvironment.name}`,
taskName: generateTaskName(),
environment: destinationEnvironmentId,
service: 'cli',
command: command,
Expand Down Expand Up @@ -711,6 +750,7 @@ export const taskDrushUserLogin: ResolverFn = async (

const taskData = await Helpers(sqlClientPool).addTask({
name: 'Drush uli',
taskName: generateTaskName(),
environment: environmentId,
service: 'cli',
command: `drush uli`,
Expand Down
3 changes: 3 additions & 0 deletions services/api/src/resources/task/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const Sql = {
insertTask: ({
id,
name,
taskName,
status,
created,
started,
Expand All @@ -22,6 +23,7 @@ export const Sql = {
}: {
id: number;
name: string;
taskName: string,
status: string;
created: string;
started: string;
Expand All @@ -38,6 +40,7 @@ export const Sql = {
.insert({
id,
name,
taskName,
status,
created,
started,
Expand Down
3 changes: 3 additions & 0 deletions services/api/src/resources/task/task_definition_resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import convertDateToMYSQLDateTimeFormat from '../../util/convertDateToMYSQLDateT
import * as advancedTaskToolbox from './advancedtasktoolbox';
import { IKeycloakAuthAttributes, KeycloakUnauthorizedError } from '../../util/auth';
import { Environment } from '../../resolvers';
import { generateTaskName } from '@lagoon/commons/dist/util';

enum AdvancedTaskDefinitionTarget {
Group,
Expand Down Expand Up @@ -532,6 +533,7 @@ export const invokeRegisteredTask = async (

const taskData = await Helpers(sqlClientPool).addTask({
name: task.name,
taskName: generateTaskName(),
environment: environment,
service: task.service,
command: taskCommand,
Expand All @@ -554,6 +556,7 @@ export const invokeRegisteredTask = async (

const advancedTaskData = await Helpers(sqlClientPool).addAdvancedTask({
name: task.name,
taskName: generateTaskName(),
created: undefined,
started: undefined,
completed: undefined,
Expand Down
Loading

0 comments on commit 56567d1

Please sign in to comment.