Skip to content

Commit

Permalink
Add DrushSqlDump and DrushCacheClear Tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Schnitzel committed Jan 22, 2019
1 parent b6de96a commit 356d452
Show file tree
Hide file tree
Showing 10 changed files with 294 additions and 21 deletions.
6 changes: 3 additions & 3 deletions services/api/src/clients/aws.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const R = require('ramda');
const AWS = require('aws-sdk');

const s3Host = R.propOr('http://docker.for.mac.localhost:9000', 'S3_HOST', process.env);
const accessKeyId = R.propOr('minio', 'S3_ACCESS_KEY_ID', process.env);
const secretAccessKey = R.propOr('minio123', 'S3_SECRET_ACCESS_KEY', process.env);
const bucket = R.propOr('api-files', 'S3_BUCKET', process.env);
const accessKeyId = R.propOr('minio', 'S3_ACCESS_KEY_ID', process.env);
const secretAccessKey = R.propOr('minio123', 'S3_SECRET_ACCESS_KEY', process.env);
const bucket = R.propOr('lagoon-files', 'S3_BUCKET', process.env);

const s3 = new AWS.S3({
endpoint: s3Host,
Expand Down
4 changes: 4 additions & 0 deletions services/api/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const {
deleteTask,
updateTask,
taskDrushArchiveDump,
taskDrushSqlDump,
taskDrushCacheClear,
taskDrushSqlSync,
taskDrushRsyncFiles,
taskSubscriber,
Expand Down Expand Up @@ -273,6 +275,8 @@ const resolvers /* : { [string]: ResolversObj | typeof GraphQLDate } */ = {
deleteEnvVariable,
addTask,
taskDrushArchiveDump,
taskDrushSqlDump,
taskDrushCacheClear,
taskDrushSqlSync,
taskDrushRsyncFiles,
deleteTask,
Expand Down
78 changes: 74 additions & 4 deletions services/api/src/resources/task/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,12 @@ const taskDrushArchiveDump = async (
await envValidators.userAccessEnvironment(credentials, environmentId);
await envValidators.environmentHasService(environmentId, 'cli');

const command = String.raw`drush ard --pipe | \
xargs -I_file curl -sS "$TASK_API_HOST"/graphql \
const command = String.raw`file="/tmp/$LAGOON_SAFE_PROJECT-$LAGOON_GIT_SAFE_BRANCH-$(date --iso-8601=seconds).tar" && drush ard --destination=$file && \
curl -sS "$TASK_API_HOST"/graphql \
-H "Authorization: Bearer $TASK_API_AUTH" \
-F operations='{ "query": "mutation ($task: Int!, $files: [Upload!]!) { uploadFilesForTask(input:{task:$task, files:$files}) { id files { filename } } }", "variables": { "task": '"$TASK_DATA_ID"', "files": [null] } }' \
-F map='{ "0": ["variables.files.0"] }' \
-F 0=@_file
-F 0=@$file; rm -rf $file;
`;

const taskData = await Helpers.addTask({
Expand All @@ -281,6 +281,73 @@ xargs -I_file curl -sS "$TASK_API_HOST"/graphql \
return taskData;
};

const taskDrushSqlDump = async (
root,
{
environment: environmentId,
},
{
credentials,
},
) => {
await envValidators.environmentExists(environmentId);
await envValidators.userAccessEnvironment(credentials, environmentId);
await envValidators.environmentHasService(environmentId, 'cli');

const command = String.raw`file="/tmp/$LAGOON_SAFE_PROJECT-$LAGOON_GIT_SAFE_BRANCH-$(date --iso-8601=seconds).sql" && drush sql-dump --result-file=$file --gzip && \
curl -sS "$TASK_API_HOST"/graphql \
-H "Authorization: Bearer $TASK_API_AUTH" \
-F operations='{ "query": "mutation ($task: Int!, $files: [Upload!]!) { uploadFilesForTask(input:{task:$task, files:$files}) { id files { filename } } }", "variables": { "task": '"$TASK_DATA_ID"', "files": [null] } }' \
-F map='{ "0": ["variables.files.0"] }' \
-F 0=@$file.gz; rm -rf $file.gz
`;

const taskData = await Helpers.addTask({
name: 'Drush sql-dump',
environment: environmentId,
service: 'cli',
command,
execute: true,
});

return taskData;
};

const taskDrushCacheClear = async (
root,
{
environment: environmentId,
},
{
credentials,
},
) => {
await envValidators.environmentExists(environmentId);
await envValidators.userAccessEnvironment(credentials, environmentId);
await envValidators.environmentHasService(environmentId, 'cli');

const command = 'drupal_version=$(drush status drupal-version --format=list) && \
if [ ${drupal_version%.*.*} == "8" ]; then \
drush cr; \
elif [ ${drupal_version%.*} == "7" ]; then \
drush cc all; \
else \
echo \"could not clear cache for found Drupal Version ${drupal_version}\"; \
exit 1; \
fi';


const taskData = await Helpers.addTask({
name: 'Drush cache-clear',
environment: environmentId,
service: 'cli',
command,
execute: true,
});

return taskData;
};

const taskDrushSqlSync = async (
root,
{
Expand Down Expand Up @@ -347,7 +414,7 @@ const taskSubscriber = createEnvironmentFilteredSubscriber(
[
EVENTS.TASK.ADDED,
EVENTS.TASK.UPDATED,
]
],
);

const Resolvers /* : ResolversObj */ = {
Expand All @@ -357,9 +424,12 @@ const Resolvers /* : ResolversObj */ = {
deleteTask,
updateTask,
taskDrushArchiveDump,
taskDrushSqlDump,
taskDrushCacheClear,
taskDrushSqlSync,
taskDrushRsyncFiles,
taskSubscriber,
};


module.exports = Resolvers;
2 changes: 2 additions & 0 deletions services/api/src/typeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,8 @@ const typeDefs = gql`
deleteEnvVariable(input: DeleteEnvVariableInput!): String
addTask(input: TaskInput!): Task
taskDrushArchiveDump(environment: Int!): Task
taskDrushSqlDump(environment: Int!): Task
taskDrushCacheClear(environment: Int!): Task
taskDrushSqlSync(
sourceEnvironment: Int!
destinationEnvironment: Int!
Expand Down
96 changes: 96 additions & 0 deletions services/ui/src/components/AddTask/components/DrushCacheClear.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import React from 'react';
import { Mutation } from 'react-apollo';
import gql from 'graphql-tag';
import ReactSelect from 'react-select';
import { bp, color, fontSize } from '../../../variables';

const taskDrushCacheClear = gql`
mutation taskDrushCacheClear(
$environment: Int!
) {
taskDrushCacheClear(
environment: $environment
) {
id
name
status
created
started
completed
remoteId
command
service
}
}
`;

const DrushCacheClear = ({
pageEnvironment,
onCompleted,
onError,
}) => (
<Mutation
mutation={taskDrushCacheClear}
onCompleted={onCompleted}
onError={onError}
>
{(taskDrushCacheClear, { loading, called, error, data }) => {
return (
<React.Fragment>
<div className="envSelect">
<label id="dest-env">Environment:</label>
<ReactSelect
aria-labelledby="dest-env"
name="dest-environment"
value={{
label: pageEnvironment.name,
value: pageEnvironment.id,
}}
options={[
{
label: pageEnvironment.name,
value: pageEnvironment.id,
}
]}
isDisabled
required
/>
</div>
<button
onClick={() =>
taskDrushCacheClear({
variables: {
environment: pageEnvironment.id
}
})
}
>
Add task
</button>
<style jsx>{`
.envSelect {
margin-top: 10px;
}
button {
align-self: flex-end;
background-color: ${color.lightestGrey};
border: none;
border-radius: 20px;
color: ${color.darkGrey};
font-family: 'source-code-pro', sans-serif;
${fontSize(13)};
margin-top: 10px;
padding: 3px 20px 2px;
text-transform: uppercase;
@media ${bp.tinyUp} {
align-self: auto;
}
}
`}</style>
</React.Fragment>
);
}}
</Mutation>
);

export default DrushCacheClear;
96 changes: 96 additions & 0 deletions services/ui/src/components/AddTask/components/DrushSqlDump.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import React from 'react';
import { Mutation } from 'react-apollo';
import gql from 'graphql-tag';
import ReactSelect from 'react-select';
import { bp, color, fontSize } from '../../../variables';

const taskDrushSqlDump = gql`
mutation taskDrushSqlDump(
$environment: Int!
) {
taskDrushSqlDump(
environment: $environment
) {
id
name
status
created
started
completed
remoteId
command
service
}
}
`;

const DrushSqlDump = ({
pageEnvironment,
onCompleted,
onError,
}) => (
<Mutation
mutation={taskDrushSqlDump}
onCompleted={onCompleted}
onError={onError}
>
{(taskDrushSqlDump, { loading, called, error, data }) => {
return (
<React.Fragment>
<div className="envSelect">
<label id="dest-env">Environment:</label>
<ReactSelect
aria-labelledby="dest-env"
name="dest-environment"
value={{
label: pageEnvironment.name,
value: pageEnvironment.id,
}}
options={[
{
label: pageEnvironment.name,
value: pageEnvironment.id,
}
]}
isDisabled
required
/>
</div>
<button
onClick={() =>
taskDrushSqlDump({
variables: {
environment: pageEnvironment.id
}
})
}
>
Add task
</button>
<style jsx>{`
.envSelect {
margin-top: 10px;
}
button {
align-self: flex-end;
background-color: ${color.lightestGrey};
border: none;
border-radius: 20px;
color: ${color.darkGrey};
font-family: 'source-code-pro', sans-serif;
${fontSize(13)};
margin-top: 10px;
padding: 3px 20px 2px;
text-transform: uppercase;
@media ${bp.tinyUp} {
align-self: auto;
}
}
`}</style>
</React.Fragment>
);
}}
</Mutation>
);

export default DrushSqlDump;
4 changes: 4 additions & 0 deletions services/ui/src/components/AddTask/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React from 'react';
import ReactSelect from 'react-select';
import withLogic from './logic';
import DrushArchiveDump from './components/DrushArchiveDump';
import DrushSqlDump from './components/DrushSqlDump';
import DrushCacheClear from './components/DrushCacheClear';
import DrushRsyncFiles from './components/DrushRsyncFiles';
import DrushSqlSync from './components/DrushSqlSync';
import Empty from './components/Empty';
Expand All @@ -20,6 +22,8 @@ const AddTask = ({
}) => {
const newTaskComponents = {
DrushArchiveDump,
DrushSqlDump,
DrushCacheClear,
DrushRsyncFiles,
DrushSqlSync,
Empty,
Expand Down
17 changes: 13 additions & 4 deletions services/ui/src/components/AddTask/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,26 @@ const withOptions = withProps(({ pageEnvironment }) => {
return {
options: [
{
label: 'Drush sql-sync',
value: 'DrushSqlSync'
label: 'Drush cache-clear',
value: 'DrushCacheClear'
},
{
label: 'Drush archive-dump',
value: 'DrushArchiveDump'
label: 'Drush sql-sync',
value: 'DrushSqlSync'
},
{
label: 'Drush rsync',
value: 'DrushRsyncFiles'
},
{
label: 'Drush sql-dump',
value: 'DrushSqlDump'
},
{
label: 'Drush archive-dump (D7 only)',
value: 'DrushArchiveDump'
}

]
};
});
Expand Down
Loading

0 comments on commit 356d452

Please sign in to comment.