This repository has been archived by the owner on Apr 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfileutils.ts
978 lines (903 loc) · 33.3 KB
/
fileutils.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
import fs from "fs";
import yaml from "js-yaml";
import path from "path";
import { readYaml, write } from "../config";
import {
ACCESS_FILENAME,
BEDROCK_FILENAME,
HELM_VERSION,
HLD_COMPONENT_FILENAME,
PROJECT_PIPELINE_FILENAME,
RENDER_HLD_PIPELINE_FILENAME,
SERVICE_PIPELINE_FILENAME,
VERSION_MESSAGE,
VM_IMAGE,
} from "../lib/constants";
import { build as buildError } from "../lib/errorBuilder";
import { errorStatusCode } from "../lib/errorStatusCode";
import { logger } from "../logger";
import {
AccessYaml,
AzurePipelinesYaml,
ComponentYaml,
MaintainersFile,
User,
} from "../types";
/**
* Read given pipeline file as json object.
*
* @param dir path
* @param pipelineFileName pipeline definition filename. Should be a value from "../lib/constants"
*/
const readPipelineFile = (
dir: string,
pipelineFileName: string
): AzurePipelinesYaml => {
const absPath = path.resolve(dir);
const file = path.join(absPath, pipelineFileName);
return yaml.safeLoad(fs.readFileSync(file, "utf8"));
};
/**
* Create an access.yaml file for fabrikate authorization.
* Should only be used by spk hld reconcile, which is an idempotent operation, but will not overwrite existing access.yaml keys
* @param accessYamlPath
* @param gitRepoUrl
* @param accessTokenEnvVar the environment variable to which will contain the PAT
*/
export const generateAccessYaml = (
accessYamlPath: string,
gitRepoUrl: string,
accessTokenEnvVar = "ACCESS_TOKEN_SECRET"
): void => {
const filePath = path.resolve(path.join(accessYamlPath, ACCESS_FILENAME));
let accessYaml: AccessYaml | undefined;
if (fs.existsSync(filePath)) {
logger.info(
`Existing ${ACCESS_FILENAME} found at ${filePath}, loading and updating, if needed.`
);
accessYaml = yaml.load(fs.readFileSync(filePath, "utf8")) as AccessYaml;
accessYaml = {
[gitRepoUrl]: accessTokenEnvVar,
...accessYaml, // Keep any existing configurations. Do not overwrite what's in `gitRepoUrl`.
};
} else {
accessYaml = {
[gitRepoUrl]: accessTokenEnvVar,
};
}
// Always overwrite what exists.
fs.writeFileSync(
filePath,
yaml.safeDump(accessYaml, { lineWidth: Number.MAX_SAFE_INTEGER }),
"utf8"
);
};
/**
* Outputs a bash string for a _safe_ source branch string -- a string where all
* '/', '.', and '_' in the string have been replaced with a '-'`
*/
export const SAFE_SOURCE_BRANCH = `$(echo $(Build.SourceBranchName) | tr / - | tr . - | tr _ - )`;
/**
* Outputs a bash script to generate a _safe_ azure container registry url where it's all lowercase.
* This will require ACR_NAME as an environment variable.
*/
export const IMAGE_REPO = `$(echo $(ACR_NAME).azurecr.io | tr '[:upper:]' '[:lower:]')`;
/**
* Outputs a bash string for a _safe_ image tag -- a string where all
* '/' and '.' in the string have been replaced with a '-'`
*/
export const IMAGE_TAG = `${SAFE_SOURCE_BRANCH}-$(Build.BuildNumber)`;
/**
* Outputs a bash string of `<repository>-<service-name>` in lowercase
*
* @param serviceName name of the service being built
*/
export const BUILD_REPO_NAME = (serviceName: string): string =>
`$(echo $(Build.Repository.Name)-${serviceName} | tr '[:upper:]' '[:lower:]')`;
/**
* Concatenates all lines into a single string and injects `set -e` to the top
* of it
*
* @param lines lines of script to execute
*/
export const generateYamlScript = (lines: string[]): string =>
["set -e", ...lines].join("\n");
/**
* Sanitize the given path to format Azure DevOps can properly utilize
*
* Transforms:
* - If present, removes leading dot-slash (`./`) prefix from the path
*
* @param pathLike a path-like string to sanitize
*/
export const sanitizeTriggerPath = (pathLike: string): string => {
return pathLike.replace(/^\.\//, "");
};
/**
* Returns a build-update-hld-pipeline.yaml string
* based on: https://github.com/andrebriggs/monorepo-example/blob/master/service-A/azure-pipelines.yml
*
* @param serviceName
* @param relServicePath
* @param ringBranches
* @param variableGroups
*/
export const serviceBuildAndUpdatePipeline = (
serviceName: string,
relServicePath: string,
ringBranches: string[],
variableGroups?: string[]
): AzurePipelinesYaml => {
const relativeServicePathFormatted = sanitizeTriggerPath(relServicePath);
const relativeServiceForDockerfile = relServicePath.startsWith("./")
? relServicePath
: "./" + relServicePath;
const pipelineYaml: AzurePipelinesYaml = {
trigger: {
branches: { include: [...new Set(ringBranches)] },
...(relativeServicePathFormatted === ""
? {}
: {
paths: {
include: [relativeServicePathFormatted],
exclude: [BEDROCK_FILENAME],
},
}),
},
variables: [...(variableGroups ?? []).map((group) => ({ group }))],
stages: [
{
// Build stage
stage: "build",
jobs: [
{
job: "run_build_push_acr",
pool: {
vmImage: VM_IMAGE,
},
steps: [
{
task: "HelmInstaller@1",
inputs: {
helmVersionToInstall: HELM_VERSION,
},
},
{
script: generateYamlScript([
`echo "az login --service-principal --username $(SP_APP_ID) --password $(SP_PASS) --tenant $(SP_TENANT)"`,
`az login --service-principal --username "$(SP_APP_ID)" --password "$(SP_PASS)" --tenant "$(SP_TENANT)"`,
]),
displayName: "Azure Login",
},
{
script: generateYamlScript([
`# Download build.sh`,
`curl $BEDROCK_BUILD_SCRIPT > build.sh`,
`chmod +x ./build.sh`,
]),
displayName: "Download bedrock bash scripts",
env: {
BEDROCK_BUILD_SCRIPT: "$(BUILD_SCRIPT_URL)",
},
},
{
script: generateYamlScript([
`. ./build.sh --source-only`,
`get_spk_version`,
`download_spk`,
`export BUILD_REPO_NAME=${BUILD_REPO_NAME(serviceName)}`,
`tag_name="$BUILD_REPO_NAME:${IMAGE_TAG}"`,
`commitId=$(Build.SourceVersion)`,
`commitId=$(echo "\${commitId:0:7}")`,
`service=$(./spk/spk service get-display-name -p ${relativeServiceForDockerfile})`,
`url=$(git remote --verbose | grep origin | grep fetch | cut -f2 | cut -d' ' -f1)`,
`repourl=\${url##*@}`,
`./spk/spk deployment create -n $(INTROSPECTION_ACCOUNT_NAME) -k $(INTROSPECTION_ACCOUNT_KEY) -t $(INTROSPECTION_TABLE_NAME) -p $(INTROSPECTION_PARTITION_KEY) --p1 $(Build.BuildId) --image-tag $tag_name --commit-id $commitId --service $service --repository $repourl`,
]),
displayName:
"If configured, update Spektate storage with build pipeline",
condition:
"and(ne(variables['INTROSPECTION_ACCOUNT_NAME'], ''), ne(variables['INTROSPECTION_ACCOUNT_KEY'], ''),ne(variables['INTROSPECTION_TABLE_NAME'], ''),ne(variables['INTROSPECTION_PARTITION_KEY'], ''))",
},
{
script: generateYamlScript([
`export BUILD_REPO_NAME=${BUILD_REPO_NAME(serviceName)}`,
`export IMAGE_TAG=${IMAGE_TAG}`,
`export IMAGE_NAME=$BUILD_REPO_NAME:$IMAGE_TAG`,
`echo "Image Name: $IMAGE_NAME"`,
`cd ${relativeServiceForDockerfile}`,
`echo "az acr build -r $(ACR_NAME) --image $IMAGE_NAME ."`,
`az acr build -r $(ACR_NAME) --image $IMAGE_NAME .`,
]),
displayName: "ACR Build and Publish",
},
],
},
],
},
{
// Update HLD Stage
stage: "hld_update",
dependsOn: "build",
condition: "succeeded('build')",
jobs: [
{
job: "update_image_tag",
pool: {
vmImage: VM_IMAGE,
},
steps: [
{
task: "HelmInstaller@1",
inputs: {
helmVersionToInstall: HELM_VERSION,
},
},
{
script: generateYamlScript([
`# Download build.sh`,
`curl $BEDROCK_BUILD_SCRIPT > build.sh`,
`chmod +x ./build.sh`,
]),
displayName: "Download bedrock bash scripts",
env: {
BEDROCK_BUILD_SCRIPT: "$(BUILD_SCRIPT_URL)",
},
},
{
script: generateYamlScript([
`export SERVICE_NAME_LOWER=$(echo ${serviceName} | tr '[:upper:]' '[:lower:]')`,
`export BUILD_REPO_NAME=${BUILD_REPO_NAME(serviceName)}`,
`export BRANCH_NAME=DEPLOY/$BUILD_REPO_NAME-${IMAGE_TAG}`,
`export FAB_SAFE_SERVICE_NAME=$(echo $SERVICE_NAME_LOWER | tr . - | tr / -)`,
`# --- From https://raw.githubusercontent.com/Microsoft/bedrock/master/gitops/azure-devops/release.sh`,
`. build.sh --source-only`,
``,
`# Initialization`,
`verify_access_token`,
`init`,
`helm_init`,
``,
`# Fabrikate`,
`get_fab_version`,
`download_fab`,
``,
`# Clone HLD repo`,
`git_connect`,
`# --- End Script`,
``,
`# Update HLD`,
`git checkout -b "$BRANCH_NAME"`,
`export BUILD_REPO_NAME=${BUILD_REPO_NAME(serviceName)}`,
`export IMAGE_TAG=${IMAGE_TAG}`,
`export IMAGE_NAME=$BUILD_REPO_NAME:$IMAGE_TAG`,
`echo "Image Name: $IMAGE_NAME"`,
`export IMAGE_REPO=${IMAGE_REPO}`,
`echo "Image Repository: $IMAGE_REPO"`,
`cd $(Build.Repository.Name)/$FAB_SAFE_SERVICE_NAME/${SAFE_SOURCE_BRANCH}`,
`echo "FAB SET"`,
`fab set --subcomponent chart image.tag=$IMAGE_TAG image.repository=$IMAGE_REPO/$BUILD_REPO_NAME`,
``,
`# Set git identity`,
`git config user.email "admin@azuredevops.com"`,
`git config user.name "Automated Account"`,
``,
`# Commit changes`,
`echo "GIT ADD and COMMIT -- Will throw error if there is nothing to commit."`,
`git_commit_if_changes "Updating $SERVICE_NAME_LOWER image tag to ${IMAGE_TAG}." 1 unusedVar`,
``,
`# Git Push`,
`git_push`,
``,
`# Open PR via az repo cli`,
`echo 'az extension add --name azure-devops'`,
`az extension add --name azure-devops`,
``,
`echo 'az repos pr create --description "Updating $SERVICE_NAME_LOWER to ${IMAGE_TAG}." "PR created by: $(Build.DefinitionName) with buildId: $(Build.BuildId) and buildNumber: $(Build.BuildNumber)"'`,
`response=$(az repos pr create --description "Updating $SERVICE_NAME_LOWER to ${IMAGE_TAG}." "PR created by: $(Build.DefinitionName) with buildId: $(Build.BuildId) and buildNumber: $(Build.BuildNumber)")`,
`pr_id=$(echo $response | jq -r '.pullRequestId')`,
``,
`# Update introspection storage with this information, if applicable`,
`if [ -z "$(INTROSPECTION_ACCOUNT_NAME)" -o -z "$(INTROSPECTION_ACCOUNT_KEY)" -o -z "$(INTROSPECTION_TABLE_NAME)" -o -z "$(INTROSPECTION_PARTITION_KEY)" ]; then`,
`echo "Introspection variables are not defined. Skipping..."`,
`else`,
`latest_commit=$(git rev-parse --short HEAD)`,
`tag_name="$BUILD_REPO_NAME:$(Build.SourceBranchName)-$(Build.BuildNumber)"`,
`url=$(git remote --verbose | grep origin | grep fetch | cut -f2 | cut -d' ' -f1)`,
`repourl=\${url##*@}`,
`get_spk_version`,
`download_spk`,
`./spk/spk deployment create -n $(INTROSPECTION_ACCOUNT_NAME) -k $(INTROSPECTION_ACCOUNT_KEY) -t $(INTROSPECTION_TABLE_NAME) -p $(INTROSPECTION_PARTITION_KEY) --p2 $(Build.BuildId) --hld-commit-id $latest_commit --env $(Build.SourceBranchName) --image-tag $tag_name --pr $pr_id --repository $repourl`,
`fi`,
]),
displayName:
"Download Fabrikate, Update HLD, Push changes, Open PR, and if configured, push to Spektate storage",
env: {
ACCESS_TOKEN_SECRET: "$(PAT)",
AZURE_DEVOPS_EXT_PAT: "$(PAT)",
REPO: "$(HLD_REPO)",
},
},
],
},
],
},
],
};
const requiredPipelineVariables = [
`'ACR_NAME' (name of your ACR)`,
`'HLD_REPO' (Repository for your HLD in AzDo. eg. 'dev.azure.com/bhnook/fabrikam/_git/hld')`,
`'PAT' (AzDo Personal Access Token with permissions to the HLD repository.)`,
`'SP_APP_ID' (service principal ID with access to your ACR)`,
`'SP_PASS' (service principal secret)`,
`'SP_TENANT' (service principal tenant)`,
].join(", ");
const spkServiceBuildPipelineCmd =
"spk service install-build-pipeline " + serviceName;
logger.info(
`Generated ${SERVICE_PIPELINE_FILENAME} for service in path '${relativeServicePathFormatted}'. Commit and push this file to master before attempting to deploy via the command '${spkServiceBuildPipelineCmd}'; before running the pipeline ensure the following environment variables are available to your project variable groups: ${requiredPipelineVariables}`
);
return pipelineYaml;
};
/**
* Gets the spk version
*/
export const getVersion = (): string => {
return require("../../package.json").version;
};
/**
* Gets the spk version message
*/
export const getVersionMessage = (): string => {
return VERSION_MESSAGE + getVersion();
};
/**
* Writes the spk version to the given file
* @param filePath The path to the file
*/
export const writeVersion = (filePath: string): void => {
fs.writeFileSync(filePath, `${getVersionMessage()}\n`, "utf8");
};
/**
* Creates the service multistage build and update image tag pipeline.
* One pipeline should exist for each service.
*
* @param projectRoot Full path to the root of the project (where the bedrock.yaml file exists)
* @param ringBranches Branches to trigger builds off of. Should be all the defined rings for this service.
* @param serviceName
* @param servicePath Full path to service directory
* @param variableGroups Azure DevOps variable group names
*/
export const generateServiceBuildAndUpdatePipelineYaml = (
projectRoot: string,
ringBranches: string[],
serviceName: string,
servicePath: string,
variableGroups: string[]
): void => {
const absProjectRoot = path.resolve(projectRoot);
const absServicePath = path.resolve(servicePath);
logger.info(`Generating ${SERVICE_PIPELINE_FILENAME} in ${absServicePath}`);
logger.debug(`variableGroups length: ${variableGroups?.length}`);
// Check if build-update-hld-pipeline.yaml already exists; if it does, skip generation
const pipelineYamlFullPath = path.join(
absServicePath,
SERVICE_PIPELINE_FILENAME
);
logger.debug(
`Writing ${SERVICE_PIPELINE_FILENAME} file to ${pipelineYamlFullPath}`
);
if (fs.existsSync(pipelineYamlFullPath)) {
logger.warn(
`Existing ${SERVICE_PIPELINE_FILENAME} found at ${pipelineYamlFullPath}, skipping generation.`
);
return;
}
const buildYaml = serviceBuildAndUpdatePipeline(
serviceName,
path.relative(absProjectRoot, absServicePath),
ringBranches,
variableGroups
);
writeVersion(pipelineYamlFullPath);
fs.appendFileSync(
pipelineYamlFullPath,
yaml.safeDump(buildYaml, { lineWidth: Number.MAX_SAFE_INTEGER }),
"utf8"
);
};
/**
* Updates the service build and update pipeline with the given rings list
*
* @param ringBranches Branches to trigger builds off of. Should be all the defined rings for this project.
* @param servicePath Full path to service directory
*/
export const updateTriggerBranchesForServiceBuildAndUpdatePipeline = (
ringBranches: string[],
servicePath: string
): void => {
const absServicePath = path.resolve(servicePath);
const pipelineYamlFullPath = path.join(
absServicePath,
SERVICE_PIPELINE_FILENAME
);
// Check if build-update-hld-pipeline.yaml already exists; if it doesn't, throw error.
if (!fs.existsSync(pipelineYamlFullPath)) {
throw buildError(errorStatusCode.FILE_IO_ERR, {
errorKey: "fileutils-update-ring-trigger-svc-file-not-found",
values: [SERVICE_PIPELINE_FILENAME, pipelineYamlFullPath],
});
}
logger.info(
`Updating ${pipelineYamlFullPath} file with trigger rings: ${ringBranches}.`
);
const buildPipelineYaml: AzurePipelinesYaml = readPipelineFile(
servicePath,
SERVICE_PIPELINE_FILENAME
);
if (buildPipelineYaml.trigger && buildPipelineYaml.trigger.branches) {
buildPipelineYaml.trigger.branches.include = ringBranches;
}
writeVersion(pipelineYamlFullPath);
fs.appendFileSync(
pipelineYamlFullPath,
yaml.safeDump(buildPipelineYaml, { lineWidth: Number.MAX_SAFE_INTEGER }),
"utf8"
);
};
/**
* Appends a variable group an Azure pipeline yaml
* @param dir The directory where the pipeline yaml file is
* @param pipelineFile The name of the pipeline yaml file
* @param variableGroupName The name of the variable group to be added
*/
export const appendVariableGroupToPipelineYaml = (
dir: string,
fileName: string,
variableGroupName: string
): void => {
try {
const pipelineFile = readYaml(
path.join(dir, fileName)
) as AzurePipelinesYaml;
pipelineFile.variables = pipelineFile.variables || [];
let variableGroupExists = false;
pipelineFile.variables.forEach((variable) => {
if ("group" in variable && variable.group === variableGroupName) {
variableGroupExists = true;
logger.info(
`Variable group '${variableGroupName}' already exits in '${dir}/${fileName}'.`
);
}
});
if (!variableGroupExists) {
pipelineFile.variables.push({ group: variableGroupName });
logger.info(`Updating '${dir}/${fileName}'.`);
write(pipelineFile, dir, fileName);
}
} catch (err) {
throw buildError(
errorStatusCode.FILE_IO_ERR,
"fileutils-append-variable-group-to-pipeline-yaml",
err
);
}
};
/**
* Returns a the Manifest Generation Pipeline as defined here: https://github.com/microsoft/bedrock/blob/master/gitops/azure-devops/ManifestGeneration.md#add-azure-pipelines-build-yaml
*/
const manifestGenerationPipelineYaml = (): string => {
// based on https://github.com/microsoft/bedrock/blob/master/gitops/azure-devops/ManifestGeneration.md#add-azure-pipelines-build-yaml
const pipelineYaml: AzurePipelinesYaml = {
trigger: {
branches: {
include: ["master"],
},
},
variables: [],
pool: {
vmImage: VM_IMAGE,
},
steps: [
{
checkout: "self",
persistCredentials: true,
clean: true,
},
{
task: "HelmInstaller@1",
inputs: {
helmVersionToInstall: HELM_VERSION,
},
},
{
script: generateYamlScript([
`# Download build.sh`,
`curl $BEDROCK_BUILD_SCRIPT > build.sh`,
`chmod +x ./build.sh`,
]),
displayName: "Download bedrock bash scripts",
env: {
BEDROCK_BUILD_SCRIPT: "$(BUILD_SCRIPT_URL)",
},
},
{
script: generateYamlScript([
`commitId=$(Build.SourceVersion)`,
`commitId=$(echo "\${commitId:0:7}")`,
`. ./build.sh --source-only`,
`get_spk_version`,
`download_spk`,
`message="$(Build.SourceVersionMessage)"`,
`if [[ $message == *"Merge"* ]]; then`,
`pr_id=$(echo $message | grep -oE '[0-9]+' | head -1 | sed -e 's/^0\\+//')`,
`./spk/spk deployment create -n $(INTROSPECTION_ACCOUNT_NAME) -k $(INTROSPECTION_ACCOUNT_KEY) -t $(INTROSPECTION_TABLE_NAME) -p $(INTROSPECTION_PARTITION_KEY) --p3 $(Build.BuildId) --hld-commit-id $commitId --pr $pr_id`,
`else`,
`./spk/spk deployment create -n $(INTROSPECTION_ACCOUNT_NAME) -k $(INTROSPECTION_ACCOUNT_KEY) -t $(INTROSPECTION_TABLE_NAME) -p $(INTROSPECTION_PARTITION_KEY) --p3 $(Build.BuildId) --hld-commit-id $commitId`,
`fi`,
]),
displayName:
"If configured, update manifest pipeline details in Spektate db before manifest generation",
condition:
"and(ne(variables['INTROSPECTION_ACCOUNT_NAME'], ''), ne(variables['INTROSPECTION_ACCOUNT_KEY'], ''),ne(variables['INTROSPECTION_TABLE_NAME'], ''),ne(variables['INTROSPECTION_PARTITION_KEY'], ''), ne(variables['Build.Reason'], 'PullRequest'))",
},
{
task: "ShellScript@2",
displayName: "Validate fabrikate definitions",
inputs: {
scriptPath: "build.sh",
},
condition: `eq(variables['Build.Reason'], 'PullRequest')`,
env: {
VERIFY_ONLY: 1,
},
},
{
task: "ShellScript@2",
displayName:
"Transform fabrikate definitions and publish to YAML manifests to repo",
inputs: {
scriptPath: "build.sh",
},
condition: `ne(variables['Build.Reason'], 'PullRequest')`,
env: {
ACCESS_TOKEN_SECRET: "$(PAT)",
COMMIT_MESSAGE: "$(Build.SourceVersionMessage)",
REPO: "$(MANIFEST_REPO)",
BRANCH_NAME: "$(Build.SourceBranchName)",
},
},
{
script: generateYamlScript([
`. ./build.sh --source-only`,
`cd "$HOME"/\${MANIFEST_REPO##*/}`,
`latest_commit=$(git rev-parse --short HEAD)`,
`url=$(git remote --verbose | grep origin | grep fetch | cut -f2 | cut -d' ' -f1)`,
`repourl=\${url##*@}`,
`get_spk_version`,
`download_spk`,
`./spk/spk deployment create -n $(INTROSPECTION_ACCOUNT_NAME) -k $(INTROSPECTION_ACCOUNT_KEY) -t $(INTROSPECTION_TABLE_NAME) -p $(INTROSPECTION_PARTITION_KEY) --p3 $(Build.BuildId) --manifest-commit-id $latest_commit --repository $repourl`,
]),
displayName:
"If configured, update manifest pipeline details in Spektate db after manifest generation",
condition:
"and(ne(variables['INTROSPECTION_ACCOUNT_NAME'], ''), ne(variables['INTROSPECTION_ACCOUNT_KEY'], ''),ne(variables['INTROSPECTION_TABLE_NAME'], ''),ne(variables['INTROSPECTION_PARTITION_KEY'], ''), ne(variables['Build.Reason'], 'PullRequest'))",
},
],
};
return yaml.safeDump(pipelineYaml, { lineWidth: Number.MAX_SAFE_INTEGER });
};
/**
* Writes out the hld manifest-generation.yaml file to `targetPath`
*
* @param hldRepoDirectory Path to write the manifest-generation.yaml file to
*/
export const generateHldAzurePipelinesYaml = (
targetDirectory: string
): void => {
try {
const absTargetPath = path.resolve(targetDirectory);
logger.info(`Generating hld manifest-generation in ${absTargetPath}`);
const azurePipelinesYamlPath = path.join(
absTargetPath,
RENDER_HLD_PIPELINE_FILENAME
);
if (fs.existsSync(azurePipelinesYamlPath)) {
logger.warn(
`Existing ${RENDER_HLD_PIPELINE_FILENAME} found at ${azurePipelinesYamlPath}, skipping generation.`
);
return;
}
const hldYaml = manifestGenerationPipelineYaml();
logger.info(
`Writing ${RENDER_HLD_PIPELINE_FILENAME} file to ${azurePipelinesYamlPath}`
);
const requiredPipelineVariables = [
`'MANIFEST_REPO' (Repository for your kubernetes manifests in AzDo. eg. 'dev.azure.com/bhnook/fabrikam/_git/materialized')`,
`'PAT' (AzDo Personal Access Token with permissions to the HLD repository.)`,
].join(", ");
logger.info(
`Generated ${RENDER_HLD_PIPELINE_FILENAME}. Commit and push this file to master before attempting to deploy via the command 'spk hld install-manifest-pipeline'; before running the pipeline ensure the following environment variables are available to your pipeline: ${requiredPipelineVariables}`
);
writeVersion(azurePipelinesYamlPath);
fs.appendFileSync(azurePipelinesYamlPath, hldYaml, "utf8");
} catch (err) {
throw buildError(
errorStatusCode.FILE_IO_ERR,
"fileutils-generate-hld-pipeline-yaml",
err
);
}
};
/**
* Populate the hld's default component.yaml
*/
const defaultComponentYaml = (
componentGit: string,
componentName: string,
componentPath: string
): ComponentYaml => {
const componentYaml: ComponentYaml = {
name: "default-component",
subcomponents: [
{
name: componentName,
method: "git",
source: componentGit,
path: componentPath,
},
],
};
return componentYaml;
};
/**
* Add a default component.yaml when running `hld init`.
*/
export const generateDefaultHldComponentYaml = (
targetDirectory: string,
componentGit: string,
componentName: string,
componentPath: string
): void => {
try {
const absTargetPath = path.resolve(targetDirectory);
logger.info(`Generating component.yaml in ${absTargetPath}`);
const fabrikateComponentPath = path.join(absTargetPath, "component.yaml");
if (fs.existsSync(fabrikateComponentPath)) {
logger.warn(
`Existing component.yaml found at ${fabrikateComponentPath}, skipping generation.`
);
return;
}
const componentYaml = defaultComponentYaml(
componentGit,
componentName,
componentPath
);
logger.info(
`Writing ${HLD_COMPONENT_FILENAME} file to ${fabrikateComponentPath}`
);
fs.writeFileSync(
fabrikateComponentPath,
yaml.safeDump(componentYaml, { lineWidth: Number.MAX_SAFE_INTEGER }),
"utf8"
);
} catch (err) {
throw buildError(
errorStatusCode.FILE_IO_ERR,
"fileutils-generate-default-hld-component-yaml",
err
);
}
};
const hldLifecyclePipelineYaml = (): string => {
const pipelineyaml: AzurePipelinesYaml = {
trigger: {
branches: {
include: ["master"],
},
paths: {
include: ["bedrock.yaml"],
},
},
variables: [],
pool: {
vmImage: VM_IMAGE,
},
steps: [
{
task: "HelmInstaller@1",
inputs: {
helmVersionToInstall: HELM_VERSION,
},
},
{
script: generateYamlScript([
`# Download build.sh`,
`curl $BEDROCK_BUILD_SCRIPT > build.sh`,
`chmod +x ./build.sh`,
]),
displayName: "Download bedrock bash scripts",
env: {
BEDROCK_BUILD_SCRIPT: "$(BUILD_SCRIPT_URL)",
},
},
{
script: generateYamlScript([
`# From https://raw.githubusercontent.com/Microsoft/bedrock/master/gitops/azure-devops/release.sh`,
`. build.sh --source-only`,
``,
`# Initialization`,
`verify_access_token`,
`init`,
`helm_init`,
``,
`# Fabrikate`,
`get_fab_version`,
`download_fab`,
``,
`# SPK`,
`get_spk_version`,
`download_spk`,
``,
`# Clone HLD repo`,
`git_connect`,
``,
`# Update HLD via spk`,
`git checkout -b "RECONCILE/$(Build.Repository.Name)-$(Build.BuildNumber)"`,
`echo "spk hld reconcile $(Build.Repository.Name) $PWD ./.."`,
`spk hld reconcile $(Build.Repository.Name) $PWD ./..`,
``,
`# Set git identity`,
`git config user.email "admin@azuredevops.com"`,
`git config user.name "Automated Account"`,
``,
`# Commit changes`,
`echo "GIT ADD and COMMIT -- Will NOT throw error if there is nothing to commit."`,
`didCommit=0`,
`git_commit_if_changes "Reconciling HLD with $(Build.Repository.Name)-$(Build.BuildNumber)." 0 didCommit`,
``,
`# Skip push and opening PR steps if there were no changes changes to commit.`,
`if [ $didCommit == 0 ]; then`,
`echo "DID NOT FIND CHANGES TO COMMIT. EXITING."`,
`exit 0`,
`fi`,
``,
`# Git Push`,
`git_push`,
``,
`# Open PR via az repo cli`,
`echo 'az extension add --name azure-devops'`,
`az extension add --name azure-devops`,
``,
`echo 'az repos pr create --description "Reconciling HLD with $(Build.Repository.Name)-$(Build.BuildNumber)." "PR created by: $(Build.DefinitionName) with buildId: $(Build.BuildId) and buildNumber: $(Build.BuildNumber)"'`,
`az repos pr create --description "Reconciling HLD with $(Build.Repository.Name)-$(Build.BuildNumber)." "PR created by: $(Build.DefinitionName) with buildId: $(Build.BuildId) and buildNumber: $(Build.BuildNumber)"`,
]),
displayName:
"Download Fabrikate and SPK, Update HLD, Push changes, Open PR",
env: {
ACCESS_TOKEN_SECRET: "$(PAT)",
APP_REPO_URL: "$(Build.Repository.Uri)",
AZURE_DEVOPS_EXT_PAT: "$(PAT)",
REPO: "$(HLD_REPO)",
},
},
],
};
return yaml.safeDump(pipelineyaml, { lineWidth: Number.MAX_SAFE_INTEGER });
};
/**
* Writes out the service to hld lifecycle pipeline.
* This pipeline utilizes spk hld reconcile to add/remove services from the hld repository.
*
* @param projectRoot
*/
export const generateHldLifecyclePipelineYaml = async (
projectRoot: string
): Promise<void> => {
logger.info(
`Generating hld lifecycle pipeline ${PROJECT_PIPELINE_FILENAME} in ${projectRoot}`
);
const azurePipelinesYamlPath = path.join(
projectRoot,
PROJECT_PIPELINE_FILENAME
);
if (fs.existsSync(azurePipelinesYamlPath)) {
logger.warn(
`Existing ${PROJECT_PIPELINE_FILENAME} found at ${azurePipelinesYamlPath}, skipping generation.`
);
return;
}
const lifecycleYaml = hldLifecyclePipelineYaml();
logger.info(
`Writing ${PROJECT_PIPELINE_FILENAME} file to ${azurePipelinesYamlPath}`
);
writeVersion(azurePipelinesYamlPath);
fs.appendFileSync(azurePipelinesYamlPath, lifecycleYaml, "utf8");
const requiredPipelineVariables = [
`'HLD_REPO' (Repository for your HLD in AzDo. eg. 'dev.azure.com/bhnook/fabrikam/_git/hld')`,
`'PAT' (AzDo Personal Access Token with permissions to the HLD repository.)`,
].join(", ");
logger.info(
`Generated ${PROJECT_PIPELINE_FILENAME}. Commit and push this file to master before attempting to deploy via the command 'spk project install-lifecycle-pipeline'; before running the pipeline ensure the following environment variables are available to your pipeline: ${requiredPipelineVariables}`
);
};
/**
* Update maintainers.yml with new service
*
* TODO: support for contributors(?)
*
* @param maintainersFilePath
* @param newServicePath
* @param serviceMaintainers
*/
export const addNewServiceToMaintainersFile = (
maintainersFilePath: string,
newServicePath: string,
serviceMaintainers: User[]
): void => {
const maintainersFile = yaml.safeLoad(
fs.readFileSync(maintainersFilePath, "utf8")
) as MaintainersFile;
maintainersFile.services["./" + newServicePath] = {
maintainers: serviceMaintainers,
};
logger.info("Updating maintainers.yaml");
fs.writeFileSync(maintainersFilePath, yaml.safeDump(maintainersFile), "utf8");
};
/**
* Writes out a default .gitignore file if one doesn't exist
*
* @param targetDirectory directory to generate the .gitignore file
* @param content content of file
*/
export const generateGitIgnoreFile = (
targetDirectory: string,
content: string
): void => {
const absTargetPath = path.resolve(targetDirectory);
logger.info(`Generating starter .gitignore in ${absTargetPath}`);
try {
const gitIgnoreFilePath = path.join(absTargetPath, ".gitignore");
if (fs.existsSync(gitIgnoreFilePath)) {
logger.warn(
`Existing .gitignore found at ${gitIgnoreFilePath}, skipping generation.`
);
return;
}
logger.info(`Writing .gitignore file to ${gitIgnoreFilePath}`);
fs.writeFileSync(gitIgnoreFilePath, content, "utf8");
} catch (err) {
throw buildError(
errorStatusCode.FILE_IO_ERR,
{
errorKey: "fileutils-generate-git-ignore-file",
values: [absTargetPath],
},
err
);
}
};
/**
* Writes out a default Dockerfile if one doesn't exist
*
* @param targetDirectory directory to generate the Dockerfile
* @param content content of file
*/
export const generateDockerfile = (targetDirectory: string): void => {
const absTargetPath = path.resolve(targetDirectory);
logger.info(`Generating starter Dockerfile in ${absTargetPath}`);
const dockerfilePath = path.join(absTargetPath, "Dockerfile");
if (fs.existsSync(dockerfilePath)) {
logger.warn(
`Existing Dockerfile found at ${dockerfilePath}, skipping generation.`
);
return;
}
logger.info(`Writing Dockerfile to ${dockerfilePath}`);
fs.writeFileSync(
dockerfilePath,
"FROM alpine\nRUN echo 'hello world'",
"utf8"
);
};