if you ever need to create a new python 2 virtual environment: conda create --name thenameyouwant python=2 anaconda


In [1]:
from fabric.api import *

env.host_string="ec2-35-166-108-198.us-west-2.compute.amazonaws.com"
env.user = "ubuntu"
env.key_filename = ['/home/cognizac/Downloads/Stevens-key2.pem']

In [2]:
# run a command on the server to make sure you're connected
run('uname -a')


[ec2-35-166-108-198.us-west-2.compute.amazonaws.com] run: uname -a
[ec2-35-166-108-198.us-west-2.compute.amazonaws.com] out: Linux ip-172-31-28-6 4.4.0-59-generic #80-Ubuntu SMP Fri Jan 6 17:47:47 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[ec2-35-166-108-198.us-west-2.compute.amazonaws.com] out: 

Out[2]:
'Linux ip-172-31-28-6 4.4.0-59-generic #80-Ubuntu SMP Fri Jan 6 17:47:47 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux'

In [4]:
# stop the service for your project so you can edit the code
sudo('service myproject stop')


[ec2-35-166-108-198.us-west-2.compute.amazonaws.com] sudo: service myproject stop
Out[4]:
''

In [5]:
# stop nginx so you can use port 41593 for testing
sudo('service nginx stop')


[ec2-35-166-108-198.us-west-2.compute.amazonaws.com] sudo: service nginx stop
Out[5]:
''

In [6]:
# download the current code for your project
get('myproject.py')


[ec2-35-166-108-198.us-west-2.compute.amazonaws.com] download: /home/cognizac/PycharmProjects/BIA-660-C-Spring2017/ec2-35-166-108-198.us-west-2.compute.amazonaws.com/myproject.py <- /home/ubuntu/myproject.py
Out[6]:
[u'/home/cognizac/PycharmProjects/BIA-660-C-Spring2017/ec2-35-166-108-198.us-west-2.compute.amazonaws.com/myproject.py']

In [7]:
# see what directory your currently in on the local machine
pwd


Out[7]:
u'/home/cognizac/PycharmProjects/BIA-660-C-Spring2017'

In [10]:
# cd into the directory created when you downloaded the project.py file
import os
os.chdir('ec2-35-166-108-198.us-west-2.compute.amazonaws.com/')

In [11]:
# checking to make sure we're in the right working directory
pwd


Out[11]:
u'/home/cognizac/PycharmProjects/BIA-660-C-Spring2017/ec2-35-166-108-198.us-west-2.compute.amazonaws.com'

In [12]:
# list the files to see what's there
ls


myproject.py

In [26]:
# edit the code in a separate window, feel free to use my code
put('myproject.py')


[ec2-35-166-108-198.us-west-2.compute.amazonaws.com] put: myproject.py -> /home/ubuntu/myproject.py
Out[26]:
[u'/home/ubuntu/myproject.py']

In [ ]:
# if you are using my code with TextBlob make sure to install TextBlob to the chatbot env
with prefix('source activate chatbot'):
    run('pip install textblob')

In [27]:
# we need to use our chatbot virtual environment to run the server code
with prefix('source activate chatbot'):
    run('python myproject.py')
    
#if you need to stop the server script, just select this cell and click the stop button at the top


