2014-07-13 3 views
0

로깅 사용자에서 시스템 사용자로 전환하고 해당 사용자로 명령을 실행할 수 있습니까? 직물 사용.시스템 사용자로 fabric 실행 명령

def sometask(): 
    run('mkdir foo') 

@hosts([email protected]) 
def dothis() 
    with change_user(user2):<-- this is what i want to do change user to user2 who is a system user with no password and can't login via ssh 
     with cd('~'): <-- cd to users ~ 
      sometask() <-- run this as user2 
      yetanothertask() <-- run this as user2 
      ............. 
+0

관련 : http://stackoverflow.com/questions/12641514/switch-to-different-user-using-fabric 및 http://stackoverflow.com이 좀처럼 보이는 뭔가 것/questions/10144430/fabric-and-sudo-as-another-user를 사용하십시오. – alecxe

답변

0

당신은 sudo를()를 사용하여 간단한 토글을 허용하고 사용자가 당신이 만들려는 목적의 문맥 관리자로 전환 변수이기로 걸릴있을 수 있습니다.

def set_user(user): 
    #Make into context manager 
    env.custom_user = user 

def bar(): 
    with set_user("foo"): 
     execute("sometask") 

def sometask(): 
    sudo("command", user=env.custom_user)