Skip to content

Commit

Permalink
Add dev container
Browse files Browse the repository at this point in the history
  • Loading branch information
ShimShtein committed Oct 5, 2023
1 parent c9e9fc8 commit 77670d8
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM registry.access.redhat.com/ubi7

RUN yum install go-toolset-1.19 git make curl gzip tar --enablerepo=rhel-7-server-devtools-rpms -y

COPY ./entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh

COPY ./enable_go_scl.sh /etc/profile.d/
RUN chmod +x /etc/profile.d/enable_go_scl.sh

ENTRYPOINT ["entrypoint.sh"]
41 changes: 41 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/go
{
"name": "Go",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
// "image": "mcr.microsoft.com/devcontainers/go:1.19",
"build": {
"dockerfile": "Dockerfile"
},

// Features to add to the dev container. More info: https://containers.dev/features.
"features": {
// "ghcr.io/devcontainers/features/common-utils:2": {}
},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "go version",

// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"

"remoteUser": "root",
"remoteEnv": {
"HOME": "/home"
},
"containerUser": "root",
"containerEnv": {
"HOME": "/home",
"WORKDIR": "/workspaces/${localWorkspaceFolderBasename}"
},
"workspaceMount": "",
"runArgs": [
"--volume=${localWorkspaceFolder}:/workspaces/${localWorkspaceFolderBasename}:Z"
]
}
2 changes: 2 additions & 0 deletions .devcontainer/enable_go_scl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
source scl_source enable go-toolset-1.19
9 changes: 9 additions & 0 deletions .devcontainer/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
SCLS=go-toolset-1.19

if [ -x "$(command -v scl_source)" ]; then
source scl_source enable $SCLS
fi

cd $WORKDIR
exec "$@"
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ coverage.*

# Binary
host-metering

#dev
mocks/subscription-manager-*
mocks/consumer/
21 changes: 21 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Run once [mocked]",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"args": ["once"],
"env": {
"HOST_METERING_HOST_CERT_PATH": "${workspaceFolder}/mocks/consumer/cert.pem",
"HOST_METERING_HOST_CERT_KEY_PATH": "${workspaceFolder}/mocks/consumer/key.pem",
"PATH": "${workspaceFolder}/mocks:${env:PATH}"
}
}
]
}
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Host Metering

Host metering client.

## Running in a container
This project has configuration for running inside VSCode container.

You have to have a system with `subscription-manager` installed and registered correctly to use the UBI image (which is the base for this Dockerfile) and to have access to the relevan Red Hat repositories.

## Mocking subscription-manager commands
`mocked_run.sh` is a wrapper of `go run main.go` to execute the command in mocked context.

### Preparing the mocks
Inside the `mocks` folder run `./prepare_mock.sh` to generate outputs that will be used as mocks.
You have to have a system with `subscription-manager` installed and registered correctly to generate the mocks.
11 changes: 11 additions & 0 deletions mocks/mocked_run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

pushd $(dirname "$0")
#prepend path with the mocks folder
MOCKS_PATH=$(pwd)
PATH=$MOCKS_PATH:$PATH
popd

pushd $(dirname "$0")/..
HOST_METERING_HOST_CERT_PATH=$MOCKS_PATH/consumer/cert.pem HOST_METERING_HOST_CERT_KEY_PATH=$MOCKS_PATH/consumer/key.pem go run main.go $@
popd
17 changes: 17 additions & 0 deletions mocks/prepare_mock.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

ACTUAL_USER=$(id -u)
ACTUAL_GROUP=$(id -g)

echo running subscription-manager identity
sudo subscription-manager identity > subscription-manager-identity
echo running subscription-manager usage
sudo subscription-manager usage > subscription-manager-usage
echo running subscription-manager service-level
sudo subscription-manager service-level > subscription-manager-service-level
echo running subscription-manager facts
sudo subscription-manager facts > subscription-manager-facts

echo preparing /etc/pki/consumer
sudo cp -r /etc/pki/consumer ./
sudo chown $ACTUAL_USER:$ACTUAL_GROUP -R ./consumer
4 changes: 4 additions & 0 deletions mocks/subscription-manager
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

echo "#######Mocked output#######"
cat $(dirname "$0")/subscription-manager-$@

0 comments on commit 77670d8

Please sign in to comment.