Skip to content

ukhan2014/petbuddy_cloud

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

These instructions are for raspi 3 rev B hardware running raspbian.

Python Flask web server which will:
  1. enumerate any petbuddy-device (PBD) which connects to the server and manage a connection to it.
  2. enumerate any petbuddy-companion app (PBC) which connects to the server and manage a connection to it.
  3. serve as a middleman between the PBD and the PBC.
  4. use a sql server which will track all PBDs out in the field. It will offer a signup process during the onboarding that will be accessed through the PBC app and perhaps also a web interface


New instructions (python3):
        i. Clone this repo
       ii. Do python3 in terminal and make sure it is installed
      iii. Do pip3 in terminal to check if it is there [if not do "sudo apt-get install python3-pip"]
       iv. Once pip3 is installed do "sudo pip3 install virtualenv" to install virtualenv
        v. Do "virtualenv petbuddy_cloud" to create a virtualenv inside the repo folder. Three additional folders will be created [bin/ include/ lib/]
       vi. After the virtualenv is set up, go into the petbuddy cloud folder "cd petbuddy_cloud" and activate the virtualenv ". bin/activate"
      vii. 

Old instructions (python2):
1. Clone the petbuddy_cloud repo and Install python2

2. Install virtualenv:
             $ sudo apt-get install virtualenv
             $ virtualenv --version
             $ virtualenv petbuddy_cloud          #this will create a virtual python environment inside the petbuddy_cloud folder
             $ cd petbuddy_cloud
             $ . bin/activate                     #this will get virtualenv to treat this like a local environment, after this
                                                  #the shell prompt will have (petbuddy_cloud) before the $ sign:
             (petbuddy_cloud) $                   #Now everything that is installed for python using pip will be installed locally in this folder

             You can see the local virtual environment that is created by using the tree command inside the petbuddy_cloud folder:
             (petbuddy_cloud) cabana@cabana-desktop:~/petbuddy_cloud$ tree -L 1
              .
              ├── bin
              ├── include
              ├── lib
              ├── local
              ├── petbuddy_cloud
              ├── petbuddy.wsgi
              ├── pip-selfcheck.json
              ├── README
              ├── runserver.py
              └── share

3. Install python stuff, this must be done in the virtualenv:
             - pip install Flask
             - pip install flask-wtf
             - pip install flask-mail
             - pip install flask-sqlalchemy

             - pip install mysql-python
               ** if this fails then try this:
                  $ apt-get install libmysqlclient-dev
                  $ pip install mysql-python

4. Install apache2 and mod wsgi:
             $ sudo apt-get install apache2
                ** visit the ip address of the server machine from an external machine's browser
                   you should see the apache2 Ubuntu default page

             $ sudo apt-get install libapache2-mod-wsgi python-dev
                ** WSGI (Web Server Gateway Interface) is an interface between web servers and web
                   apps for python. Mod_wsgi is an Apache HTTP server mod that enables Apache to 
                   serve Flask applications.

             $ cd /var/www
             $ ls
                ** there should be a html folder in here
             $ sudo ln -s /path/to/petbuddy_cloud ./petbuddy_cloud
                ** this will create a symlink to the petbuddy_cloud repo so that apache sees it

             $ sudo cp petbuddy_cloud/apache/petbuddy_cloud.conf /etc/apache2/sites-available/000-default.conf
                ** This will configure a new virtual host for petbuddy_cloud app with apache.
                   There is apparently a better way to do this, with each site having it's own
                   conf file, but for me something was missing and I didn't have time to make
                   it work. 

             - Location of apache logs:
               /var/log/apache2/error.log
               ** This log file will help debug if the site doesn't load or has some errors.
                  A good way to use it is to delete it and then restart apache, and then check
                  the logs.
             - To restart the apache server:
             $ sudo apachectl restart

             - Better way to setup the conf file is to name the conf file petbuddy_cloud
               and then:
             $ sudo a2ensite petbuddy_cloud
             Enabling site petbuddy_cloud.
             To activate the new configuration, you need to run:
               service apache2 reload
                ** This will enable the virtual host
                ** With this method the 000-default.conf file that was already there
                   for the default apache web page kept displaying that index.html
                   file whenever I accessed the server from outside. So I just ended
                   up removing the 000-default.conf file and renaming my
                   petbuddy_cloud.conf file to 000-default.conf

5. Webapp:
            Automated welcome e-mail:
            Need to modify the __init__.py file to contain the app e-mail account login info under:
              app.config["MAIL_USERNAME"] = '[email protected]'
              app.config["MAIL_PASSWORD"] = "S0m3P@55w0rd"
              
            Logging:
              Using this: https://docs.python.org/2/howto/logging.html
              Apache is unable to create a logfile in the pbd directory though. So first step of creating the logfile ends up failing.
              Permissions issue. mod_wsgi seems to be running as a different user and doesn't have permissions for the project directory.
              So to fix this, just create a folder that mod_wsgi is allowed to access (r/w):
              1. Create a dir under /var/www/ called logs:
                 $ sudo mkdir /var/www/logs
              2. Go to the directory:
                 $ cd /var/www/
              3. Change the owner to www-data user:
                 $ sudo chown www-data logs
              4. Change the group to www-data group:
                 $ sudo chgrp www-data logs

