-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.yml
378 lines (328 loc) · 10.5 KB
/
deploy.yml
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
---
# Configure and update the cloudformation stack and provision the EC2.
- name: provision stack
hosts: localhost
connection: local
gather_facts: false
vars_files:
- vars.yml
- passwords.yml
vars:
cloudformation_template: tmp_cloudformation.json
tasks:
- name: generate cloudformation template from troposphere file
shell: python aws/cloudformation_ec2_default_vpc.py > {{ cloudformation_template }}
- name: deploy cloudformation
cloudformation:
stack_name: "{{ stack_name }}"
state: present
region: "{{ aws_region }}"
template: "{{ cloudformation_template }}"
args:
template_parameters:
KeyName: "{{ key_name }}"
InstanceType: "{{ instance_type }}"
SSHLocation: "{{ ssh_location }}"
HostedZone: "{{ hosted_zone }}"
HostName: "{{ host_name }}"
register: stack
- name: show stack outputs
debug: msg="My stack outputs are {{ stack.stack_outputs }}"
- name: change the host based on the stack output
add_host:
hostname: "{{ stack.stack_outputs.PublicIP }}"
groups: ec2_host
ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
- name: configure ec2
hosts: ec2_host
remote_user: ubuntu
become_method: sudo
gather_facts: False
vars:
home_dir: "/home/ubuntu/"
download_dir: "{{ home_dir }}/download"
anaconda_mirror : "http://repo.continuum.io/archive"
anaconda_python_ver : "3"
anaconda_ver : "4.3.1"
anaconda_timeout_seconds : 600
anaconda_platform : "{{ansible_system}}-{{ansible_machine}}"
anaconda_name : "Anaconda{{anaconda_python_ver}}-{{anaconda_ver}}-{{anaconda_platform}}"
anaconda_installer_sh : "{{anaconda_name}}.sh"
anaconda_installer_url : "{{anaconda_mirror}}/{{anaconda_installer_sh}}"
anaconda_home_dir: "{{ home_dir }}/anaconda{{ anaconda_python_ver }}"
cuda_home: "/usr/local/cuda-8.0/"
cudnn_filename: "cudnn-8.0-linux-x64-v5.1.tgz"
jupyter_notebook_certfile: "{{ home_dir }}/etc/sslcert.pem"
jupyter_notebook_keyfile: "{{ home_dir }}/etc/sslkey.key"
jupyter_notebook_working_dir: "{{ home_dir }}/notebooks_repo"
env:
PATH: "{{ anaconda_home_dir }}/bin:$PATH"
vars_files:
- vars.yml
- passwords.yml
pre_tasks:
- name: Install python 2
raw: test -e /usr/bin/python || (apt -y update && apt-get install -y python2.7 python-simplejson)
become: true
- name: Gather facts
setup: # aka gather_facts
tasks:
- name: Install packages with apt
apt: pkg={{ item }} state=present force=yes update_cache=yes
become: true
with_items:
- build-essential
- software-properties-common
- binutils
- tmux
- unzip
- htop
- tree
- ffmpeg
#
# Anaconda
#
- name: Detect conda version
shell: conda --version
executable=/bin/bash
environment: "{{ env }}"
register: conda_result
ignore_errors: True
- name: Debug conda version
debug:
var: conda_result
- name: Create download directory
file:
path: "{{ download_dir }}"
state: directory
mode: 0755
- name: Download Anaconda installer
get_url:
url: "{{ anaconda_installer_url }}"
dest: "{{ download_dir }}/{{ anaconda_installer_sh }}"
mode: 0755
timeout: "{{ anaconda_timeout_seconds }}"
when: conda_result|failed
- name: Installing Anaconda
command: bash {{ download_dir }}/{{ anaconda_installer_sh }} -b
args:
creates: "{{ anaconda_home_dir }}/bin/conda"
when: conda_result|failed
- name: Delete Anaconda installer
file:
path: "{{ download_dir }}/{{ anaconda_installer_sh }}"
state: absent
- name: Add Anaconda to .bashrc
lineinfile:
dest: "{{ home_dir }}/.bashrc"
state: present
line: export PATH="{{ anaconda_home_dir }}/bin:$PATH"
#
# - name: Fixup PATH
# command: "{{ item }}"
# with_items:
# - echo 'export PATH="{{ anaconda_home_dir }}/bin:$PATH"' >> ~/.bashrc
# - export PATH="{{ anaconda_home_dir }}/bin:$PATH"
#
# Jupyter notebook
#
# - name: Generate Jupyter Notebook config - REMOVED
# shell: jupyter notebook --generate-config
# executable=/bin/bash
# environment: "{{ env }}"
- name: Ensures ~/etc dir exists
file:
path: ~/etc
state: directory
- name: Create cert for https
shell: "openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout {{ jupyter_notebook_keyfile }} -out {{ jupyter_notebook_certfile }} -subj {{ openssl_subject_arg }}"
args:
executable: /bin/bash
creates: "{{ jupyter_notebook_keyfile }}"
environment: "{{ env }}"
- name: Get hash for jupyter notebook auth
shell: python -c "from notebook.auth import passwd; print(passwd({{ jupyter_notebook_password | quote }}))"
args:
executable: /bin/bash
environment: "{{ env }}"
register: notebook_auth
- name: Ensures .jupyter dir exists
file:
path: .jupyter
state: directory
- name: Copy jupyter_notebook_config.py
template:
src: templates/jupyter_notebook_config.py
dest: .jupyter/jupyter_notebook_config.py
- name: Check if the notebooks repo is already cloned
stat:
path: "{{ jupyter_notebook_working_dir }}"
register: repo_dir
- name: git clone notebooks repo
git:
repo: "{{ jupyter_notebooks_repo_url}}"
dest: "{{ jupyter_notebook_working_dir }}"
remote: "{{ jupyter_notebooks_repo_remote }}"
version: "{{ jupyter_notebooks_repo_version }}"
when: not repo_dir.stat.exists
#
# nVidia CUDA
#
- name: Test for nVidia hardware
shell: lspci | grep -i nvidia
register: nvidia_result
ignore_errors: True
- name: Test for Cuda install
shell: dpkg-query -l cuda
register: cuda_result
ignore_errors: True
# TODO: validate checksum: 16b0946a3c99ca692c817fb7df57520c
- name: Install nVidia CUDA drivers for Ubuntu 16.04
apt:
deb: http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_8.0.44-1_amd64.deb
become: true
when: nvidia_result|succeeded and cuda_result|failed
- name: Install cuda package
apt:
name: cuda
update_cache: yes
become: true
when: nvidia_result|succeeded and cuda_result|failed
- name: Cuda post-install steps
lineinfile:
dest: "{{ home_dir }}/.bashrc"
state: present
line: "{{ item }}"
with_items:
- "export PATH={{ cuda_home }}/bin:${PATH}"
- "export LD_LIBRARY_PATH={{ cuda_home }}/lib64:${LD_LIBRARY_PATH}"
when: nvidia_result|succeeded and cuda_result|failed
- name: Configure nvidia
command: "{{ item }}"
with_items:
- modprobe nvidia
- nvidia-smi
become: true
when: nvidia_result|succeeded
- name: Ensures ~/opt dir exists
file:
path: opt
state: directory
- name: Test for CUDNN install
shell: "ls {{ cuda_home }}/include/cudnn.h"
register: cudnn_result
ignore_errors: True
- name: Install CUDNN
unarchive:
src: "files/{{ cudnn_filename }}"
dest: "{{ home_dir }}/opt"
when: nvidia_result|succeeded and cudnn_result|failed
- name: Copy CUDNN header and lib files
command: "{{ item }}"
with_items:
- "cp -P {{ home_dir }}/opt/cuda/include/cudnn.h {{ cuda_home }}/include/"
- "cp -P {{ home_dir }}/opt/cuda/lib64/libcudnn* {{ cuda_home }}/lib64/"
- "chmod a+r {{ cuda_home }}/lib64/libcudnn*"
become: true
when: nvidia_result|succeeded and cudnn_result|failed
#
# Install conda packages in environments
#
- name: Add conda-forge
shell: conda config --add channels conda-forge
executable=/bin/bash
environment: "{{ env }}"
- name: Create conda environment
shell: conda create --name py3 python=3 jupyter notebook ipykernel
args:
executable: /bin/bash
creates: "{{ anaconda_home_dir }}/envs/py3"
environment: "{{ env }}"
- name: Add kernel to list in Jupyter Notebook
shell: "{{ anaconda_home_dir }}/envs/py3/bin/python -m ipykernel install --user"
args:
executable: /bin/bash
environment: "{{ env }}"
- name: copy conda environment file to server
copy:
src: envs/py3-environment.yml
dest: "{{ home_dir }}/py3-environment.yml"
owner: ubuntu
group: ubuntu
mode: 0644
- name: update conda environment from file
shell: "{{ anaconda_home_dir }}/envs/py3/bin/conda env update --name py3 --file {{ home_dir }}/py3-environment.yml"
args:
executable: /bin/bash
environment: "{{ env }}"
# # TODO: find a faster way to do this, like a batch
# - name: Install conda packages
# shell: conda install --name py3 --channel conda-forge -y {{item}}
# args:
# executable: /bin/bash
# environment: "{{ env }}"
# with_items:
# - numpy==1.11.2
# - theano==0.8.2
# - tensorflow=0.11.0rc2
# - keras==1.0.7
# - pandas==0.19.1
# - matplotlib=1.5.3
# - seaborn=0.7.1
# - bokeh==0.12.3
# - bcolz==1.0.0
# - scikit-learn=0.18
# - scikit-image=0.12.3
# - statsmodels=0.6.1
# - boto3==1.4.0
# - ipython==5.1.0
# - networkx=1.11
# - requests=2.11.1
# - scipy=0.18.1
# - cryptography=1.5.2
# - sh=1.11
# - sympy=1.0
#
# - name: Install pip packages into py3 env (not available via conda)
# pip:
# name: "{{ item }}"
# executable: "{{ anaconda_home_dir }}/envs/py3/bin/pip"
# environment: "{{ env }}"
# with_items:
# - kaggle-cli
# FIXME: jupyter notebook is launched in the default env, but notebooks in py3 env use default conda env bin/
# Is this a conda bug? Or Jupyter? Or (more likely) something I've misconfigured?
- name: Install pip packages into default env (not available via conda)
pip:
name: "{{ item }}"
executable: "{{ anaconda_home_dir }}/bin/pip"
environment: "{{ env }}"
with_items:
- kaggle-cli
- name: Copy .theanorc
template:
src: templates/theanorc
dest: "{{ home_dir }}/.theanorc"
- name: Copy .keras
template:
src: templates/keras.json
dest: "{{ home_dir }}//.keras/keras.json"
#
# Start Jupyter Notebook
#
- name: Copy jupyter-notebook systemd script
template:
src: templates/jupyter-notebook.service
dest: /etc/systemd/system/jupyter-notebook.service
become: true
notify:
- restart jupyter
handlers:
- name: restart jupyter
# debug: msg="restarting jupyter notebook"
systemd:
name: jupyter-notebook
state: restarted
enabled: yes
daemon_reload: yes
become: true