Skip to content

Commit

Permalink
44 Build Once and Use Git-Tags
Browse files Browse the repository at this point in the history
This removes the .version files and uses git-tags. This also builds canu
inside of the RPM spec file, and then uses the RPM from there.

The spec file will create a canu user.
  • Loading branch information
rustydb committed Jul 18, 2022
1 parent ee60521 commit 137c830
Show file tree
Hide file tree
Showing 19 changed files with 170 additions and 131 deletions.
10 changes: 5 additions & 5 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
Description: What does this change do? Use examples of new options and output changes when possible. If other changes were made list these as well in a list.

PR checklist (you may replace this section):
- [ ] I have run `nox` locally and all tests, linting, and code coverage pass.
- [ ] I have run `nox` locally and all tests, linting, and code coverage pass
- [ ] I have added new tests to cover the new code
- [ ] My code follows the style guidelines of this project
- [ ] If adding a new file, I have updated pyinstaller.py
- [ ] I have updated the appropriate Changelog entries in readme.md
- [ ] I have incremented the version in the readme.md
- [ ] I have incremented the version in `canu/.version` and `.version`
- [ ] If adding a new file, I have updated `pyinstaller.py`
- [ ] I have updated the appropriate Changelog entries in `readme.md`
- [ ] I have incremented the version in the `readme.md`
- [ ] I have incremented the version in `setup.py`

### Issues and Related PRs

Expand Down
1 change: 0 additions & 1 deletion .version

This file was deleted.

23 changes: 5 additions & 18 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,22 @@
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

from artifactory.algol60.net/csm-docker/stable/docker.io/python:slim-bullseye

# create canu user
RUN useradd -ms /bin/bash canu
FROM artifactory.algol60.net/csm-docker/stable/docker.io/python:slim-bullseye

# update command prompt
RUN echo 'export PS1="canu \w : "' >> /etc/bash.bashrc

# make files dir
RUN mkdir /files

# prep image layer for faster builds
COPY requirements.txt /app/canu/

RUN apt-get -yq update && apt-get -yq install gcc openssl jq vim libffi-dev musl-dev \
python3 python3-dev python3-pip

RUN pip3 install --upgrade pip && pip3 install -r /app/canu/requirements.txt

# copy canu files
COPY . /app/canu
COPY dist/rpmbuild/RPMS/x86_64/ /files

# install canu
RUN pip3 install --editable /app/canu/
RUN zypper in -y /files/canu*.rpm

# set file perms for canu
RUN chown -R canu /app/canu /files
RUN chown -R canu /files

# set none root user: canu
USER canu

WORKDIR /files
WORKDIR /files
22 changes: 6 additions & 16 deletions Jenkinsfile.github
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

@Library('csm-shared-library') _

