-
-
Save andrewsmedina/6eaa717c5ea8eadc4af7 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
import argparse | |
import json | |
import os | |
import urllib2 | |
def get_envs(app_name): | |
resp = request("/apps/{}/env".format(app_name)) | |
raw_envs = json.load(resp) | |
private_envs = {} | |
public_envs = {} | |
for env in raw_envs: | |
if env["name"].startswith("TSURU_"): | |
continue | |
if env["public"]: | |
public_envs[env["name"]] = env["value"] | |
else: | |
private_envs[env["name"]] = env["value"] | |
return public_envs, private_envs | |
def set_envs(app_name, envs, private=False): | |
path = "/apps/{}/env?private={}".format(app_name, int(private)) | |
data = json.dumps(envs) | |
resp = request(path, method="POST", body=data, | |
headers={"Content-Type": "application/json"}) | |
print "Setting environment variables (private=%s)" % repr(private) | |
data = resp.read() | |
print data | |
while data != "": | |
data = resp.read() | |
print data | |
def request(path, method="GET", body=None, headers=None): | |
target = os.environ.get("TSURU_TARGET").rstrip("/") | |
token = os.environ.get("TSURU_TOKEN") | |
url = "{}{}".format(target, path) | |
request = urllib2.Request(url) | |
request.add_header("Authorization", "bearer " + token) | |
if body: | |
request.add_data(body) | |
if headers: | |
for k, v in headers.items(): | |
request.add_header(k, v) | |
return urllib2.urlopen(request) | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("source_app") | |
parser.add_argument("destination_app") | |
args = parser.parse_args() | |
public_envs, private_envs = get_envs(args.source_app) | |
set_envs(args.destination_app, public_envs) | |
set_envs(args.destination_app, private_envs, private=True) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment