Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fabric/Python: AttributeError: 'NoneType' object has no attribute 'partition'

Tags:

python

fabric

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

The tasks

@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)

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.

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

like image 860
user3699853 Avatar asked Jun 02 '14 15:06

user3699853


1 Answers

You should change the line :

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

to :

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

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.

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.

like image 155
Xion345 Avatar answered Oct 28 '22 20:10

Xion345