forked from Kinto/kinto-alwaysdata
/
deploy-kinto.py
134 lines (121 loc) · 4.36 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
# -*- 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)
if ID_ALWAYSDATA is None:
response = requests.get("http://api.alwaysdata.com/v1/account/", auth=credentials)
try:
response.raise_for_status()
ID_ALWAYSDATA = response.json()[0]['name']
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)
# 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)
# 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:
with open("kinto.ini", "rb") as f:
ini_content = f.read().format(
bucket_id='{bucket_id}',
collection_id='{collection_id}',
**settings)
ftp.storbinary("STOR kinto/kinto.ini", StringIO(ini_content))
except ftplib.error_perm:
print("A kinto config already exist.")
else:
print("A kinto config has been uploaded.")
try:
with open("kinto.fcgi", "rb") as f:
ftp.storbinary("STOR www/kinto.fcgi", f)
ftp.sendcmd('SITE CHMOD 755 www/kinto.fcgi')
ftp.delete('www/index.html')
except ftplib.error_perm:
print("A kinto.fcgi already exist.")
else:
print("A kinto.fcgi has been uploaded.")
try:
with open("htaccess", "rb") as f:
ftp.storbinary("STOR www/.htaccess", f)
except ftplib.error_perm:
print("A .htaccess already exist.")
else:
print("A .htaccess 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[postgresql] kinto-attachment flup')
print(stdout.read(), stderr.read())
# Run kinto migration to setup the database.
stdin, stdout, stderr = ssh.exec_command('kinto/venv/bin/kinto --ini kinto/kinto.ini migrate')
print(stdout.read(), stderr.read())
ssh.close()