Merge ~oddbloke/cloud-init/+git/cloud-init:useradd into cloud-init:master

Proposed by Dan Watkins
Status: Merged
Approved by: Chad Smith
Approved revision: a0cc273d4147c4ccabd88d7bc443d70f9a126cde
Merge reported by: Server Team CI bot
Merged at revision: not available
Proposed branch: ~oddbloke/cloud-init/+git/cloud-init:useradd
Merge into: cloud-init:master
Diff against target: 166 lines (+38/-38)
2 files modified
cloudinit/distros/__init__.py (+22/-22)
cloudinit/distros/freebsd.py (+16/-16)
Reviewer Review Type Date Requested Status
Server Team CI bot continuous-integration Approve
Chad Smith Approve
Review via email: mp+371203@code.launchpad.net

Commit message

distros: fix confusing variable names

Building the subp arguments for a `useradd` call in a variable named
`adduser_cmd` is extremely confusing; let's not do that.

(This also changes the snap and freebsd variables to something more
apropos.)

To post a comment you must log in.
Revision history for this message
Server Team CI bot (server-team-bot) wrote :

PASSED: Continuous integration, rev:65f501f39cc3646a761028733a4bfeb370cd9cf5
https://jenkins.ubuntu.com/server/job/cloud-init-ci/1036/
Executed test runs:
    SUCCESS: Checkout
    SUCCESS: Unit & Style Tests
    SUCCESS: Ubuntu LTS: Build
    SUCCESS: Ubuntu LTS: Integration
    IN_PROGRESS: Declarative: Post Actions

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/1036//rebuild

review: Approve (continuous-integration)
Revision history for this message
Ryan Harper (raharper) wrote :

The method is call add_user and the variables match the method.

So now we have

def add_user()
   useradd_cmd =

Isn't this the same confusion?

The command to add a user could be something else. In cloudinit/distros/freebsd.py for example, the adduser_cmd , is 'pw useradd'.

Revision history for this message
Dan Watkins (oddbloke) wrote :

On Mon, Aug 12, 2019 at 02:31:18PM -0000, Ryan Harper wrote:
> The method is call add_user and the variables match the method.

If the variable matched the method, I'd expect it to be add_user_cmd.
I'd be fine with that, if that's what you would prefer?

> So now we have
>
> def add_user()
> useradd_cmd =
>
> Isn't this the same confusion?

I don't think so; the name of the command we do actually use is
hardcoded in a string literal later on that line:

    useradd_cmd = ['useradd', ...]

> The command to add a user could be something else.

Not without code changes, which I would expect to also change the
variable name.

> In cloudinit/distros/freebsd.py for example, the adduser_cmd , is 'pw
> useradd'.

Right, I didn't touch the variable name there.

Revision history for this message
Chad Smith (chad.smith) wrote :

So, can we say freebsd.py should be pw_cmd or pw_useradd_cmd? Since this is not a functional change I'm fine with whatever you gents prefer but maybe since it is a cosmetic/style thing maybe we can fix both implementations to adhere to the same guideline. Then we don't have to look at this specific case in the future.

review: Approve
a0cc273... by Dan Watkins

distros/freebsd: clean up variable name for consistency

Revision history for this message
Chad Smith (chad.smith) wrote :

LGTM!

review: Approve
Revision history for this message
Server Team CI bot (server-team-bot) wrote :

PASSED: Continuous integration, rev:a0cc273d4147c4ccabd88d7bc443d70f9a126cde
https://jenkins.ubuntu.com/server/job/cloud-init-ci/1044/
Executed test runs:
    SUCCESS: Checkout
    SUCCESS: Unit & Style Tests
    SUCCESS: Ubuntu LTS: Build
    SUCCESS: Ubuntu LTS: Integration
    IN_PROGRESS: Declarative: Post Actions

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/1044//rebuild

