forked from kau-masa/Komi_SmartAlarmClock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_demo.py
68 lines (58 loc) · 2.79 KB
/
local_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/python
import os
import requests
import json
import subprocess
import time
def userInput(config):
def defaultValue(config, key):
return config[key] if key in config else ""
res = {}
invalid = {}
res['user_name'] = raw_input("your GitHub username (default = " + defaultValue(config, 'user_name') + "): ")
res['project_name'] = raw_input("name of your SAC project on GitHub (default = " + defaultValue(config, 'project_name') + "): ")
res['project_branch'] = raw_input("current working branch of your SAC project on GitHub (default = " + defaultValue(config, 'project_branch') + "): ")
res['sac_app_id'] = raw_input("generated SAC app ID (default = " + defaultValue(config, 'sac_app_id') + "): ")
res['sac_app_secret'] = raw_input("generated SAC app secret (default = " + defaultValue(config, 'sac_app_secret') + "): ")
for k, v in res.items():
if v == "": res[k] = defaultValue(config, k)
invalid = [k for k, v in res.items() if v == ""]
if len(invalid) > 0:
print "invalid configuration: properties", invalid, "must be set"
res = userInput(config)
return res
config_file = open('config.json', 'r')
config = json.load(config_file)
invalid_properties = [k for k, v in config.items() if v == ""]
print "current configuration:", json.dumps(config, indent=2)
if len(invalid_properties) > 0:
print "invalid configuration: properties", invalid_properties, "must be set"
config = userInput(config)
if 'user_name' and 'project_name' and 'project_branch' and 'sac_app_id' and 'sac_app_secret' in config:
reply = str(raw_input('config file complete. do you wish to reset it? (y/n): ')).lower().strip()
if reply[0] == 'y':
config = userInput(config)
else:
config = userInput(config)
with open('config.json', 'w') as config_file:
json.dump(config, config_file, indent=2)
p = subprocess.Popen(["ngrok", "http", "8080"])
time.sleep(1.5)
# retrieving public url for exposed localhost:8080
headers = {'Content-Type': 'application/json'}
r = requests.get('http://127.0.0.1:4040/api/tunnels', headers=headers)
public_url = json.loads(r.text)['tunnels'][0]['public_url']
# setting environment variables with user input
os.environ["CRAFT_DEMO_SAC_USER"] = config['user_name']
os.environ["CRAFT_DEMO_SAC_PROJECT"] = config['project_name']
os.environ["CRAFT_DEMO_SAC_VERSION"] = config['project_branch']
os.environ["CRAFT_DEMO_SAC_APP_ID"] = config['sac_app_id']
os.environ["CRAFT_DEMO_SAC_APP_SECRET"] = config['sac_app_secret']
os.environ["CRAFT_DEMO_SAC_PORT"] = '8080'
os.environ["CRAFT_DEMO_SAC_URL"] = 'http://localhost:8080'
os.environ["CRAFT_DEMO_SAC_WS_URL"] = 'ws://localhost:8080'
os.environ["CRAFT_RUNTIME_SERVER_URL"] = 'https://api.craft.ai'
os.environ["CRAFT_HUB_URL"] = 'https://hub.craft.ai'
os.environ["CRAFT_DEMO_SAC_ACTIONS_URL"] = public_url
subprocess.call(["python", "-u", "src/server/main.py"])
p.terminate()