Like.
I haven't touched on the content that went into it.
It's halfway, but I spent a lot of time because I didn't have all the information. I wonder if it's okay to put together that area. Like tips or addiction.
The final file structure looks like this.
.
├── Vagrantfile
├── ansible.cfg
├── fabfile.py
├── flask
│ ├── main.py
│ ├── reload.trigger
│ ├── runserver.py
│ └── uwsgi.ini
├── inventory
├── playbooks
│ └── python.yml
└── ssh.config
Added a vagrant box with the name centos6.
bash
vagrant box add centos6 http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_centos-6.5_chef-provisionerless.box
There is no particular reason, but I'll make two. I want to deploy all at once with rsync.
If you only need one, just node1.
Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.define :node1 do |node|
node.vm.box = "centos6"
node.vm.network :private_network, ip: "192.168.33.10"
node.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "512"]
end
end
config.vm.define :node2 do |node|
node.vm.box = "centos6"
node.vm.network :private_network, ip: "192.168.33.11"
node.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "512"]
end
end
end
It's convenient when connecting with ssh.
bash
vagrant ssh-config > ssh.config
The contents should look like this.
ssh.config
Host node1
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /Users/kuryu/.vagrant.d/insecure_private_key
IdentitiesOnly yes
LogLevel FATAL
Host node2
HostName 127.0.0.1
User vagrant
Port 2200
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /Users/kuryu/.vagrant.d/insecure_private_key
IdentitiesOnly yes
LogLevel FATAL
bash
vagrant up
Since it will be long, I divided the articles. See here. http://qiita.com/arc279/items/44ac688a2df24569f8af
The inventory looks like this.
inventory
[servers]
node1
node2
Although it is not written in the article above, Write the setting that refers to config when sshing with ansible in the ansible configuration file. Also specify the inventory file.
ansible.cfg
[defaults]
hostfile = inventory
[ssh_connection]
ssh_args = -F ssh.config
If you put ansible.cfg in the current state, it will be read without permission.
Execution is like this.
ansible-playbook playbooks/python.yml
main.py
# -*- coding:utf-8 -*-
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "hello flask!!"
@app.route("/foo")
def foo():
return "foo"
@app.route("/foo/bar")
def foo_bar():
return "foobar"
runserver.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from main import app
options = {
"debug": True,
"threaded": True,
"host": "0.0.0.0",
}
app.run(**options)
If you want to start debugging with Werkzeug, give execution permission to this and hit it.
With touch-reload, specify the file to reload uwsgi when touched. If necessary, you should also specify daemonize.
uwsgi.ini
[uwsgi]
socket = /tmp/flask.wsgi.sock
master = true
uid = vagrant
gid = vagrant
http = :5000
python-path = . #uwsgi startup directory is current
wsgi = main:app
processes = 3
threads = 2
pidfile = ./flask.pid
touch-reload = ./reload.trigger
env = environment=development
It's a very unnatural way of writing, If you specify here in the form of _env = _ __ [key] = [value] __, At startup
import os
print os.environ["environment"]
You can read it at.
When I made pyenv with ansible in ↑, uwsgi should be included with pip, so
With the current of flask
bash
uwsgi uwsgi.ini
Should start with.
rsync must be on both the sync side and the sync side.
Since rsync is also included in the ansible article above, If the playbook passes properly, rsync should be on the vm side.
See here for details. http://qiita.com/shivaken/items/c679ae9d15ac1cbebd0b
like this.
env.use_ssh_config = True
env.ssh_config_path = "ssh.config"
ssh-config is specified to connect as vagrant user, so The connection destination __ $ HOME__ is _ / home / vagrant_.
So
local_dir='./flask/', Flask at hand
remote_dir='~/flask', In _ / home / vagrant / flask_ in vm
rysnc with rsync_project ()
.
fabfile.py
from fabric.api import *
from fabric.decorators import roles
from fabric.contrib.project import rsync_project
import ansible.inventory
env.use_ssh_config = True
env.ssh_config_path = "ssh.config"
inventory = ansible.inventory.Inventory('inventory')
env.roledefs = inventory.groups_list()
def hostname():
run("echo %s" % env.host)
def deploy():
rsync_project(
local_dir='./flask/',
remote_dir='~/flask',
exclude=['.DS_Tore', '*.tmp'],
delete=True
)
reload()
@roles('servers')
def deployall():
deploy()
def reload():
run('touch ~/flask/reload.trigger')
fab
Specify role with -R option.
It seems that the group name of the ansible inventory is role.
bash
fab -R servers deploy
@roles ('servers')
Even here with a decorator.
bash
fab deployall
bash
fab -H node1 deploy
The content has become disorganized.
Deploy to git pull instead of rsync, I think it's okay to make it modern.
If you want to do more
I think there are various things, but that's the point of creating a development environment.
Recommended Posts