Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic Teamcity agent - only supports guest authentication, default branch #1171

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions checks.d/teamcity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# stdlib
import requests
import time

# project
from checks import AgentCheck


class TeamCity(AgentCheck):
headers = {'Accept': 'application/json'}
server = None

def __init__(self, name, init_config, agentConfig):
AgentCheck.__init__(self, name, init_config, agentConfig)
self.last_build_ids = {}
if self.init_config.get("server") is None:
raise Exception("You must specify a server in teamcity.yaml")
self.server = self.init_config["server"]

def _initialize_if_required(self, instance_name, build_configuration):
if self.last_build_ids.get(instance_name, None) is None:
self.log.info("Initializing {}".format(instance_name))
request = requests.get(
"http://{}/guestAuth/app/rest/builds/?locator=buildType:{},count:1".format(self.server,
build_configuration),
timeout=30, headers=self.headers)
if request.status_code != requests.codes.ok:
raise Exception("TeamCity reported error on initialization. Status code: {}".format(request.status_code))
last_build_id = request.json()["build"][0]["id"]
self.log.info("Last build id for {} is {}.".format(instance_name, last_build_id))
self.last_build_ids[instance_name] = last_build_id

def _build_and_send_event(self, new_build, instance_name, is_deployment, host, tags):
self.log.info("Found new build with id {}, saving and alerting.".format(new_build["id"]))
self.last_build_ids[instance_name] = new_build["id"]

output = {
"timestamp": int(time.time()),
"alert_type": "info",
"tags": []
}
if is_deployment:
output["event_type"] = "deployment"
output["msg_title"] = "{} deployed to {}".format(instance_name, host)
output["msg_text"] = "Build Number: {}\n\nMore Info: {}".format(new_build["number"], new_build["webUrl"])
output["tags"].append("deployment")
else:
output["event_type"] = "build"
output["msg_title"] = "Build for {} successful".format(instance_name)
output["msg_text"] = "Build Number: {}\nDeployed To: {}\n\nMore Info: {}".format(new_build["number"], host,
new_build["webUrl"])
output["tags"].append("build")

if tags is not None:
output["tags"].extend(tags)
if host is not None:
output["host"] = host

self.event(output)

def check(self, instance):
instance_name = instance.get("name")
if instance_name is None:
raise Exception("Each instance must have a name")
build_configuration = instance.get("build_configuration")
if build_configuration is None:
raise Exception("Each instance must have a build configuration")
host = instance.get("host_affected")
tags = instance.get("tags")
is_deployment = instance.get("is_deployment")
if type(is_deployment) is not bool:
is_deployment = instance.get("is_deployment").lower() == "true"

self._initialize_if_required(instance_name, build_configuration)

request = requests.get(
"http://{}/guestAuth/app/rest/builds/?locator=buildType:{},sinceBuild:id:{},status:SUCCESS".format(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This string formatting syntax is not compatible with python 2.6

Python 2.6.9 (unknown, Sep  9 2014, 15:05:12)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = "{}".format("hello")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: zero length field name in format

Could you replace it with this syntax please ?

Python 2.6.9 (unknown, Sep  9 2014, 15:05:12)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = "{0}".format("hello")

self.server, build_configuration, self.last_build_ids[instance_name]), timeout=30,
headers=self.headers)

if request.status_code != requests.codes.ok:
raise Exception("TeamCity reported error on check. Status code: {}".format(request.status_code))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here.


new_builds = request.json()

if new_builds["count"] == 0:
self.log.info("No new builds found.")
else:
self._build_and_send_event(new_builds["build"][0], instance_name, is_deployment, host, tags)

29 changes: 29 additions & 0 deletions conf.d/teamcity.yaml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
init_config:

#
# Specify the server name of your teamcity instance here
#
# server: teamcity.mycompany.com

instances:
# - name: My Website
# build_configuration: MyWebsite_Deploy

# This is the internal build ID of the build configuration you wish to track.
# You can find it labelled as "Build configuration ID" when editing the configuration in question.

# host_affected: msicalweb6

# Optional, if you wish to override the host that is affected by this build configuration.
# Defaults to the host that the agent is running on.

# is_deployment: true

# Optional, this changes the event message slightly to specify that TeamCity was used to deploy something
# rather than just that a successful build happened

# tags:
# - test

# Optional, any additional tags you'd like to add to the event