Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/quickie/site-create-form #985

Merged
merged 6 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/database/models/Deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ export class Deployment extends Model {
})
hostingId!: string

@Column({
allowNull: false,
type: DataType.TEXT,
})
stagingLiteHostingId!: string

@Column({
allowNull: true,
type: DataType.TEXT,
Expand Down
2 changes: 2 additions & 0 deletions src/fixtures/sites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const MOCK_DEPLOYMENT_DBENTRY_ONE = {
hostingId: "1",
encryptionIv: null,
encryptedPassword: null,
stagingLiteHostingId: "2",
}

export const MOCK_DEPLOYMENT_DBENTRY_TWO: Attributes<Deployment> = {
Expand All @@ -98,6 +99,7 @@ export const MOCK_DEPLOYMENT_DBENTRY_TWO: Attributes<Deployment> = {
encryptionIv: "12345678901234561234567890123456",
encryptedPassword:
"1234567890123456789012345678901234567890123456789012345678901234",
stagingLiteHostingId: "2",
}

export const MOCK_SITEMEMBER_DBENTRY_ONE: Attributes<SiteMember> = {
Expand Down
1 change: 0 additions & 1 deletion src/routes/formsgSiteCreation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import UsersService from "@services/identity/UsersService"
import InfraService from "@services/infra/InfraService"
import { mailer } from "@services/utilServices/MailClient"

const SITE_CLONE_FORM_KEY = config.get("formSg.siteCloneFormKey")
const SITE_CREATE_FORM_KEY = config.get("formSg.siteCreateFormKey")
const REQUESTER_EMAIL_FIELD = "Government E-mail"
const SITE_NAME_FIELD = "Site Name"
Expand Down
49 changes: 38 additions & 11 deletions src/services/identity/DeploymentsService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { errAsync, okAsync } from "neverthrow"
import { Result, errAsync, okAsync } from "neverthrow"
import { ModelStatic } from "sequelize"

import { config } from "@config/config"
Expand Down Expand Up @@ -43,32 +43,59 @@ class DeploymentsService {
repoName: string
site: Site
}): Promise<Deployment> => {
const amplifyResult = await this.createAmplifyAppOnAws(repoName)
if (amplifyResult.isErr()) {
logger.error(`Amplify set up error: ${amplifyResult.error}`)
throw amplifyResult.error
const [
amplifyStagingResult,
amplifyStagingLiteResult,
] = await this.createAmplifyAppsOnAws(repoName)
if (amplifyStagingResult.isErr()) {
logger.error(
`Amplify set up error for main app: ${amplifyStagingResult.error}`
)
throw amplifyStagingResult.error
}

if (amplifyStagingLiteResult.isErr()) {
logger.error(
`Amplify set up error for staging-lite app: ${amplifyStagingLiteResult.error}`
)
throw amplifyStagingLiteResult.error
}
const amplifyInfo = amplifyResult.value

const amplifyInfoStaging = amplifyStagingResult.value
const amplifyInfoStagingLite = amplifyStagingLiteResult.value

return this.create({
stagingUrl: Brand.fromString(
`https://staging.${amplifyInfo.defaultDomain}`
`https://staging.${amplifyStagingLiteResult.value.defaultDomain}`
),
productionUrl: Brand.fromString(
`https://master.${amplifyInfo.defaultDomain}`
`https://master.${amplifyStagingResult.value.defaultDomain}`
),
site,
siteId: site.id,
hostingId: amplifyInfo.id,
hostingId: amplifyInfoStaging.id,
stagingLiteHostingId: amplifyInfoStagingLite.id,
})
}

createAmplifyAppOnAws = async (repoName: string) => {
createAmplifyAppsOnAws = async (repoName: string) => {
const stagingApp = await this.createAmplifyAppOnAws(repoName, repoName)
const stagingLiteApp = await this.createAmplifyAppOnAws(
repoName,
`${repoName}-staging-lite`
)
return [stagingApp, stagingLiteApp]
}

createAmplifyAppOnAws = async (
repoName: string,
appName: string
): Promise<Result<AmplifyInfo, AmplifyError>> => {
const repoUrl = `https://github.com/isomerpages/${repoName}`
logger.info(`PublishToAmplify ${repoUrl}`)

const createAppOptions = this.deploymentClient.generateCreateAppInput(
repoName,
appName,
repoUrl
)
// 1. Create Amplify app
Expand Down
63 changes: 50 additions & 13 deletions src/services/identity/ReposService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { exec } from "child_process"
import fs from "fs"
import path from "path"

import { retry } from "@octokit/plugin-retry"
import { Octokit } from "@octokit/rest"
Expand All @@ -13,7 +15,11 @@ import { config } from "@config/config"
import { UnprocessableError } from "@errors/UnprocessableError"

import { Repo, Site } from "@database/models"
import { DNS_INDIRECTION_REPO, EFS_VOL_PATH_STAGING } from "@root/constants"
import {
DNS_INDIRECTION_REPO,
EFS_VOL_PATH_STAGING,
EFS_VOL_PATH_STAGING_LITE,
} from "@root/constants"
import GitHubApiError from "@root/errors/GitHubApiError"
import logger from "@root/logger/logger"

Expand Down Expand Up @@ -63,7 +69,11 @@ export default class ReposService {
this.simpleGit = simpleGit
}

getLocalRepoPath = (repoName: string) => `${EFS_VOL_PATH_STAGING}/${repoName}`
getLocalStagingRepoPath = (repoName: string) =>
path.join(EFS_VOL_PATH_STAGING, repoName)

getLocalStagingLiteRepoPath = (repoName: string) =>
path.join(EFS_VOL_PATH_STAGING_LITE, repoName)

create = (createParams: repoCreateParamsType): Promise<Repo> =>
this.repository.create(createParams)
Expand Down Expand Up @@ -107,7 +117,7 @@ export default class ReposService {
productionUrl: string,
stagingUrl: string
) => {
const dir = this.getLocalRepoPath(repoName)
const dir = this.getLocalStagingRepoPath(repoName)

// 1. Set URLs in local _config.yml
this.setUrlsInLocalConfig(dir, repoName, stagingUrl, productionUrl)
Expand Down Expand Up @@ -206,45 +216,72 @@ export default class ReposService {
repoName: string,
repoUrl: string
): Promise<void> => {
const dir = this.getLocalRepoPath(repoName)
const stgDir = this.getLocalStagingRepoPath(repoName)
const stgLiteDir = this.getLocalStagingLiteRepoPath(repoName)

// Make sure the local path is empty, just in case dir was used on a previous attempt.
fs.rmSync(`${dir}`, { recursive: true, force: true })
fs.rmSync(`${stgDir}`, { recursive: true, force: true })

// Clone base repo locally
fs.mkdirSync(dir)
fs.mkdirSync(stgDir)
await this.simpleGit
.cwd(dir)
.clone(SITE_CREATION_BASE_REPO_URL, dir, ["-b", "staging"])
.cwd(stgDir)
.clone(SITE_CREATION_BASE_REPO_URL, stgDir, ["-b", "staging"])

// Clear git
fs.rmSync(`${dir}/.git`, { recursive: true, force: true })
fs.rmSync(`${stgDir}/.git`, { recursive: true, force: true })

// Prepare git repo
await this.simpleGit
.cwd(dir)
.cwd(stgDir)
.init(["--initial-branch=staging"])
.checkoutLocalBranch("staging")

// Add all the changes
await this.simpleGit.cwd(dir).add(".")
await this.simpleGit.cwd(stgDir).add(".")

// Commit
await this.simpleGit
.cwd(dir)
.cwd(stgDir)
.addConfig("user.name", "isomeradmin")
.addConfig("user.email", ISOMER_GITHUB_EMAIL)
.commit("Initial commit")

// Push to origin
await this.simpleGit
.cwd(dir)
.cwd(stgDir)
.addRemote("origin", repoUrl)
.checkout("staging")
.push(["-u", "origin", "staging"]) // push to staging first to make it the default branch on GitHub
.checkoutLocalBranch("master")
.push(["-u", "origin", "master"])
.checkout("staging") // reset local branch back to staging

// Make sure the local path is empty, just in case dir was used on a previous attempt.
fs.rmSync(`${stgLiteDir}`, { recursive: true, force: true })
// create a empty folder stgLiteDir
fs.mkdirSync(stgLiteDir)

// Create staging lite branch in other repo path
await this.simpleGit
.cwd(stgLiteDir)
.clone(repoUrl, stgLiteDir)
.checkout("staging")
.rm(["-r", "images"])
.rm(["-r", "files"])

// Clear git
fs.rmSync(`${stgLiteDir}/.git`, { recursive: true, force: true })

// Prepare git repo
await this.simpleGit
.cwd(stgLiteDir)
.init()
.checkoutLocalBranch("staging-lite")
.add(".")
.commit("Initial commit")
.addRemote("origin", repoUrl)
.push(["origin", "staging-lite", "-f"])
}

createDnsIndirectionFile = (
Expand Down
Loading