/usr/share/gocode/src/github.com/lxc/lxd/lxc/main.go is in golang-github-lxc-lxd-dev 2.0.2-0ubuntu1~16.04.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | package main
import (
"fmt"
"net"
"net/url"
"os"
"os/exec"
"path"
"strings"
"syscall"
"github.com/lxc/lxd"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/gnuflag"
"github.com/lxc/lxd/shared/i18n"
"github.com/lxc/lxd/shared/logging"
)
var configPath string
func main() {
if err := run(); err != nil {
// The action we take depends on the error we get.
msg := fmt.Sprintf(i18n.G("error: %v"), err)
switch t := err.(type) {
case *url.Error:
switch u := t.Err.(type) {
case *net.OpError:
if u.Op == "dial" && u.Net == "unix" {
switch errno := u.Err.(type) {
case syscall.Errno:
switch errno {
case syscall.ENOENT:
msg = i18n.G("LXD socket not found; is LXD running?")
case syscall.ECONNREFUSED:
msg = i18n.G("Connection refused; is LXD running?")
case syscall.EACCES:
msg = i18n.G("Permisson denied, are you in the lxd group?")
default:
msg = fmt.Sprintf("%d %s", uintptr(errno), errno.Error())
}
}
}
}
}
fmt.Fprintln(os.Stderr, fmt.Sprintf("%s", msg))
os.Exit(1)
}
}
func run() error {
verbose := gnuflag.Bool("verbose", false, i18n.G("Enables verbose mode."))
debug := gnuflag.Bool("debug", false, i18n.G("Enables debug mode."))
forceLocal := gnuflag.Bool("force-local", false, i18n.G("Force using the local unix socket."))
noAlias := gnuflag.Bool("no-alias", false, i18n.G("Ignore aliases when determining what command to run."))
configDir := "$HOME/.config/lxc"
if os.Getenv("LXD_CONF") != "" {
configDir = os.Getenv("LXD_CONF")
}
configPath = os.ExpandEnv(path.Join(configDir, "config.yml"))
if len(os.Args) >= 3 && os.Args[1] == "config" && os.Args[2] == "profile" {
fmt.Fprintf(os.Stderr, i18n.G("`lxc config profile` is deprecated, please use `lxc profile`")+"\n")
os.Args = append(os.Args[:1], os.Args[2:]...)
}
if len(os.Args) >= 2 && (os.Args[1] == "-h" || os.Args[1] == "--help") {
os.Args[1] = "help"
}
if len(os.Args) >= 2 && (os.Args[1] == "--all") {
os.Args[1] = "help"
os.Args = append(os.Args, "--all")
}
if len(os.Args) == 2 && os.Args[1] == "--version" {
os.Args[1] = "version"
}
if len(os.Args) < 2 {
commands["help"].run(nil, nil)
os.Exit(1)
}
var config *lxd.Config
var err error
if *forceLocal {
config = &lxd.DefaultConfig
} else {
config, err = lxd.LoadConfig(configPath)
if err != nil {
return err
}
}
// This is quite impolite, but it seems gnuflag needs us to shift our
// own exename out of the arguments before parsing them. However, this
// is useful for execIfAlias, which wants to know exactly the command
// line we received, and in some cases is called before this shift, and
// in others after. So, let's save the original args.
origArgs := os.Args
name := os.Args[1]
/* at this point we haven't parsed the args, so we have to look for
* --no-alias by hand.
*/
if !shared.StringInSlice("--no-alias", origArgs) {
execIfAliases(config, origArgs)
}
cmd, ok := commands[name]
if !ok {
commands["help"].run(nil, nil)
fmt.Fprintf(os.Stderr, "\n"+i18n.G("error: unknown command: %s")+"\n", name)
os.Exit(1)
}
cmd.flags()
gnuflag.Usage = func() {
fmt.Fprintf(os.Stderr, i18n.G("Usage: %s")+"\n\n"+i18n.G("Options:")+"\n\n", strings.TrimSpace(cmd.usage()))
gnuflag.PrintDefaults()
}
os.Args = os.Args[1:]
gnuflag.Parse(true)
shared.Log, err = logging.GetLogger("", "", *verbose, *debug, nil)
if err != nil {
return err
}
certf := config.ConfigPath("client.crt")
keyf := config.ConfigPath("client.key")
if !*forceLocal && os.Args[0] != "help" && os.Args[0] != "version" && (!shared.PathExists(certf) || !shared.PathExists(keyf)) {
fmt.Fprintf(os.Stderr, i18n.G("Generating a client certificate. This may take a minute...")+"\n")
err = shared.FindOrGenCert(certf, keyf)
if err != nil {
return err
}
if shared.PathExists("/var/lib/lxd/") {
fmt.Fprintf(os.Stderr, i18n.G("If this is your first time using LXD, you should also run: sudo lxd init")+"\n")
fmt.Fprintf(os.Stderr, i18n.G("To start your first container, try: lxc launch ubuntu:16.04")+"\n\n")
}
}
err = cmd.run(config, gnuflag.Args())
if err == errArgs {
/* If we got an error about invalid arguments, let's try to
* expand this as an alias
*/
if !*noAlias {
execIfAliases(config, origArgs)
}
fmt.Fprintf(os.Stderr, "%s\n\n"+i18n.G("error: %v")+"\n", cmd.usage(), err)
os.Exit(1)
}
return err
}
type command interface {
usage() string
flags()
showByDefault() bool
run(config *lxd.Config, args []string) error
}
var commands = map[string]command{
"config": &configCmd{},
"copy": ©Cmd{},
"delete": &deleteCmd{},
"exec": &execCmd{},
"file": &fileCmd{},
"finger": &fingerCmd{},
"help": &helpCmd{},
"image": &imageCmd{},
"info": &infoCmd{},
"init": &initCmd{},
"launch": &launchCmd{},
"list": &listCmd{},
"monitor": &monitorCmd{},
"move": &moveCmd{},
"pause": &actionCmd{shared.Freeze, false, false, "pause", -1, false, false, false},
"profile": &profileCmd{},
"publish": &publishCmd{},
"remote": &remoteCmd{},
"restart": &actionCmd{shared.Restart, true, true, "restart", -1, false, false, false},
"restore": &restoreCmd{},
"snapshot": &snapshotCmd{},
"start": &actionCmd{shared.Start, false, true, "start", -1, false, false, false},
"stop": &actionCmd{shared.Stop, true, true, "stop", -1, false, false, false},
"version": &versionCmd{},
}
var errArgs = fmt.Errorf(i18n.G("wrong number of subcommand arguments"))
func expandAlias(config *lxd.Config, origArgs []string) ([]string, bool) {
foundAlias := false
aliasKey := []string{}
aliasValue := []string{}
for k, v := range config.Aliases {
matches := false
for i, key := range strings.Split(k, " ") {
if len(origArgs) <= i+1 {
break
}
if origArgs[i+1] == key {
matches = true
aliasKey = strings.Split(k, " ")
aliasValue = strings.Split(v, " ")
break
}
}
if !matches {
continue
}
foundAlias = true
break
}
if !foundAlias {
return []string{}, false
}
newArgs := []string{origArgs[0]}
hasReplacedArgsVar := false
for i, aliasArg := range aliasValue {
if aliasArg == "@ARGS@" && len(origArgs) > i {
newArgs = append(newArgs, origArgs[i+1:]...)
hasReplacedArgsVar = true
} else {
newArgs = append(newArgs, aliasArg)
}
}
if !hasReplacedArgsVar {
/* add the rest of the arguments */
newArgs = append(newArgs, origArgs[len(aliasKey)+1:]...)
}
/* don't re-do aliases the next time; this allows us to have recursive
* aliases, e.g. `lxc list` to `lxc list -c n`
*/
newArgs = append(newArgs[:2], append([]string{"--no-alias"}, newArgs[2:]...)...)
return newArgs, true
}
func execIfAliases(config *lxd.Config, origArgs []string) {
newArgs, expanded := expandAlias(config, origArgs)
if !expanded {
return
}
path, err := exec.LookPath(origArgs[0])
if err != nil {
fmt.Fprintf(os.Stderr, i18n.G("processing aliases failed %s\n"), err)
os.Exit(5)
}
ret := syscall.Exec(path, newArgs, syscall.Environ())
fmt.Fprintf(os.Stderr, i18n.G("processing aliases failed %s\n"), ret)
os.Exit(5)
}
|