forked from Kinto/kinto-alwaysdata
/
deploy-kinto.py
158 lines (133 loc) · 4.35 KB
/
deploy-kinto.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# -*- coding: utf-8 -*-
import hashlib
import requests
import ftplib
from paramiko.client import SSHClient, AutoAddPolicy
from six import StringIO
# CONFIGURATION
LOGIN = "EMAIL_ADDRESS"
PASSWORD = "AD_PASSWORD"
ID_ALWAYSDATA = "AD_USERNAME"
credentials = (LOGIN, PASSWORD)
# CALCULATE VALUES
settings = {
'id_alwaysdata': ID_ALWAYSDATA,
'login': LOGIN,
'password': PASSWORD,
'postgresql_host': "postgresql-%s.alwaysdata.net" % ID_ALWAYSDATA,
'ssh_host': "ssh-%s.alwaysdata.net" % ID_ALWAYSDATA,
'ftp_host': "ftp-%s.alwaysdata.net" % ID_ALWAYSDATA,
'prefixed_username': "%s_kinto" % ID_ALWAYSDATA,
'hmac_secret': hashlib.sha256(':'.join(credentials)).hexdigest()
}
# Create database
response = requests.post("http://api.alwaysdata.com/v1/database/", json={
"name": settings['prefixed_username'],
"type": "POSTGRESQL",
"permissions": {ID_ALWAYSDATA: "FULL"}
}, auth=credentials)
try:
response.raise_for_status()
except requests.exceptions.HTTPError as error:
# The database may already exist.
if error.response.status_code != 400:
raise
else:
print("Database `%s` already exists." % "%s_kinto" % ID_ALWAYSDATA)
else:
print("Database `%s` created." % "%s_kinto" % ID_ALWAYSDATA)
# Build configuration
config = """
[app:main]
use = egg:kinto
pyramid.debug_notfound = false
cliquet.http_scheme = https
cliquet.http_host = https://%(id_alwaysdata)s.alwaysdata.net/
cliquet.project_name = kinto
cliquet.project_docs = https://kinto.readthedocs.org/
#
# Backends.
#
cliquet.cache_backend = cliquet.cache.postgresql
cliquet.cache_url = postgres://%(id_alwaysdata)s:%(password)s@%(postgresql_host)s/%(prefixed_username)s
cliquet.storage_backend = cliquet.storage.postgresql
cliquet.storage_url = postgres://%(id_alwaysdata)s:%(password)s@%(postgresql_host)s/%(prefixed_username)s
cliquet.permission_backend = cliquet.permission.postgresql
cliquet.permission_url = postgres://%(id_alwaysdata)s:%(password)s@%(postgresql_host)s/%(prefixed_username)s
# cliquet.backoff = 10
cliquet.batch_max_requests = 25
# cliquet.retry_after_seconds = 30
# cliquet.eos =
#
# Auth configuration.
#
cliquet.userid_hmac_secret = %(hmac_secret)s
multiauth.policies = basicauth
[loggers]
keys = root, cliquet
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = INFO
handlers = console
[logger_cliquet]
level = DEBUG
handlers =
qualname = cliquet
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %%(asctime)s %%(levelname)-5.5s [%%(name)s][%%(threadName)s] %%(message)s
# End logging configuration
""" % settings
# Create ssh user
response = requests.post("http://api.alwaysdata.com/v1/ssh/", json={
"name": settings['prefixed_username'],
"password": PASSWORD
}, auth=credentials)
try:
response.raise_for_status()
except requests.exceptions.HTTPError as error:
# The database may already exist.
if error.response.status_code != 400:
raise
else:
print("SSH User `%s` already exists." % "%s_kinto" % ID_ALWAYSDATA)
else:
print("SSH User `%s` created." % "%s_kinto" % ID_ALWAYSDATA)
# Copy the config
ftp = ftplib.FTP(settings['ftp_host'], ID_ALWAYSDATA, PASSWORD)
try:
ftp.mkd(".local")
except ftplib.error_perm:
pass
try:
ftp.mkd("kinto")
except ftplib.error_perm:
pass
try:
ftp.storbinary("STOR kinto/kinto.ini", StringIO(config))
except ftplib.error_perm:
print("A kinto config already exist.")
else:
print("A kinto config has been uploaded.")
ftp.close()
# Install kinto[postgresql]
ssh = SSHClient()
ssh.set_missing_host_key_policy(AutoAddPolicy())
ssh.connect(settings['ssh_host'], username=settings['prefixed_username'],
password=PASSWORD, look_for_keys=False)
stdin, stdout, stderr = ssh.exec_command('PYTHONPATH=~/.local/ easy_install-2.6 --install-dir=~/.local -U pip')
print(stdout.read(), stderr.read())
stdin, stdout, stderr = ssh.exec_command('PYTHONPATH=~/.local/ ~/.local/pip install --user --no-binary --upgrade setuptools virtualenv virtualenvwrapper')
print(stdout.read(), stderr.read())
stdin, stdout, stderr = ssh.exec_command('~/.local/bin/virtualenv kinto/venv/ --python=python2.7')
print(stdout.read(), stderr.read())
stdin, stdout, stderr = ssh.exec_command('kinto/venv/bin/pip install kinto cliquet[postgresql]')
print(stdout.read(), stderr.read())
ssh.close()