[ec2-35-166-108-198.us-west-2.compute.amazonaws.com] run: python myproject.py
[ec2-35-166-108-198.us-west-2.compute.amazonaws.com] out:  * Running on http://0.0.0.0:41953/ (Press CTRL+C to quit)
[ec2-35-166-108-198.us-west-2.compute.amazonaws.com] out: zac.wentzell in bot_chat says: finally working?
[ec2-35-166-108-198.us-west-2.compute.amazonaws.com] out: ImmutableMultiDict([('user_id', u'U3ST4U747'), ('channel_id', u'C3X9ZE7A5'), ('timestamp', u'1487294707.000195'), ('token', u'enPKGZJWARXYjbq4V8rYInYM'), ('channel_name', u'bot_chat'), ('team_id', u'T3S93LZK6'), ('team_domain', u'bia660-c-spring2017'), ('text', u'finally working?'), ('service_id', u'134698444054'), ('user_name', u'zac.wentzell')])
[ec2-35-166-108-198.us-west-2.compute.amazonaws.com] out: 52.90.33.223 - - [17/Feb/2017 01:25:08] "POST /slack HTTP/1.1" 200 -
[ec2-35-166-108-198.us-west-2.compute.amazonaws.com] out: slackbot in bot_chat says: Hey dude!
[ec2-35-166-108-198.us-west-2.compute.amazonaws.com] out: ImmutableMultiDict([('user_id', u'USLACKBOT'), ('channel_id', u'C3X9ZE7A5'), ('timestamp', u'1487294708.000196'), ('token', u'enPKGZJWARXYjbq4V8rYInYM'), ('bot_name', u'zac_bot'), ('channel_name', u'bot_chat'), ('team_id', u'T3S93LZK6'), ('team_domain', u'bia660-c-spring2017'), ('text', u'Hey dude!'), ('service_id', u'134698444054'), ('user_name', u'slackbot'), ('bot_id', u'B3Y34B94M')])
[ec2-35-166-108-198.us-west-2.compute.amazonaws.com] out: 54.174.114.150 - - [17/Feb/2017 01:25:08] "POST /slack HTTP/1.1" 200 -
[ec2-35-166-108-198.us-west-2.compute.amazonaws.com] out: 
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-27-372ad0bbe1a0> in <module>()
      1 with prefix('source activate chatbot'):
----> 2     run('python myproject.py')

/home/cognizac/anaconda2/lib/python2.7/site-packages/fabric/network.pyc in host_prompting_wrapper(*args, **kwargs)
    681                                     " host string for connection: ")
    682             env.update(to_dict(host_string))
--> 683         return func(*args, **kwargs)
    684     host_prompting_wrapper.undecorated = func
    685     return host_prompting_wrapper

/home/cognizac/anaconda2/lib/python2.7/site-packages/fabric/operations.pyc in run(command, shell, pty, combine_stderr, quiet, warn_only, stdout, stderr, timeout, shell_escape, capture_buffer_size)
   1088         command, shell, pty, combine_stderr, quiet=quiet,
   1089         warn_only=warn_only, stdout=stdout, stderr=stderr, timeout=timeout,
-> 1090         shell_escape=shell_escape, capture_buffer_size=capture_buffer_size,
   1091     )
   1092 

/home/cognizac/anaconda2/lib/python2.7/site-packages/fabric/operations.pyc in _run_command(command, shell, pty, combine_stderr, sudo, user, quiet, warn_only, stdout, stderr, group, timeout, shell_escape, capture_buffer_size)
    931             combine_stderr=combine_stderr, invoke_shell=False, stdout=stdout,
    932             stderr=stderr, timeout=timeout,
--> 933             capture_buffer_size=capture_buffer_size)
    934 
    935         # Assemble output string

/home/cognizac/anaconda2/lib/python2.7/site-packages/fabric/operations.pyc in _execute(channel, command, pty, combine_stderr, invoke_shell, stdout, stderr, timeout, capture_buffer_size)
    815                     worker.raise_if_needed()
    816             try:
--> 817                 time.sleep(ssh.io_sleep)
    818             except KeyboardInterrupt:
    819                 if not remote_interrupt:

KeyboardInterrupt: 

In [28]:
# after you've stopped the server, make sure it's not still running remotely
sudo('killall python')


[ec2-35-166-108-198.us-west-2.compute.amazonaws.com] sudo: killall python
/bin/bash: line 1:  8006 Terminated              python myproject.py
Out[28]:
''
[ec2-35-166-108-198.us-west-2.compute.amazonaws.com] out: 

Once everything works, restart the project and nginx services to serve the routes you've set up:


In [29]:
sudo('service myproject start')
sudo('service nginx start')


[ec2-35-166-108-198.us-west-2.compute.amazonaws.com] sudo: service myproject start
[ec2-35-166-108-198.us-west-2.compute.amazonaws.com] sudo: service nginx start
Out[29]:
''

In [ ]: