From 0951c51575ee1af5d67e18230a8e3b6b46937214 Mon Sep 17 00:00:00 2001 From: Joel Rebello Date: Fri, 13 May 2022 16:40:11 +0200 Subject: [PATCH] init project --- .github/ISSUE_TEMPLATE/default.md | 9 ++ .github/PULL_REQUEST_TEMPLATE.md | 11 ++ .github/mergify.yml | 24 +++ .github/workflows/codeql-analysis.yml | 72 +++++++++ .github/workflows/push-pr-lint.yaml | 59 ++++++++ .github/workflows/release.yml | 15 ++ .gitignore | 11 ++ .golangci.yml | 128 ++++++++++++++++ CODEOWNERS | 2 + CONTRIBUTING.md | 13 ++ Dockerfile | 4 + LICENSE | 202 ++++++++++++++++++++++++++ Makefile | 77 ++++++++++ README.md | 20 +++ cli.go | 62 ++++++++ collector.go | 42 ++++++ go.mod | 26 ++++ go.sum | 55 +++++++ main.go | 5 + renovate.json | 26 ++++ 20 files changed, 863 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE/default.md create mode 100644 .github/PULL_REQUEST_TEMPLATE.md create mode 100644 .github/mergify.yml create mode 100644 .github/workflows/codeql-analysis.yml create mode 100644 .github/workflows/push-pr-lint.yaml create mode 100644 .github/workflows/release.yml create mode 100644 .gitignore create mode 100644 .golangci.yml create mode 100644 CODEOWNERS create mode 100644 CONTRIBUTING.md create mode 100644 Dockerfile create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 cli.go create mode 100644 collector.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 renovate.json diff --git a/.github/ISSUE_TEMPLATE/default.md b/.github/ISSUE_TEMPLATE/default.md new file mode 100644 index 00000000..c585fee0 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/default.md @@ -0,0 +1,9 @@ +Please don't forget to include the following information in your issue: + +- The HW vendor impacted by this issue (if applicable) + +- The HW model number, BMC firmware and/or BIOS versions impacted by this issue (if applicable) + +- What version of Alloy exhibits this behavior (if applicable) + +- Detailed steps to verify the issue(s) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 00000000..189695be --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,11 @@ +# What does this PR do + +# The HW vendor this change applies to (if applicable) + +# The HW model number, product name this change applies to (if applicable) + +# The BMC firmware and/or BIOS versions that this change applies to (if applicable) + +# What version of tooling - vendor specific or opensource does this change depend on (if applicable) + +# How can this change be tested by a PR reviewer? diff --git a/.github/mergify.yml b/.github/mergify.yml new file mode 100644 index 00000000..af151c5a --- /dev/null +++ b/.github/mergify.yml @@ -0,0 +1,24 @@ +queue_rules: + - name: default + conditions: + # Conditions to get out of the queue (= merged) + - check-success=lint-test + - check-success=build + +pull_request_rules: + - name: Automatic merge on approval + conditions: + - base=main + - "#approved-reviews-by>=1" + - "#changes-requested-reviews-by=0" + - check-success='lint-test' + - check-success='build' + - label!=do-not-merge + - label=ready-to-merge + actions: + queue: + method: merge + name: default + commit_message_template: | + {{ title }} (#{{ number }}) + diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml new file mode 100644 index 00000000..d54b5135 --- /dev/null +++ b/.github/workflows/codeql-analysis.yml @@ -0,0 +1,72 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: "CodeQL" + +on: + push: + branches: [ main ] + pull_request: + # The branches below must be a subset of the branches above + branches: [ main ] + schedule: + - cron: '34 3 * * 0' + +jobs: + analyze: + name: Analyze + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + security-events: write + + strategy: + fail-fast: false + matrix: + language: [ 'go' ] + # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] + # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v2 + with: + languages: ${{ matrix.language }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + + # Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs + # queries: security-extended,security-and-quality + + + # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). + # If this step fails, then you should remove it and run the build manually (see below) + - name: Autobuild + uses: github/codeql-action/autobuild@v2 + + # ℹī¸ Command-line programs to run using the OS shell. + # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun + + # If the Autobuild fails above, remove it and uncomment the following three lines. + # modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance. + + # - run: | + # echo "Run, Build Application using script" + # ./location_of_script_within_repo/buildscript.sh + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v2 diff --git a/.github/workflows/push-pr-lint.yaml b/.github/workflows/push-pr-lint.yaml new file mode 100644 index 00000000..7a9738fa --- /dev/null +++ b/.github/workflows/push-pr-lint.yaml @@ -0,0 +1,59 @@ +name: lint, test and build image +on: [pull_request, push] + +jobs: + lint-test: + runs-on: ubuntu-latest + steps: + - name: Install Go + uses: actions/setup-go@v3 + with: + go-version: '^1.17.0' + - name: Checkout code + uses: actions/checkout@v3 + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + args: --config .golangci.yml + version: v1.45.2 + - name: Test + run: go test ./... + build: + runs-on: ubuntu-latest + needs: [lint-test] + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Build image - no push + id: dockerbuild + uses: docker/build-push-action@v3 + with: + context: . + push: false + tags: ghcr.io/metal-toolbox/alloy:latest + + - name: Scan image + id: scan + uses: anchore/scan-action@v3 + with: + image: ghcr.io/metal-toolbox/alloy:latest + acs-report-enable: true + # TODO(jaosorior): Fail build once we migrate off CentOS. + fail-build: false + + # TODO(jaosorior): Uncomment once we migrate off CentOS. + # - name: upload Anchore scan SARIF report + # uses: github/codeql-action/upload-sarif@v2 + # with: + # sarif_file: ${{ steps.scan.outputs.sarif }} + # # This should run even if we fail the container scan + # if: always() + + - name: Inspect action SARIF report + run: cat ${{ steps.scan.outputs.sarif }} + # This should run even if we fail the container scan + if: always() diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 00000000..9046a313 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,15 @@ +name: Release latest + +on: + push: + branches: + - main + +jobs: + # Push to latest + container-push-latest: + uses: metal-toolbox/container-push/.github/workflows/container-push.yml@main + with: + name: alloy + tag: latest + dockerfile_path: Dockerfile diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..a79c69e6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +### Go template + +# Test binary, build with `go test -c` +*.test + +# Application binary itself +/fup + +*~ +.*.swp +.*.swo diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 00000000..e318c4ce --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,128 @@ +# golangci.com configuration +# https://github.com/golangci/golangci/wiki/Configuration +service: + golangci-lint-version: 1.45.2 # use the fixed version to not introduce new linters unexpectedly + +linters-settings: + govet: + enable: + - fieldalignment + check-shadowing: true + settings: + printf: + funcs: + - (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof + - (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf + - (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf + - (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf + revive: + min-confidence: 0 + gocyclo: + min-complexity: 10 + maligned: + suggest-new: true + dupl: + threshold: 100 + goconst: + min-len: 2 + min-occurrences: 2 + depguard: + list-type: blacklist + packages: + # logging is allowed only by logutils.Log, logrus + # is allowed to use only in logutils package + - github.com/sirupsen/logrus + misspell: + locale: US + auto-fix: true + lll: + line-length: 140 + goimports: + local-prefixes: github.com/golangci/golangci-lint + gocritic: + enabled-tags: + - performance + - style + - experimental + disabled-checks: + - wrapperFunc + gofumpt: + extra-rules: true + wsl: + auto-fix: true + +linters: + enable: + - errcheck + - gosimple + - govet + - gofmt + - gocyclo + - ineffassign + - stylecheck + - misspell + - deadcode + - staticcheck + - structcheck + - unused + - prealloc + - typecheck + - varcheck + # additional linters + - bodyclose + - gocritic + - goerr113 + - goimports + - revive + - gomnd + - misspell + - noctx + - stylecheck + - whitespace + - wsl + enable-all: false + disable-all: true + +run: + build-tags: + - gingonic + skip-dirs: + - scripts + - docker + - samples + #modules-download-mode: vendor + +issues: + exclude-rules: + - linters: + - gosec + text: "weak cryptographic primitive" + + - linters: + - stylecheck + text: "ST1016" + exclude: + # Default excludes from `golangci-lint run --help` with EXC0002 removed + # EXC0001 errcheck: Almost all programs ignore errors on these functions and in most cases it's ok + - Error return value of .((os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*print(f|ln)?|os\.(Un)?Setenv). is not checked + # EXC0002 golint: Annoying issue about not having a comment. The rare codebase has such comments + # - (comment on exported (method|function|type|const)|should have( a package)? comment|comment should be of the form) + # EXC0003 golint: False positive when tests are defined in package 'test' + - func name will be used as test\.Test.* by other packages, and that stutters; consider calling this + # EXC0004 govet: Common false positives + - (possible misuse of unsafe.Pointer|should have signature) + # EXC0005 staticcheck: Developers tend to write in C-style with an explicit 'break' in a 'switch', so it's ok to ignore + - ineffective break statement. Did you mean to break out of the outer loop + # EXC0006 gosec: Too many false-positives on 'unsafe' usage + - Use of unsafe calls should be audited + # EXC0007 gosec: Too many false-positives for parametrized shell calls + - Subprocess launch(ed with variable|ing should be audited) + # EXC0008 gosec: Duplicated errcheck checks + - (G104|G307) + # EXC0009 gosec: Too many issues in popular repos + - (Expect directory permissions to be 0750 or less|Expect file permissions to be 0600 or less) + # EXC0010 gosec: False positive is triggered by 'src, err := ioutil.ReadFile(filename)' + - Potential file inclusion via variable +exclude-use-default: false + + diff --git a/CODEOWNERS b/CODEOWNERS new file mode 100644 index 00000000..a23e4d78 --- /dev/null +++ b/CODEOWNERS @@ -0,0 +1,2 @@ +* @metal-toolbox/diogomatsubara +* @metal-toolbox/joelrebel diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..65ff34df --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,13 @@ +# How to contribute + +## Submitting changes + +Run `make test` and `make lint` and ensure it all passes before submitting a PR. +Checkout the [PR template](https://github.com/metal-toolbox/alloy/blob/main/.github/PULL_REQUEST_TEMPLATE.md) and create a pull request at [alloy PRs](https://github.com/metal-toolbox/alloy/pulls) + +## Coding conventions + +https://talks.golang.org/2013/bestpractices.slide#1 +https://golang.org/doc/effective_go + +Run `make lint` on your changes and that should get it more than half way there. diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..5ee4eab2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,4 @@ +# https://github.com/metal-toolbox/ironlib/blob/main/Dockerfile +FROM ghcr.io/metal-toolbox/alloy:latest + +ENTRYPOINT [ "/bin/bash", "-l", "-c" ] diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..2adaf34f --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2021 Equinix Metal + + 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 + + http://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. diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..ee212754 --- /dev/null +++ b/Makefile @@ -0,0 +1,77 @@ +export DOCKER_BUILDKIT=1 +GIT_COMMIT_FULL := $(shell git rev-parse HEAD) +GO_VERSION := $(shell expr `go version |cut -d ' ' -f3 |cut -d. -f2` \>= 16) +DOCKER_REGISTRY := "ghcr.io/metal-toolbox/alloy:latest" +REPO := "https://github.com/metal-toolbox/alloy.git" + +.DEFAULT_GOAL := help + +## lint +lint: + golangci-lint run --config .golangci.yml + +## Go test +test: + CGO_ENABLED=0 go test -v -covermode=atomic ./... + +## build osx bin +build-osx: +ifeq ($(GO_VERSION), 0) + $(error build requies go version 1.17.n or higher) +endif + GOOS=darwin GOARCH=amd64 go build -o alloy + + +## Build linux bin +build-linux: +ifeq ($(GO_VERSION), 0) + $(error build requies go version 1.16.n or higher) +endif + GOOS=linux GOARCH=amd64 go build -o alloy + +## build docker image and tag as ghcr.io/metal-toolbox/alloy:latest +build-image: + @echo ">>>> NOTE: You may want to execute 'make build-image-nocache' depending on the Docker stages changed" + docker build --rm=true -f Dockerfile -t ${DOCKER_REGISTRY}:latest . \ + --label org.label-schema.schema-version=1.0 \ + --label org.label-schema.vcs-ref=$(GIT_COMMIT_FULL) \ + --label org.label-schema.vcs-url=$(REPO) + +## build docker image, ignoring the cache +build-image-nocache: + docker build --no-cache --rm=true -f Dockerfile -t ${DOCKER_REGISTRY}:latest . \ + --label org.label-schema.schema-version=1.0 \ + --label org.label-schema.vcs-ref=$(GIT_COMMIT_FULL) \ + --label org.label-schema.vcs-url=$(REPO) + + +## push docker image +push-image: + docker push ${DOCKER_REGISTRY}:latest + + +# https://gist.github.com/prwhite/8168133 +# COLORS +GREEN := $(shell tput -Txterm setaf 2) +YELLOW := $(shell tput -Txterm setaf 3) +WHITE := $(shell tput -Txterm setaf 7) +RESET := $(shell tput -Txterm sgr0) + + +TARGET_MAX_CHAR_NUM=20 +## Show help +help: + @echo '' + @echo 'Usage:' + @echo ' ${YELLOW}make${RESET} ${GREEN}${RESET}' + @echo '' + @echo 'Targets:' + @awk '/^[a-zA-Z\-\\_0-9]+:/ { \ + helpMessage = match(lastLine, /^## (.*)/); \ + if (helpMessage) { \ + helpCommand = substr($$1, 0, index($$1, ":")-1); \ + helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \ + printf " ${YELLOW}%-$(TARGET_MAX_CHAR_NUM)s${RESET} ${GREEN}%s${RESET}\n", helpCommand, helpMessage; \ + } \ + } \ + { lastLine = $$0 }' $(MAKEFILE_LIST) diff --git a/README.md b/README.md new file mode 100644 index 00000000..cf8ce9c3 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +Alloy collects and reports hardware inventory inband. + +``` +❯ ./alloy inventory +NAME: + alloy inventory - collect inventory + +USAGE: + alloy inventory [command options] [arguments...] + +OPTIONS: + --component-type value, -t value Component slug to collect inventory for. + --server-url value, -u value server URL to submit inventory. [$SERVER_URL] + --local-file value, -l value write inventory results to local file. + --dry-run, -d collect inventory, skip posting data to server URL. + --verbose, -v Turn on verbose messages for debugging. + +2022/05/13 16:37:29 Required flag "server-url" not set + +``` \ No newline at end of file diff --git a/cli.go b/cli.go new file mode 100644 index 00000000..09f783ac --- /dev/null +++ b/cli.go @@ -0,0 +1,62 @@ +package main + +import ( + "log" + "os" + + "github.com/urfave/cli" +) + +func run() { + collector := &collector{} + + app := cli.NewApp() + + app.Commands = []cli.Command{ + { + Name: "inventory", + Usage: "collect inventory", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "component-type, t", + Usage: "Component slug to collect inventory for.", + Destination: &collector.component, + }, + cli.StringFlag{ + Name: "server-url, u", + Usage: "server URL to submit inventory.", + EnvVar: "SERVER_URL", + Required: true, + Destination: &collector.serverURL, + }, + cli.StringFlag{ + Name: "local-file, l", + Usage: "write inventory results to local file.", + Required: false, + Destination: &collector.localFile, + }, + cli.BoolFlag{ + Name: "dry-run, d", + Usage: "collect inventory, skip posting data to server URL.", + Required: false, + Destination: &collector.dryRun, + }, + cli.BoolFlag{ + Name: "verbose, v", + Usage: "Turn on verbose messages for debugging.", + Required: false, + Destination: &collector.verbose, + }, + }, + + Action: func(c *cli.Context) error { + return collector.inventory() + }, + }, + } + + err := app.Run(os.Args) + if err != nil { + log.Fatal(err) + } +} diff --git a/collector.go b/collector.go new file mode 100644 index 00000000..b7a2e559 --- /dev/null +++ b/collector.go @@ -0,0 +1,42 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/metal-toolbox/ironlib" + "github.com/sirupsen/logrus" +) + +// collector collects hardware inventory +type collector struct { + component string + serverURL string + localFile string + verbose bool + dryRun bool +} + +func (*collector) inventory() error { + logger := logrus.New() + + device, err := ironlib.New(logger) + if err != nil { + logger.Fatal(err) + } + + inv, err := device.GetInventory(context.TODO()) + if err != nil { + logger.Fatal(err) + } + + j, err := json.MarshalIndent(inv, " ", " ") + if err != nil { + logger.Fatal(err) + } + + fmt.Println(j) + + return nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..9f80b226 --- /dev/null +++ b/go.mod @@ -0,0 +1,26 @@ +module github.com/metal-toolbox/alloy + +go 1.17 + +require github.com/urfave/cli v1.22.9 + +require ( + github.com/beevik/etree v1.1.0 // indirect + github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect + github.com/dselans/dmidecode v0.0.0-20180814053009-65c3f9d81910 // indirect + github.com/golang/protobuf v1.3.1 // indirect + github.com/metal-toolbox/ironlib v0.0.0-20220513131631-9be17392c451 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/r3labs/diff/v2 v2.15.1 // indirect + github.com/russross/blackfriday/v2 v2.0.1 // indirect + github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect + github.com/sirupsen/logrus v1.8.1 // indirect + github.com/tidwall/gjson v1.14.1 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.0 // indirect + github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect + golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect + golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect + golang.org/x/text v0.3.7 // indirect + google.golang.org/appengine v1.6.6 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..6146b089 --- /dev/null +++ b/go.sum @@ -0,0 +1,55 @@ +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/beevik/etree v1.1.0 h1:T0xke/WvNtMoCqgzPhkX2r4rjY3GDZFi+FjpRZY2Jbs= +github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dselans/dmidecode v0.0.0-20180814053009-65c3f9d81910 h1:w9T/rS5VP0SPXqYr7BpUeqf6ukp/GEWm6nXhziWzBY4= +github.com/dselans/dmidecode v0.0.0-20180814053009-65c3f9d81910/go.mod h1:yGxJ4za56u74+F00gmg9RJoyXLzvrOrIat4b/Dgw9Lo= +github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/metal-toolbox/ironlib v0.0.0-20220513131631-9be17392c451 h1:F5YOcFz88G3j/BmmstwdgUyxk70cPO9AnT5mbC9+N9k= +github.com/metal-toolbox/ironlib v0.0.0-20220513131631-9be17392c451/go.mod h1:KBIWTZR2FSPkdPfNrvrka1wglKoJl0zMPdLEdPv/obc= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/r3labs/diff/v2 v2.15.1 h1:EOrVqPUzi+njlumoqJwiS/TgGgmZo83619FNDB9xQUg= +github.com/r3labs/diff/v2 v2.15.1/go.mod h1:I8noH9Fc2fjSaMxqF3G2lhDdC0b+JXCfyx85tWFM9kc= +github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= +github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo= +github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/urfave/cli v1.22.9 h1:cv3/KhXGBGjEXLC4bH0sLuJ9BewaAbpk5oyMOveu4pw= +github.com/urfave/cli v1.22.9/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= +github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI= +github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 h1:HVyaeDAYux4pnY+D/SiwmLOR36ewZ4iGQIIrtnuCjFA= +golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +google.golang.org/appengine v1.6.6 h1:lMO5rYAqUxkmaj76jAkRUvt5JZgFymx/+Q5Mzfivuhc= +google.golang.org/appengine v1.6.6/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go new file mode 100644 index 00000000..ffee7e43 --- /dev/null +++ b/main.go @@ -0,0 +1,5 @@ +package main + +func main() { + run() +} diff --git a/renovate.json b/renovate.json new file mode 100644 index 00000000..9a9a0b54 --- /dev/null +++ b/renovate.json @@ -0,0 +1,26 @@ +{ + "extends": [ + "config:base", + ":rebaseStalePrs", + ":dependencyDashboard" + ], + "vulnerabilityAlerts": { + "enabled": true, + "labels": ["security"] + }, + "labels": ["dependencies"], + "rollbackPrs": true, + "rebaseWhen": "auto", + "addLabels": ["dependencies"], + "stabilityDays": 3, + "postUpdateOptions": [ + "gomodTidy" + ], +"packageRules": [ + { + "description": "Automatically merge minor and patch-level updates", + "matchUpdateTypes": ["minor", "patch", "digest"], + "automerge": true + } +] +}