Fabric/Python:AttributeError:"NoneType"对象没有属性"partition" [英] Fabric/Python: AttributeError: 'NoneType' object has no attribute 'partition'

查看:112
本文介绍了Fabric/Python:AttributeError:"NoneType"对象没有属性"partition"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在结构中具有以下用于添加用户帐户的功能.
〜/scripts #fab -l <​​/p>

Have the following function in fabric for adding user accounts.
~/scripts #fab -l

Python source code

Available commands:

    OS_TYPE
    adduser_createcmd  Create command line for adding user
    adduser_getinfo    Prompts for user input for adding user
    go                 The main launcher for adding user

任务

@task
@runs_once

def go():
    """
    The main launcher for adding user 
    """
    user, uid, comment, group, Group, shell = adduser_getinfo()
    execute(adduser_createcmd(user, uid, comment, group, Group, shell))

@task
@runs_once
def adduser_getinfo ():
    """
    Prompts for user input for adding user 
    """
    with settings(warn_only=True):
        df_user = "test"
        df_uid = "6666"
        df_comment = "Test User"
        df_group = "1936"
        df_Group = "sshusers"
        df_shell = "/bin/bash"

        #read user input or use default values
        user = raw_input ("Enter Username [%s]:" %df_user) or df_user 
        uid = raw_input ("Enter UID # [%s]:" %df_uid) or df_uid
        comment = raw_input ("Enter comments [%s]:" %df_comment) or df_comment
        group = raw_input ("Enter main group [%s]:" %df_group) or df_group 
        Group = raw_input ("Enter supplemental group [%s]:" %df_Group) or df_Group 
        shell = raw_input ("Enter shell [%s]:" %df_shell) or df_shell
            return user, uid, comment, group, Group, shell

@task
def adduser_createcmd (user='', uid='', comment='', group='', Group='', shell=''):
    """
    Create command line for adding user 
    """

    #Linux uses the username for main group, solaris uses companyname
    TYPE = OS_TYPE()
    if TYPE == 'Linux':
            Group = "2222,9999"
            sudo ("useradd -u " + uid + " -c \"" + comment + "\" -G " + Group + " -m " + " -s " + shell + " " + user)
    else:
            env.sudo_prefix = "/usr/local/bin/sudo -S -p '%(sudo_prompt)s' "
            env.shell = "bash --noprofile -l -c "
            Group = "2345,500"
            sudo ("/usr/sbin/useradd -u " + uid + " -c \"" + comment + "\" -g " + group + " -G " + Group + " -m " + " -s " + shell + " " + user)

我是Fabric/python的新手,我想创建一个脚本来将用户添加到多台计算机上.根据计算机的类型,useradd cmd行会更改不同组的b/c.当我运行脚本时,它将在指定的第一台主机上添加用户,然后出错.我从其他答案中看到,某些内容设置为无",但是我不确定哪些内容设置为无".这是运行中的输出和错误.

I am new to fabric/python and I wantaed to create a script that will add users on multiple machines. Depending on the type of machine the useradd cmd line changes b/c of different groups. When I run the script it will add the user on the first host specified then error out. I saw from other answers that something is set to none but I'm not sure what is set to none. This is the running output and error.

~/scripts #fab -H ns1,ons2 go
Enter Username [test]:
Enter UID # [6666]:
Enter comments [Test User]:
Enter main group [1936]:
Enter supplemental group [sshusers]:
Enter shell [/bin/bash]:
Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/fabric/main.py", line 743, in main
    *args, **kwargs
  File "/usr/lib/python2.6/site-packages/fabric/tasks.py", line 368, in execute    
    multiprocessing
  File "/usr/lib/python2.6/site-packages/fabric/tasks.py", line 264, in _execute
    return task.run(*args, **kwargs)
  File "/usr/lib/python2.6/site-packages/fabric/tasks.py", line 171, in run
    return self.wrapped(*args, **kwargs)
  File "/usr/lib/python2.6/site-packages/fabric/decorators.py", line 139, in decorated
    decorated.return_value = func(*args, **kwargs)
  File "/home/jespenc/scripts/fabfile.py", line 70, in go
    execute(adduser_createcmd(user, uid, comment, group, Group, shell))
  File "/usr/lib/python2.6/site-packages/fabric/tasks.py", line 321, in execute
    task = crawl(task, state.commands)
  File "/usr/lib/python2.6/site-packages/fabric/task_utils.py", line 23, in crawl
    result = _crawl(name, mapping)
  File "/usr/lib/python2.6/site-packages/fabric/task_utils.py", line 14, in _crawl
    key, _, rest = name.partition('.')
AttributeError: 'NoneType' object has no attribute 'partition'
Disconnecting from ns1... done.

我确定代码很丑陋,只是我拼凑起来学习python的东西.

I'm sure the code is ugly, just something I've pieced together to learn python.

推荐答案

您应更改行:

 execute(adduser_createcmd(user, uid, comment, group, Group, shell))

至:

 execute(adduser_createcmd, user, uid, comment, group, Group, shell)

第一行告诉Python使用参数(user, uid, comment, group, Group, shell)执行adduser_createcmd函数并将结果传递给execute函数.但是,Python无法执行adduser_createcmd,因为它是Fabric任务,应该由Fabric运行时在远程主机上执行.

The first line tells Python to execute the adduser_createcmd function with the arguments (user, uid, comment, group, Group, shell)and pass the result to the execute function. However, Python is not able to execute the adduser_createcmd as it is a Fabric task which should be executed on a remote host by the Fabric runtime.

第二行将函数adduser_createcmd和参数(user, uid, comment, group, Group, shell)作为参数传递给execute函数. Fabric运行时将在您指定的远程主机上运行adduser_createcmd函数,从而传播参数.

The second line passes as argument the function adduser_createcmd and the arguments (user, uid, comment, group, Group, shell) to the execute function. The Fabric runtime will run the adduser_createcmd function on the remote hosts you specified, propagating the arguments.

这篇关于Fabric/Python:AttributeError:"NoneType"对象没有属性"partition"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