Check for group existence before adding group
This commit is contained in:
parent
0f389b4c6d
commit
de0fd7af24
@ -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,14 +99,25 @@ class LinuxGroup(Recipe):
|
|||||||
# acknowledge tracking
|
# acknowledge tracking
|
||||||
kitchen.get_dependency_tracker()
|
kitchen.get_dependency_tracker()
|
||||||
|
|
||||||
result = await kitchen.ut1areq(
|
grp_entry = await kitchen.ut1a(
|
||||||
SimpleExec(["groupadd", self.group_name], "/"), SimpleExec.Result
|
GetGroupEntry(self.group_name), GetGroupEntry.Result
|
||||||
)
|
)
|
||||||
|
|
||||||
if result.exit_code != 0:
|
if grp_entry:
|
||||||
raise RuntimeError(
|
logger.warning(
|
||||||
"Failed to create group. Error was: " + result.stderr.strip().decode()
|
"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):
|
class DeclareLinuxUser(Recipe):
|
||||||
|
@ -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,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user