-
Notifications
You must be signed in to change notification settings - Fork 88
/
kots_app_mutations.ts
163 lines (138 loc) · 6.37 KB
/
kots_app_mutations.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
import _ from "lodash";
import { Context } from "../../context";
import path from "path";
import tmp from "tmp";
import yaml from "js-yaml";
import NodeGit from "nodegit";
import { Stores } from "../../schema/stores";
import { Cluster } from "../../cluster";
import { ReplicatedError } from "../../server/errors";
import {
kotsDecryptString,
} from "../kots_ffi";
import { KotsApp } from "../kots_app"
import * as k8s from "@kubernetes/client-node";
import { Params } from "../../server/params";
import { Repeater } from "../../util/repeater";
import { sendInitialGitCommitsForAppDownstream } from "../gitops";
import { StatusServer } from "../../airgap/status";
import { logger } from "../../server/logger";
// tslint:disable-next-line max-func-body-length cyclomatic-complexity
export function KotsMutations(stores: Stores) {
return {
async updateAppGitOps(root: any, args: any, context: Context): Promise<boolean> {
const { appId, clusterId, gitOpsInput } = args;
const app = await context.getApp(appId);
await stores.kotsAppStore.setDownstreamGitOps(app.id, clusterId, gitOpsInput.uri, gitOpsInput.branch, gitOpsInput.path, gitOpsInput.format, gitOpsInput.action);
return true;
},
async disableAppGitops(root: any, args: any, context: Context): Promise<boolean> {
const { appId, clusterId } = args;
const app = await context.getApp(appId);
const downstreamGitops = await stores.kotsAppStore.getDownstreamGitOps(app.id, clusterId);
if (downstreamGitops.enabled) {
await stores.kotsAppStore.disableDownstreamGitOps(appId, clusterId);
}
return true;
},
async ignorePreflightPermissionErrors(root: any, args: any, context: Context): Promise<boolean> {
const { appSlug, clusterSlug, sequence } = args;
const appId = await stores.kotsAppStore.getIdFromSlug(appSlug);
const clusterId = await stores.clusterStore.getIdFromSlug(clusterSlug);
await stores.kotsAppStore.ignorePreflightPermissionErrors(appId, clusterId, sequence);
return true;
},
async retryPreflights(root: any, args: any, context: Context): Promise<boolean> {
const { appSlug, clusterSlug, sequence } = args;
const appId = await stores.kotsAppStore.getIdFromSlug(appSlug);
const clusterId = await stores.clusterStore.getIdFromSlug(clusterSlug);
await stores.kotsAppStore.retryPreflights(appId, clusterId, sequence);
return true;
},
async testGitOpsConnection(root: any, args: any, context: Context) {
const { appId, clusterId } = args;
const gitOpsCreds = await stores.kotsAppStore.getGitOpsCreds(appId, clusterId);
const localPath = tmp.dirSync().name;
const params = await Params.getParams();
const decryptedPrivateKey = await kotsDecryptString(params.apiEncryptionKey, gitOpsCreds.privKey);
const cloneOptions = {
fetchOpts: {
callbacks: {
certificateCheck: () => { return 0; },
credentials: async (url, username) => {
const creds = await NodeGit.Cred.sshKeyMemoryNew(username, gitOpsCreds.pubKey, decryptedPrivateKey, "")
return creds;
}
}
},
};
try {
await NodeGit.Clone(gitOpsCreds.cloneUri, localPath, cloneOptions);
NodeGit.Repository.openBare(localPath);
// TODO check if we have write access!
await stores.kotsAppStore.setGitOpsError(appId, clusterId, "");
// Send current and pending versions to git
// We need a persistent, durable queue for this to handle the api container
// being rescheduled during this long-running operation
const clusterIDs = await stores.kotsAppStore.listClusterIDsForApp(appId);
if (clusterIDs.length === 0) {
throw new Error("no clusters to transition for application");
}
// NOTE (ethan): this is asyncronous but i didn't write the code
sendInitialGitCommitsForAppDownstream(stores, appId, clusterIDs[0]);
return true;
} catch (err) {
logger.error(err);
const gitOpsError = err.errno ? `Error code ${err.errno}` : "Unknown error connecting to repo";
await stores.kotsAppStore.setGitOpsError(appId, clusterId, gitOpsError);
return false;
}
},
async createKotsDownstream(root: any, args: any, context: Context) {
context.requireSingleTenantSession();
const { appId, clusterId } = args;
const clusters = await stores.clusterStore.listAllUsersClusters();
const cluster = _.find(clusters, (c: Cluster) => {
return c.id === clusterId;
});
if (!cluster) {
throw new ReplicatedError(`Cluster with the ID of ${clusterId} was either not found or you do not have permission to access it.`);
}
await stores.kotsAppStore.createDownstream(appId, cluster.title, clusterId);
return true;
},
async deployKotsVersion(root: any, args: any, context: Context) {
const { upstreamSlug, sequence, clusterSlug } = args;
const appId = await stores.kotsAppStore.getIdFromSlug(upstreamSlug);
const app = await context.getApp(appId);
await stores.kotsAppStore.deployVersion(app.id, sequence);
return true;
},
async deleteKotsDownstream(root: any, args: any, context: Context) {
const { slug, clusterId } = args;
const appId = await stores.kotsAppStore.getIdFromSlug(slug);
const app = await context.getApp(appId);
await stores.kotsAppStore.deleteDownstream(app.id, clusterId);
return true;
},
async deleteKotsApp(root: any, args: any, context: Context) {
const { slug } = args;
const appId = await stores.kotsAppStore.getIdFromSlug(slug);
const app = await context.getApp(appId);
await stores.kotsAppStore.deleteApp(app.id);
return true;
},
async updateKotsApp(root: any, args: any, context: Context): Promise<Boolean> {
const app = await context.getApp(args.appId);
await stores.kotsAppStore.updateApp(app.id, args.appName, args.iconUri);
return true;
},
async updateDownstreamsStatus(root: any, args: any, context: Context): Promise<Boolean> {
const { slug, sequence, status } = args;
const appId = await stores.kotsAppStore.getIdFromSlug(slug);
const app = await context.getApp(appId);
await stores.kotsAppStore.updateDownstreamsStatus(app.id, sequence, status, "");
return true;
},
}
}