torstai 25. huhtikuuta 2013

Fabric

Fabric is a Python (2.5 or higher) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.

It provides a basic suite of operations for executing local or remote shell commands (normally or via sudo) and uploading/downloading files, as well as auxiliary functionality such as prompting the running user for input, or aborting execution.

More from Fabric documentation -- > http://docs.fabfile.org/en/1.6/


$ sudo apt-get install fabric

Create new python file

$ nano fabfile.py

insert code

def hi():
print("Hello World!")

$ cat fabfile.py

$ fab -l (list functions)

Install ssh + server
$ apt-get install openssh-server

import fabric library to fabfile.py

from fabric.api import*

env.hosts=["localhost", "172.28.9.100"]

passwd on live cd

Run function from python script

$ fab koneenimi

Make ssh login automatic with keys

$ ssh-keygen
$ ssh-copy-id xubuntu@localhost

sudo visudo

add line under #allow members of group sudo to execute any command

xubuntu ALL=(ALL) NOPASSWD: ALL

to see if package installed correctly
$ dpkg -s softwarename


And now I just post wall of code here. Remember that this is Python so indents must be correct to get this to work!

from fabric.api import*

env.hosts=["localhost","172.28.9.100"] #, "simo@10.10.10.2"
env.roledefs={"server":["localhost"], "workstation": ["10.10.10.10", "10.10.10.11"]}

env.user="xubuntu"
env.parallel=True
env.skip_bad_hosts=True
env.timeout=2
env.warn_only=True

def hi():
    print("Hello World!")

@parallel
@roles ("server")
def koneenimi():
    run("hostname")
@serial
def moi():
    run("echo moi")

@with_settings (warn_only=True)
@ hosts("localhost")
def komento(cmd):
    with settings(warn_only=True):
        if run(cmd).failed:
            sudo(cmd)

def skomento(cmd):
    sudo(cmd)

def asennus():
    sudo("apt-get update")
    sudo("apt-get -y install e3")

def puppetasennus():
    sudo("apt-get update")
    sudo("apt-get -y install puppet")

def puppetmoduli():
    put("/etc/puppet/modules/apache2", "/etc/puppet/modules/", use_sudo=True)

def asen(paketti):
    sudo("apt-get update")
    sudo("apt-get -y install %s" % paketti)

def paikallinenkomento():
    local("echo asensin e3 paketin %s' >> loki.log" % env.hosts)
    sudo("apt-get -y install e3")

def send():
    put("/home/xubuntu/gittiprojekti/loki.log", "/tmp/loki.log",use_sudo=True)

def lataus():
    get("/tmp/loki.log", "/home/xubuntu/gittiprojekti/loki.log" + "." +env.host)
    run("echo jee")

def b():
    put("loki.log.localhost", "/tmp/loki.log.localhost", mode=0755)
    sudo("chown root.root /tmp/loki.log.localhost")

def a():
        put("loki.log.localhost", "/tmp/loki.log.localhost", mirror_local_mode=True)

def testi():
    with settings(user="niki", host_string="localhost"):
        run("whoami")
    with settings(user="pena", host_string="10.10.10.10"):
        run("hostname")

def cdtesti():
    with cd("/tmp/"):
        local("pwd")

def test():
    env.hosts=["localhost"]
    env.user="xubuntu"
    env.parallel=True
    env.warn_only=True

def production():
    env.hosts=["webserver"]
    env.warn_only=False






Ei kommentteja:

Lähetä kommentti