FastHandle - IT Operations Examples

FastHandle is fast operation tools for infrastructure configurations and tests.

User Tools

Site Tools


preparation:fhhome:test.py.html



Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
preparation:fhhome:test.py.html [2017/12/04 00:34]
kurihara
preparation:fhhome:test.py.html [2019/02/12 23:29]
kurihara
Line 1: Line 1:
-~~NOTOC~~ 
 [[:index.html#Preparation|Top#Preparation]] [[:index.html#Preparation|Top#Preparation]]
 +====== $FHHOME/$FHLINUX/fabfile/__init__.py ======
  
-====== fabfile/test.py ======+ 
 + 
 + 
 +===== fabfile/__init__.py ===== 
 + 
 +<sxh bash toolbar:false gutter:false> 
 +import sys, os, glob, select, importlib, re 
 +from fabric.api import * 
 +from fabric.contrib import files 
 +from datetime import datetime 
 + 
 +#====================================================== 
 +# SSH User 
 +#====================================================== 
 +hostname = os.uname()[1] 
 + 
 +if hostname == "fasthandle-1": 
 +    env.user = 'fasthandle' 
 +    env.password = 'fastpass' 
 +elif hostname == "stg-fasthandle-1": 
 +    env.user = 'fasthandle' 
 +    env.password = 'fastpass' 
 +elif hostname == "dev-fasthandle-1": 
 +    env.user = 'fasthandle' 
 +    env.key_filename = '$FHHOME/key/id_rsa.fasthandle.dev-fasthandle-1' 
 +    #env.password = 'passphrase-for-key' 
 + 
 +#====================================================== 
 +# Variable 
 +#====================================================== 
 +FHHOME=os.environ["FHHOME"
 + 
 +env.warn_only = True 
 +env.port = 22 
 +env.eagerly_disconnect = True 
 + 
 +#don't create *.pyc without __init__.pyc 
 +sys.dont_write_bytecode = True 
 + 
 + 
 + 
 +#====================================================== 
 +#====================================================== 
 +# standard in for example echo x.x.x.x 
 +if select.select([sys.stdin,],[],[],0.0)[0]: 
 +    lines = sys.stdin.read().splitlines() 
 +    env.hosts = filter(bool, lines) 
 + 
 + 
 +#FastHandle Operation History to log file 
 +TIME = datetime.now().strftime('%Y-%m-%d %H:%M:%S'
 +yymm = datetime.now().strftime('%Y%m'
 + 
 +f=open("%s/log/fab.%s.log" % (FHHOME,yymm) ,"a"
 + 
 +if env.hosts == []: 
 +  f.write("%s stdin-null " % TIME) 
 +  f.write(' '.join(str(p) for p in sys.argv)) 
 +  f.write("\n"
 + 
 +for p in env.hosts: 
 +  f.write("%s %s " % (TIME,p)) 
 +  f.write(' '.join(str(p) for p in sys.argv)) 
 +  f.write("\n"
 +f.close() 
 + 
 + 
 +# auto import *.py 
 +# Ref. https://gist.github.com/fereria/3331554f4c480679716b#file-__init__-py 
 +pathThisFile = os.path.dirname(os.path.abspath(__file__)) 
 + 
 +def loadModule(): 
 + 
 +    myself = sys.modules[__name__] 
 + 
 +    #print __name__ 
 + 
 +    mod_paths = glob.glob(os.path.join(pathThisFile, '*.py')) 
 +    for py_file in mod_paths: 
 +        mod_name = os.path.splitext(os.path.basename(py_file))[0] 
 +        if re.search(".*__init__.*",mod_name) is None: 
 +            mod = importlib.import_module(__name__+ "." + mod_name) 
 +            #for m in mod.__dict__.keys(): 
 +                #if not m in ['__builtins__', '__doc__', '__file__', '__name__', '__package__']: 
 +                    #myself.__dict__[m] = mod.__dict__[m] 
 +loadModule() 
 +</sxh> 
 + 
 + 
 + 
 +\\ 
 +\\ 
 +<WRAP box 90%> 
 +[[:index.html#Preparation|Top#Preparation]] 
 +<catlist ..: -noAddPageButton -noHead> 
 +</WRAP> 
 +\\ 
 +[[:index.html#Preparation|Top#Preparation]]~~NOTOC~~ 
 +[[:index.html#Preparation|Top#Preparation]] 
 + 
 +====== $FHLINUX/fabfile/test.py ======
  
  
Line 12: Line 112:
 $ fab local_hostname $ fab local_hostname
  
-$ fab -l |grep test. +$ fab -l |grep test.    # check options 
-$ echo xx.xx.xx.xx | fab auth.test test.hostname+ 
 +$ echo xx.xx.xx.xx | fab test.hostname 
 +$ fhghost.sh "*" hosts/test | fab test.hostname
 </sxh> </sxh>
  
Line 20: Line 122:
  
 <sxh bash toolbar:false gutter:false> <sxh bash toolbar:false gutter:false>
-import sys+import sys, os
 from fabric.api import * from fabric.api import *
 from fabric.contrib import files from fabric.contrib import files
 +from datetime import datetime
  
 +FHHOME=os.environ["FHHOME"]
 +
 +# test.local_hostname
 @task @task
 def local_hostname(): def local_hostname():
     local('hostname')     local('hostname')
  
 +# test.hostname
 @task @task
 def hostname(): def hostname():
     run('hostname')     run('hostname')
  
 +
 +# test.sudo
 @task @task
-def cat_etc_shadow():+def sudo(): 
 +    """ sudo cat /etc/shadow """
     sudo('cat /etc/shadow')     sudo('cat /etc/shadow')
 +
 +
 +# test.arg1:hostname
 +@task
 +def arg1(cmd):
 +    """ fab test.arg1:hostname """
 +    run('%s' % cmd)
 +
 +
 +# test.arg2:hostname,whoami
 +@task
 +def arg2(cmd1, cmd2):
 +    """ fab test.arg2:hostname,whoami """
 +    run('%s' % cmd1)
 +    run('%s' % cmd2)
 +    run('%s ; %s' % (cmd1, cmd2) )
 +
 +
 +# test.put
 +@task
 +def put():
 +    date = datetime.now().strftime('%Y%m%d_%H%M')
 +    local('hostname > /tmp/test.%s' % date)
 +    put('/tmp/test.%s' % date, '/tmp/test.%s' % date)
 +    run('ls -lh /tmp/test.%s' % date)
 +    run('cat /tmp/test.%s' % date)
 +
 +# test.get
 +@task
 +def get():
 +    date = datetime.now().strftime('%Y%m%d_%H%M')
 +    run('hostname > /tmp/test.%s' % date)
 +    get('/tmp/test.%s' % date, '/tmp/test.%s' % date)
 +    local('ls -lh /tmp/test.%s' % date)
 +    local('cat /tmp/test.%s' % date)
  
 </sxh> </sxh>


preparation/fhhome/test.py.html.txt · Last modified: 2019/02/13 00:18 by kurihara