2013-04-12 6 views
0

Je suis en train de développer un script python pour exécuter la procédure REST de sauvegarde comme indiqué dans http://confluence.jetbrains.com/display/TW/REST+API+Plugin#RESTAPIPlugin-DataBackupPython REST/backup TeamCity

voici mon code:

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

import urllib 
import urllib2 

""" 
Data Backup 
+++++++++++ 
Start backup: POST http://teamcity:8111/httpAuth/app/rest/ 
server/backup?includeConfigs=true&includeDatabase=true& 
includeBuildLogs=true&fileName=<fileName> 
where <fileName> is the prefix of the file to save backup to. 
The file will be created in the default backup directory (see more). 
""" 

url = "http://localhost/httpAuth/app/rest/server/backup" 

# === EDITED code = now working (see my answer below) === 
url = "http://localhost/httpAuth/app/rest/server/backup?includeConfigs=true&includeDatabase=true& 
includeBuildLogs=true&fileName=TCBACKUP" 
# === /EDITED code (see my answer below) === 

params = { 
'fileName':'TCBACKUP', 
'includeBuildLogs':'false', 
'includeDatabase':'true', 
'includeConfigs':'true' 
} 

post_data = urllib.urlencode(params) 

req = urllib2.Request(url, post_data, headers={'Content-Type': 'application/xml'}) 

password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm() 
password_manager.add_password(None, url, 'user', 'pass') 

auth_manager = urllib2.HTTPBasicAuthHandler(password_manager) 
opener = urllib2.build_opener(auth_manager) 

urllib2.install_opener(opener) 

handler = urllib2.urlopen(req) 
handler.close() 

ne peut pas comprendre pourquoi il échoue avec

urllib2.HTTPError: HTTP Error 400: Bad Request 

et REST journaux montrent

WARN [hon-urllib/2.7 ] - est.jersey.ExceptionMapperUtil - 
Error 'Invalid request. Please check the request URL and 
data are correct.' for request http://server/app/rest/server/backup. 
Sending Bad Request error in response: jetbrains.buildServer.server.rest.errors.BadRequestException: 
No target file name specified. 

Aucun nom de fichier cible? Vraiment? impression post_data me donne:

includeConfigs=true&includeDatabase=true&includeBuildLogs=false&fileName=TCBACKUP 

tous les pointeurs seraient grandement appréciés

Répondre

0

code Python était bon - la seule chose qui n'a pas été tout à fait claire de TC docs - l'URL doit encore être construit comme

"POST http://teamcity:8111/httpAuth/app/rest/server/backup? 
includeConfigs=true&includeDatabase=true&includeBuildLogs=true& 
fileName=backup" 

même si les paramètres POST urllib2 sont passés à la demande.

oh bien