def get_cursor(connection=None): if connection is None: connection = psycopg2.connect( host=cfg('pghost', 'http://api-postgres'), port=cfg('pgport', '5433'), dbname=cfg('pgdatabase', 'herd'), user=cfg('pguser', None), password=cfg('pgpassword', None), ) return connection.cursor(cursor_factory=PoliteCursor)
def m2_get_cursor(): connection = psycopg2.connect( # Model version 2 cursor is configured with dashes in keys host=cfg( 'pg-host', 'herd-postgres'), port=cfg( 'pg-port', '5433'), dbname=cfg( 'pg-database', 'herd'), user=cfg( 'pg-user', 'herd_user'), password=cfg('pg-password', None), ) return connection.cursor(cursor_factory=PoliteCursor)
def k8s_endpoint(resource): """ return the endpoint for the given resource type """ endpoint = "http://{}/api/v1/namespaces/default/{}".format( cfg('kubeproxy'), resource, ) return endpoint
def idem_post(resource, description): """ idempotently post a resource to k8s """ endpoint = k8s_endpoint(resource) response = requests.post( endpoint, json=description, verify="/secret/k8s.pem", auth=('admin', cfg("k8spassword")), ) return response
def idem_post(resource, description): """ idempotently post a resource to k8s """ endpoint = k8s_endpoint(resource) print("posting {} request".format(endpoint)) pp.pprint(description) response = requests.post( endpoint, json=description, verify="/secret/k8s.pem", auth=('admin', cfg("k8spassword")), ) print("return:") pp.pprint(response.text) return response
def gc_repcons(service_name, branch_name, environment_name, commit_hash, config_id): """ delete all other repcons for this branch of this service """ # get all repcons creating pods labeled for this service # the repcon should be labeled with service=this_service_label rc_name = make_rc_name( branch_name, environment_name, commit_hash, config_id, ) selector = "service={}".format(service_identity(service_name, branch_name)) response = requests.get( k8s_endpoint("replicationcontrollers"), params={ "labelSelector": selector, }, ) delete_repcon_uris = [] # normally we would exclude the current repcon name, except that # we want to start fresh and update in case there is new config. # we are deleting all repcons for this branch in order to get settings updates # until we refactor to the simpler data model. for item in response.json()['items']: # dont' exclude anything # if item['metadata']['name'] != rc_name: delete_repcon_uris.append( "http://{}{}".format( cfg("kubeproxy"), item['metadata']['selfLink'] ) ) # scale to zero and delete the remaining repcons for uri in delete_repcon_uris: print("Scaling repcon at {} to zero".format(uri)) sync_scale(uri, 0) print("Delete request to {}".format(uri)) requests.delete(uri)
def new_env(based_on_id=None, infrastructure_backend=None, environment_name='qa-sandbox'): """ Make a new environment based on the given config or an empty one """ if infrastructure_backend is None: infrastructure_backend = cfg('default_infrastructure_backend', None) if based_on_id: based_on_env = get_env(based_on_id) settings_value = based_on_env['settings'] infrastructure_backend = based_on_env['infrastructure_backend'] else: settings_value = '' cursor = get_cursor() cursor.execute( "INSERT INTO environment\n" + \ "(settings, infrastructure_backend, environment_name)\n" + \ "VALUES (%s, %s, %s)\n" + \ "RETURNING environment_id", (settings_value, infrastructure_backend, environment_name), ) env_id = cursor.fetchone()[0] cursor.close() return env_id
def cfg_handler(key): print("looking for key: {}".format(key)) return {key: cfg(key)}
import bottle from config_finder import cfg from handlers import ( handle_branch_commit, handle_build, ) from security import restricted commit_path = "/commit/<{}>/<{}>/<{}>/<{}>".format( "repo_name", "feature_name", "branch_name", "commit_hash", ) # regex [^\/]+\/? [^\/]+ (\/?[^\/]+)? # path followed by a slash path optional slash and path build_path = "/build/<commit_hash>/<image_name:re:[^\/]+\/?[^\/]+(\/?[^\/]+)?>" bottle.route(commit_path, ["GET"], restricted(handle_branch_commit)) bottle.route(build_path, ["GET"], restricted(handle_build)) debug = cfg('debug', "false") == "true" print("running herd api, debug? {}".format(debug)) bottle.run(host='0.0.0.0', port='8000', debug=debug)
) from security import restricted ### v1 paths ### # regex [^\/]+\/? [^\/]+ (\/?[^\/]+)? # path followed by a slash path optional slash and path v1_build_path = "/v1/build/<service_name>/<branch_name>/<commit_hash>" + \ "/<image_name:re:[^\/]+\/?[^\/]+(\/?[^\/]+)?>" bottle.route(v1_build_path, ["GET"], restricted(handle_build)) ### legacy paths ### leg_commit_path = "/commit/<{}>/<{}>/<{}>/<{}>".format( "repo_name", "feature_name", "branch_name", "commit_hash", ) # regex [^\/]+\/? [^\/]+ (\/?[^\/]+)? # path followed by a slash path optional slash and path leg_build_path = "/build/<commit_hash>/<image_name:re:[^\/]+\/?[^\/]+(\/?[^\/]+)?>" bottle.route(leg_commit_path, ["GET"], restricted(leg_handle_branch_commit)) bottle.route(leg_build_path, ["GET"], restricted(leg_handle_build)) debug = cfg("debug", "false") == "true" print("running herd api, debug? {}".format(debug)) bottle.run(host="0.0.0.0", port="8000", debug=debug)