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 200 201 202 203 204 205 206
| import { createTemplateAction } from '@backstage/plugin-scaffolder-node'; import myCache from '../../../../packages/global/node-cache';
export function createExampleAction() { return createTemplateAction<{ name: string; repoUrl: string; GitLab_Username: string; GitLab_Personal_Access_Token: string; Kubernetes_Env: string; }>({ id: 'acme:argocd:create', description: 'create a app in argocd', schema: { input: { type: 'object', required: ['name', 'repoUrl'], properties: { name: { title: 'An example parameter', description: "This is an example parameter, don't set it to foo", type: 'string', }, repoUrl: { title: 'A target project url', describe: 'a gitlab repoUrl', type: 'string', }, GitLab_Username: { title: 'gitlab username', describe: 'your gitlab username', type: 'string', }, GitLab_Personal_Access_Token: { title: 'GitLab_Personal_Access_Token', describe: 'your GitLab_Personal_Access_Token', type: 'string', }, Kubernetes_Env: { title: 'KUBE NAMESPACE', describe: 'KUBE NAMESPACE', type: 'string', }, }, }, }, async handler(ctx) { ctx.logger.info( `Running example template with parameters: ${ctx.input.name}`, );
const userInfo = await ctx.getInitiatorCredentials(); const userEntityRef = userInfo.principal.userEntityRef; const name = userEntityRef.split('/').pop();
const url = new URL('https://' + ctx.input.repoUrl); const params = new URLSearchParams(url.search); const owner = params.get('owner')?.toLocaleLowerCase(); const repo = params.get('repo');
const projectUrl = `https://gitlabcode.xxxxx.com.cn/${owner}/${repo}.git`; const ARGOCD_BASE_URL = 'https://argocd.xxxxx.com.cn'; const tenantId: string = 'xxxxxxxxxxx'; const client_id = 'xxxxxxxxxxx'; const client_secret = 'xxxxxxxxxxxx'; const MICROSOFT_LOGIN_BASE_URL = `https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`; const GITLAB_TOKEN = ctx.input.GitLab_Personal_Access_Token; const username = ctx.input.GitLab_Username; const kube_namespace = ctx.input.Kubernetes_Env.toLocaleLowerCase();
let ARGOCD_AUTH_TOKEN = "";
let refresh_Token: string | undefined = myCache.get(`${name}_refreshToken`); let id_token = "";
try { const params = new URLSearchParams({ client_id: client_id, client_secret: client_secret, grant_type: 'refresh_token', redirect_uri: 'https://argocd.xxxxx.com.cn/auth/callback', scope: 'openid profile offline_access' });
if (refresh_Token) { params.append('refresh_token', refresh_Token); }
const response = await fetch(`${MICROSOFT_LOGIN_BASE_URL}`, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, body: params.toString() });
if (!response.ok) { throw new Error(`Fail to connect to Microsoft login site, Status: ${response.status}`); }
const data = await response.json(); id_token = data.id_token; refresh_Token = data.refresh_token; } catch (error) { myCache.set(`${name}_refreshToken`, refresh_Token, 0); ctx.logger.error('Error connect to Microsoft login site:', error); throw error; }
try { const data = { repo: projectUrl, type: "git", username: username, password: GITLAB_TOKEN, }; const response = await fetch(`${ARGOCD_BASE_URL}/api/v1/repositories`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Accept': '*/*', 'Authorization': `Bearer ${id_token}` }, body: JSON.stringify(data) }); if (!response.ok) { throw new Error(`Failed to create repository. Status: ${response.status}.`); } } catch (error) { ctx.logger.error('Error creating repository:', error); throw error; }
try { const data = { metadata: { name: ctx.input.name }, spec: { project: "default", source: { repoURL: projectUrl, targetRevision: "HEAD", path: "argocd-yaml" }, destination: { server: "https://xxxxxxxxxx", namespace: kube_namespace } } }; const response = await fetch(`${ARGOCD_BASE_URL}/api/v1/applications`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Accept': '*/*', 'Authorization': `Bearer ${id_token}` }, body: JSON.stringify(data) }); if (!response.ok) { throw new Error(`Failed to create application. Status: ${response.status}.`); } } catch (error) { ctx.logger.error('Error creating application:', error); throw error; } }, }); }
|