Skip to content

Commit

Permalink
Merge pull request #38 from palfrey/test-add-app
Browse files Browse the repository at this point in the history
Test/fix "add app"
  • Loading branch information
palfrey committed Feb 4, 2019
2 parents 2ff4bc7 + bf8dcf4 commit 508bf1e
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ repos/
/static/
.vagrant/
__pycache__/
*.log
*.log
where.png
2 changes: 1 addition & 1 deletion apps/templates/app_info.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "base.html" %}
{% block body %}
<h1>Wharf: {{ app }}</h1>
<h1 id="app_page" >Wharf: {{ app }}</h1>
<a href="{{ url('index') }}">Return to apps index</a><br />

<h3>Actions</h3>
Expand Down
6 changes: 3 additions & 3 deletions apps/templates/list_apps.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{% extends "base.html" %}
{% block body %}
<h1>Wharf</h1>
<h1 id="list_apps">Wharf</h1>
<form class="form" action="{{ url('refresh_all') }}" method="POST">
{% csrf_token %}
<button type="submit" class="btn btn-primary">Refresh Dokku information</button>
<button type="submit" class="btn btn-primary" id="refresh_info">Refresh Dokku information</button>
</form>
<h3>Apps</h3>
<ul class=apps>
Expand All @@ -18,7 +18,7 @@ <h3>New app</h3>
{% csrf_token %}
<input type="hidden" name="kind" value="app" />
{{ app_form | bootstrap }}
<button type="submit" class="btn btn-primary">Create app</button>
<button type="submit" class="btn btn-primary" id="create_app">Create app</button>
</form>
<br />
<h2>Global Config</h2>
Expand Down
2 changes: 1 addition & 1 deletion apps/templates/setup_key.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<h1 id="initial-setup-header">Wharf: Initial setup</h1>
Save the following key (as one line) to a file on your server, and run the
<code>ssh-keys:add</code> command from the <a href="http://dokku.viewdocs.io/dokku/deployment/user-management/#adding-ssh-keys">Dokku instructions for setting up SSH keys</a><br /><br />
<code>
<code id="ssh-key">
{{ key }}
</code><br />
<br />
Expand Down
22 changes: 12 additions & 10 deletions apps/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ def get_log(res):

def wait_for_command(request, app_name, task_id, after):
res = AsyncResult(task_id)
app = models.App.objects.get(name=app_name)
task, created = models.TaskLog.objects.get_or_create(task_id=task_id, defaults={'app': app, 'when': datetime.now()})
if app_name != '_':
app = models.App.objects.get(name=app_name)
task, created = models.TaskLog.objects.get_or_create(task_id=task_id, defaults={'app': app, 'when': datetime.now()})
description = task.description
else:
description = ""
if res.state == state(SUCCESS):
if app_name == '_': # global
return redirect(reverse(after, kwargs={'task_id': task_id}))
else:
return redirect(reverse(after, kwargs={'app_name': app_name, 'task_id': task_id}))
return redirect(reverse(after, kwargs={'app_name': app_name, 'task_id': task_id}))
log = ansi_escape.sub("", get_log(res))
if res.state == state(FAILURE):
log += str(res.traceback)
Expand All @@ -85,7 +86,7 @@ def wait_for_command(request, app_name, task_id, after):
'log': log,
'state': res.state,
'running': res.state in [state(PENDING), state(STARTED)],
'description': task.description
'description': description
})

def show_log(request, task_id):
Expand Down Expand Up @@ -237,7 +238,7 @@ def letsencrypt(app_name):
def process_info(app_name):
data = run_cmd_with_cache("ps:report %s" % app_name)
lines = data.split("\n")
if lines[0].find("%s process information" % app_name) == -1:
if lines[0].find("%s process information" % app_name) == -1 and lines[0].find("%s ps information" % app_name) == -1: # Different versions
raise Exception(data)
results = {}
processes = {}
Expand Down Expand Up @@ -281,7 +282,7 @@ def check_domain(request, app_name, task_id):
raise Exception(data)

