Package: gocd-cli
For work I and colleagues have for the last nine months been writing a lot of
different small shell scripts with curl that interacts with the Go API.Most of them are quick n’ dirty scripts that aren’t very robust, and because
they’re written in the mindset of the now we just end up copy+pasting it
everywhere. As well as figuring out what API endpoints are available, how they
work and so on.The goal of this project is to make these and similar tasks super simple,
without having to write a so-so reliable bash script. And for the most common
things just one invocation.NoteThis is still early in the development and a bit rough around the edges.
Any bug reports, feature suggestions, etc are greatly appreciated. :)I’m planning to add support for all the API endpoints that make sense from a CLI
perspective. And also to handle some of the scenarios where we ended up writing
shell scripts.Installation and usageInstallationSince this is a Python package available on PyPi you can install it like
any other Python package.# on modern systems with Python you can install with pip$pipinstallgocd-cli# on older systems you can install using easy_install$easy_installgocd-cliUsageThe commands should be mostly self-documenting in how they are defined,
which is made available through thehelpcommand.$gocdusage:gocd<command><subcommand>[<posarg1>,...][--kwarg1=value,...]Commands:pipelinecheck:Checkwhetherapipelinehasrunsuccessfullycheck-all:Checksallpipelinestobegreen/non-stalledlist:Listsallpipelineswiththeircurrentstatuspause:Pausesthenamedpipelineretrigger-failed:Retriggerapipeline/stagethathasfailedtrigger:Triggersthenamedpipelineunlock:Unlocksthenamedpipelineifit's currently locked
      unpause: Unpauses the named pipeline

$ gocd help pipeline retrigger-failed
retrigger-failed <name> [--counter] [--stage] [--retrigger]

Retrigger a pipeline/stage that has failed

Flags:
   counter: the pipeline counter to check. Default: latest
   stage: if given the pipeline will only be retriggered if
     this stage failed
   retrigger: possible values (pipeline, stage) default pipeline.
     When pipeline and there'safailedstageretriggersthepipeline.Whenstageandthere'safailureretriggersonlythatstage.$gocdpipelineretrigger-failedIntegration--stageexternal-points--retriggerstageConfigurationThis script has been prepared to be run two situations:From your local machineFrom inside of GoBecause of this the configuration is handled by a config file and
it can be overridden by environment variables.The current options are:server:The server to connect to, example:http://go.example.com:8153/user:The user to login aspassword:The corresponding passwordThe configuration file is stored in~/.gocd/gocd-cli.cfgand is an ini file.
Example:[gocd]server=http://localhost:8153/user=adminpassword=badgerThe environment variables are prefixed withGOCD_and always ALL CAPS.
Example:GOCD_SERVER=http://loaclhost:8153/GOCD_USER=adminGOCD_PASSWORD=badgerEncrypted configuration keys>From version 0.9 there’s support for encrypted configuration keys.
There’s a builtin module for Rot13, orCaesar cipher, as well as
a standalone module usingblowfishcalledgocd-cli.encryption.blowfish.This feature was added to handle a very specific use case, where the
password to the Go server was not allowed to be stored in plaintext. But
it was okay if the decryption key was stored on the same machine. The
builtin implementation is to be seen as a reference implementation and
not to be used. But then again, if you just need it to not be plaintext…UsageTo then encrypt the current plaintext password do:$gocdsettingsencrypt--keypasswordencryption_module=gocd_cli.encryption.caesarpassword_encrypted=fhcrefrpergCopy these two values into your~/.gocd/gocd-cli.cfgfile and remove
the oldpasswordand next time it’ll use the encrypted password instead.To decrypt:$gocdsettingsdecrypt--keypasswordencryption_module=gocd_cli.encryption.caesarpassword=supersecretWriting your own commandsThis project usesnamespaced packageswhich means that you as a
plugin/command author will extend the official namespace with your
commands.There are several advantages to this:The CLI can dynamically be updated with new commands, just
install a Python package to get it integratedInternal/private commands can easily be used side-by-side with public
commands, no need to maintain a fork for your personal commandsLow entry to making your own commandsThe way the cli searches for commands is quite straightforward:The first argument is the package the command belongs toThe second argument is the class to callAny unnamed parameters are passed in the same order as on the cliAny--parametersgets the dashes stripped and sent as keyword argumentsTo make it work this way there’s a pattern to keep to. For each package the__init__.pyfile will have to provide all the subcommands in the__all__variable. Each command is a class and it’s the name of those classes that are in
the__all__variable. There is an examplegocd-cli.commands.echowhich only does the bare minimum to show how all this works.The subcommands will on the command line be divided by dashes, meaning thatRetriggerFailedwill becomeretrigger-failedon the command line.$gocd<command><subcommand>posarg1--kwarg1# or how it's referred to in code$gocd<package><commandclass>posarg1--kwarg1# or when used$gocdpipelineretrigger-failedSimple-with-lock--stage=firstStage\--retrigger=stageCalling help for a command or subcommand will list all available commands, for
more information about each command ask for help on each in turn.