From 34421568f576f7d0410cb07e175d87fb4ea04b84 Mon Sep 17 00:00:00 2001 From: Ludovico Magnocavallo Date: Mon, 9 Sep 2019 11:58:27 +0200 Subject: [PATCH] Turn on ci, add linting and boilerplate steps (#6) * ci: testing build steps * ci: lint build step * ci: add simple license boilerplate check script and step * org env: add missing boilerplate * ci: use consistent args in linting build steps --- .ci/cloudbuild.lint.yaml | 24 ++++++++ .ci/scripts/check_boilerplate.py | 60 +++++++++++++++++++ .../environments/providers.tf | 14 +++++ .../environments/variables.tf | 14 +++++ .../environments/versions.tf | 13 ++++ 5 files changed, 125 insertions(+) create mode 100644 .ci/cloudbuild.lint.yaml create mode 100755 .ci/scripts/check_boilerplate.py diff --git a/.ci/cloudbuild.lint.yaml b/.ci/cloudbuild.lint.yaml new file mode 100644 index 000000000..1c6dcf169 --- /dev/null +++ b/.ci/cloudbuild.lint.yaml @@ -0,0 +1,24 @@ +# Copyright 2019 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +steps: + - name: "python:3.6-alpine" + id: "boilerplate" + args: ["/workspace/.ci/scripts/check_boilerplate.py", "/workspace"] + - name: "wata727/tflint" + id: "lint" + args: ["/workspace"] +tags: + - "ci" + - "lint" diff --git a/.ci/scripts/check_boilerplate.py b/.ci/scripts/check_boilerplate.py new file mode 100755 index 000000000..762d67d76 --- /dev/null +++ b/.ci/scripts/check_boilerplate.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 + +# Copyright 2019 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import glob +import os +import re +import sys + + +_EXCLUDE_DIRS = ('.git', '.terraform') +_MATCH_FILES = ( + 'Dockerfile', '.py', '.sh', '.tf', '.yaml', '.yml' +) +_MATCH_STRING = ( + r'^\s*[#\*]\sCopyright [0-9]{4} Google LLC$\s+[#\*]\s+' + r'[#\*]\sLicensed under the Apache License, Version 2.0 ' + r'\(the "License"\);\s+' +) +_MATCH_RE = re.compile(_MATCH_STRING, re.M) + + +def main(dir): + "Cycle through files in dir and check for the Apache 2.0 boilerplate." + errors, warnings = [], [] + for root, dirs, files in os.walk(dir): + dirs[:] = [d for d in dirs if d not in _EXCLUDE_DIRS] + for fname in files: + if fname in _MATCH_FILES or os.path.splitext(fname)[1] in _MATCH_FILES: + fpath = os.path.abspath(os.path.join(root, fname)) + try: + if not _MATCH_RE.search(open(fpath).read()): + errors.append(fpath) + except (IOError, OSError): + warnings.append(fpath) + if warnings: + print('The following files cannot be accessed:') + print('\n'.join(' - {}'.format(s) for s in warnings)) + if errors: + print('The following files are missing the license boilerplate:') + print('\n'.join(' - {}'.format(s) for s in errors)) + sys.exit(1) + + +if __name__ == '__main__': + if len(sys.argv) != 2: + raise SystemExit('No directory passed.') + main(sys.argv[1]) diff --git a/organization-bootstrap/environments/providers.tf b/organization-bootstrap/environments/providers.tf index 33091732e..cffb31afc 100644 --- a/organization-bootstrap/environments/providers.tf +++ b/organization-bootstrap/environments/providers.tf @@ -1 +1,15 @@ +# Copyright 2019 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + provider "google" {} diff --git a/organization-bootstrap/environments/variables.tf b/organization-bootstrap/environments/variables.tf index 42d2ca250..99497c30c 100644 --- a/organization-bootstrap/environments/variables.tf +++ b/organization-bootstrap/environments/variables.tf @@ -1,3 +1,17 @@ +# Copyright 2019 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + variable "audit_viewers" { description = "Audit project viewers, in IAM format." default = [] diff --git a/organization-bootstrap/environments/versions.tf b/organization-bootstrap/environments/versions.tf index ac97c6ac8..4eb1500c5 100644 --- a/organization-bootstrap/environments/versions.tf +++ b/organization-bootstrap/environments/versions.tf @@ -1,3 +1,16 @@ +# Copyright 2019 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. terraform { required_version = ">= 0.12"