-
Notifications
You must be signed in to change notification settings - Fork 813
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
Closed
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
924306a
Basic Teamcity agent - only supports guest authentication, default br…
692d9e2
* Correcting usage of is_deployment to support string parsing to bool
e924765
Falling back to simple string check as a number of python libraries a…
b63ca00
Fixing problem with appending tags.
b262f20
Fixing string formatting syntax for Python 2.6 compatibility.
64bd008
Missed one spot for string formatting corrections.
d2e0beb
Missed another spot. Sigh mornings.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
Could you replace it with this syntax please ?