From f43e5928495098631c2a190b5d4de19adcdbc727 Mon Sep 17 00:00:00 2001 From: Nolan Davidson Date: Tue, 13 Feb 2018 15:13:22 -0500 Subject: [PATCH] [provisioner-habitat] Detect if hab user exists (#17195) Currently the provisioner will fail if the `hab` user already exists on the target system. This adds a check to see if we need to create the user before trying to add it. Fixes #17159 Signed-off-by: Nolan Davidson --- .../habitat/resource_provisioner.go | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/builtin/provisioners/habitat/resource_provisioner.go b/builtin/provisioners/habitat/resource_provisioner.go index 14a98db9f..f9d47ff07 100644 --- a/builtin/provisioners/habitat/resource_provisioner.go +++ b/builtin/provisioners/habitat/resource_provisioner.go @@ -594,7 +594,8 @@ func (p *provisioner) startHabSystemd(o terraform.UIOutput, comm communicator.Co } func (p *provisioner) createHabUser(o terraform.UIOutput, comm communicator.Communicator) error { - // Create the hab user + addUser := false + // Install busybox to get us the user tools we need command := fmt.Sprintf("env HAB_NONINTERACTIVE=true hab install core/busybox") if p.UseSudo { command = fmt.Sprintf("sudo %s", command) @@ -603,11 +604,25 @@ func (p *provisioner) createHabUser(o terraform.UIOutput, comm communicator.Comm return err } - command = fmt.Sprintf("hab pkg exec core/busybox adduser -D -g \"\" hab") + // Check for existing hab user + command = fmt.Sprintf("hab pkg exec core/busybox id hab") if p.UseSudo { command = fmt.Sprintf("sudo %s", command) } - return p.runCommand(o, comm, command) + if err := p.runCommand(o, comm, command); err != nil { + o.Output("No existing hab user detected, creating...") + addUser = true + } + + if addUser { + command = fmt.Sprintf("hab pkg exec core/busybox adduser -D -g \"\" hab") + if p.UseSudo { + command = fmt.Sprintf("sudo %s", command) + } + return p.runCommand(o, comm, command) + } + + return nil } func (p *provisioner) startHabService(o terraform.UIOutput, comm communicator.Communicator, service Service) error {