-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsst.config.ts
199 lines (172 loc) · 5.76 KB
/
sst.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { SSTConfig } from 'sst'
import { API } from './stacks/summer-stack'
import { ExternalAPI } from './stacks/partners-stack'
import { SdkAPI } from './stacks/sdk-stack'
import { $, chalk, echo } from 'zx'
const availableStage = ['dev', 'staging', 'production', 'armada-prod']
enum App {
SummerfiStack = 'summerfi-stack',
Sdk = 'sdk',
}
const availableApps: string[] = [App.SummerfiStack, App.Sdk]
const getCurrentBranch = async () => {
const { stdout: currentBranch } = await $`git branch --show-current`
return currentBranch.trim()
}
const checkRemoteBranchExists = async (branchName: string) => {
try {
await $`git ls-remote --exit-code --heads origin ${branchName}`
return true
} catch (error) {
return false
}
}
const checkForUncommittedOrUntrackedChanges = async () => {
const { stdout: status } = await $`git status --porcelain`
const statusList = status.trim().split('\n').filter(Boolean)
return {
list: statusList,
count: statusList.length,
}
}
const runInstallAndChecks = async () => {
try {
await $`pnpm install`
await $`pnpm run cicheck`
} catch (error) {
echo(`cicheck failed: ${error.message}`)
return false
}
return true
}
const getCommitsToFetch = async (currentBranch: string): Promise<number | null> => {
if (!(await checkRemoteBranchExists(currentBranch))) {
echo('Remote branch does not exist')
return null
}
await $`git fetch origin ${currentBranch}`
const { stdout: commitsToFetch } =
await $`git rev-list ${currentBranch}..origin/${currentBranch} --count`
return Number(commitsToFetch.trim())
}
const checkIfDockerIsRunning = async () => {
try {
await $`docker version > /dev/null 2>&1`
} catch (error) {
return false
}
return true
}
const checkIfDockerComposeIsRunning = async () => {
try {
const { stdout } =
await $`docker compose --file ./stacks/local-env/docker-compose.yaml ps --services --filter "status=running"`
const services = (stdout ?? '').trim().split('\n').filter(Boolean)
return services.length === 3 // 3 services are expected to be running - rays-db, redis-cache, oasis-borrow-db
} catch (error) {
return false
}
}
const runDockerCompose = async () => {
try {
await $`docker compose --file ./stacks/local-env/docker-compose.yaml up --build -d`
} catch (error) {
echo(`${chalk.bgRed('Failed to start docker-compose services' + error.message)}`)
return false
}
return true
}
export const sstConfig: SSTConfig = {
async config(_input) {
// read app from cli input
const { app } = _input as { app?: string }
const currentBranch = await getCurrentBranch()
const commitsToFetch = await getCommitsToFetch(currentBranch)
if (commitsToFetch === null) {
echo(
`You are on the ${currentBranch} branch and there is no remote branch to fetch commits from`,
)
} else {
echo(
`You are on the ${currentBranch} branch and there are ${commitsToFetch} commits to fetch from origin`,
)
}
if (
_input.stage === 'staging' ||
_input.stage === 'production' ||
_input.stage === 'armada-prod'
) {
if (_input.stage === 'production') {
if (currentBranch !== 'main' && currentBranch !== 'dev') {
throw new Error('You can only deploy to production from main or dev branch')
}
}
if (commitsToFetch === null) {
throw new Error('Cannot find remote branch to fetch commits from')
}
if (commitsToFetch > 0) {
throw new Error(
`You have ${commitsToFetch} commits to fetch from origin. Please fetch them before deploying to staging or production`,
)
}
const result = await runInstallAndChecks()
if (!result) {
throw new Error('Cannot deploy with failing checks')
}
const changes = await checkForUncommittedOrUntrackedChanges()
if (changes.count > 0) {
throw new Error(
`Cannot deploy with uncommitted or untracked changes. Current changes: ${changes.list.join(', ')}`,
)
}
}
if (_input.stage === undefined && process.env.SST_USER === undefined) {
throw new Error('Please specify stage or set SST_USER env variable')
}
if (_input.stage && !availableStage.includes(_input.stage)) {
throw new Error('Invalid stage, use one of: ' + availableStage.join(', '))
}
const stage = _input.stage ?? `dev-${process.env.SST_USER}`
if (stage.startsWith('dev-')) {
const isDockerRunning = await checkIfDockerIsRunning()
if (!isDockerRunning) {
echo`${chalk.yellow('Docker is not running. ')}`
echo`${chalk.yellow('Docker is required for some resources to work properly.')}`
} else {
echo`${chalk.green('Docker is running. ')}`
const isDockerComposeRunning = await checkIfDockerComposeIsRunning()
if (!isDockerComposeRunning) {
echo`${chalk.bgBlue('Local docker-compose is not running. Trying to start the services... ')}`
await runDockerCompose()
} else {
echo`${chalk.green('Docker Compose is running. ')}`
}
}
}
if (!app || availableApps.includes(app) === false) {
throw new Error('Invalid --app argument, use one of: ' + availableApps.join(', '))
}
return {
name: app,
region: `${process.env.AWS_REGION}`,
profile: `${process.env.AWS_PROFILE}`,
stage: stage,
}
},
stacks(app) {
if (app.stage !== 'production' && app.stage !== 'staging') {
app.setDefaultRemovalPolicy('destroy')
} else {
app.setDefaultRemovalPolicy('retain')
}
echo`\n`
if (app.name === App.SummerfiStack) {
app.stack(API)
app.stack(ExternalAPI)
}
if (app.name === App.Sdk) {
app.stack(SdkAPI)
}
},
}
export default sstConfig