Check for group existence before adding group

This commit is contained in:
Olivier 'reivilibre' 2021-07-10 16:21:33 +01:00
parent 0f389b4c6d
commit de0fd7af24
2 changed files with 40 additions and 7 deletions

View File

@ -21,7 +21,7 @@ from typing import Optional
from scone.default.steps import linux_steps
from scone.default.utensils.basic_utensils import SimpleExec
from scone.default.utensils.linux_utensils import GetPasswdEntry
from scone.default.utensils.linux_utensils import GetGroupEntry, GetPasswdEntry
from scone.head.head import Head
from scone.head.kitchen import Kitchen, Preparation
from scone.head.recipe import Recipe, RecipeContext
@ -99,14 +99,25 @@ class LinuxGroup(Recipe):
# acknowledge tracking
kitchen.get_dependency_tracker()
result = await kitchen.ut1areq(
SimpleExec(["groupadd", self.group_name], "/"), SimpleExec.Result
grp_entry = await kitchen.ut1a(
GetGroupEntry(self.group_name), GetGroupEntry.Result
)
if result.exit_code != 0:
raise RuntimeError(
"Failed to create group. Error was: " + result.stderr.strip().decode()
if grp_entry:
logger.warning(
"Not updating existing os-group '%s' as it exists already.",
self.group_name,
)
else:
result = await kitchen.ut1areq(
SimpleExec(["groupadd", self.group_name], "/"), SimpleExec.Result
)
if result.exit_code != 0:
raise RuntimeError(
"Failed to create group. Error was: "
+ result.stderr.strip().decode()
)
class DeclareLinuxUser(Recipe):

View File

@ -14,7 +14,7 @@
#
# You should have received a copy of the GNU General Public License
# along with Scone. If not, see <https://www.gnu.org/licenses/>.
import grp
import pwd
import attr
@ -50,3 +50,25 @@ class GetPasswdEntry(Utensil):
shell=entry.pw_shell,
)
)
@attr.s(auto_attribs=True)
class GetGroupEntry(Utensil):
group_name: str
@attr.s(auto_attribs=True)
class Result:
gid: int
async def execute(self, channel: Channel, worktop: Worktop):
try:
entry = grp.getgrnam(self.group_name)
except KeyError:
await channel.send(None)
return
await channel.send(
GetGroupEntry.Result(
gid=entry.gr_gid,
)
)