forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fleetctl: Add restart and ssh-port functionality
Starts and stops a unit in one command. Adds support to specify SSH port for SSH based commands: * journal * status * ssh * restart refs coreos#760
- Loading branch information
Ryan Walker
committed
Oct 9, 2014
1 parent
2da0dc7
commit bdd106b
Showing
5 changed files
with
162 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"time" | ||
|
||
"github.com/coreos/fleet/schema" | ||
) | ||
|
||
var ( | ||
flagRolling bool | ||
cmdRestartUnit = &Command{ | ||
Name: "restart", | ||
Summary: "Instruct systemd to restart one or more units in the cluster.", | ||
Usage: "[--rolling] [--block-attempts=N] [--ssh-port=N] UNIT...", | ||
Description: `Restart one or more units running in the cluster. | ||
Instructs systemd on the host machine to restart the unit, deferring to systemd | ||
completely for any custom start and stop directives (i.e. ExecStop or ExecStart | ||
options in the unit file). | ||
Restart a single unit: | ||
fleetctl restart foo.service | ||
Restart an entire directory of units with glob matching, one at a time: | ||
fleetctl restart --rolling myservice/*`, | ||
Run: runRestartUnit, | ||
} | ||
) | ||
|
||
func init() { | ||
cmdRestartUnit.Flags.IntVar(&sharedFlags.BlockAttempts, "block-attempts", 0, "Run the restart command, performing up to N attempts before giving up. A value of 0 indicates no limit.") | ||
cmdRestartUnit.Flags.BoolVar(&flagRolling, "rolling", false, "Restart each unit one at a time.") | ||
cmdRestartUnit.Flags.IntVar(&sharedFlags.sshPort, "ssh-port", 22, "Use this SSH port to connect to host machine.") | ||
} | ||
|
||
func runRestartUnit(args []string) (exit int) { | ||
units, err := findUnits(args) | ||
if err != nil { | ||
stderr("%v", err) | ||
return 1 | ||
} | ||
|
||
if !flagRolling { | ||
errchan := waitForUnitsToRestart(units, sharedFlags.BlockAttempts, os.Stdout) | ||
for err := range errchan { | ||
stderr("Error waiting for units: %v", err) | ||
exit = 1 | ||
} | ||
} else { | ||
for _, unit := range units { | ||
rollingRestart(unit, sharedFlags.BlockAttempts) | ||
} | ||
} | ||
return | ||
} | ||
|
||
func rollingRestart(unit schema.Unit, maxAttempts int) (ret bool) { | ||
sleep := 500 * time.Millisecond | ||
if maxAttempts < 1 { | ||
for { | ||
if assertUnitRestart(unit, out) { | ||
ret = true | ||
return | ||
} | ||
time.Sleep(sleep) | ||
} | ||
stderr("Error restarting unit %s", unit.Name) | ||
} else { | ||
for attempt := 0; attempt < maxAttempts; attempt++ { | ||
if assertUnitRestart(unit, out) { | ||
ret = true | ||
return | ||
} | ||
time.Sleep(sleep) | ||
} | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters