Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
Envbuild (#100)
Browse files Browse the repository at this point in the history
* Added new build system to Amplify Video. Allows easier env switches and custom overrides.

* Removed setup command from commands. Replaced with build command.

* Version bump

* Added envInit due to PrePush not working for new one click apps

* Changed error message for failed S3 sync
  • Loading branch information
wizage authored May 28, 2020
1 parent 796df36 commit a69493a
Show file tree
Hide file tree
Showing 13 changed files with 407 additions and 261 deletions.
2 changes: 1 addition & 1 deletion amplify-plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"type": "category",
"commands": [
"add",
"build",
"get-info",
"help",
"push",
"remove",
"setup",
"setup-obs",
"start",
"stop",
Expand Down
43 changes: 43 additions & 0 deletions commands/video/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const inquirer = require('inquirer');
const fs = require('fs');

const subcommand = 'build';
const category = 'video';

module.exports = {
name: subcommand,
run: async (context) => {
const { amplify } = context;
const targetDir = amplify.pathManager.getBackendDirPath();
const amplifyMeta = amplify.getProjectMeta();

if (!(category in amplifyMeta) || Object.keys(amplifyMeta[category]).length === 0) {
context.print.error(`You have no ${category} projects.`);
return;
}

const chooseProject = [
{
type: 'list',
name: 'resourceName',
message: 'Choose what project you want to build?',
choices: Object.keys(amplifyMeta[category]),
default: Object.keys(amplifyMeta[category])[0],
},
];
const shared = await inquirer.prompt(chooseProject);

const options = amplifyMeta.video[shared.resourceName];

const { buildTemplates } = require(`../../provider-utils/${options.providerPlugin}/utils/video-staging`);
if (!buildTemplates) {
context.print.error('No builder is configured for this provider');
return;
}

const props = JSON.parse(fs.readFileSync(`${targetDir}/video/${shared.resourceName}/props.json`));

return buildTemplates(context, props);
},

};
38 changes: 0 additions & 38 deletions commands/video/setup.js

This file was deleted.

29 changes: 14 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const category = 'video';
const path = require('path');
const ora = require('ora');
const { pushTemplates } = require('./provider-utils/awscloudformation/utils/video-staging');

const { copyFilesToS3 } = require('./provider-utils/awscloudformation/utils/video-staging');

async function add(context, providerName, service) {
const options = {
Expand All @@ -27,6 +26,16 @@ async function onAmplifyCategoryOutputChange(context) {
await infoController.getInfoVideoAll(context);
}

async function initEnv(context) {
const { amplify } = context;
const amplifyMeta = amplify.getProjectMeta();

if (!(category in amplifyMeta) || Object.keys(amplifyMeta[category]).length === 0) {
return;
}

await pushTemplates(context);
}

async function migrate(context) {
const { projectPath, amplifyMeta } = context.migrationInfo;
Expand Down Expand Up @@ -71,30 +80,19 @@ async function executeAmplifyCommand(context) {

async function handleAmplifyEvent(context, args) {
if (args.event === 'PrePush') {
handlePrePush(context);
} else {
// console.log(args.event);
await handlePrePush(context);
}
}

async function handlePrePush(context) {
const { amplify } = context;
const amplifyMeta = amplify.getProjectMeta();
const spinner = ora('Copying video resources. This may take a few minutes...');

if (!(category in amplifyMeta) || Object.keys(amplifyMeta[category]).length === 0) {
return;
}

spinner.start();

Object.keys(amplifyMeta[category]).forEach((resourceName) => {
const options = amplifyMeta.video[resourceName];
const serviceMetadata = context.amplify.readJsonFile(`${__dirname}/provider-utils/supported-services.json`)[options.serviceType];
const { stackFolder } = serviceMetadata;
copyFilesToS3(context, options, resourceName, stackFolder);
});
spinner.succeed('All resources copied.');
await pushTemplates(context);
}

module.exports = {
Expand All @@ -104,4 +102,5 @@ module.exports = {
onAmplifyCategoryOutputChange,
executeAmplifyCommand,
handleAmplifyEvent,
initEnv,
};
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "amplify-category-video",
"version": "2.7.7",
"version": "2.8.3",
"description": "Plugin for Amplify to add support for live streaming. Made for Unicorn Trivia Workshop",
"main": "index.js",
"scripts": {
Expand All @@ -22,6 +22,7 @@
"dependencies": {
"archiver": "^3.1.1",
"chalk": "^2.4.2",
"child_process": "^1.0.2",
"ejs": "^2.7.1",
"eslint": "^5.16.0",
"fs-extra": "^7.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"AWSTemplateFormatVersion": "2010-09-09",

"Description": "<%= props.shared.resourceName %>",
"Description": "AmplifyLivestream - Amplify Livestream template. Do not make changes to the root template. This gets overwritten every push.",

"Metadata": {
"AWS::CloudFormation::Interface": {
Expand Down Expand Up @@ -451,7 +451,7 @@
"Type": "String",
"Description": "store template and lambda package",
"AllowedPattern" : "[a-zA-Z][a-zA-Z0-9-_]*",
"Default": "<%= props.shared.bucket %>"
"Default": "<%= props.env.bucket %>"
},

"pSourceFolder": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Description: <%= props.shared.resourceName %>
Description: "AmplifyVOD - Amplify Video on Demand template. Do not make changes to the root template. This gets overwritten every push."

Parameters:
env:
Expand All @@ -13,21 +13,25 @@ Parameters:
Type: String
Description: Arn for authRoleArn
Default: NONE
s3UUID:
Type: String
Description: Unique UUID for env
Default: xxxxx-xxxxx
pS3:
Type: String
Description: Store template and lambda package
AllowedPattern: "[a-zA-Z][a-zA-Z0-9-_]*"
Default: "<%= props.shared.bucket %>"
Default: "<%= props.env.bucket %>"
pS3InputName:
Type: String
Description: Store template and lambda package
AllowedPattern: "[a-zA-Z][a-zA-Z0-9-_]*"
Default: "<%= props.shared.bucketInput %>"
Default: "<%= props.env.bucketInput %>"
pS3OutputName:
Type: String
Description: Store template and lambda package
AllowedPattern: "[a-zA-Z][a-zA-Z0-9-_]*"
Default: "<%= props.shared.bucketOutput %>"
Default: "<%= props.env.bucketOutput %>"
pSourceFolder:
Type: String
Description: Store template and lambda package
Expand Down Expand Up @@ -118,6 +122,9 @@ Resources:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: !Sub "https://s3.amazonaws.com/${pS3}/${pSourceFolder}/CreateJobTemplate.template"
Parameters:
pProjectName: !Ref pProjectName
env: !Ref env
rInputLambdaTriggerFunction:
Type: AWS::CloudFormation::Stack
Properties:
Expand Down
43 changes: 24 additions & 19 deletions provider-utils/awscloudformation/index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,50 @@
const fs = require('fs');
const chalk = require('chalk');
const { stageVideo, resetupFiles } = require('./utils/video-staging');
const { buildTemplates } = require('./utils/video-staging');

let serviceMetadata;

async function addResource(context, service, options) {
serviceMetadata = context.amplify.readJsonFile(`${__dirname}/../supported-services.json`)[service];
const { cfnFilename, stackFolder } = serviceMetadata;
const targetDir = context.amplify.pathManager.getBackendDirPath();
const { serviceWalkthroughFilename, defaultValuesFilename } = serviceMetadata;
const serviceWalkthroughSrc = `${__dirname}/service-walkthroughs/${serviceWalkthroughFilename}`;
const { serviceQuestions } = require(serviceWalkthroughSrc);
const result = await serviceQuestions(context, options, defaultValuesFilename);
await stageVideo(context, options, result, cfnFilename, stackFolder, 'add');
context.amplify.updateamplifyMetaAfterResourceAdd(
'video',
result.shared.resourceName,
options,
);
if (!fs.existsSync(`${targetDir}/video/${result.shared.resourceName}/`)) {
fs.mkdirSync(`${targetDir}/video/${result.shared.resourceName}/`, { recursive: true });
}
if (result.parameters !== undefined) {
await fs.writeFileSync(`${targetDir}/video/${result.shared.resourceName}/parameters.json`, JSON.stringify(result.parameters, null, 4));
}
await fs.writeFileSync(`${targetDir}/video/${result.shared.resourceName}/props.json`, JSON.stringify(result, null, 4));
await buildTemplates(context, result);
console.log(chalk`{green Successfully configured ${result.shared.resourceName}}`);
}

async function updateResource(context, service, options, resourceName) {
serviceMetadata = context.amplify.readJsonFile(`${__dirname}/../supported-services.json`)[service];
const { cfnFilename, stackFolder } = serviceMetadata;
const targetDir = context.amplify.pathManager.getBackendDirPath();
const { serviceWalkthroughFilename, defaultValuesFilename } = serviceMetadata;
const serviceWalkthroughSrc = `${__dirname}/service-walkthroughs/${serviceWalkthroughFilename}`;
const { serviceQuestions } = require(serviceWalkthroughSrc);
const result = await serviceQuestions(context, options, defaultValuesFilename, resourceName);
await stageVideo(context, options, result, cfnFilename, stackFolder, 'update');
if (result.parameters !== undefined) {
await fs.writeFileSync(`${targetDir}/video/${result.shared.resourceName}/parameters.json`, JSON.stringify(result.parameters, null, 4));
}
await fs.writeFileSync(`${targetDir}/video/${result.shared.resourceName}/props.json`, JSON.stringify(result, null, 4));
await buildTemplates(context, result);
console.log(chalk`{green Successfully updated ${result.shared.resourceName}}`);
}

async function livestreamStartStop(context, service, options, resourceName, start) {
serviceMetadata = context.amplify.readJsonFile(`${__dirname}/../supported-services.json`)[service];
const { cfnFilename, stackFolder } = serviceMetadata;

const { amplify } = context;
const amplifyMeta = context.amplify.getProjectMeta();
if (amplifyMeta.video[resourceName].output) {
Expand All @@ -37,11 +53,8 @@ async function livestreamStartStop(context, service, options, resourceName, star
const props = JSON.parse(fs.readFileSync(`${targetDir}/video/${resourceName}/props.json`));
if ((props.mediaLive.autoStart === 'YES' && start) || (props.mediaLive.autoStart === 'NO' && !start)) {
props.mediaLive.autoStart = !start ? 'YES' : 'NO';
props.shared.resourceName = resourceName;
const serviceWalkthroughSrc = `${__dirname}/utils/video-staging.js`;
const { updateWithProps } = require(serviceWalkthroughSrc);
await updateWithProps(context, options, props, resourceName, cfnFilename, stackFolder);
await amplify.constructExeInfo(context);
await fs.writeFileSync(`${targetDir}/video/${props.shared.resourceName}/props.json`, JSON.stringify(props, null, 4));
await buildTemplates(context, props);
await amplify.pushResources(context, 'video', resourceName).catch((err) => {
context.print.info(err.stack);
context.print.error('There was an error pushing the video resource');
Expand All @@ -57,16 +70,8 @@ async function livestreamStartStop(context, service, options, resourceName, star
}
}

async function setupCloudFormation(context, service, options, resourceName) {
serviceMetadata = context.amplify.readJsonFile(`${__dirname}/../supported-services.json`)[service];
const { stackFolder } = serviceMetadata;
await resetupFiles(context, options, resourceName, stackFolder);
}


module.exports = {
addResource,
updateResource,
setupCloudFormation,
livestreamStartStop,
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module.exports = {

async function serviceQuestions(context, options, defaultValuesFilename, resourceName) {
const { amplify } = context;
const projectMeta = context.amplify.getProjectMeta();
const defaultLocation = path.resolve(`${__dirname}/../default-values/${defaultValuesFilename}`);
let resource = {};
const targetDir = amplify.pathManager.getBackendDirPath();
Expand Down Expand Up @@ -184,7 +183,6 @@ async function serviceQuestions(context, options, defaultValuesFilename, resourc

// main question control flow
const answers = {};
answers.bucket = projectMeta.providers.awscloudformation.DeploymentBucketName;

answers.resourceName = resource.name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ async function serviceQuestions(context, options, defaultValuesFilename, resourc
} else {
props.template.name = template.encodingTemplate;
const jobTemplate = JSON.parse(fs.readFileSync(`${pluginDir}/templates/${template.encodingTemplate}`));
jobTemplate.Name = `${jobTemplate.Name}-${props.shared.resourceName}-${projectDetails.localEnvInfo.envName}`;
fs.outputFileSync(`${targetDir}/video/${props.shared.resourceName}/mediaconvert-job-temp.json`, JSON.stringify(jobTemplate, null, 4));
}

Expand Down
2 changes: 1 addition & 1 deletion provider-utils/awscloudformation/utils/video-getinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async function generateAWSExportsVideo(context) {
target: filePath,
},
];
await context.amplify.copyBatch(context, copyJobs, props);
await context.amplify.copyBatch(context, copyJobs, props, true);
} else {
fs.writeFileSync(filePath, JSON.stringify(props, null, 4));
}
Expand Down
Loading

0 comments on commit a69493a

Please sign in to comment.