diff --git a/scripts/localstack-setup.js b/scripts/localstack-setup.js index 9e2122fbb..f9e5abce6 100755 --- a/scripts/localstack-setup.js +++ b/scripts/localstack-setup.js @@ -20,7 +20,7 @@ const s3 = new S3Client({ }, }); -const fakeIndex = ` +const fakeHtml = ` site @@ -30,37 +30,41 @@ Website `; -const cmd = new CreateBucketCommand({ Bucket: config.s3BuildLogs.bucket }); -s3.send(cmd); +async function initSitesAndBuckets() { + // create Build Logs bucket + const cmd = new CreateBucketCommand({ Bucket: config.s3BuildLogs.bucket }); + await s3.send(cmd); -Site.findAll() - .then((sites) => { - sites.forEach(async (site) => { - const siteCmd = new CreateBucketCommand({ Bucket: site.awsBucketName }); - await s3.send(siteCmd); - const folders = ['site', 'demo']; - folders.forEach((folder) => { - const folderCmd = new PutObjectCommand({ - Body: fakeIndex, - Bucket: site.awsBucketName, - Key: `${folder}/${site.owner}/${site.repository}/index.html`, + // create a bucket and some files for each Site + await Site.findAll() + .then(async (sites) => { + await Promise.all(sites.map(async (site) => { + const siteCmd = new CreateBucketCommand({ Bucket: site.awsBucketName }); + await s3.send(siteCmd); + const folders = ['site', 'demo']; + folders.forEach(async (folder) => { + const folderCmd = new PutObjectCommand({ + Body: fakeHtml, + Bucket: site.awsBucketName, + Key: `${folder}/${site.owner}/${site.repository}/index.html`, + }); + await s3.send(folderCmd); }); - s3.send(folderCmd); - }); - const fakePreviews = ['branch1', 'branch2', 'branch3']; - fakePreviews.forEach((branch) => { - const previewCmd = new PutObjectCommand({ - Body: fakeIndex, - Bucket: site.awsBucketName, - Key: `preview/${site.owner}/${site.repository}/${branch}/index.html`, + const fakePreviews = ['branch1', 'branch2', 'branch3']; + fakePreviews.forEach(async (branch) => { + const previewCmd = new PutObjectCommand({ + Body: fakeHtml, + Bucket: site.awsBucketName, + Key: `preview/${site.owner}/${site.repository}/${branch}/index.html`, + }); + await s3.send(previewCmd); }); - s3.send(previewCmd); - }); - }); - }) - .catch(err => console.error(err)); + })); + }) + .catch(err => console.error(err)); +} -console.log('Uploading logs to S3'); +// archive older build logs async function archiveLogs() { const date = moment().subtract(3, 'days').startOf('day'); @@ -78,4 +82,27 @@ async function archiveLogs() { builds.forEach(build => BuildLogs.archiveBuildLogsForBuildId(build.id)); } -archiveLogs(); +// create artifacts for Build Tasks +async function createBuildTaskArtifacts() { + const artifactedTasks = await BuildTask.findAll({ + where: { + artifact: { + [Op.not]: null, + }, + }, + include: [{ model: Build, include: [Site] }], + }); + artifactedTasks.forEach((task) => { + console.log(task.Build.Site) + const artifactCmd = new PutObjectCommand({ + Body: fakeHtml, + Bucket: task.Build.Site.awsBucketName, + Key: task.artifact, + }); + s3.send(artifactCmd); + }); +} + +initSitesAndBuckets() + .then(archiveLogs) + .then(createBuildTaskArtifacts);