Skip to content

Commit

Permalink
Merge pull request #3185 from akutz/feature/dockerfile-for-generating…
Browse files Browse the repository at this point in the history
…-types

chore: Dockerfile & steps for generating types
  • Loading branch information
akutz authored Jul 21, 2023
2 parents 7254d72 + c460f70 commit cd0cb63
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ doc: install
doc: ## Generates govc USAGE.md
./govc/usage.sh > ./govc/USAGE.md

.PHONY: generate-types
generate-types: ## Generate the types
$(MAKE) -C ./gen/ $@


## --------------------------------------
## Tests
Expand Down
1 change: 1 addition & 0 deletions gen/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
sdk/
rbvmomi/
.Gemfile.lock.tmp
76 changes: 76 additions & 0 deletions gen/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
## The version of Go from which this image is based.
ARG GO_VERSION=1.20.6

## Docker image used as base of this image.
FROM --platform=${BUILDPLATFORM} golang:${GO_VERSION}


## --------------------------------------
## Multi-platform support
## --------------------------------------

ARG TARGETOS
ARG TARGETARCH


## --------------------------------------
## Environment variables
## --------------------------------------

ENV GOOS=${TARGETOS}
ENV GOARCH=${TARGETARCH}


## --------------------------------------
## Update the apt cache & essentials
## --------------------------------------
RUN apt-get update && \
apt-get install -y build-essential curl


## --------------------------------------
## Install the version of openssl
## required by ruby 2.x, which is
## required to generate the types
## --------------------------------------
RUN mkdir -p /opt/src /opt/lib && \
curl -sSL https://www.openssl.org/source/openssl-1.1.1g.tar.gz | \
tar -C /opt/src -xz && \
cd /opt/src/openssl-1.1.1g && \
./config --prefix=/opt/lib/openssl-1.1.1g \
--openssldir=/opt/lib/openssl-1.1.1g && \
make && \
make install && \
rm -fr /opt/lib/openssl-1.1.1g/certs && \
ln -s /etc/ssl/certs /opt/lib/openssl-1.1.1g/certs


## --------------------------------------
## Install Ruby & Bundler
## --------------------------------------

ENV PATH="/root/.rbenv/shims:${PATH}" \
RUBY_CONFIGURE_OPTS="--with-openssl-dir=/opt/lib/openssl-1.1.1g"

RUN apt-get install -y rbenv && \
rbenv install 2.7.6 && \
rbenv rehash && \
rbenv global 2.7.6 && \
gem install bundler


## --------------------------------------
## Configure the working directory
## --------------------------------------

WORKDIR /govmomi/gen


## --------------------------------------
## Cache the gen program dependencies
## --------------------------------------

COPY Gemfile Gemfile.lock .
RUN go install golang.org/x/tools/cmd/goimports@latest && \
bundle update --bundler && \
bundle install
58 changes: 58 additions & 0 deletions gen/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright (c) 2023 VMware, Inc. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

# If you update this file, please follow
# https://www.thapaliya.com/en/writings/well-documented-makefiles/

# Ensure Make is run with bash shell as some syntax below is bash-specific
SHELL := /usr/bin/env bash

# Print the help/usage when make is executed without any other arguments
.DEFAULT_GOAL := help


## --------------------------------------
## Help
## --------------------------------------

.PHONY: help
help: ## Display usage
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make [target] \033[36m\033[0m\n\nTargets:\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-20s\033[0m %s\n", $$1, $$2 }' $(MAKEFILE_LIST)


## --------------------------------------
## Image
## --------------------------------------

IMAGE_NAME ?= govmomi-gen-types
IMAGE_TAG ?= latest
IMAGE ?= $(IMAGE_NAME):$(IMAGE_TAG)

.PHONY: image-build
image-build: ## Build the image for generating types
docker build -t $(IMAGE) .


## --------------------------------------
## Generate
## --------------------------------------

ABS_PATH_PARENT_DIR := $(abspath $(dir $(shell pwd)))

#
# Please note the use of the .Gemfile.lock.tmp file below. This is to prevent
# the container from modifying the local copy of the Gemfile.lock that would
# otherwise be bind mounted into the container courtesy of the first bind mount.
#

.PHONY: generate-types
generate-types: image-build
generate-types: ## Generate the types
@cp -f Gemfile.lock .Gemfile.lock.tmp
docker run -it --rm \
-v $(ABS_PATH_PARENT_DIR):/govmomi \
-v $(ABS_PATH_PARENT_DIR)/gen/.Gemfile.lock.tmp:/govmomi/gen/Gemfile.lock \
$(IMAGE) \
/bin/bash -c 'bundle update --bundler && bundle install && ./gen.sh'


27 changes: 27 additions & 0 deletions gen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generating the types

This document describes how a VMware engineer can generate the vim types from internal builds:

## Requirements

* Docker

## Steps

1. Find the desired build of `vcenter-all`.
2. Click on the `vsphere-h5client` dependency.
3. Click on the `vimbase` dependency.
4. Download the published deliverable `wsdl.zip` and inflate it into the directory `./gen/sdk`.
5. Navigate back to the `vsphere-h5client` dependency.
6. Click on the `eam-vcenter` dependency.
7. Download the published deliverable `eam-wsdl.zip` and copy its `eam-messagetypes.xsd` and `eam-types.xsd` files into the directory `./gen/sdk`.
8. Open a terminal window.
9. Run `make generate-types`.

---

:warning: **Please note**

This can take a while the first time because it has to build the image used to generate the types, and building the image includes building OpenSSL and Ruby2, since the GoVmomi generator requires Ruby2, and Ruby2 requires an older version of OpenSSL than is available on recent container images.

---
32 changes: 31 additions & 1 deletion gen/gen.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

# Copyright (c) 2014-2022 VMware, Inc. All Rights Reserved.
# Copyright (c) 2014-2023 VMware, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,14 @@

set -e

ensure_rb_vmodl() {
mkdir -p ./rbvmomi
[ -f ./rbvmomi/vmodl.db ] || \
curl -sSLo \
./rbvmomi/vmodl.db \
https://github.com/vmware-archive/rbvmomi/raw/master/vmodl.db
}

generate() {
dst="$1"
wsdl="$2"
Expand Down Expand Up @@ -47,6 +55,28 @@ generate() {
done
}

update_vim_version() {
sed -i'.bak' -e 's~^[[:blank:]]\{1,\}Version[[:blank:]]\{1,\}=.\{1,\}$~Version = '"\"${1}\""'~g' ../vim25/client.go
rm -f ../vim25/client.go.bak
go fmt ../vim25/client.go
}

#
# Make sure the vmodl.db file exists.
#
ensure_rb_vmodl

#
# The VIM API version used by the vim25 client.
#
VIM_VERSION="${VIM_VERSION:-8.0.0.1}"

#
# Update the vim25 client's VIM version.
#
update_vim_version "${VIM_VERSION}"


#
# All types derive from vSphere 8.0 GA, vcenter-all build 20519528.
#
Expand Down

0 comments on commit cd0cb63

Please sign in to comment.