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

View File

@ -14,7 +14,7 @@
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with Scone. If not, see <https://www.gnu.org/licenses/>. # along with Scone. If not, see <https://www.gnu.org/licenses/>.
import grp
import pwd import pwd
import attr import attr
@ -50,3 +50,25 @@ class GetPasswdEntry(Utensil):
shell=entry.pw_shell, 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,
)
)