#! /usr/bin/env python
#
# This sample shows how to create a new project that matches an existing
# template with optional updates to properties. Properties may be added,
# or inherited property values overridden.
#
from xmlrpclib import ServerProxy, Error
import sys
#
# These values must be edited to match your installation.
# You may also want to prompt for the password to avoid saving it in this file.
#
PULSE_URL = "http:
PULSE_USER = "admin"
PULSE_PASSWORD = "admin"
KEY_SYMBOLIC_NAME = "meta.symbolicName"
KEY_NAME = "name"
KEY_VALUE = "value"
TYPE_PROJECT = "zutubi.projectConfig"
TYPE_PROPERTY = "zutubi.resourceProperty"
def fatal(message):
print >> sys.stderr, message
sys.exit(1)
def add_project(server, token, template, name):
project = {KEY_SYMBOLIC_NAME: TYPE_PROJECT, KEY_NAME: name}
return server.RemoteApi.insertTemplatedConfig(token, "projects/" + template, project, False)
def parse_property(arg):
parts = arg.split("=")
if len(parts) != 2:
raise Error("Invalid property spec '" + arg + "'")
return parts
def parse_properties(args):
return [parse_property(arg) for arg in args];
def set_properties(server, token, project_path, specs):
for spec in specs:
properties_path = project_path + "/properties"
property_path = properties_path + "/" + spec[0]
if server.RemoteApi.configPathExists(token, property_path):
existing_property = server.RemoteApi.getConfig(token, property_path)
if existing_property[KEY_VALUE] != spec[1]:
existing_property[KEY_VALUE] = spec[1]
server.RemoteApi.saveConfig(token, property_path, existing_property, True)
else:
property = {KEY_SYMBOLIC_NAME: TYPE_PROPERTY, KEY_NAME: spec[0], KEY_VALUE: spec[1]}
server.RemoteApi.insertConfig(token, properties_path, property)
if __name__ == "__main__":
if len(sys.argv) < 3:
fatal("Usage: %s <template project> <new project> [<property name>=<value> ...]" % sys.argv[0])
specs = parse_properties(sys.argv[3:])
server = ServerProxy(PULSE_URL)
token = server.RemoteApi.login(PULSE_USER, PULSE_PASSWORD)
try:
project_path = add_project(server, token, sys.argv[1], sys.argv[2])
set_properties(server, token, project_path, specs)
except Error, e:
fatal("Error: %s" % e)
finally:
server.RemoteApi.logout(token)