-
Notifications
You must be signed in to change notification settings - Fork 18
/
fabfile.py
156 lines (131 loc) · 5.55 KB
/
fabfile.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
from fabric.api import run, sudo, env, require, settings, local, put
from fabric.contrib.files import exists
import subprocess
import os
### VARIABLES ###
# Origin of our repository, and the repository
GIT_ORIGIN = '[email protected]'
GIT_REPO = 'nsh87/shiny'
# Packages to install in Vagrant
INSTALL_PACKAGES = ['r-base',
'gdebi-core',
'haskell-platform',
'libghc-pandoc-dev',
'pandoc']
### ENVIRONMENTS ###
def vagrant():
"""Defines the Vagrant virtual machine's environment variables.
Local development and server will use this environment.
"""
# Configure SSH things
raw_ssh_config = subprocess.Popen(['vagrant', 'ssh-config'], stdout=subprocess.PIPE).communicate()[0]
ssh_config = dict([l.strip().split() for l in raw_ssh_config.decode('utf-8').split("\n") if l])
env.hosts = ['127.0.0.1:%s' % (ssh_config['Port'])]
env.user = ssh_config['User']
env.key_filename = ssh_config['IdentityFile'].replace("'", "").replace('"', '')
# Development will happen on the master branch
env.repo = ('origin', 'master')
def remote():
"""Defines a remote server on AWS running Ubuntu 14.x, ideally with >1.5GB
RAM.
"""
# Insert values for:
env.hosts = 'ec2-52-13.us-west-2.compute.amazonaws.com' # Your public DNS
env.key_filename = os.path.join('/', 'my.pem') # Path to your AWS .pem key
# Don't change unless you know what you are doing:
env.base = '/www-shiny' # Default location for Shiny Server apps
env.user = 'ubuntu' # Default username for AWS SSH login
### ROUTINES ###
def setup_vagrant():
"""Sets up the Vagrant environment"""
require('hosts', provided_by=[vagrant]) # Sets the environment for Fabric
sub_add_repos()
sub_install_packages()
sub_install_shiny()
reload_server()
def bootstrap():
"""Sets up the remote environment"""
require('hosts', provided_by=[remote])
sub_add_repos()
sub_install_packages()
sub_install_shiny()
reload_server()
def reload_server():
require('hosts', provided_by=[vagrant, remote])
sudo('restart shiny-server')
def push():
"""Pushes the current folder to the remote machine's Shiny apps folder"""
require('hosts', provided_by=[remote])
sudo('mkdir -p /www-shiny')
sudo('chown -R ubuntu:ubuntu /www-shiny')
sudo('rm -rf /www-shiny/*')
put('./', '/www-shiny/')
### SUB-ROUTINES ###
def sub_add_repos():
"""Adds any repositories needed to install packages"""
if not exists('/etc/apt/sources.list.d/cran.list', use_sudo=True):
# Add the repository for R
sudo('sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 '
'--recv-keys E084DAB9')
run('sudo sh -c "echo \'deb http://cran.rstudio.com/bin/linux/ubuntu '
'trusty/\' >> /etc/apt/sources.list.d/cran.list"')
def sub_install_packages():
"""Installs the necessary packages to get Shiny running"""
sudo('apt-get update')
sudo('apt-get -y upgrade')
package_str = ' '.join(INSTALL_PACKAGES)
sudo('apt-get -y --force-yes install ' + package_str)
def sub_install_shiny():
"""Installs Shiny package and Shiny Server"""
# Install Shiny package for R
sudo('R -e "install.packages(\'shiny\', '
'repos=\'http://cran.rstudio.com/\')"')
# Install Shiny Server using gdebi. According to documentation, will be
# installed into /opt/shiny-server/ with main executable in
# /opt/shiny-server/bin/shiny-server. Also new user 'shiny' will be
# created.
sudo('mkdir -p /usr/src/shiny')
sudo('''cd /usr/src/shiny; if [ ! -e shiny-server-1.4.2.786-amd64.deb ];
then a='http://download3.rstudio.org/ubuntu-12.04/x86_64/shiny-server-' ; \
b='1.4.2.786-amd64.deb' ; \
wget $a$b ; \
echo y | gdebi shiny-server-1.4.2.786-amd64.deb ; \
fi''')
# Move server directory at /srv/shiny-server to shared Vagrant folder if
# the shared folder is empty. This will copy sample Shiny app in the
# process.
sudo('''if [ 'find /www-shiny -maxdepth 0 -empty | read v' ]; then \
cp -LR /srv/shiny-server/* /www-shiny/ ; \
rm -rf /srv/shiny-server ; \
ln -s /www-shiny/ /srv/shiny-server;
fi''')
sub_install_rmarkdown()
sub_install_additional_packages()
sub_make_writeable_project()
def sub_make_writeable_project():
"""Creates a symlink from Shiny's web server folder to a shiny:shiny
writeable folder for app development.
Shiny server runs in /www-shiny (/project on the host machine), whose
owner is vagrant:vagrant. Shiny server runs as user shiny:shiny; so if
you have an app that needs to write anything, you can't do it in
/www-shiny. You have to do it in another owned by shiny:shiny.
"""
sudo('''cd /www-shiny; if [ ! -L proj ]; then \
ln -s ../writeable-project/ proj ;
fi''')
def sub_install_rmarkdown():
"""Installs the packages required for Shiny to serve markdown documents.
Haskell is a prerequisite that should have been installed earlier. Pandoc is
also required."""
run('cabal update')
sudo('R -e "install.packages(\'rmarkdown\', '
'repos=\'http://cran.rstudio.com/\')"')
def sub_install_additional_packages():
"""This is an example function demonstrating how to add the installation of
an R package to the setup process.
Simply uncomment the lines below and replace the name of the package and the
URL to its repo if necessary. Then delete the line that says 'pass'.
"""
# sudo('R -e "install.packages(\'rmarkdown\', '
# 'repos=\'http://cran.rstudio.com/\')"')
pass