6. mysql setup:

              *************************************************************************************
              * 1. First install mysql
              *    - check if you have it:
              *      $ mysql --version
              *    - if not then:
              *      $ sudo apt-get install mysql-server mysql-client
              *    - mysql-server will ask for root install password during install, set
              *      this and save it in something like keepass
              *
              * 2. Sign into mysql
              *    - $ mysql -u root -p
              *
              * 3. Create a database where we will store our data tables
              *   - CREATE DATABASE development;
              * 
              * 4. Create a table under the development database:
              *   - CREATE TABLE pbdusrs (
              *       device_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
              *       serial_no VARCHAR(100) NOT NULL,
              *       last_ping INT NOT NULL,
              *       ip_add VARCHAR(100) NOT NULL,
              *       email VARCHAR(120) NOT NULL UNIQUE,
              *       pwdhash VARCHAR(100) NOT NULL
              *     );
              *
              * 5. Create a user for the petbuddy application that will run on the server. This
              *    user will be the petbuddy app itself. Whenever the app needs to access user
              *    info, it will access the database as this user:
              *    - CREATE USER 'petbuddycloud'@'localhost' IDENTIFIED BY 'soMeP@55W0RD';
              *        ** We set it for localhost so that only the local app can have access
              *    - GRANT ALL PRIVILEGES ON development.* TO 'petbuddycloud'@'localhost' identified by 'soMeP@55W0RD'
              *        ** give all db permissions to user=petbuddycloud for database=development
              *     
              *    To see all the users:
              *    SELECT User, Host, Password FROM mysql.user;
              *
              *    To add a row entry in the table:
              *     insert into pbdusrs (serial_no, last_ping, ip_add, email, pwdhash)
              *     values ("3rdpbd", 40, "192.168.1.155", "[email protected]", "8838");
              *
              *    To update a row in the table:
              *     UPDATE pbdusrs SET last_ping = 68, ip_add = "192.168.1.155" WHERE device_id = 3;
              *
              *************************************************************************************

7. Testing the server
          To send a POST request with a json string we can use cURL:
          curl -v -H "Content-Type: application/json" -X POST -d '{"key1":"val1","key2":"val2"}' <server ip>/<pagename>

              Additional info:
              ****************
              To access db:
                 ubuntu:$ mysql -u root -p
              This will give you a sql prompt like:
                 mysql>
              We are using the development db. To see names of all the dbs:
                 mysql> show databases;

              Table structure in db that stores info about PBDs:
              | serial_no | last_ping	| ip_add | email_add | password |

              mysql> describe pbdusrs;
              +-----------+--------------+------+-----+---------+----------------+
              | Field     | Type         | Null | Key | Default | Extra          |
              +-----------+--------------+------+-----+---------+----------------+
              | device_id | int(11)      | NO   | PRI | NULL    | auto_increment |
              | serial_no | varchar(100) | NO   |     | NULL    |                |
              | last_ping | int(11)      | NO   |     | NULL    |                |
              | ip_add    | varchar(100) | NO   |     | NULL    |                |
              | email     | varchar(120) | NO   | UNI | NULL    |                |
              | pwdhash   | varchar(100) | NO   |     | NULL    |                |
              +-----------+--------------+------+-----+---------+----------------+

              Debugging stuff with tables in database:
              ****************************************
              First sign into mysql root:
                PB> mysql -u root -p

              To see the entries in the development.pbdusrs table:
                mysql> use development;
                mysql> select * from pbdusrs;
                mysql> select serial_no, last_ping, ip_add, email from pbdusrs;

              To delete an row from the table:
                mysql> delete from pbdusrs where email =  "[email protected]"; 
                
8. Setup with nginx, uwsgi on macOS high sierra:
          1. install nginx
             $ brew install nginx
             The configuration file will be at: /usr/local/etc/nginx/nginx.conf
          2. uwsgi will be started from within the virtual env, it will get nginx going as well
             Put a print statement or two into the __init__.py file of the python app package
             and uwsgi will log it when it starts. This will confirm the package has been started
             properly by uwsgi.
          3. TODO: localhost:8080 does not show app, need to figure out what's going on

***********************************************************************************

Removed stuff from Amazon AWS and switched to home raspi server. Amazon was charging.

Stuff handy for setup on Amazon AWS:
To get the flask app working through apache on the AWS instance, you have to create a virtual environment
using the virtualenv tool. This is because on AWS the environment is local, so all installed tools/utilities
go under a weird ~/.local/ directory. Coupled with apache running as root user, this may have been causing
problems. So what I had to do was create a virtualenv in my petbuddy-cloud directory, install flask in it
and then add the following to the wsgi file:

activate_this = '/path/to/virtualenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this)

import sys
sys.path.insert(0, '/path/to/flask/appname')

***********************************************************************************

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published