func checkProvisioned(host string) (bool, error) { logger.Infof("Checking if %s is already provisioned", host) script := service.ListServicesScript() cmd := ssh.Command("ubuntu@"+host, []string{"/bin/bash"}, nil) var stdout, stderr bytes.Buffer cmd.Stdout = &stdout cmd.Stderr = &stderr cmd.Stdin = strings.NewReader(script) if err := cmd.Run(); err != nil { if stderr.Len() != 0 { err = fmt.Errorf("%v (%v)", err, strings.TrimSpace(stderr.String())) } return false, err } output := strings.TrimSpace(stdout.String()) provisioned := strings.Contains(output, "juju") if provisioned { logger.Infof("%s is already provisioned [%q]", host, output) } else { logger.Infof("%s is not provisioned", host) } return provisioned, nil }
// install installs fake SSH commands, which will respond to // manual provisioning/bootstrapping commands with the specified // output and exit codes. func (r fakeSSH) install(c *gc.C) testing.Restorer { var restore testing.Restorer add := func(input, output interface{}, rc int) { restore = restore.Add(installFakeSSH(c, input, output, rc)) } if !r.SkipProvisionAgent { add(nil, nil, r.ProvisionAgentExitCode) } if !r.SkipDetection { restore.Add(installDetectionFakeSSH(c, r.Series, r.Arch)) } var checkProvisionedOutput interface{} if r.Provisioned { checkProvisionedOutput = "/etc/init/jujud-machine-0.conf" } listCmd := service.ListServicesScript() add(listCmd, checkProvisionedOutput, r.CheckProvisionedExitCode) if r.InitUbuntuUser { add("", nil, 0) } return restore }
func (*serviceSuite) TestListServicesScript(c *gc.C) { script := service.ListServicesScript() expected := strings.Split(service.DiscoverInitSystemScript(), "\n") expected[0] = "init_system=$(" + expected[0] expected[len(expected)-1] += ")" expected = append(expected, `case "$init_system" in`, `systemd)`, ` /bin/systemctl list-unit-files --no-legend --no-page -t service`+ ` | grep -o -P '^\w[\S]*(?=\.service)'`, ` ;;`, `upstart)`, ` sudo initctl list | awk '{print $1}' | sort | uniq`, ` ;;`, `*)`, ` exit 1`, ` ;;`, `esac`, ) c.Check(strings.Split(script, "\n"), jc.DeepEquals, expected) }
func (s *initialisationSuite) TestCheckProvisioned(c *gc.C) { listCmd := service.ListServicesScript() defer installFakeSSH(c, listCmd, "", 0)() provisioned, err := manual.CheckProvisioned("example.com") c.Assert(err, jc.ErrorIsNil) c.Assert(provisioned, jc.IsFalse) defer installFakeSSH(c, listCmd, "juju...", 0)() provisioned, err = manual.CheckProvisioned("example.com") c.Assert(err, jc.ErrorIsNil) c.Assert(provisioned, jc.IsTrue) // stderr should not affect result. defer installFakeSSH(c, listCmd, []string{"", "non-empty-stderr"}, 0)() provisioned, err = manual.CheckProvisioned("example.com") c.Assert(err, jc.ErrorIsNil) c.Assert(provisioned, jc.IsFalse) // if the script fails for whatever reason, then checkProvisioned // will return an error. stderr will be included in the error message. defer installFakeSSH(c, listCmd, []string{"non-empty-stdout", "non-empty-stderr"}, 255)() _, err = manual.CheckProvisioned("example.com") c.Assert(err, gc.ErrorMatches, "subprocess encountered error code 255 \\(non-empty-stderr\\)") }