def isStable = env.TAG_NAME != null ? true : false
def pythonVersion = '3.10'
pipeline {
agent {
Expand All @@ -34,17 +35,15 @@ pipeline {
}

environment {
DESCRIPTION = "CANU (CSM Automatic Network Utility) will float through a Shasta network and make switch setup and validation a breeze."
IS_STABLE = getBuildIsStable()
DOCKER_ARGS = getDockerBuildArgs(name: "cray-${getRepoName()}")
GIT_REPO_NAME = getRepoName()
BUILD_METADATA = getRpmRevision(isStable: env.IS_STABLE)
IMAGE_VERSION = getDockerBuildVersion(isStable: env.IS_STABLE)
DOCKER_ARGS = getDockerBuildArgs(name: "cray-${getRepoName()}", description: env.DESCRIPTION)
IMAGE_VERSION = sh(returnStdout: true, script: "git describe --tags | tr -s '-' '_' | tr -d '^v'").trim()
VERSION = sh(returnStdout: true, script: "git describe --tags | tr -s '-' '~' | tr -d '^v'").trim()
}

stages {

stage("Prepare: RPM") {
stage('Prepare: RPM') {
agent {
docker {
image "artifactory.algol60.net/csm-docker/stable/csm-docker-sle-python:${pythonVersion}"
Expand All @@ -61,16 +60,7 @@ pipeline {
}
}

stage("Build: Binary") {
steps {
script {
sh "make binary"
}
}
}


stage("Build: RPMs") {
stage('Build: RPMs') {
agent {
docker {
image "artifactory.algol60.net/csm-docker/stable/csm-docker-sle-python:${pythonVersion}"
Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include readme.md
33 changes: 14 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,41 @@
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
name ?= ${GIT_REPO_NAME}
NAME ?= ${GIT_REPO_NAME}

version ?= $(shell cat .version)
build_image := cdrx/pyinstaller-linux:python3
ifeq ($(VERSION),)
VERSION := $(shell git describe --tags | tr -s '-' '~' | tr -d '^v')
endif

# Default release if not set
BUILD_METADATA ?= "1~development~$(shell git rev-parse --short HEAD)"
SPEC_FILE := ${NAME}.spec
SOURCE_NAME := ${NAME}-${VERSION}

spec_file := ${name}.spec
source_name := ${name}-${version}

build_dir := $(PWD)/dist/rpmbuild
source_path := ${build_dir}/SOURCES/${source_name}.tar.bz2
BUILD_DIR := $(PWD)/dist/rpmbuild
SOURCE_PATH := ${BUILD_DIR}/SOURCES/${SOURCE_NAME}.tar.bz2
IMAGE_VERSION ?= ${IMAGE_VERSION}

all : prepare binary test rpm
rpm: rpm_package_source rpm_build_source rpm_build

prepare:
rm -rf dist
mkdir -p $(build_dir)/SPECS $(build_dir)/SOURCES
cp $(spec_file) $(build_dir)/SPECS/

binary:
docker run --rm -v $(PWD):/src $(build_image) ./pyinstaller.sh
mkdir -p $(BUILD_DIR)/SPECS $(BUILD_DIR)/SOURCES
cp $(SPEC_FILE) $(BUILD_DIR)/SPECS/

test:
docker run --rm -v $(PWD):/src $(build_image) nox

image:
docker build --no-cache --pull ${DOCKER_ARGS} --tag 'cray-${name}:${IMAGE_VERSION}' .
docker build --no-cache --pull ${DOCKER_ARGS} --tag 'cray-${NAME}:${IMAGE_VERSION}' .

snyk:
$(MAKE) -s image | xargs --verbose -n 1 snyk container test

rpm_package_source:
tar --transform 'flags=r;s,^,/$(source_name)/,' --exclude .git --exclude .nox --exclude dist/rpmbuild -cvjf $(source_path) .
tar --transform 'flags=r;s,^,/$(SOURCE_NAME)/,' --exclude .git --exclude .nox --exclude dist/rpmbuild -cvjf $(SOURCE_PATH) .

rpm_build_source:
BUILD_METADATA=$(BUILD_METADATA) rpmbuild -ts $(source_path) --define "_topdir $(build_dir)"
rpmbuild -ts $(SOURCE_PATH) --define "_topdir $(BUILD_DIR)"

rpm_build:
BUILD_METADATA=$(BUILD_METADATA) rpmbuild -ba $(spec_file) --define "_topdir $(build_dir)"
rpmbuild -ba $(SPEC_FILE) --define "_topdir $(BUILD_DIR)"
31 changes: 24 additions & 7 deletions canu.spec
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
%global __python /usr/local/bin/python3.10
%define __pyinstaller /home/jenkins/.local/bin/pyinstaller

Name: canu
BuildArch: x86_64
License: MIT License
Summary: CSM Automatic Network Utility
Version: %(cat .version)
Release: %(echo ${BUILD_METADATA})
Version: %(echo $VERSION)
Release: 1
Source: %{name}-%{version}.tar.bz2
Vendor: Cray Inc.
Group: Metal
Vendor: Cray HPE

%description
CSM Automatic Network Utility
%{summary}

%prep
%setup -q

%build
# PIP and Setuptools updates.
%{__python} -m pip install -U pyinstaller

# Build the wheel.
%{__python} -m pip install -q build
%{__python} -m build

%install
mv pyinstaller.py pyinstaller.spec
%{__pyinstaller} pyinstaller.spec

mkdir -p %{buildroot}%{_bindir}
install -m 755 dist/linux/canu %{buildroot}%{_bindir}/canu
install -m 755 dist/canu %{buildroot}%{_bindir}/canu

%pre
useradd -ms /bin/bash canu

%preun
userdel canu

%files
%{_bindir}/canu
%attr(755, canu, canu) %{_bindir}/canu
%license LICENSE

%changelog
1 change: 0 additions & 1 deletion canu/.version

This file was deleted.

8 changes: 3 additions & 5 deletions canu/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import sys

import click
import pkg_resources
from click_help_colors import HelpColorsCommand, HelpColorsGroup
import requests
from ruamel.yaml import YAML
Expand All @@ -52,12 +53,9 @@
else:
parent_directory = path.abspath(path.dirname(path.dirname(__file__)))

canu_config_file = path.join(parent_directory, "canu", "canu.yaml")
canu_version_file = path.join(parent_directory, "canu", ".version")

with open(canu_version_file, "r") as version_file:
version = version_file.read().replace("\n", "")
canu_version = pkg_resources.get_distribution('canu').version

canu_config_file = path.join(parent_directory, "canu", "canu.yaml")
with open(canu_config_file, "r") as canu_f:
canu_config = yaml.load(canu_f)

Expand Down
7 changes: 2 additions & 5 deletions canu/generate/switch/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import sys

import click
import pkg_resources
from click_help_colors import HelpColorsCommand
from click_option_group import optgroup, RequiredMutuallyExclusiveOptionGroup
from hier_config import HConfig, Host
Expand Down Expand Up @@ -68,7 +69,6 @@
# Schema and Data files
canu_cache_file = path.join(cache_directory(), "canu_cache.yaml")
canu_config_file = path.join(project_root, "canu", "canu.yaml")
canu_version_file = path.join(project_root, "canu", ".version")

# ttp preserve templates
# pulls the interface and lag from switch configs.
Expand Down Expand Up @@ -112,10 +112,7 @@

csm_options = canu_config["csm_versions"]

# Get CANU version from .version
with open(canu_version_file, "r") as file:
canu_version = file.readline()
canu_version = canu_version.strip()
canu_version = pkg_resources.get_distribution('canu').version

dash = "-" * 60

Expand Down
9 changes: 2 additions & 7 deletions canu/utils/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import tempfile

import click
import pkg_resources
from ruamel.yaml import YAML

yaml = YAML()
Expand Down Expand Up @@ -66,27 +67,21 @@ def cache_directory():
project_root = Path(__file__).resolve().parent.parent.parent

canu_cache_file = path.join(cache_directory(), "canu_cache.yaml")
canu_version_file = path.join(project_root, "canu", ".version")

version = pkg_resources.get_distribution('canu').version
file_exists = path.isfile(canu_cache_file)

# Open the Cache file, and generate it if it does not exist
if file_exists: # pragma: no cover
with open(canu_cache_file, "r+") as canu_exist_f:
canu_cache = yaml.load(canu_exist_f)
if canu_cache is None:
with open(canu_version_file, "r") as version_file:
version = version_file.read().replace("\n", "")

with open(canu_cache_file, "w+") as f:
f.write(f"version: {version}\n")
f.write("switches:\n")

with open(canu_cache_file, "r+") as canu_f:
canu_cache = yaml.load(canu_f)
else: # pragma: no cover
with open(canu_version_file, "r") as version_f:
version = version_f.read().replace("\n", "")

with open(canu_cache_file, "w+") as new_cache_f:
new_cache_f.write(f"version: {version}\n")
Expand Down
6 changes: 2 additions & 4 deletions canu/validate/shcd/shcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import sys

import click
import pkg_resources
from click_help_colors import HelpColorsCommand
import natsort
from network_modeling.NetworkNodeFactory import NetworkNodeFactory
Expand All @@ -45,10 +46,7 @@
prog = __file__
project_root = Path(__file__).resolve().parent.parent.parent.parent

canu_version_file = path.join(project_root, "canu", ".version")

with open(canu_version_file, "r") as version_file:
version = version_file.read().replace("\n", "")
version = pkg_resources.get_distribution('canu').version

log = logging.getLogger("validate_shcd")

Expand Down
1 change: 0 additions & 1 deletion pyinstaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
block_cipher = None

added_files = [
("canu/.version", "canu"),
("canu/canu.yaml", "canu"),
("canu/validate/switch/config/*.yaml", "canu/validate/switch/config"),
("network_modeling/models/*", "network_modeling/models"),
Expand Down
Loading

0 comments on commit 137c830

Please sign in to comment.