edx/gomatic

Name: gomatic

Owner: edX

Description: A Python API for configuring GoCD

Forked from: gocd-contrib/gomatic

Created: 2016-05-31 17:30:47.0

Updated: 2017-03-10 18:19:05.0

Pushed: 2017-06-16 20:51:42.0

Homepage:

Size: 330

Language: Python

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Gomatic

This is a Python API for configuring ThoughtWorks GoCD.

What does it do?

If you wanted to configure a pipeline something like that shown in the GoCD documentation then you could run the following script:

sr/bin/env python
 gomatic import *

igurator = GoCdConfigurator(HostRestClient("localhost:8153"))
line = configurator \
.ensure_pipeline_group("Group") \
.ensure_replacement_of_pipeline("first_pipeline") \
.set_git_url("http://git.url")
e = pipeline.ensure_stage("a_stage")
= stage.ensure_job("a_job")
add_task(ExecTask(['thing']))

igurator.save_updated_config()
How does it work?

Gomatic uses the same mechanism as editing the config XML through the GoCD web based UI. The GoCdConfigurator gets the current config XML from the GoCD server, re-writes it as a result of the methods called, then posts the re-written config XML back to the GoCD server when save_updated_config is called.

Gomatic doesn't use the RESTful Go API because that is (currently) far too limited for our needs.

Limitations

We wrote it for our purposes and find it very useful; however, the current version has limitations (e.g. only really supports “Custom Command” task type) and allows you to try to configure GoCD incorrectly (which GoCD will refuse to allow). We will continue to work on it and will address its current limitations.

We believe it works for the following versions (as indicated by integration_test.py):

Install

We've written it using Python 2.7 (for the moment - should be simple to port to Python 3 - which we might do in the future). You can install it using “pip”:

sudo pip install gomatic

which will install the gomatic package.

Usage

We won't document all of the options. Most of the behaviour is covered by unit tests, so look at them.

Dry run

You can see what effect Gomatic will have on the config XML by using configurator.save_updated_config(save_config_locally=True, dry_run=True). If you have kdiff3 installed, Gomatic will open it showing the diff (if there is a difference) between the config XML before and after the changes made by the GoCdConfigurator. If you don't have kdiff3 installed, use a diff tool of your choice to diff the files config-before.xml vs config-after.xml.

Reverse engineering of existing pipeline

If you have already set up a pipeline through the UI and now want to retrospectively write a script to do the equivalent, you can get Gomatic to show you the script to create an existing pipeline:

python -m gomatic.go_cd_configurator -s <GoCD server hostname> -p <pipeline name>

This mechanism can also be useful if you can't work out how to script something; you just make the change you want through the GoCD web based UI and then reverse engineer to see how to do it using Gomatic. Bear in mind that Gomatic does not currently support every configuration option available in GoCD, so it might not be possible to do everything you want to do.

Gotchas
Developing Gomatic

You need to install Python's virtualenv tool and create a virtual environment (once):

pip install virtualenv

Then, to create the virtual environment, either:

Or:

Then, if you are using IntelliJ IDEA:

  1. File -> Project Structure -> Project SDK
  2. New -> Python SDK -> Add local
  3. select .../gomatic/venv/bin/python
Run the tests

Unit tests:

  1. ./build.sh

Integration tests (takes a long time to download many versions of GoCD) (requires docker to be installed in order to run):

  1. python -m unittest tests.integration_test
Contributing to Gomatic via pull request

To have the best chance of your pull request being merged, please:

  1. Include tests for any new functionality
  2. Separate out different changes into different pull requests
  3. Don't delete any existing tests unless absolutely necessary
  4. Don't change too much in one pull request
CI and releasing packages to pypi

Gomatic uses travis-ci to run the unit tests and deploy to pypi. Only tagged commits are deployed to pypi.

  1. update value of version in setup.py
  2. git tag -a v<version> -m 'version <version>' (e.g. git tag -a v0.3.10 -m 'version 0.3.10')
  3. git push origin --tags

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.