This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 303
fleetctl: Adding restart command #1238
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,104 @@ | ||
/* | ||
Copyright 2014 CoreOS, Inc. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2015 |
||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/coreos/fleet/job" | ||
"github.com/coreos/fleet/log" | ||
"github.com/coreos/fleet/schema" | ||
) | ||
|
||
var cmdRestartUnit = &Command{ | ||
Name: "restart", | ||
Summary: "Instruct systemd to rolling restart one or more units in the cluster.", | ||
Usage: "[--block-attempts=N] UNIT...", | ||
Description: `Restarts one or more units in the cluster. If they are stopped it starts them. | ||
|
||
Instructs systemd on the host machine to stop then start the unit, deferring to systemd | ||
completely for any custom restart directives (i.e. ExecStop options in the unit | ||
file). | ||
|
||
For units which are not global, restart operations are performed synchronously, | ||
which means fleetctl will block until it detects that the unit(s) have | ||
transitioned to a stopped state and then back to a started state. This behaviour can be configured with the | ||
respective --block-attempts options. Restart operations on global | ||
units are always non-blocking. | ||
|
||
Restart a single unit: | ||
fleetctl restart foo.service | ||
|
||
Restart an entire directory of units with glob matching, without waiting: | ||
fleetctl restart myservice/*`, | ||
Run: runRestartUnit, | ||
} | ||
|
||
func init() { | ||
cmdRestartUnit.Flags.IntVar(&sharedFlags.BlockAttempts, "block-attempts", 0, "Wait until the units are stopped/started, performing up to N attempts before giving up. A value of 0 indicates no limit. Does not apply to global units.") | ||
} | ||
|
||
func runRestartUnit(args []string) (exit int) { | ||
units, err := findUnits(args) | ||
if err != nil { | ||
stderr("%v", err) | ||
return 1 | ||
} | ||
if len(units) == 0 { | ||
stderr("No units were found") | ||
return 1 | ||
} | ||
for _, u := range units { | ||
if job.JobState(u.CurrentState) == job.JobStateInactive && !suToGlobal(u) { | ||
stderr("Unable to restart unit %s in state %s", u.Name, job.JobStateInactive) | ||
return 1 | ||
} else if job.JobState(u.CurrentState) == job.JobStateLoaded { | ||
log.Infof("Unit(%s) already %s, starting.", u.Name, job.JobStateLoaded) | ||
|
||
exit = setUnitStateAndWait(u, job.JobStateLaunched, sharedFlags.BlockAttempts) | ||
if exit == 1 { | ||
return exit | ||
} | ||
continue | ||
} else { | ||
//stop and start it | ||
exit = setUnitStateAndWait(u, job.JobStateLoaded, sharedFlags.BlockAttempts) | ||
if exit == 1 { | ||
return exit | ||
} | ||
exit = setUnitStateAndWait(u, job.JobStateLaunched, sharedFlags.BlockAttempts) | ||
if exit == 1 { | ||
return exit | ||
} | ||
} | ||
log.Infof("Unit(%s) was restarted.", u.Name) | ||
} | ||
return | ||
} | ||
|
||
func setUnitStateAndWait(unit schema.Unit, targetState job.JobState, blockAttempts int) (exit int) { | ||
cAPI.SetUnitTargetState(unit.Name, string(targetState)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle the error from this |
||
|
||
if !suToGlobal(unit) { | ||
errchan := waitForUnitStates([]string{unit.Name}, targetState, blockAttempts, os.Stdout) | ||
for err := range errchan { | ||
stderr("Error waiting for unit %s to change states: %v", unit.Name, err) | ||
return 1 | ||
} | ||
} | ||
return | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move the restart-related documentation to the bottom of the "Start and stop units" block.