Skip to content

Commit

Permalink
Merge pull request #1 from knowabli/master
Browse files Browse the repository at this point in the history
pull
  • Loading branch information
timshik authored Apr 15, 2020
2 parents 31ee7de + 4e9196a commit 805c595
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 87 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/edisonCI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python package

on:
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
sudo apt-get install -y python3-pip
pip3 install -r requirements.txt
- name: Lint with flake8
run: |
pip install flake8
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

#Build result

*.log
_pycache_/
58 changes: 57 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,57 @@
# Edison
# Edison - Automated house managment

### Overview

Edison is an open-source home management system.
In its core, Edison is a regular automatic home management system: it reads data from humidity and light sensors and decides when to turn on the air condition and turn on the lights in different rooms.

Edison allows users to write their own house management plans.
Users can share their management plans through the Edison network and users can also download and review plans from other users.

Edison can also manage different sensors from different rooms with different management plans for each room.

## How to spin and tear down the environment

Edison makes the initial setup easy for you by including a provisioner file that runs automatically on **`vagrant up`**.
You will have a fully ready-to-go work environment with a single command.

### How to get started

- Download the proper [VirtualBox](https://www.virtualbox.org/wiki/Downloads) and [Vagrant](https://www.vagrantup.com/downloads.html) package for your operating system and architecture.
- Download Edison to your machine and then open a console window on *edison* folder.
- Write **`vagrant up`** and press enter.
- That's it! Your virtual machine is up and running.
You can enter your virtual machine with **`vagrant ssh`**.
> **Important!** In case that, after `vagrant ssh`, your virtual machine asks for a password the default password is `vagrant`.
### How to save virtual machine state

In order for your next **`vagrant up`** to execute quicker you should save your virtual machine state into a box file format.

- If you are inside the virtual machine write **`exit`** and press enter. Now you are back on your host command line.
- Write **`vagrant halt`** and press enter.
> **Note:** It will shut down the running machine Vagrant is managing.
- Write **`vagrant package --output box-name.box`** and press enter. You can replace *box-name* with any name you want.
> **Note:** It will output your virtual machine into a box file format with .box extension.
- Write **`vagrant box add box-name box-name.box`** and press enter.
Make sure you replaced *box-name* with the name you have chosen on the previous step.
- Edit *Vagrantfile* inside the *edison* folder.
Change the following line `config.vm.box = "ubuntu/xenial64"` to `config.vm.box = "box-name"` (replace *box-name* with the name of the box you have created).
- Run **`vagrant up --no-provision`** on future spins.

### How to tear down the environment

- If you are inside your virtual machine execute the **`exit`** command so you would get back to your host command line.
- Write **`vagrant destroy -f`** and press enter. It sould terminate your virtual machine.
> **Important!** If you have some unsaved work on your virtual machine your data will be lost! In order to save your data you should follow the steps we provided on **How to save virtual machine state**.
## FAQ

Here are some answers to some frequently asked questions:

**Why does `vagrant up` take so long ?**
If you don't have a box to spin your environment from, **`vagrant up`** will take longer to be done.
As part of this command execution our included file `setup.sh` starts to execute and configure Edison environment inside the virtual machine. It downloads all the dependencies Edison requires to run.

**What is box file format ?**
Please read [box file format](https://www.vagrantup.com/docs/boxes/format.html) documentation for more information.
11 changes: 7 additions & 4 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
FLASK_PORT=5000
POSTGRESQL_PORT=5432

Vagrant.configure("2") do |config|
config.vm.box = "bento/ubuntu-18.04"
config.vm.box_version = "202002.14.0"
config.vm.provision :shell, path: "setup.sh", :privileged => false
config.vm.network :forwarded_port, guest: 5000, host: 5000, host_ip: "127.0.0.1"
config.vm.box = "ubuntu/bionic64"
config.vm.provision :shell, path: "setup.sh"
config.vm.network :forwarded_port, guest: FLASK_PORT, host: FLASK_PORT
config.vm.network :forwarded_port, guest: POSTGRESQL_PORT, host: POSTGRESQL_PORT
config.vm.provider "virtualbox" do |v|
v.gui = false
v.name = "Edison_test"
Expand Down
3 changes: 0 additions & 3 deletions killFlask.sh

This file was deleted.

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Flask==1.1.1
9 changes: 0 additions & 9 deletions runFlask.sh

This file was deleted.

82 changes: 13 additions & 69 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -1,76 +1,20 @@
#!/bin/bash

time=`TZ=Asia/Jerusalem date '+%Y%m%d_%H%M'`
VAG_COMMON_DIR="/vagrant"
HOME="/home/vagrant"
LOG_DIR="$VAG_COMMON_DIR/log"
OUTFILE="$LOG_DIR/${time}_vagrant-out.log"
FLASK_APP_PY="$VAG_COMMON_DIR/flask_init.py"
FLASK_EXEC="$HOME/runFlask.sh"
FLASK_LOG="$LOG_DIR/flask-out.log"
KILLFLASK_SCRIPT="$VAG_COMMON_DIR/killFlask.sh"
FLASK_ONBOOT=false # Currently Flask On Startup feature disabled [ Not Supported ]
echo "updating apt before installation"
sudo apt-get update

runSetup() {
step=1
echo "installing python 3.7"
sudo apt-get install -y python3.7

if [ ! -d $LOG_DIR ]; then
mkdir $LOG_DIR
else
# Deleting all vagrant-out log files except for the 5 most recent
cd $LOG_DIR
ls -t | grep vagrant-out.log$ | tail -n +6 | xargs -d '\n' -r rm --
cd $HOME
fi
touch $OUTFILE
echo "installing python3-pip"
sudo apt-get install -y python3-pip

if [ ! -e $FLASK_LOG ]; then
touch $FLASK_LOG
fi
echo "installing PostgreSQL"
sudo apt-get install -y postgresql postgresql-contrib

if [ ! -e $FLASK_APP_PY ]; then
echo "ERROR: Missing $FLASK_APP_PY file..." >> $OUTFILE
exit 1
else
chmod +x $FLASK_APP_PY
fi
echo "install requirements"
pip3 install -r /vagrant/requirements.txt

# Adding execution permission to $KILLFLASK_SCRIPT
if [ -e $KILLFLASK_SCRIPT ]; then
chmod +x $KILLFLASK_SCRIPT
fi

echo " "
echo " "
echo "==> Running Provision Script: $0:"
echo " "
echo "$((step++)). Installing Python PIP3..."
sudo apt-get -yq update >> $OUTFILE 2>&1
sudo apt-get -yq install python3-pip >> $OUTFILE 2>&1
echo ""
echo "$((step++)). Installing Flask..."
sudo pip3 install Flask >> $OUTFILE 2>&1

# Set Flask Run On Starup:
if [ $FLASK_ONBOOT == true ] && [ -e $FLASK_EXEC ]; then
echo "@reboot $FLASK_EXEC >> /dev/null 2>&1 &" > temp_cron
crontab temp_cron
rm temp_cron
fi
echo " "
echo "$((step++)). Initiating Flask Server..."
export FLASK_APP=$FLASK_APP_PY
flask run --host=0.0.0.0 >> $FLASK_LOG 2>&1 &
echo " "

exit 0
}

(runSetup)
if [ $? -eq 0 ]; then
echo "Vagrant Completed Successfully!!!"
exit 0
else
echo "Vagrant Failed! Please see log file under $LOG_DIR"
exit 1
fi
echo "running flask_init.py"
export FLASK_APP=/vagrant/flask_init.py
python3 -m flask run --host=0.0.0.0 >> /vagrant/log.log 2>&1 &
2 changes: 1 addition & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ <h1>Hello World!</h1>
<img style="max-width: 100%" src="{{ url_for('static', filename='hello-world-image.png') }}">

</body>
</html>
</html>

0 comments on commit 805c595

Please sign in to comment.