Skip to content

Commit

Permalink
Merge pull request #1309 from sdodson/tito-tagging
Browse files Browse the repository at this point in the history
Merged by openshift-bot
  • Loading branch information
OpenShift Bot committed Apr 13, 2015
2 parents e8e2d9b + c6be0f7 commit 7dc33d0
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 18 deletions.
9 changes: 9 additions & 0 deletions HACKING.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,12 @@ both of these changes with message `bump(<pkgname>): <pkgcommit>`.

If you run into difficulties running OpenShift, start by reading through the
[troubleshooting guide](https://github.com/openshift/origin/blob/master/docs/debugging-openshift.md).

## RPM Packaging

A specfile is included in this repo which can be used to produce RPMs including
the openshift binary. While the specfile will be kept up to date with build
requirements the version is not updated. You will need to either update the
Version, %commit, and %ldflags values on your own or you may use
[tito](https://github.com/dgoodwin/tito) to build
and tag releases.
16 changes: 8 additions & 8 deletions openshift.spec
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
%global debug_package %{nil}
%global gopath %{_datadir}/gocode
%global import_path github.com/openshift/origin
# %commit and %ldflags are intended to be set by tito custom builders provided
# in the rel-eng directory. The values in this spec file will not be kept up to date.
%{!?commit:
%global commit 21fb40637c4e3507cca1fcab6c4d56b06950a149
%global commit 86b5e46426ba828f49195af21c56f7c6674b48f7
}
%global shortcommit %(c=%{commit}; echo ${c:0:7})
# OpenShift specific ldflags from hack/common.sh os::build:ldflags
%{!?ldflags:
%global ldflags -X github.com/openshift/origin/pkg/version.majorFromGit 0 -X github.com/openshift/origin/pkg/version.minorFromGit 2+ -X github.com/openshift/origin/pkg/version.versionFromGit v0.2.2-134-gc9e7c25aaf0e61-dirty -X github.com/openshift/origin/pkg/version.commitFromGit c9e7c25 -X github.com/GoogleCloudPlatform/kubernetes/pkg/version.gitCommit 72ad4f1 -X github.com/GoogleCloudPlatform/kubernetes/pkg/version.gitVersion v0.10.0-46-g72ad4f1
%global ldflags -X github.com/openshift/origin/pkg/version.majorFromGit 0 -X github.com/openshift/origin/pkg/version.minorFromGit 0+ -X github.com/openshift/origin/pkg/version.versionFromGit v0.0.1 -X github.com/openshift/origin/pkg/version.commitFromGit 86b5e46 -X github.com/GoogleCloudPlatform/kubernetes/pkg/version.gitCommit 6241a21 -X github.com/GoogleCloudPlatform/kubernetes/pkg/version.gitVersion v0.11.0-330-g6241a21
}
# String used for --images flag
# If you're setting docker_registry make sure it ends in a trailing /
Expand All @@ -23,9 +25,10 @@
%global docker_images %{?docker_registry}%{docker_namespace}/%{docker_prefix}-${component}:${version}

Name: openshift
Version: 0.2.2
#Release: 1git%{shortcommit}%{?dist}
Release: 4%{?dist}
# Version is not kept up to date and is intended to be set by tito custom
# builders provided in the rel-eng directory of this project
Version: 0.0.1
Release: 0%{?dist}
Summary: Open Source Platform as a Service by Red Hat
License: ASL 2.0
URL: https://%{import_path}
Expand Down Expand Up @@ -194,9 +197,6 @@ fi


%changelog
* Fri Feb 06 2015 Scott Dodson <sdodson@redhat.com>
- new package built with tito

* Mon Jan 26 2015 Scott Dodson <sdodson@redhat.com> 0.2-3
- Update to 21fb40637c4e3507cca1fcab6c4d56b06950a149
- Split packaging of openshift-master and openshift-node
Expand Down
25 changes: 24 additions & 1 deletion rel-eng/lib/openshift/builder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Code for building Openshift v3
"""

from tito.common import get_latest_commit, run_command, get_script_path
from tito.common import get_latest_commit, get_latest_tagged_version, check_tag_exists, run_command, get_script_path
from tito.builder import Builder

class OpenshiftBuilder(Builder):
Expand Down Expand Up @@ -54,3 +54,26 @@ def _setup_test_specfile(self):
self.build_version += ".git." + str(self.commit_count) + "." + str(self.git_commit_id[:7])
self.ran_setup_test_specfile = True

def _get_build_version(self):
"""
Figure out the git tag and version-release we're building.
"""
# Determine which package version we should build:
build_version = None
if self.build_tag:
build_version = self.build_tag[len(self.project_name + "-"):]
else:
build_version = get_latest_tagged_version(self.project_name)
if build_version is None:
if not self.test:
error_out(["Unable to lookup latest package info.",
"Perhaps you need to tag first?"])
sys.stderr.write("WARNING: unable to lookup latest package "
"tag, building untagged test project\n")
build_version = get_spec_version_and_release(self.start_dir,
find_spec_file(in_dir=self.start_dir))
self.build_tag = "v%s" % (build_version)

if not self.test:
check_tag_exists(self.build_tag, offline=self.offline)
return build_version
192 changes: 184 additions & 8 deletions rel-eng/lib/openshift/tagger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,55 @@
Code for tagging Openshift v3 packages
"""

from tito.common import get_latest_commit, run_command
import os
import re
import rpm
import shutil
import subprocess
import tempfile
import textwrap
import sys

from tito.common import (get_latest_commit, run_command,
get_latest_tagged_version, increase_version, increase_zstream,
get_spec_version_and_release, tag_exists_locally, tag_exists_remotely,
head_points_to_tag, undo_tag)
#, get_spec_version_and_release
from tito.compat import write
from tito.tagger import VersionTagger
from tito.exception import TitoException


class OpenshiftTagger(VersionTagger):
"""
Tagger which defines a specfile global 'commit' with the git hash at
which the tag was created. This also defines ldflags by importing
hack/common.sh and executing os::build::ldflags Setting %commit isn't
currently used, but seems to be the norm for RPM packaging of golang apps.
OpenShift v3 custom tagger. This tagger has several deviations from normal
the normal tito tagger.
** Rather than versions being tagged %{name}-%{version}-%{release} they're
tagged as v%{version} in order to preserve compatibility with origin build
processes. This means you really should not attempt to use the release field
for anything useful, it should probably always remain zero.
** RPM specfile global commit is updated with the git hash, this may be
relevant and popular with other golang projects, so TODO: submit to tito
upstream.
Requires that your commit global is written on one single line like this:
Requires that your commit is written on one single line as:
%global commit 460abe2a3abe0fa22ac96c551fe71c0fc36f7475
And that your ldflags are similarly on a single line, ie:
** RPM specfile global ldflags is updated with os::build::ldflags as generated
by importing hack/common.sh this absolutely depends on the non standard
version tagging outlined above. This is 100% openshift specific
Requires that your ldflags global is written on one single line like this:
%global ldflags -X foo -X bar
NOTE: Does not work with --use-version as tito does not provide a way to
override the forced version tagger, see
https://github.com/dgoodwin/tito/pull/163
Used For:
- Openshift v3, probably not much else
"""
Expand All @@ -28,18 +61,161 @@ def _tag_release(self):
x.y.z-r+1) and ldflags from hack/common.sh os::build::ldflags
"""
self._make_changelog()
new_version = self._bump_version(release=True)
new_version = self._bump_version()
new_version = re.sub(r"-.*","",new_version)
git_hash = get_latest_commit()
update_commit = "sed -i 's/^%%global commit .*$/%%global commit %s/' %s" % \
(git_hash, self.spec_file)
output = run_command(update_commit)

cmd = '. ./hack/common.sh ; echo $(os::build::ldflags)'
ldflags = run_command('bash -c \'%s\'' % (cmd) )
# hack/common.sh will tell us that the tree is dirty because tito has
# already mucked with things, but lets not consider the tree to be dirty
ldflags = ldflags.replace('-dirty','')
update_ldflags = "sed -i 's|^%%global ldflags .*$|%%global ldflags %s|' %s" % \
(ldflags, self.spec_file)
output = run_command(update_ldflags)

self._check_tag_does_not_exist(self._get_new_tag(new_version))
self._update_changelog(new_version)
self._update_package_metadata(new_version)

def _get_new_tag(self, new_version):
""" Returns the actual tag we'll be creating. """
return "v%s" % (new_version)

def get_latest_tagged_version(package_name):
"""
Return the latest git tag for this package in the current branch.
Uses the info in rel-eng/packages/package-name.
Returns None if file does not exist.
"""
git_root = find_git_root()
rel_eng_dir = os.path.join(git_root, "rel-eng")
file_path = "%s/packages/%s" % (rel_eng_dir, package_name)
debug("Getting latest package info from: %s" % file_path)
if not os.path.exists(file_path):
return None

output = run_command("awk '{ print $1 ; exit }' %s" % file_path)
if output is None or output.strip() == "":
error_out("Error looking up latest tagged version in: %s" % file_path)

return output

def _make_changelog(self):
"""
Create a new changelog entry in the spec, with line items from git
"""
if self._no_auto_changelog:
debug("Skipping changelog generation.")
return

in_f = open(self.spec_file, 'r')
out_f = open(self.spec_file + ".new", 'w')

found_changelog = False
for line in in_f.readlines():
out_f.write(line)

if not found_changelog and line.startswith("%changelog"):
found_changelog = True

old_version = get_latest_tagged_version(self.project_name)

# don't die if this is a new package with no history
if old_version is not None:
last_tag = "v%s" % (old_version)
output = self._generate_default_changelog(last_tag)
else:
output = self._new_changelog_msg

fd, name = tempfile.mkstemp()
write(fd, "# Create your changelog entry below:\n")
if self.git_email is None or (('HIDE_EMAIL' in self.user_config) and
(self.user_config['HIDE_EMAIL'] not in ['0', ''])):
header = "* %s %s\n" % (self.today, self.git_user)
else:
header = "* %s %s <%s>\n" % (self.today, self.git_user,
self.git_email)

write(fd, header)

for cmd_out in output.split("\n"):
write(fd, "- ")
write(fd, "\n ".join(textwrap.wrap(cmd_out, 77)))
write(fd, "\n")

write(fd, "\n")

if not self._accept_auto_changelog:
# Give the user a chance to edit the generated changelog:
editor = 'vi'
if "EDITOR" in os.environ:
editor = os.environ["EDITOR"]
subprocess.call(editor.split() + [name])

os.lseek(fd, 0, 0)
file = os.fdopen(fd)

for line in file.readlines():
if not line.startswith("#"):
out_f.write(line)

output = file.read()

file.close()
os.unlink(name)

if not found_changelog:
print("WARNING: no %changelog section find in spec file. Changelog entry was not appended.")

in_f.close()
out_f.close()

shutil.move(self.spec_file + ".new", self.spec_file)

def _undo(self):
"""
Undo the most recent tag.
Tag commit must be the most recent commit, and the tag must not
exist in the remote git repo, otherwise we report and error out.
"""
tag = "v%s" % (get_latest_tagged_version(self.project_name))
print("Undoing tag: %s" % tag)
if not tag_exists_locally(tag):
raise TitoException(
"Cannot undo tag that does not exist locally.")
if not self.offline and tag_exists_remotely(tag):
raise TitoException("Cannot undo tag that has been pushed.")

# Tag must be the most recent commit.
if not head_points_to_tag(tag):
raise TitoException("Cannot undo if tag is not the most recent commit.")

# Everything looks good:
print
undo_tag(tag)

# This won't do anything until tito supports configuring the forcedversion tagger
# See https://github.com/dgoodwin/tito/pull/163
class OpenshiftForceVersionTagger(OpenshiftTagger):
"""
Tagger which forcibly updates the spec file to a version provided on the
command line by the --use-version option.
TODO: could this be merged into main taggers?
"""

def _tag_release(self):
"""
Tag a new release of the package.
"""
self._make_changelog()
new_version = self._bump_version(force=True)
self._check_tag_does_not_exist(self._get_new_tag(new_version))
self._update_changelog(new_version)
self._update_setup_py(new_version)
self._update_package_metadata(new_version)
1 change: 1 addition & 0 deletions rel-eng/packages/openshift
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.1 ./
1 change: 0 additions & 1 deletion rel-eng/packages/origin

This file was deleted.

1 change: 1 addition & 0 deletions rel-eng/tito.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[buildconfig]
builder = openshift.builder.OpenshiftBuilder
tagger = openshift.tagger.OpenshiftTagger
forceversiontagger = openshift.tagger.OpenshiftForceVersionTagger
changelog_do_not_remove_cherrypick = 0
changelog_format = %s (%ae)
lib_dir = rel-eng/lib

0 comments on commit 7dc33d0

Please sign in to comment.