Skip to content

Commit 16f53ae

Browse files
authored
Adds an init script and image for running collect_signals without GCP PubSub (#517)
* Add a script and Dockerfile for initing repos for the collector Signed-off-by: Caleb Brown <[email protected]> * Add a cloudbuild config for the init image. Signed-off-by: Caleb Brown <[email protected]> * Remove unnecessary cat, pin base image in Dockerfile Signed-off-by: Caleb Brown <[email protected]> --------- Signed-off-by: Caleb Brown <[email protected]>
1 parent 9ed435e commit 16f53ae

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ images
66

77
# Ignore Dockerfile - this improve caching.
88
**/Dockerfile
9+
10+
!infra/images
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2023 Criticality Score Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
steps:
16+
- name: 'gcr.io/cloud-builders/docker'
17+
args: ['build', '.',
18+
'--build-arg', 'COMMIT_SHA=$COMMIT_SHA',
19+
'-t', 'gcr.io/openssf/criticality-score-init-collect-signals:$COMMIT_SHA',
20+
'-t', 'gcr.io/openssf/criticality-score-init-collect-signals:latest',
21+
'-f', 'infra/images/init_collect_signals/Dockerfile']
22+
images: ['gcr.io/openssf/criticality-score-init-collect-signals']
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2023 Criticality Score Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
FROM gcr.io/google.com/cloudsdktool/google-cloud-cli:slim@sha256:3497ad3a1053bda2c99a766e8764dd27756fdaf84191dd1501405779688abf58
16+
17+
# Add "yq" to the image so the YAML config can be read.
18+
RUN apt-get update -qqy && apt-get install -qqy yq
19+
20+
WORKDIR /bin
21+
COPY ./infra/images/init_collect_signals/init.sh ./
22+
RUN chmod u+x init.sh
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/sh
2+
# Copyright 2023 Criticality Score Authors
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# Test usage (from the base dir of the repo):
17+
# docker build . -f containers/init_collect_signals/Dockerfile -t criticality_score_init_collection
18+
# docker run
19+
# -v /tmp:/output \
20+
# -v $HOME/.config/gcloud:/root/.config/gcloud \
21+
# -v $HOME/path/to/config.yaml:/etc/config.yaml \
22+
# -ti criticality_score_init_collection \
23+
# /bin/init.sh /etc/config.yaml
24+
25+
CONFIG_FILE="$1"
26+
27+
# Read the appropriate settings from the YAML config file.
28+
BUCKET_URL=`yq -r '."additional-params"."input-bucket".url' "$CONFIG_FILE"`
29+
BUCKET_PREFIX_FILE=`yq -r '."additional-params"."input-bucket"."prefix-file"' "$CONFIG_FILE"`
30+
OUTPUT_FILE=`yq -r '."additional-params".criticality."local-url-data-file"' "$CONFIG_FILE"`
31+
echo "bucket url = $BUCKET_URL"
32+
echo "bucket prefix file = $BUCKET_PREFIX_FILE"
33+
echo "url data file = $OUTPUT_FILE"
34+
35+
LATEST_PREFIX=`gsutil cat "$BUCKET_URL"/"$BUCKET_PREFIX_FILE"`
36+
echo "latest prefix = $LATEST_PREFIX"
37+
38+
# Deinfe some temporary files based on OUTPUT_FILE so they're on the same volume.
39+
TMP_OUTPUT_FILE_1="$OUTPUT_FILE-tmp-1"
40+
TMP_OUTPUT_FILE_2="$OUTPUT_FILE-tmp-2"
41+
42+
# Iterate through all the files to merge all together.
43+
touch "$TMP_OUTPUT_FILE_1"
44+
for file in `gsutil ls "$BUCKET_URL"/"$LATEST_PREFIX"`; do
45+
echo "reading $file"
46+
# Read the file, remove the header and turn it into a plain list of repos.
47+
gsutil cat "$file" | tail -n +2 | cut -d',' -f1 >> "$TMP_OUTPUT_FILE_1"
48+
done
49+
50+
# Ensure the file contains only one entry per repo, and shuffle it.
51+
sort "$TMP_OUTPUT_FILE_1" | uniq | shuf > "$TMP_OUTPUT_FILE_2"
52+
rm "$TMP_OUTPUT_FILE_1"
53+
54+
# Move the final tmp file to the output file to ensure the change is atomic.
55+
mv "$TMP_OUTPUT_FILE_2" "$OUTPUT_FILE"
56+
57+
echo "wrote $OUTPUT_FILE"

0 commit comments

Comments
 (0)