1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016-2017 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package main
import (
"errors"
"fmt"
"strconv"
"github.com/jessevdk/go-flags"
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/client/clientutil"
"github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/osutil/user"
)
type svcStatus struct {
clientMixin
Positional struct {
ServiceNames []serviceName
} `positional-args:"yes"`
Global bool `long:"global" short:"g"`
User bool `long:"user" short:"u"`
}
type svcLogs struct {
clientMixin
timeMixin
N string `short:"n" default:"10"`
Follow bool `short:"f"`
Positional struct {
ServiceNames []serviceName `required:"1"`
} `positional-args:"yes" required:"yes"`
}
var (
shortServicesHelp = i18n.G("Query the status of services")
longServicesHelp = i18n.G(`
The services command lists information about the services specified, or about
the services in all currently installed snaps.
If executed as root user, the 'Startup' column of any user service will be whether
it's globally enabled (i.e systemctl is-enabled). To view the actual 'Startup'|'Current'
status of the user services for the root user itself, --user can be provided.
If executed as a non-root user, the 'Startup'|'Current' status of user services
will be the current status for the invoking user. To view the global enablement
status of user services, --global can be provided.
`)
shortLogsHelp = i18n.G("Retrieve logs for services")
longLogsHelp = i18n.G(`
The logs command fetches logs of the given services and displays them in
chronological order.
`)
shortStartHelp = i18n.G("Start services")
longStartHelp = i18n.G(`
The start command starts, and optionally enables, the given services.
`)
shortStopHelp = i18n.G("Stop services")
longStopHelp = i18n.G(`
The stop command stops, and optionally disables, the given services.
`)
shortRestartHelp = i18n.G("Restart services")
longRestartHelp = i18n.G(`
The restart command restarts the given services.
If the --reload option is given, for each service whose app has a reload
command, a reload is performed instead of a restart.
`)
)
func init() {
argdescs := []argDesc{{
// TRANSLATORS: This needs to begin with < and end with >
name: i18n.G("<service>"),
// TRANSLATORS: This should not start with a lowercase letter.
desc: i18n.G("A service specification, which can be just a snap name (for all services in the snap), or <snap>.<app> for a single service."),
}}
addCommand("services", shortServicesHelp, longServicesHelp, func() flags.Commander { return &svcStatus{} }, map[string]string{
// TRANSLATORS: This should not start with a lowercase letter.
"global": i18n.G("Show the global enable status for user services instead of the status for the current user."),
// TRANSLATORS: This should not start with a lowercase letter.
"user": i18n.G("Show the current status of the user services instead of the global enable status."),
}, argdescs)
addCommand("logs", shortLogsHelp, longLogsHelp, func() flags.Commander { return &svcLogs{} },
timeDescs.also(map[string]string{
// TRANSLATORS: This should not start with a lowercase letter.
"n": i18n.G("Show only the given number of lines, or 'all'."),
// TRANSLATORS: This should not start with a lowercase letter.
"f": i18n.G("Wait for new lines and print them as they come in."),
}), argdescs)
addCommand("start", shortStartHelp, longStartHelp, func() flags.Commander { return &svcStart{} },
waitDescs.also(userAndScopeDescs).also(map[string]string{
// TRANSLATORS: This should not start with a lowercase letter.
"enable": i18n.G("As well as starting the service now, arrange for it to be started on boot."),
}), argdescs)
addCommand("stop", shortStopHelp, longStopHelp, func() flags.Commander { return &svcStop{} },
waitDescs.also(userAndScopeDescs).also(map[string]string{
// TRANSLATORS: This should not start with a lowercase letter.
"disable": i18n.G("As well as stopping the service now, arrange for it to no longer be started on boot."),
}), argdescs)
addCommand("restart", shortRestartHelp, longRestartHelp, func() flags.Commander { return &svcRestart{} },
waitDescs.also(userAndScopeDescs).also(map[string]string{
// TRANSLATORS: This should not start with a lowercase letter.
"reload": i18n.G("If the service has a reload command, use it instead of restarting."),
}), argdescs)
}
func svcNames(s []serviceName) []string {
svcNames := make([]string, len(s))
for i, svcName := range s {
svcNames[i] = string(svcName)
}
return svcNames
}
func (s *svcStatus) showGlobalEnablement(u *user.User) bool {
if u.Uid == "0" && !s.User {
return true
} else if u.Uid != "0" && s.Global {
return true
}
return false
}
func (s *svcStatus) validateArguments() error {
// can't use --global and --user together
if s.Global && s.User {
return errors.New(i18n.G("cannot combine --global and --user switches."))
}
return nil
}
func (s *svcStatus) Execute(args []string) error {
if len(args) > 0 {
return ErrExtraArgs
}
if err := s.validateArguments(); err != nil {
return err
}
u, err := userCurrent()
if err != nil {
return fmt.Errorf(i18n.G("cannot get the current user: %s."), err)
}
isGlobal := s.showGlobalEnablement(u)
services, err := s.client.Apps(svcNames(s.Positional.ServiceNames), client.AppOptions{
Service: true,
Global: isGlobal,
})
if err != nil {
return err
}
if len(services) == 0 {
fmt.Fprintln(Stderr, i18n.G("There are no services provided by installed snaps."))
return nil
}
w := tabWriter()
defer w.Flush()
fmt.Fprintln(w, i18n.G("Service\tStartup\tCurrent\tNotes"))
for _, svc := range services {
fmt.Fprintln(w, clientutil.FmtServiceStatus(svc, clientutil.FmtServiceStatusOptions{
IsUserGlobal: isGlobal,
}))
}
return nil
}
func (s *svcLogs) Execute(args []string) error {
if len(args) > 0 {
return ErrExtraArgs
}
sN := -1
if s.N != "all" {
n, err := strconv.ParseInt(s.N, 0, 32)
if n < 0 || err != nil {
return errors.New(i18n.G("invalid argument for flag ‘-n’: expected a non-negative integer argument, or “all”."))
}
sN = int(n)
}
logs, err := s.client.Logs(svcNames(s.Positional.ServiceNames), client.LogOptions{N: sN, Follow: s.Follow})
if err != nil {
return err
}
for log := range logs {
if s.AbsTime {
fmt.Fprintln(Stdout, log.StringInUTC())
} else {
fmt.Fprintln(Stdout, log)
}
}
return nil
}
var userAndScopeDescs = mixinDescs{
// TRANSLATORS: This should not start with a lowercase letter.
"system": i18n.G("The operation should only affect system services."),
// TRANSLATORS: This should not start with a lowercase letter.
"user": i18n.G("The operation should only affect user services for the current user."),
// TRANSLATORS: This should not start with a lowercase letter.
"users": i18n.G("If provided and set to 'all', the operation should affect services for all users."),
}
type svcStart struct {
waitMixin
clientutil.ServiceScopeOptions
Positional struct {
ServiceNames []serviceName `required:"1"`
} `positional-args:"yes" required:"yes"`
Enable bool `long:"enable"`
}
func (s *svcStart) Execute(args []string) error {
if len(args) > 0 {
return ErrExtraArgs
}
if err := s.Validate(); err != nil {
return err
}
names := svcNames(s.Positional.ServiceNames)
changeID, err := s.client.Start(names, s.Scope(), s.Users(), client.StartOptions{Enable: s.Enable})
if err != nil {
return err
}
if _, err := s.wait(changeID); err != nil {
if err == noWait {
return nil
}
return err
}
fmt.Fprintln(Stdout, i18n.G("Started."))
return nil
}
type svcStop struct {
waitMixin
clientutil.ServiceScopeOptions
Positional struct {
ServiceNames []serviceName `required:"1"`
} `positional-args:"yes" required:"yes"`
Disable bool `long:"disable"`
}
func (s *svcStop) Execute(args []string) error {
if len(args) > 0 {
return ErrExtraArgs
}
if err := s.Validate(); err != nil {
return err
}
names := svcNames(s.Positional.ServiceNames)
changeID, err := s.client.Stop(names, s.Scope(), s.Users(), client.StopOptions{Disable: s.Disable})
if err != nil {
return err
}
if _, err := s.wait(changeID); err != nil {
if err == noWait {
return nil
}
return err
}
fmt.Fprintln(Stdout, i18n.G("Stopped."))
return nil
}
type svcRestart struct {
waitMixin
clientutil.ServiceScopeOptions
Positional struct {
ServiceNames []serviceName `required:"1"`
} `positional-args:"yes" required:"yes"`
Reload bool `long:"reload"`
}
func (s *svcRestart) Execute(args []string) error {
if len(args) > 0 {
return ErrExtraArgs
}
if err := s.Validate(); err != nil {
return err
}
names := svcNames(s.Positional.ServiceNames)
changeID, err := s.client.Restart(names, s.Scope(), s.Users(), client.RestartOptions{Reload: s.Reload})
if err != nil {
return err
}
if _, err := s.wait(changeID); err != nil {
if err == noWait {
return nil
}
return err
}
fmt.Fprintln(Stdout, i18n.G("Restarted."))
return nil
}
|