From 22a669c474bdeea84f3de54195bc33695a8b87fd Mon Sep 17 00:00:00 2001 From: Shimon Shtein Date: Tue, 24 Oct 2023 16:49:53 +0000 Subject: [PATCH] Use podman-compose for dev environment --- .devcontainer/Dockerfile | 12 +- .devcontainer/commands/prepare_containers.sh | 19 ++ .devcontainer/devcontainer.json | 23 ++- .devcontainer/docker-compose.yml | 22 ++ .devcontainer/host-metering.conf | 17 ++ .devcontainer/local_prometheus/prometheus.yml | 1 + .gitignore | 4 +- .vscode/launch.json | 6 +- .vscode/settings.json | 5 + Makefile | 66 +++--- README.md | 15 +- mocks/create-cert.sh | 20 ++ mocks/mock-cert.cnf | 16 ++ mocks/{prepare_mock.sh => mock_from_host.sh} | 0 mocks/subscription-manager | 192 +++++++++++++++++- 15 files changed, 357 insertions(+), 61 deletions(-) create mode 100755 .devcontainer/commands/prepare_containers.sh create mode 100644 .devcontainer/docker-compose.yml create mode 100644 .devcontainer/host-metering.conf create mode 100644 .devcontainer/local_prometheus/prometheus.yml create mode 100644 .vscode/settings.json create mode 100755 mocks/create-cert.sh create mode 100644 mocks/mock-cert.cnf rename mocks/{prepare_mock.sh => mock_from_host.sh} (100%) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 438463f..e3c5d12 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1,11 +1 @@ -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"] +FROM registry.access.redhat.com/ubi7/go-toolset:1.19 diff --git a/.devcontainer/commands/prepare_containers.sh b/.devcontainer/commands/prepare_containers.sh new file mode 100755 index 0000000..9f85e72 --- /dev/null +++ b/.devcontainer/commands/prepare_containers.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +pushd $(dirname "$0")/.. +DEVCONTAINER_PATH=$(pwd) +popd + +LOCAL_COMPOSE_FILE=$DEVCONTAINER_PATH/docker-compose.local.yml + +if [ ! -f "$LOCAL_COMPOSE_FILE" ]; then +cat >"$LOCAL_COMPOSE_FILE" < `Preferences: Open user settings (JSON)`). This will set up the dev containers plugin to work with `podman` and `podman-compose` +``` + "dev.containers.dockerComposePath": "podman-compose", + "dev.containers.dockerPath": "podman" +``` +3. Execute `.devcontainer/commands/prepare_containers.sh`. It will create `docker-compose.local.yml` file that will be used to run the container properly. -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. +### Running make commands in a container +There is an option to run make commands inside the `docker-compose` generated environment. Just prefix a make command you would like to run with `podman-`. e.g. to run `make test` in a container, use `make podman-test`. ## Mocking subscription-manager commands `mocked_run.sh` is a wrapper of `go run main.go` to execute the command in mocked context. diff --git a/mocks/create-cert.sh b/mocks/create-cert.sh new file mode 100755 index 0000000..ddeb6fa --- /dev/null +++ b/mocks/create-cert.sh @@ -0,0 +1,20 @@ + +# Execute this script in the directory with test-cert.cnf +# Purpose: Create a self-signed certificate for testing + +if [ -d consumer ]; then + rm -rf consumer +fi + +mkdir consumer + +openssl genrsa -out consumer/key.pem + +# To create a CSR for submitting to CA +# openssl req -new -key test-cert.key -out test-cert.csr -config test-cert.cnf + +# Create a self-signed certificate +openssl req -x509 -new -key consumer/key.pem -out consumer/cert.pem -config mock-cert.cnf + +# Show the certificate +openssl x509 -in consumer/cert.pem -text -noout \ No newline at end of file diff --git a/mocks/mock-cert.cnf b/mocks/mock-cert.cnf new file mode 100644 index 0000000..75449ed --- /dev/null +++ b/mocks/mock-cert.cnf @@ -0,0 +1,16 @@ +[req] +prompt = no +req_extensions = req_ext +distinguished_name = dn + +[ dn ] +C=US +ST=North Carolina +L=Raleigh +O=Milton +OU=Information Technology +emailAddress=test@host-metering.test +CN = test-host.host-metering.test + +[ req_ext ] +subjectAltName = DNS:test-host.host-metering.test \ No newline at end of file diff --git a/mocks/prepare_mock.sh b/mocks/mock_from_host.sh similarity index 100% rename from mocks/prepare_mock.sh rename to mocks/mock_from_host.sh diff --git a/mocks/subscription-manager b/mocks/subscription-manager index d6eed65..0857d50 100755 --- a/mocks/subscription-manager +++ b/mocks/subscription-manager @@ -1,4 +1,192 @@ #!/bin/bash -echo "#######Mocked output#######" -cat $(dirname "$0")/subscription-manager-$@ \ No newline at end of file +# Tries to look for a predefined file, if the file does not exist, it will use hard-coded version + +IDENTITY=\ +"system identity: 01234567-89ab-cdef-0123-456789abcdef +name: hostname +org name: 12345678 +org ID: 12345678" + +USAGE=\ +"Current Usage: Production" + +SERVICE_LEVEL=\ +"Current service level: Premium" + +FACTS_GENERAL=\ +"cpu.core(s)_per_socket: 4 +cpu.cpu(s): 8 +cpu.cpu_socket(s): 3 +cpu.thread(s)_per_core: 2 +cpu.topology_source: kernel /sys cpu sibling lists +distribution.id: Maipo +distribution.name: Red Hat Enterprise Linux Server +distribution.version: 7.9 +distribution.version.modifier: ga +last_boot: 2021-09-05 09:58:46 +lscpu.architecture: x86_64 +lscpu.bogomips: 5990.40 +lscpu.byte_order: Little Endian +lscpu.core(s)_per_socket: 4 +lscpu.cpu(s): 8 +lscpu.cpu_family: 6 +lscpu.cpu_max_mhz: 4800.0000 +lscpu.cpu_mhz: 400.000 +lscpu.cpu_min_mhz: 400.0000 +lscpu.cpu_op-mode(s): 32-bit, 64-bit +lscpu.flags: fpu vme +lscpu.l1d_cache: 48K +lscpu.l1i_cache: 32K +lscpu.l2_cache: 1280K +lscpu.l3_cache: 12288K +lscpu.model: 140 +lscpu.model_name: 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz +lscpu.numa_node(s): 1 +lscpu.numa_node0_cpu(s): 0-7 +lscpu.on-line_cpu(s)_list: 0-7 +lscpu.socket(s): 1 +lscpu.stepping: 1 +lscpu.thread(s)_per_core: 2 +lscpu.vendor_id: GenuineIntel +lscpu.virtualization: VT-x +memory.memtotal: 32550092 +memory.swaptotal: 8388604 +net.interface.lo.ipv4_address: 127.0.0.1 +net.interface.lo.ipv4_address_list: 127.0.0.1 +net.interface.lo.ipv4_broadcast: Unknown +net.interface.lo.ipv4_broadcast_list: Unknown +net.interface.lo.ipv4_netmask: 8 +net.interface.lo.ipv4_netmask_list: 8 +net.interface.lo.ipv6_address.host: ::1 +net.interface.lo.ipv6_address.host_list: ::1 +net.interface.lo.ipv6_netmask.host: 128 +net.interface.lo.ipv6_netmask.host_list: 128 +net.interface.tap0.ipv4_address: 10.0.2.100 +net.interface.tap0.ipv4_address_list: 10.0.2.100 +net.interface.tap0.ipv4_broadcast: 10.0.2.255 +net.interface.tap0.ipv4_broadcast_list: 10.0.2.255 +net.interface.tap0.ipv4_netmask: 24 +net.interface.tap0.ipv4_netmask_list: 24 +net.interface.tap0.ipv6_address.global: fd00::b08b:92ff:fe3f:fc3a +net.interface.tap0.ipv6_address.global_list: fd00::b08b:92ff:fe3f:fc3a +net.interface.tap0.ipv6_address.link: fe80::b08b:92ff:fe3f:fc3a +net.interface.tap0.ipv6_address.link_list: fe80::b08b:92ff:fe3f:fc3a +net.interface.tap0.ipv6_netmask.global: 64 +net.interface.tap0.ipv6_netmask.global_list: 64 +net.interface.tap0.ipv6_netmask.link: 64 +net.interface.tap0.ipv6_netmask.link_list: 64 +net.interface.tap0.mac_address: B2:8B:92:3F:FC:3A +network.fqdn: hostname +network.hostname: hostname +network.ipv4_address: 10.0.2.100 +network.ipv6_address: fd00::b08b:92ff:fe3f:fc3a, fe80::b08b:92ff:fe3f:fc3a +proc_cpuinfo.common.address_sizes: 39 bits physical, 48 bits virtual +proc_cpuinfo.common.bogomips: 5990.40 +proc_cpuinfo.common.bugs: spectre_v1 spectre_v2 spec_store_bypass swapgs eibrs_pbrsb gds +proc_cpuinfo.common.cache_alignment: 64 +proc_cpuinfo.common.cache_size: 12288 KB +proc_cpuinfo.common.clflush_size: 64 +proc_cpuinfo.common.cpu_cores: 4 +proc_cpuinfo.common.cpu_family: 6 +proc_cpuinfo.common.cpuid_level: 27 +proc_cpuinfo.common.flags: fpu +proc_cpuinfo.common.fpu: yes +proc_cpuinfo.common.fpu_exception: yes +proc_cpuinfo.common.microcode: 0xac +proc_cpuinfo.common.model: 140 +proc_cpuinfo.common.model_name: 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz +proc_cpuinfo.common.physical_id: 0 +proc_cpuinfo.common.power_management: Unknown +proc_cpuinfo.common.siblings: 8 +proc_cpuinfo.common.stepping: 1 +proc_cpuinfo.common.vendor_id: GenuineIntel +proc_cpuinfo.common.vmx_flags: vnmi +proc_cpuinfo.common.wp: yes +proc_stat.btime: 1693907926 +system.certificate_version: 3.2 +system.default_locale: Unknown +uname.machine: x86_64 +uname.nodename: hostname +uname.release: 6.4.10-100.fc11.x86_64 +uname.sysname: Linux +uname.version: #1 SMP PREEMPT_DYNAMIC Fri Aug 11 15:18:39 UTC 2021" + +FACTS_AWS=\ +"aws_account_id: 000000000000 +aws_instance_id: 1-11111111111111111" + +FACTS_AZURE=\ +"azure_subscription_id: 00000000-0000-0000-0000-000000000000 +azure_instance_id: 11111111-1111-1111-1111-111111111111" + +FACTS_GCP=\ +"gcp_project_number: 000000000000 +gcp_instance_id: 1111111111111111111" + +show_identity() { + # Show information about the host identity. + echo "${IDENTITY}" +} + +show_service_level() { + # Show the current service level. + echo "${SERVICE_LEVEL}" +} + +show_usage() { + # Show the current usage. + echo "${USAGE}" +} + +show_facts() { + # Print general facts about the host. + echo "${FACTS_GENERAL}" + + # Print cloud facts. Use the CLOUD_PROVIDER environment variable + # to choose a cloud provider to mock or select aws by default. + case "${CLOUD_PROVIDER}" in + azure|Azure|AZURE) + echo "${FACTS_AZURE}" + ;; + gcp|GCP) + echo "${FACTS_GCP}" + ;; + *) + echo "${FACTS_AWS}" + ;; + esac +} + +hard_coded() { + # Handle the specified subscription-manager command. + case "${command:=${1}}" in + identity) + show_identity + ;; + usage) + show_usage + ;; + service-level) + show_service_level + ;; + facts) + show_facts + ;; + *) + echo "This command is not supported." >&2 + exit 1 + ;; + esac +} + + +MOCK_FILE=$(dirname "$0")/subscription-manager-$@ + +if [ -f "$MOCK_FILE" ]; then + echo "####### Mocked output from $MOCK_FILE #######" + cat $MOCK_FILE +else + echo "####### Mocked output from hard coded value #######" + hard_coded "${@}" +fi \ No newline at end of file