Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit 8f0b599

Browse files
authored
Pipelines update storage via an spk deployment command (#95)
* Wrote updatetable logic and created update command * Adding unit test * Adding jsdoc * Adding some docs * Indenting steps * Prettier is messing up my formatting of docs * Address some feedback * Massive refactor, improvements and bug fixes * Renamed command based on suggestions * Updating docs to reflect the changes * Struggling to improve code coverage
1 parent 01b6cb7 commit 8f0b599

File tree

5 files changed

+747
-39
lines changed

5 files changed

+747
-39
lines changed

docs/service-introspection-onboarding.md

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,26 @@ To configure the variable group run:
9292
spk variable-group create
9393
```
9494

95+
You will need the following variables:
96+
97+
- `ACCOUNT_KEY`: Set this to the access key for your storage account
98+
- `ACCOUNT_NAME`: Set this to the name of your storage account
99+
- `PARTITION_KEY`: This field can be a distinguishing key that recognizea your
100+
source repository in the storage, for eg. in this example, we're using the
101+
name of the source repository `hello-bedrock`
102+
- `TABLE_NAME`: Set this to the name of the table in your storage account that
103+
you prefer to use
104+
105+
![](./images/variable_group.png)
106+
107+
Make sure that you update the pipelines in the following steps to include this
108+
variable group, such as below:
109+
110+
```yaml
111+
variables:
112+
- group: <your-variable-group-name>
113+
```
114+
95115
#### 2. CI pipeline configuration
96116
97117
The CI pipeline runs from the source repository to build a docker image.
@@ -100,15 +120,15 @@ Paste the following task in its corresponding `azure-pipelines.yml`:
100120

101121
```yaml
102122
- bash: |
103-
curl $SCRIPT > script.sh
104-
chmod +x ./script.sh
105123
tag_name="hello-spektate-$(Build.SourceBranchName)-$(Build.BuildId)"
106124
commitId=$(Build.SourceVersion)
107125
commitId=$(echo "${commitId:0:7}")
108-
./script.sh $(ACCOUNT_NAME) $(ACCOUNT_KEY) $(TABLE_NAME) $(PARTITION_KEY) p1 $(Build.BuildId) imageTag $tag_name commitId $commitId service $(Build.Repository.Name)
109-
displayName: Update manifest pipeline details in CJ db
110-
env:
111-
SCRIPT: https://raw.githubusercontent.com/catalystcode/spk/master/scripts/update_introspection.sh
126+
VERSION_TO_DOWNLOAD=$(curl -s "https://api.github.com/repos/CatalystCode/spk/releases/latest" | grep "tag_name" | sed -E 's/.*"([^"]+)".*/\1/')
127+
echo "Downloading SPK version $VERSION_TO_DOWNLOAD" && wget "https://github.com/CatalystCode/spk/releases/download/$VERSION_TO_DOWNLOAD/spk-linux"
128+
chmod +x ./spk-linux
129+
./spk-linux deployment create -n $(ACCOUNT_NAME) -k $(ACCOUNT_KEY) -t $(TABLE_NAME) -p $(PARTITION_KEY) --p1 $(Build.BuildId) --image-tag $tag_name --commit-id $commitId --service $service
130+
131+
displayName: Update manifest pipeline details in Spektate db
112132
```
113133

114134
This task will update the service introspection storage table for every build
@@ -127,58 +147,47 @@ by service introspection.
127147

128148
The CD release pipeline updates the docker image number in the HLD.
129149

130-
Paste the following task in its corresponding `azure-pipelines.yml`:
150+
Paste the following task towards the end of your release step in the release
151+
pipeline:
131152

132153
```yaml
133-
latest_commit=$(git rev-parse --short HEAD)
134-
echo "latest_commit=$latest_commit"
135-
136-
# Download update storage script
137-
curl https://raw.githubusercontent.com/catalystcode/spk/master/scripts/update_introspection.sh > script.sh
138-
chmod +x script.sh
139-
140-
./script.sh $(ACCOUNT_NAME) $(ACCOUNT_KEY) $(TABLE_NAME) $(PARTITION_KEY) imageTag $(Build.BuildId) p2 $(Release.ReleaseId) hldCommitId $latest_commit env $(Release.EnvironmentName)
154+
latest_commit=$(git rev-parse --short HEAD) echo "latest_commit=$latest_commit"
155+
156+
VERSION_TO_DOWNLOAD=$(curl -s
157+
"https://api.github.com/repos/CatalystCode/spk/releases/latest" | grep
158+
"tag_name" | sed -E 's/.*"([^"]+)".*/\1/') echo "Downloading SPK version
159+
$VERSION_TO_DOWNLOAD" && wget
160+
"https://github.com/CatalystCode/spk/releases/download/$VERSION_TO_DOWNLOAD/spk-linux"
161+
chmod +x ./spk-linux ./spk-linux deployment create -n $(ACCOUNT_NAME) -k
162+
$(ACCOUNT_KEY) -t $(TABLE_NAME) -p $(PARTITION_KEY) --p2 $(Release.ReleaseId)
163+
--hld-commit-id $latest_commit --env $(Release.EnvironmentName) --image-tag
164+
$(Build.BuildId)
141165
```
142166

143-
This task is the same as the one from step 1 but instead passes the information
167+
This task is similar to the one from step 1 but instead passes the information
144168
that corresponds to the CD release pipeline.
145169

146170
#### 4. HLD manifest pipeline configuration
147171

148172
The HLD manifest pipeline builds the HLD using `fabrikate` and generates
149173
resource manifests that are then placed in the resource manifest repository.
150174

151-
Paste the following task in the `azure-pipelines.yml` file **before** the
175+
Paste the following task in the `azure-pipelines.yml` file **after** the
152176
`fabrikate` steps:
153177

154178
```yaml
155179
- bash: |
156-
curl $SCRIPT > script.sh
157-
chmod +x ./script.sh
180+
cd "$HOME"/<name of your manifest repository>
158181
commitId=$(Build.SourceVersion)
159182
commitId=$(echo "${commitId:0:7}")
160-
./script.sh $(ACCOUNT_NAME) $(ACCOUNT_KEY) $(TABLE_NAME) $(PARTITION_KEY) hldCommitId $commitId p3 $(Build.BuildId)
161-
displayName: Update manifest pipeline details in CJ db
162-
env:
163-
SCRIPT: https://raw.githubusercontent.com/catalystcode/spk/master/scripts/update_introspection.sh
164-
```
165-
166-
Paste the following task after the `fabrikate` step:
167-
168-
```yaml
169-
- script: |
170-
cd "$HOME"/hello-bedrock-manifest
171-
curl $SCRIPT > script.sh
172-
chmod +x ./script.sh
173183
latest_commit=$(git rev-parse --short HEAD)
174-
./script.sh $(ACCOUNT_NAME) $(ACCOUNT_KEY) $(TABLE_NAME) $(PARTITION_KEY) p3 $(Build.BuildId) manifestCommitId $latest_commit
175-
displayName: Update commit id in database
176-
env:
177-
SCRIPT: https://raw.githubusercontent.com/catalystcode/spk/master/scripts/update_introspection.sh
184+
VERSION_TO_DOWNLOAD=$(curl -s "https://api.github.com/repos/CatalystCode/spk/releases/latest" | grep "tag_name" | sed -E 's/.*"([^"]+)".*/\1/')
185+
echo "Downloading SPK version $VERSION_TO_DOWNLOAD" && wget "https://github.com/CatalystCode/spk/releases/download/$VERSION_TO_DOWNLOAD/spk-linux"
186+
chmod +x ./spk-linux
187+
./spk-linux deployment create -n $(ACCOUNT_NAME) -k $(ACCOUNT_KEY) -t $(TABLE_NAME) -p $(PARTITION_KEY) --p3 $(Build.BuildId) --hld-commit-id $commitId --manifest-commit-id $latest_commit
188+
displayName: Update manifest pipeline details in Spektate db
178189
```
179190

180-
This task will update the `manifestCommitId`.
181-
182191
## Getting started
183192

184193
After completing the steps in this guide, you should be able to:

src/commands/deployment/create.ts

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import commander from "commander";
2+
import {
3+
addSrcToACRPipeline,
4+
IDeploymentTable,
5+
updateACRToHLDPipeline,
6+
updateHLDToManifestPipeline,
7+
updateManifestCommitId
8+
} from "../../lib/azure/deploymenttable";
9+
import { logger } from "../../logger";
10+
11+
/**
12+
* Creates a create command decorator for the command to update a deployment in storage
13+
* @param command
14+
*/
15+
export const createCommandDecorator = (command: commander.Command): void => {
16+
command
17+
.command("create")
18+
.alias("c")
19+
.description("Insert the deployment in storage from pipelines")
20+
.option(
21+
"-k, --access-key <access-key>",
22+
"Access key of the storage account"
23+
)
24+
.option("-n, --name <account-name>", "Name of the storage account")
25+
.option(
26+
"-p, --partition-key <partition-key>",
27+
"Partition key for the storage account"
28+
)
29+
.option("-t, --table-name <table-name>", "Name of table in storage account")
30+
.option("--p1 <p1>", "Identifier for the first pipeline")
31+
.option("--image-tag <image-tag>", "Image tag")
32+
.option("--commit-id <commit-id>", "Commit Id in source repository")
33+
.option("--service <service>", "Service name")
34+
.option("--p2 <p2>", "Identifier for the second pipeline")
35+
.option("--hld-commit-id <hld-commit-id>", "Commit id in HLD repository")
36+
.option("--env <env>", "Release environment name")
37+
.option("--p3 <p3>", "Identifier for the third pipeline")
38+
.option(
39+
"--manifest-commit-id <manifest-commit-id>",
40+
"Commit Id in the manifest repository"
41+
)
42+
.action(async opts => {
43+
if (
44+
!opts.accessKey ||
45+
!opts.name ||
46+
!opts.partitionKey ||
47+
!opts.tableName
48+
) {
49+
logger.error(
50+
"You must specify all of access key, storage account name, partition key, table name, filter name and filter value"
51+
);
52+
return;
53+
}
54+
55+
const tableInfo: IDeploymentTable = {
56+
accountKey: opts.accessKey,
57+
accountName: opts.name,
58+
partitionKey: opts.partitionKey,
59+
tableName: opts.tableName
60+
};
61+
62+
// This is being called from the first pipeline. Make sure all other fields are defined.
63+
if (opts.p1) {
64+
if (!opts.imageTag || !opts.commitId || !opts.service) {
65+
logger.error(
66+
"For updating the details of source pipeline, you must specify --image-tag, --commit-id and --service"
67+
);
68+
return;
69+
}
70+
71+
addSrcToACRPipeline(
72+
tableInfo,
73+
opts.p1,
74+
opts.imageTag,
75+
opts.service,
76+
opts.commitId
77+
);
78+
return;
79+
}
80+
81+
// This is being called from the second pipeline. Make sure all other fields are defined.
82+
if (opts.imageTag || opts.p2) {
83+
if (!opts.p2 || !opts.hldCommitId || !opts.env || !opts.imageTag) {
84+
logger.error(
85+
"For updating the details of image tag release pipeline, you must specify --p2, --hld-commit-id, --image-tag and --env"
86+
);
87+
return;
88+
}
89+
updateACRToHLDPipeline(
90+
tableInfo,
91+
opts.p2,
92+
opts.imageTag,
93+
opts.hldCommitId,
94+
opts.env
95+
);
96+
return;
97+
}
98+
99+
// This is being called from the third pipeline. Make sure all other fields are defined.
100+
if (opts.hldCommitId && opts.p3) {
101+
if (!opts.p3) {
102+
logger.error(
103+
"For updating the details of manifest generation pipeline, you must specify --p3"
104+
);
105+
return;
106+
}
107+
if (opts.manifestCommitId) {
108+
updateHLDToManifestPipeline(
109+
tableInfo,
110+
opts.hldCommitId,
111+
opts.p3,
112+
opts.manifestCommitId
113+
);
114+
} else {
115+
updateHLDToManifestPipeline(tableInfo, opts.hldCommitId, opts.p3);
116+
}
117+
return;
118+
}
119+
120+
// This is being called from the third pipeline to update manifest id. Make sure all other fields are defined.
121+
if (opts.p3 && opts.manifestCommitId) {
122+
if (!opts.manifestCommitId) {
123+
logger.error(
124+
"For updating the details of manifest generation pipeline, you must specify --manifest-commit-id"
125+
);
126+
return;
127+
}
128+
updateManifestCommitId(tableInfo, opts.p3, opts.manifestCommitId);
129+
return;
130+
}
131+
132+
// Execution should not get here; request user to specify arguments correctly
133+
logger.error("No action could be performed for specified arguments.");
134+
});
135+
};

src/commands/deployment/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Command } from "../command";
2+
import { createCommandDecorator } from "./create";
23
import { dashboardCommandDecorator } from "./dashboard";
34
import { getCommandDecorator } from "./get";
45
import { onboardCommandDecorator } from "./onboard";
@@ -14,6 +15,7 @@ export const deploymentCommand = Command(
1415
getCommandDecorator,
1516
onboardCommandDecorator,
1617
validateCommandDecorator,
17-
dashboardCommandDecorator
18+
dashboardCommandDecorator,
19+
createCommandDecorator
1820
]
1921
);

0 commit comments

Comments
 (0)