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

2024/9/24 12:19:30

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

Python source codeAvailable commands:OS_TYPEadduser_createcmd  Create command line for adding useradduser_getinfo    Prompts for user input for adding usergo                 The main launcher for adding user

The tasks

@task
@runs_oncedef 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 valuesuser = raw_input ("Enter Username [%s]:" %df_user) or df_user uid = raw_input ("Enter UID # [%s]:" %df_uid) or df_uidcomment = raw_input ("Enter comments [%s]:" %df_comment) or df_commentgroup = 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_shellreturn 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 companynameTYPE = 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, **kwargsFile "/usr/lib/python2.6/site-packages/fabric/tasks.py", line 368, in execute    multiprocessingFile "/usr/lib/python2.6/site-packages/fabric/tasks.py", line 264, in _executereturn task.run(*args, **kwargs)File "/usr/lib/python2.6/site-packages/fabric/tasks.py", line 171, in runreturn self.wrapped(*args, **kwargs)File "/usr/lib/python2.6/site-packages/fabric/decorators.py", line 139, in decorateddecorated.return_value = func(*args, **kwargs)File "/home/jespenc/scripts/fabfile.py", line 70, in goexecute(adduser_createcmd(user, uid, comment, group, Group, shell))File "/usr/lib/python2.6/site-packages/fabric/tasks.py", line 321, in executetask = crawl(task, state.commands)File "/usr/lib/python2.6/site-packages/fabric/task_utils.py", line 23, in crawlresult = _crawl(name, mapping)File "/usr/lib/python2.6/site-packages/fabric/task_utils.py", line 14, in _crawlkey, _, 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.

Answer

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.

https://en.xdnf.cn/q/71705.html

Related Q&A

Python object @property

Im trying to create a point class which defines a property called "coordinate". However, its not behaving like Id expect and I cant figure out why. class Point:def __init__(self, coord=None…

Tkinter - Inserting text into canvas windows

I have a Tkinter canvas populated with text and canvas windows, or widgets, created using the create_text and create_window methods. The widgets I place on the canvas are text widgets, and I want to in…

How to run nosetests without showing of my matplotlibs graph?

I try to run my test without any messages displaying from my main program. I only want verbose messages from nosetests to display.For example: nosetests -v --nologcaptureAll of my printout messages fro…

Variable name to string in Python

I have written some code in Python and I would like to save some of the variables into files having the name of the variable. The following code:variableName1 = 3.14 variableName2 = 1.09 save_variable_…

In Python, how to print FULL ISO 8601 timestamp, including current timezone

I need to print the FULL local date/time in ISO 8601 format, including the local timezone info, eg:2007-04-05T12:30:00.0000-02:00I can use datetime.isoformat() to print it, if I have the right tzinfo o…

TextVariable not working

I am trying to get the Text out of an Entry widget in Tkinter. It works with Entry1.get(), but it does not work using textvariableWhat am I doing wrong ? from Tkinter import * master = Tk() v = String…

Is there a Python module to get next runtime from a crontab-style time definition?

Im writing a dashboard application and I need a way to figure out how long an item is "valid", i.e. when should it have been superseded by a new value (its possible to have an error such that…

Django Generic Relations error: cannot resolve keyword content_object into field

Im using Djangos Generic Relations to define Vote model for Question and Answer models. Here is my vote model:models.pyclass Vote(models.Model):user_voted = models.ForeignKey(MyUser)is_upvote = models.…

How to benchmark unit tests in Python without adding any code

I have a Python project with a bunch of tests that have already been implemented, and Id like to begin benchmarking them so I can compare performance of the code, servers, etc over time. Locating the …

Digit recognition with Tesseract OCR and python

I use Tesseract and python to read digits (from a energy meter). Everything works well except for the number "1". Tesseract can not read the "1" Digit.This is the picture I send t…