furodrive

About

Archive

2014-02-20

How to Start/Stop/Restart Gunicorn using Fabric

I want to deploy easily,so I write these scripts.

gunicorn.conf.py

usr/bin/python
gunicorn.conf.py

bind = "0.0.0.0:5000"
workers = 2
worker_class = 'sync'
max_requests = 1000
timeout = 30
keep_alive = 2
preload = True
daemon = True

start.sh

GUNICORN=/usr/bin/gunicorn
ROOT=/your/app/path
PID=/var/run/gunicorn/your.pid
APP=run:app

if [ -f $PID ]; then rm $PID; fi
    cd $ROOT
    source venv/bin/activate
    exec $GUNICORN -c $ROOT/gunicorn.conf.py –pid=$PID $APP

fabfile.py

#coding: utf-8
from fabric.api import run,env,local,settings
from fabric.operations import sudo
from fabric.context_managers import cd
import os
import subprocess

from fabric.api import env, run
env.use_ssh_config = True
env.hosts = ["your ip address"]
env.key_filename = "/root/.ssh/authorized_keys"
env.user = "username"
env.password = "password"


def start():
    with settings():
        with cd("/your/path"):
            sudo("""
            source start.sh
            """,pty=False)

def stop():
    with settings():
        pid = get_pid()
        sudo("kill {}".format(pid))

def restart():
    try:
        stop()
    except:
        print("There isn't pid")
    start()

How to use

$fab start #start gunicorn
$fab stop #stop gunicorn
$fab restart #restart gunicorn

Memo

  • To kill Process , call kill pid command.
  • To call Fabric's task at background ,add sudo's arguments to pty=False
2014-02-17

I made template for making web app using Flask

I made template named heroku-ready for me to make web app made easy using Flask.

This template has these features.

  • Heroku ready.
  • compatible to Python3
  • It can migrate db using Alembic
  • Integrate Postgresql.
  • Separate views from Blueprint

It meets my needs.

INSTALL

$git clone https://github.com/okamurayasuyuki/heroku-ready
$pyvenv-3.4 venv
$source venv/bin/activate
$pip install -r requirements.txt

SET DATABASE_PATH

Set db_path at basic_app.config.py like this.

db_path = 'postgresql://username:password@hostname/db_name'

SETUP For Heroku

$git add .
$git commit -m 'init'
$heroku create
$heroku addons:add heroku-postgresql:dev


$git push heroku master

CREATE DB

Local

$python -i
>> from basic_app import create_app,db
>> create_app().app_context().push()
>> db.create_all()

Remote(Heroku)

$heroku run python -i

>> from basic_app import create_app,db
>> create_app().app_context().push()
>> db.create_all()

MIGRATE DB

Local

$alembic revision --autogenerate -m "add password"
$alembic upgrade head

Remote(Heroku)

$heroku run alembic revision --autogenerate -m "add password"
$heroku run alembic upgrade head

If you want to run some scripts,you have to type like this.

heroku run command

Finally

This template save my time a lot when I create some web app. I feel very happy. So I share This At Github.

2014-01-11

Released ChainDrive App

I released ChainDrive. It makes you more productive. This app is inspired of Don't break the chain method.

How to use

  1. Set goal.
  2. Set task for goal.
  3. Check it and keep going everyday.

You have to check it every day. If you didn't check it in the day, it can't edit and check it again. Through this, you'll try to finish this the tasks everyday more and more.


I've been trying this app for a week and I feel more productive of a person than ever.

I hope you'll be so.

You can download it here.

2013-12-29

I made my favorite UIColor Pallate for iOS

I made My color pallete site for IOS.

It scrape some Japanese color pallete sites.

By using this site,I can find favorite color and copy/paste it easily.

2013-12-22

How to get class's property or method dynamically.

My goal is achieved by getattr or __dict__method.

Code sample is here.

   class G:pass
   g = G()
   g.a = 'hello world'
   g.b = 'foobar'

   for x in ['a','b']:
       print(getattr(g,x))

or

   class G:pass
   g = G()
   g.a = 'hello world'
   g.b = 'foobar'

   for x in ['a','b']:
        g.__dict__(x)
2013-12-17

How to calculate the difference between two dates

2013-12-14

noindex is important

Until today, I don't know how important to write noarchive,noindex to meta tag.

So, I implemented tagging,then generated duplicate link, and my web site's rank was dropped because of google's penalty.

So I write this code to meta tag between head tag.

<meta name="robots" content="noarchive,follow,noindex">

What does above code mean?

I'll explain it shortly.

  • "noarchive" means google not to store a cached copy of this page.
  • "follow" means google not to follow the links on a specific page
  • "noindex" means google not to index a specific page.

I've solved registering duplicate link's problem by insert this code.

2013-12-14

Quick Intro to re.search and re.sub

re.search

re.search(pattern,target_str)

re.sub

re.sub(pattern,repcale,target_str)
2013-12-10

How to save Img's url and retrieve UIImage from this.

I want to save UIImage's path to NSUserDefaults and retrieve UIImage object from this url.

It's better than save UIImage to NSUserDefaults.

I solved it like this.

1. Save UIImage path to NSUserDefaults by ImagePickerController.

2. retrieve img from url by ALAssetsLibrary

2013-12-04

My first Web App "TrickStar"

Today, I'll promote my first web app which named TrickStar. This web service is still buggy and prototype, but I can't wait to show you this service.(Because i feel too lonly to developing only one person.)

What's this?

In short ,TrickStar makes you find good videos for geek easily by take care of the number of your github's followers number.

TrickStar gives you ...?

  • You can promote your/someone upload video and satisfy your desire for recognition by do it.
  • You can find good videos easily .

How does it work?

I'm choose flask to prototype easily. Backend is flask-sqlalchemy(DB is MySQL).

It took 2 months to work it.

What Did you learn to create TrickStar.

I learnd below this.

  • Design is important.
  • Structure is difficcult.
  • Take care of try-and-error times. It should expect 2-3 month to create useful something.
  • DB is difficult to rollback. (It's problem is solved by alembic)

The End.

I want you to try this Service.

If you want to get latest news from me , Please follow my Twitter Account.

If you feel this service is good, please upload tyour/someone youtube's video.

If you have any feedback, please send me message via Twitter.