|
1 | 1 | import * as R from 'ramda'; |
2 | 2 | import { sendToLagoonLogs } from '@lagoon/commons/dist/logs/lagoon-logger'; |
3 | | -import { createRemoveTask, seedNamespace } from '@lagoon/commons/dist/tasks'; |
| 3 | +import { |
| 4 | + createRemoveTask, |
| 5 | + createMiscTask, |
| 6 | + seedNamespace |
| 7 | +} from '@lagoon/commons/dist/tasks'; |
4 | 8 | import { ResolverFn } from '../'; |
5 | 9 | import { logger } from '../../loggers/logger'; |
6 | 10 | import { isPatchEmpty, query, knex } from '../../util/db'; |
@@ -745,7 +749,8 @@ export const updateEnvironment: ResolverFn = async ( |
745 | 749 | route: input.patch.route, |
746 | 750 | routes: input.patch.routes, |
747 | 751 | autoIdle: input.patch.autoIdle, |
748 | | - created: input.patch.created |
| 752 | + created: input.patch.created, |
| 753 | + idled: input.patch.idled |
749 | 754 | } |
750 | 755 | }) |
751 | 756 | ); |
@@ -787,7 +792,8 @@ export const updateEnvironment: ResolverFn = async ( |
787 | 792 | route: input.patch.route, |
788 | 793 | routes: input.patch.routes, |
789 | 794 | autoIdle: input.patch.autoIdle, |
790 | | - created: input.patch.created |
| 795 | + created: input.patch.created, |
| 796 | + idled: input.patch.idled |
791 | 797 | }, |
792 | 798 | data: withK8s, |
793 | 799 | ...auditLog, |
@@ -1077,3 +1083,153 @@ export const getServiceContainersByServiceId: ResolverFn = async ( |
1077 | 1083 | ); |
1078 | 1084 | return await rows; |
1079 | 1085 | }; |
| 1086 | + |
| 1087 | +export const environmentIdling = async ( |
| 1088 | + root, |
| 1089 | + input, |
| 1090 | + { sqlClientPool, hasPermission, userActivityLogger } |
| 1091 | +) => { |
| 1092 | + const environment = await Helpers(sqlClientPool).getEnvironmentById(input.id); |
| 1093 | + |
| 1094 | + if (!environment) { |
| 1095 | + throw new Error( |
| 1096 | + 'Invalid environment ID' |
| 1097 | + ); |
| 1098 | + } |
| 1099 | + |
| 1100 | + await hasPermission('environment', 'view', { |
| 1101 | + project: environment.project |
| 1102 | + }); |
| 1103 | + |
| 1104 | + // don't try idle if the environment is already idled or unidled |
| 1105 | + if (environment.idled && input.idle) { |
| 1106 | + throw new Error( |
| 1107 | + `environment is already idled` |
| 1108 | + ); |
| 1109 | + } |
| 1110 | + if (!environment.idled && !input.idle) { |
| 1111 | + throw new Error( |
| 1112 | + `environment is already unidled` |
| 1113 | + ); |
| 1114 | + } |
| 1115 | + |
| 1116 | + const project = await projectHelpers(sqlClientPool).getProjectById( |
| 1117 | + environment.project |
| 1118 | + ); |
| 1119 | + |
| 1120 | + await hasPermission('deployment', 'cancel', { |
| 1121 | + project: project.id |
| 1122 | + }); |
| 1123 | + |
| 1124 | + const data = { |
| 1125 | + environment, |
| 1126 | + project, |
| 1127 | + idling: { |
| 1128 | + idle: input.idle, |
| 1129 | + forceScale: input.disableAutomaticUnidling |
| 1130 | + } |
| 1131 | + }; |
| 1132 | + |
| 1133 | + userActivityLogger(`User requested environment idling for '${environment.name}'`, { |
| 1134 | + project: '', |
| 1135 | + event: 'api:idleEnvironment', |
| 1136 | + payload: { |
| 1137 | + project: project.name, |
| 1138 | + environment: environment.name, |
| 1139 | + idle: input.idle, |
| 1140 | + disableAutomaticUnidling: input.disableAutomaticUnidling, |
| 1141 | + } |
| 1142 | + }); |
| 1143 | + |
| 1144 | + try { |
| 1145 | + await createMiscTask({ key: 'environment:idling', data }); |
| 1146 | + return 'success'; |
| 1147 | + } catch (error) { |
| 1148 | + sendToLagoonLogs( |
| 1149 | + 'error', |
| 1150 | + '', |
| 1151 | + '', |
| 1152 | + 'api:idleEnvironment', |
| 1153 | + { environment: environment.id }, |
| 1154 | + `Environment idle attempt possibly failed, reason: ${error}` |
| 1155 | + ); |
| 1156 | + throw new Error( |
| 1157 | + error.message |
| 1158 | + ); |
| 1159 | + } |
| 1160 | +}; |
| 1161 | + |
| 1162 | +export const environmentService = async ( |
| 1163 | + root, |
| 1164 | + input, |
| 1165 | + { sqlClientPool, hasPermission, userActivityLogger } |
| 1166 | +) => { |
| 1167 | + const environment = await Helpers(sqlClientPool).getEnvironmentById(input.id); |
| 1168 | + |
| 1169 | + if (!environment) { |
| 1170 | + throw new Error( |
| 1171 | + 'Invalid environment ID' |
| 1172 | + ); |
| 1173 | + } |
| 1174 | + |
| 1175 | + await hasPermission('environment', 'view', { |
| 1176 | + project: environment.project |
| 1177 | + }); |
| 1178 | + |
| 1179 | + // don't try idle if the environment is already idled or unidled |
| 1180 | + if (environment.idled && input.idle) { |
| 1181 | + throw new Error( |
| 1182 | + `environment is already idled` |
| 1183 | + ); |
| 1184 | + } |
| 1185 | + if (!environment.idled && !input.idle) { |
| 1186 | + throw new Error( |
| 1187 | + `environment is already unidled` |
| 1188 | + ); |
| 1189 | + } |
| 1190 | + |
| 1191 | + const project = await projectHelpers(sqlClientPool).getProjectById( |
| 1192 | + environment.project |
| 1193 | + ); |
| 1194 | + |
| 1195 | + await hasPermission('deployment', 'cancel', { |
| 1196 | + project: project.id |
| 1197 | + }); |
| 1198 | + |
| 1199 | + const data = { |
| 1200 | + environment, |
| 1201 | + project, |
| 1202 | + lagoonService: { |
| 1203 | + name: input.serviceName, |
| 1204 | + state: input.state |
| 1205 | + } |
| 1206 | + }; |
| 1207 | + |
| 1208 | + userActivityLogger(`User requested environment idling for '${environment.name}'`, { |
| 1209 | + project: '', |
| 1210 | + event: 'api:idleEnvironment', |
| 1211 | + payload: { |
| 1212 | + project: project.name, |
| 1213 | + environment: environment.name, |
| 1214 | + idle: input.idle, |
| 1215 | + disableAutomaticUnidling: input.disableAutomaticUnidling, |
| 1216 | + } |
| 1217 | + }); |
| 1218 | + |
| 1219 | + try { |
| 1220 | + await createMiscTask({ key: 'environment:idling', data }); |
| 1221 | + return 'success'; |
| 1222 | + } catch (error) { |
| 1223 | + sendToLagoonLogs( |
| 1224 | + 'error', |
| 1225 | + '', |
| 1226 | + '', |
| 1227 | + 'api:idleEnvironment', |
| 1228 | + { environment: environment.id }, |
| 1229 | + `Environment idle attempt possibly failed, reason: ${error}` |
| 1230 | + ); |
| 1231 | + throw new Error( |
| 1232 | + error.message |
| 1233 | + ); |
| 1234 | + } |
| 1235 | +}; |
0 commit comments