Skip to content

Commit

Permalink
start sshd (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
hopkiw authored Apr 14, 2021
1 parent 2e65c15 commit cfc05e2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
9 changes: 9 additions & 0 deletions google_guest_agent/non_windows_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ func (a *accountsMgr) set() error {
if err := writeGoogleUsersFile(); err != nil {
logger.Errorf("Error writing google_users file: %v.", err)
}

// Start SSHD if not started. We do this in agent instead of adding a
// Wants= directive, and here instead of instance setup, so that this
// can be disabled by the instance configs file.
for _, svc := range []string{"ssh", "sshd"} {
// Ignore output, it's just a best effort.
startService(svc, false)
}

return nil
}

Expand Down
53 changes: 28 additions & 25 deletions google_guest_agent/oslogin.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,22 @@ func (o *osloginMgr) set() error {
logger.Errorf("Error updating group.conf: %v.", err)
}

for _, svc := range []string{"ssh", "sshd", "nscd", "unscd", "systemd-logind", "cron", "crond"} {
for _, svc := range []string{"nscd", "unscd", "systemd-logind", "cron", "crond"} {
if err := restartService(svc); err != nil {
logger.Errorf("Error restarting service: %v.", err)
}
}

// SSH should be explicitly started if not running.
for _, svc := range []string{"ssh", "sshd"} {
if err := startService(svc, true); err != nil {
logger.Errorf("Error restarting service: %v.", err)
} else {
// Stop on first matching, to avoid double restarting.
break
}
}

if enable {
if err := createOSLoginDirs(); err != nil {
logger.Errorf("Error creating OS Login directory: %v.", err)
Expand Down Expand Up @@ -340,35 +350,28 @@ func createOSLoginSudoersFile() error {
return sudoFile.Close()
}

// restartService tries to restart a service on linux-like systems. It attempts
// to find and use the following mechanisms in order:
// 1. The `systemctl` utility, if in a systemd environment.
// 2. The `service` command, if present.
// 3. A SysVinit script directly, if present.
// Missing mechanisms and missing or disabled services are ignored.
// restartService tries to restart a systemd service if it is already running.
func restartService(servicename string) error {
init, err := os.Readlink("/sbin/init")
if err == nil && strings.Contains(init, "systemd") {
if systemctl, err := exec.LookPath("systemctl"); err == nil {
if err := runCmd(exec.Command(systemctl, "is-active", servicename+".service")); err == nil {
return runCmd(exec.Command(systemctl, "restart", servicename+".service"))
}
return nil
if systemctl, err := exec.LookPath("systemctl"); err == nil {
if err := runCmd(exec.Command(systemctl, "is-active", servicename+".service")); err == nil {
return runCmd(exec.Command(systemctl, "restart", servicename+".service"))
}
}
service, err := exec.LookPath("service")
if err == nil {
if err := runCmd(exec.Command(service, servicename, "status")); err == nil {
return runCmd(exec.Command(service, servicename, "restart"))

return nil
}

// startService tries to start a systemd service. If the service is already
// running and restart is true, the service is restarted.
func startService(servicename string, restart bool) error {
if systemctl, err := exec.LookPath("systemctl"); err == nil {
started := nil == runCmd(exec.Command(systemctl, "is-active", servicename+".service"))
if !started {
return runCmd(exec.Command(systemctl, "start", servicename+".service"))
}
return nil
}
initService := "/etc/init.d/" + servicename
if _, err := os.Stat(initService); err == nil {
if err := runCmd(exec.Command(initService, "status")); err == nil {
return runCmd(exec.Command(initService, "restart"))
if started && restart {
return runCmd(exec.Command(systemctl, "restart", servicename+".service"))
}
return nil
}

return nil
Expand Down

0 comments on commit cfc05e2

Please sign in to comment.