def app_info(request, app_name):
app, _ = models.App.objects.get_or_create(name=app_name)
app = models.App.objects.get(name=app_name)
config = app_config(app_name)
if "GITHUB_URL" in config:
app.github_url = config["GITHUB_URL"]
Expand Down Expand Up @@ -352,7 +353,8 @@ def check_redis(request, app_name, task_id):
return redirect(reverse('app_info', args=[app_name]))

def create_app(app_name):
return run_cmd_with_log(None, "Add app %s" % app_name, "apps:create %s" % app_name, "check_app")
models.App(name=app_name).save()
return run_cmd_with_log(app_name, "Add app %s" % app_name, "apps:create %s" % app_name, "check_app")

def check_app(request, app_name, task_id):
res = AsyncResult(task_id)
Expand Down
59 changes: 58 additions & 1 deletion check_boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,76 @@
from selenium.webdriver.support import expected_conditions as EC
import sys
import os
from subprocess import Popen, PIPE, STDOUT, run, check_call, check_output, CalledProcessError
import io
import uuid

chrome_options = webdriver.ChromeOptions()
chrome_options.headless = True
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
driver = webdriver.Chrome(os.environ["CHROMEDRIVER_PATH"], chrome_options=chrome_options, service_args=['--verbose'])
driver.implicitly_wait(0)

def find_element_by_id(id):
def find(driver):
elements = driver.find_elements_by_id(id)
if len(elements) == 1:
return elements[0]
elif len(elements) == 0:
return None
else:
raise Exception(elements)
return find

def wait_for_one(driver, locators):
for locator in locators:
element = locator(driver)
if element != None:
return element
return False

def get_main_id():
res = WebDriverWait(driver, 10).until(
lambda driver: wait_for_one(driver, [find_element_by_id("initial-setup-header"), find_element_by_id("list_apps")])
)
return res.get_attribute("id")

try:
driver.get(sys.argv[1])
driver.find_element_by_name("username").send_keys("admin")
driver.find_element_by_name("password").send_keys("password")
driver.find_element_by_name("submit").click()
id = get_main_id()
if id == "list_apps":
print("Checking SSH status")
driver.find_element_by_id("refresh_info").click()
id = get_main_id() # because keys might not work any more
if id == "initial-setup-header":
print("Adding new keys")
keys = check_output("sudo dokku ssh-keys:list".split(" ")).decode("utf-8")
if "check_boot" in keys:
check_call("sudo dokku ssh-keys:remove check_boot".split(" "))
element = driver.find_element_by_id("ssh-key")
cmd = "echo " + element.text + " | sudo dokku ssh-keys:add check_boot"
print(cmd)
ret = os.system(cmd)
assert ret == 0
driver.get(sys.argv[1])
elif id == "list_apps":
pass
else:
raise Exception(id)
app_name = uuid.uuid4().hex
print("Making new app " + app_name)
driver.find_element_by_id("id_name").send_keys(app_name)
driver.find_element_by_id("create_app").click()
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "initial-setup-header"))
lambda driver: wait_for_one(driver, [find_element_by_id("app_page")])
)
assert driver.page_source.find(app_name) != -1
except:
driver.get_screenshot_as_file('where.png')
raise
finally:
driver.quit()
7 changes: 7 additions & 0 deletions wharf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import os
import dj_database_url
import subprocess
import re

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Expand Down Expand Up @@ -147,6 +149,11 @@
# Wharf settings

DOKKU_HOST = os.environ.get("DOKKU_SSH_HOST", "127.0.0.1")
if DOKKU_HOST == "127.0.0.1": # default, so need to detect host
route = subprocess.check_output(["/sbin/ip", "route"]).decode("utf-8")
ip = re.match("default via (\d+\.\d+\.\d+.\d+)", route)
DOKKU_HOST = ip.groups()[0]

DOKKU_SSH_PORT = int(os.environ.get("DOKKU_SSH_PORT", "22"))
GITHUB_SECRET = os.environ.get("GITHUB_SECRET", "password")
ADMIN_LOGIN = os.environ.get("ADMIN_LOGIN", "admin")
Expand Down

0 comments on commit 508bf1e

Please sign in to comment.