review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/cloudinit/distros/__init__.py b/cloudinit/distros/__init__.py
2index 20c994d..00bdee3 100644
3--- a/cloudinit/distros/__init__.py
4+++ b/cloudinit/distros/__init__.py
5@@ -396,16 +396,16 @@ class Distro(object):
6 else:
7 create_groups = True
8
9- adduser_cmd = ['useradd', name]
10- log_adduser_cmd = ['useradd', name]
11+ useradd_cmd = ['useradd', name]
12+ log_useradd_cmd = ['useradd', name]
13 if util.system_is_snappy():
14- adduser_cmd.append('--extrausers')
15- log_adduser_cmd.append('--extrausers')
16+ useradd_cmd.append('--extrausers')
17+ log_useradd_cmd.append('--extrausers')
18
19 # Since we are creating users, we want to carefully validate the
20 # inputs. If something goes wrong, we can end up with a system
21 # that nobody can login to.
22- adduser_opts = {
23+ useradd_opts = {
24 "gecos": '--comment',
25 "homedir": '--home',
26 "primary_group": '--gid',
27@@ -418,7 +418,7 @@ class Distro(object):
28 "selinux_user": '--selinux-user',
29 }
30
31- adduser_flags = {
32+ useradd_flags = {
33 "no_user_group": '--no-user-group',
34 "system": '--system',
35 "no_log_init": '--no-log-init',
36@@ -453,32 +453,32 @@ class Distro(object):
37 # Check the values and create the command
38 for key, val in sorted(kwargs.items()):
39
40- if key in adduser_opts and val and isinstance(val, str):
41- adduser_cmd.extend([adduser_opts[key], val])
42+ if key in useradd_opts and val and isinstance(val, str):
43+ useradd_cmd.extend([useradd_opts[key], val])
44
45 # Redact certain fields from the logs
46 if key in redact_opts:
47- log_adduser_cmd.extend([adduser_opts[key], 'REDACTED'])
48+ log_useradd_cmd.extend([useradd_opts[key], 'REDACTED'])
49 else:
50- log_adduser_cmd.extend([adduser_opts[key], val])
51+ log_useradd_cmd.extend([useradd_opts[key], val])
52
53- elif key in adduser_flags and val:
54- adduser_cmd.append(adduser_flags[key])
55- log_adduser_cmd.append(adduser_flags[key])
56+ elif key in useradd_flags and val:
57+ useradd_cmd.append(useradd_flags[key])
58+ log_useradd_cmd.append(useradd_flags[key])
59
60 # Don't create the home directory if directed so or if the user is a
61 # system user
62 if kwargs.get('no_create_home') or kwargs.get('system'):
63- adduser_cmd.append('-M')
64- log_adduser_cmd.append('-M')
65+ useradd_cmd.append('-M')
66+ log_useradd_cmd.append('-M')
67 else:
68- adduser_cmd.append('-m')
69- log_adduser_cmd.append('-m')
70+ useradd_cmd.append('-m')
71+ log_useradd_cmd.append('-m')
72
73 # Run the command
74 LOG.debug("Adding user %s", name)
75 try:
76- util.subp(adduser_cmd, logstring=log_adduser_cmd)
77+ util.subp(useradd_cmd, logstring=log_useradd_cmd)
78 except Exception as e:
79 util.logexc(LOG, "Failed to create user %s", name)
80 raise e
81@@ -490,15 +490,15 @@ class Distro(object):
82
83 snapuser = kwargs.get('snapuser')
84 known = kwargs.get('known', False)
85- adduser_cmd = ["snap", "create-user", "--sudoer", "--json"]
86+ create_user_cmd = ["snap", "create-user", "--sudoer", "--json"]
87 if known:
88- adduser_cmd.append("--known")
89- adduser_cmd.append(snapuser)
90+ create_user_cmd.append("--known")
91+ create_user_cmd.append(snapuser)
92
93 # Run the command
94 LOG.debug("Adding snap user %s", name)
95 try:
96- (out, err) = util.subp(adduser_cmd, logstring=adduser_cmd,
97+ (out, err) = util.subp(create_user_cmd, logstring=create_user_cmd,
98 capture=True)
99 LOG.debug("snap create-user returned: %s:%s", out, err)
100 jobj = util.load_json(out)
101diff --git a/cloudinit/distros/freebsd.py b/cloudinit/distros/freebsd.py
102index ff22d56..f7825fd 100644
103--- a/cloudinit/distros/freebsd.py
104+++ b/cloudinit/distros/freebsd.py
105@@ -185,10 +185,10 @@ class Distro(distros.Distro):
106 LOG.info("User %s already exists, skipping.", name)
107 return False
108
109- adduser_cmd = ['pw', 'useradd', '-n', name]
110- log_adduser_cmd = ['pw', 'useradd', '-n', name]
111+ pw_useradd_cmd = ['pw', 'useradd', '-n', name]
112+ log_pw_useradd_cmd = ['pw', 'useradd', '-n', name]
113
114- adduser_opts = {
115+ pw_useradd_opts = {
116 "homedir": '-d',
117 "gecos": '-c',
118 "primary_group": '-g',
119@@ -196,34 +196,34 @@ class Distro(distros.Distro):
120 "shell": '-s',
121 "inactive": '-E',
122 }
123- adduser_flags = {
124+ pw_useradd_flags = {
125 "no_user_group": '--no-user-group',
126 "system": '--system',
127 "no_log_init": '--no-log-init',
128 }
129
130 for key, val in kwargs.items():
131- if (key in adduser_opts and val and
132+ if (key in pw_useradd_opts and val and
133 isinstance(val, six.string_types)):
134- adduser_cmd.extend([adduser_opts[key], val])
135+ pw_useradd_cmd.extend([pw_useradd_opts[key], val])
136
137- elif key in adduser_flags and val:
138- adduser_cmd.append(adduser_flags[key])
139- log_adduser_cmd.append(adduser_flags[key])
140+ elif key in pw_useradd_flags and val:
141+ pw_useradd_cmd.append(pw_useradd_flags[key])
142+ log_pw_useradd_cmd.append(pw_useradd_flags[key])
143
144 if 'no_create_home' in kwargs or 'system' in kwargs:
145- adduser_cmd.append('-d/nonexistent')
146- log_adduser_cmd.append('-d/nonexistent')
147+ pw_useradd_cmd.append('-d/nonexistent')
148+ log_pw_useradd_cmd.append('-d/nonexistent')
149 else:
150- adduser_cmd.append('-d/usr/home/%s' % name)
151- adduser_cmd.append('-m')
152- log_adduser_cmd.append('-d/usr/home/%s' % name)
153- log_adduser_cmd.append('-m')
154+ pw_useradd_cmd.append('-d/usr/home/%s' % name)
155+ pw_useradd_cmd.append('-m')
156+ log_pw_useradd_cmd.append('-d/usr/home/%s' % name)
157+ log_pw_useradd_cmd.append('-m')
158
159 # Run the command
160 LOG.info("Adding user %s", name)
161 try:
162- util.subp(adduser_cmd, logstring=log_adduser_cmd)
163+ util.subp(pw_useradd_cmd, logstring=log_pw_useradd_cmd)
164 except Exception as e:
165 util.logexc(LOG, "Failed to create user %s", name)
166 raise e

Subscribers

People subscribed via source and target branches