Skip to content

Instantly share code, notes, and snippets.

@jochumdev
Last active June 12, 2018 09:07
Show Gist options
  • Save jochumdev/76fb8060517bb51210ac to your computer and use it in GitHub Desktop.
Save jochumdev/76fb8060517bb51210ac to your computer and use it in GitHub Desktop.
codegangsta/cli for LXD
package main
import (
"github.com/codegangsta/cli"
"os"
"strings"
"github.com/lxc/lxd/shared/i18n"
)
func commandPrintAction(c *cli.Context) {
var flags = []string{}
for _, fn := range c.FlagNames() {
flags = append(flags, "--"+fn+"="+c.String(fn))
}
println(c.Command.HelpName + " " +
strings.Join(flags, " ") + " " +
strings.Join(c.Args(), " "))
}
var commandStandardFlags = []cli.Flag{
cli.StringFlag{
Name: "config",
Value: "",
Usage: i18n.G("Alternate config directory."),
EnvVar: "LXC_CONFIG",
},
// BoolFlag is a switch that defaults to false
cli.BoolFlag{
Name: "debug",
Usage: i18n.G("Print debug information."),
},
cli.BoolFlag{
Name: "verbose, v",
Usage: i18n.G("Print verbose information."),
},
}
var commandConfigDevice = cli.Command{
Name: "device",
Usage: i18n.G("Device manipulation."),
Description: i18n.G(`Device manipulation
lxc config device add <[remote:]container> <name> <type> [key=value]...
Add a device to a container
lxc config device list [remote:]<container> List devices for container
lxc config device show [remote:]<container> Show full device details for container
lxc config device remove [remote:]<container> <name> Remove device from container
`),
Subcommands: []cli.Command{
cli.Command{
Name: "add",
ArgsUsage: "<[remote:]container> <name> <type> [key=value]...",
Usage: i18n.G("Add a device to a container."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "list",
ArgsUsage: "[remote:]<container>",
Usage: i18n.G("List devices for container."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "show",
ArgsUsage: "[remote:]<container>",
Usage: i18n.G("Show full device details for container."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "remove",
ArgsUsage: "[remote:]<container> <name>",
Usage: i18n.G("Remove device from container."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
},
}
var commandConfigTrust = cli.Command{
Name: "trust",
Usage: i18n.G("Trust manipulation."),
Description: i18n.G(`Trust manipulation
lxc config trust list [remote] List all trusted certs.
lxc config trust add [remote] <certfile.crt> Add certfile.crt to trusted hosts.
lxc config trust remove [remote] [hostname|fingerprint]
Remove the cert from trusted hosts.
`),
Subcommands: []cli.Command{
cli.Command{
Name: "list",
ArgsUsage: "[remote]",
Usage: i18n.G("List all trusted certs."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "add",
ArgsUsage: "[remote] <certfile.crt>",
Usage: i18n.G("Add certfile.crt to trusted hosts."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "remove",
ArgsUsage: "[remote] [hostname|fingerprint]",
Usage: i18n.G("Remove the cert from trusted hosts."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
},
}
var commandConfig = cli.Command{
Name: "config",
Usage: i18n.G("Manage configuration."),
Description: i18n.G(`Manage configuration.
lxc config device add <[remote:]container> <name> <type> [key=value]...
Add a device to a container
lxc config device list [remote:]<container> List devices for container
lxc config device show [remote:]<container> Show full device details for container
lxc config device remove [remote:]<container> <name> Remove device from container
lxc config edit [remote:]<container> Edit container configuration in external editor
lxc config get [remote:]<container> key Get configuration key
lxc config set [remote:]<container> key value Set container configuration key
lxc config unset [remote:]<container> key Unset container configuration key
lxc config set key value Set server configuration key
lxc config unset key Unset server configuration key
lxc config show [remote:]<container> Show container configuration
lxc config trust list [remote] List all trusted certs.
lxc config trust add [remote] <certfile.crt> Add certfile.crt to trusted hosts.
lxc config trust remove [remote] [hostname|fingerprint]
Remove the cert from trusted hosts.
Examples:
To mount host's /share/c1 onto /opt in the container:
lxc config device add [remote:]container1 <device-name> disk source=/share/c1 path=opt
To set an lxc config value:
lxc config set [remote:]<container> raw.lxc 'lxc.aa_allow_incomplete = 1'
To listen on IPv4 and IPv6 port 8443 (you can omit the 8443 its the default):
lxc config set core.https_address [::]:8443
To set the server trust password:
lxc config set core.trust_password bla`),
Flags: commandStandardFlags,
Subcommands: []cli.Command{
commandConfigDevice,
cli.Command{
Name: "edit",
ArgsUsage: "[remote:]<container>",
Usage: i18n.G("Edit container configuration in external editor."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "get",
ArgsUsage: "[[remote:]<container>] key",
Usage: i18n.G("Get configuration key."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "unset",
ArgsUsage: "key",
Usage: i18n.G("Unset server configuration key."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "show",
ArgsUsage: "[remote:]<container> key",
Usage: i18n.G("Show container configuration."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
commandConfigTrust,
},
}
var commandFile = cli.Command{
Name: "file",
Usage: i18n.G("Manage files on a container."),
Description: i18n.G(`Manage files on a container.
lxc file pull <source> [<source>...] <target>
lxc file push [--uid=UID] [--gid=GID] [--mode=MODE] <source> [<source>...] <target>
<source> in the case of pull and <target> in the case of push are <container name>/<path>
This operation is only supported on containers that are currently running`),
Flags: commandStandardFlags,
Subcommands: []cli.Command{
cli.Command{
Name: "pull",
ArgsUsage: "<source> [<source>...] <target>",
Usage: i18n.G("Get a file from a container."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "push",
ArgsUsage: "[--uid=UID] [--gid=GID] [--mode=MODE] <source> [<source>...] <target>",
Usage: i18n.G("Push a file to a container."),
Flags: append(commandStandardFlags,
cli.IntFlag{
Name: "gid",
Value: -1,
Usage: i18n.G("Set the file's gid on push."),
},
cli.IntFlag{
Name: "uid",
Value: -1,
Usage: i18n.G("Set the file's uid on push."),
},
cli.StringFlag{
Name: "mode",
Value: "0644",
Usage: i18n.G("Set the file's perms on push."),
},
),
Action: commandPrintAction,
},
},
}
var commandImage = cli.Command{
Name: "image",
Usage: i18n.G("Manipulate container images."),
Description: i18n.G(`lxc image import <tarball> [rootfs tarball] [target] [--public] [--created-at=ISO-8601] [--expires-at=ISO-8601] [--fingerprint=FINGERPRINT] [prop=value]
lxc image copy [remote:]<image> <remote>: [--alias=ALIAS].. [--copy-alias]
lxc image delete [remote:]<image>
lxc image edit [remote:]<image>
lxc image export [remote:]<image>
lxc image info [remote:]<image>
lxc image list [remote:] [filter]
lxc image show [remote:]<image>
Lists the images at specified remote, or local images.
Filters are not yet supported.
lxc image alias create <alias> <target>
lxc image alias delete <alias>
lxc remote add images images.linuxcontainers.org
lxc image alias list images:
create, delete, list image aliases`),
Flags: commandStandardFlags,
Subcommands: []cli.Command{
cli.Command{
Name: "copy",
ArgsUsage: "[remote:]<image> <remote>:",
Usage: i18n.G("Copy an image to another destination."),
Flags: append(commandStandardFlags,
cli.StringSliceFlag{
Name: "alias",
Usage: i18n.G("An alias for this image."),
},
cli.BoolFlag{
Name: "copy-alias",
Usage: i18n.G("Also copy aliases."),
},
),
Action: commandPrintAction,
},
cli.Command{
Name: "delete",
ArgsUsage: "[remote:]<image>",
Usage: i18n.G("Delete an image."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "export",
ArgsUsage: "[remote:]<image>",
Usage: i18n.G("Export an image."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "delete",
ArgsUsage: "[remote:]<image>",
Usage: i18n.G("Delete an image."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "info",
ArgsUsage: "[remote:]<image>",
Usage: i18n.G("Get informations form an image."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "list",
ArgsUsage: "[remote:] [filter]",
Usage: i18n.G("List images."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "show",
ArgsUsage: "[remote:]<image>",
Usage: i18n.G("Show an image."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "alias",
Usage: i18n.G("Manipulate aliases."),
Subcommands: []cli.Command{
cli.Command{
Name: "create",
ArgsUsage: "<alias> <target>",
Usage: i18n.G("Create an alias."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "delete",
ArgsUsage: "<alias>",
Usage: i18n.G("Delete an alias."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "list",
ArgsUsage: "[remote:]",
Usage: i18n.G("List aliases."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
},
},
},
}
var commandProfile = cli.Command{
Name: "profile",
Usage: i18n.G("Manage configuration profiles."),
Description: i18n.G(`Manage configuration profiles.
lxc profile list [filters] List available profiles
lxc profile show <profile> Show details of a profile
lxc profile create <profile> Create a profile
lxc profile edit <profile> Edit profile in external editor
lxc profile copy <profile> <remote> Copy the profile to the specified remote
lxc profile set <profile> <key> <value> Set profile configuration
lxc profile delete <profile> Delete a profile
lxc profile apply <container> <profiles>
Apply a comma-separated list of profiles to a container, in order.
All profiles passed in this call (and only those) will be applied
to the specified container.
Example: lxc profile apply foo default,bar # Apply default and bar
lxc profile apply foo default # Only default is active
lxc profile apply '' # no profiles are applied anymore
lxc profile apply bar,default # Apply default second now
Devices:
lxc profile device list <profile> List devices in the given profile.
lxc profile device show <profile> Show full device details in the given profile.
lxc profile device remove <profile> <name> Remove a device from a profile.
lxc profile device add <profile name> <device name> <device type> [key=value]...
Add a profile device, such as a disk or a nic, to the containers
using the specified profile.`),
Subcommands: []cli.Command{
cli.Command{
Name: "list",
ArgsUsage: "[filters]",
Usage: i18n.G("List available profiles."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "show",
ArgsUsage: "<profile>",
Usage: i18n.G("Show details of a profile."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "create",
ArgsUsage: "<profile>",
Usage: i18n.G("Create a profile."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "copy",
ArgsUsage: "<profile> <remote>",
Usage: i18n.G("Copy the profile to the specified remote."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "set",
ArgsUsage: "<profile> <key> <value>",
Usage: i18n.G("Set profile configuration."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "delete",
ArgsUsage: "<profile>",
Usage: i18n.G("Delete a profile."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "apply",
ArgsUsage: "<container> <profiles>",
Usage: i18n.G("Apply a comma-separated list of profiles to a container."),
Description: i18n.G(`Apply a comma-separated list of profiles to a container.
Apply a comma-separated list of profiles to a container, in order.
All profiles passed in this call (and only those) will be applied
to the specified container.
Example: lxc profile apply foo default,bar # Apply default and bar
lxc profile apply foo default # Only default is active
lxc profile apply '' # no profiles are applied anymore
lxc profile apply bar,default # Apply default second now
`),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "device",
Usage: i18n.G("Profile device manipulation."),
Subcommands: []cli.Command{
cli.Command{
Name: "list",
ArgsUsage: "<profile>",
Usage: i18n.G("List devices in the given profile."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "show",
ArgsUsage: "<profile>",
Usage: i18n.G("Show full device details in the given profile."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "remove",
ArgsUsage: "<profile> <name>",
Usage: i18n.G("Remove a device from a profile."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "add",
ArgsUsage: "<profile name> <device name> <device type> [key=value]...",
Usage: i18n.G(`Add a profile device, such as a disk or a nic, to
the containers using the specified profile.`),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
},
},
},
}
var commandRemote = cli.Command{
Name: "remote",
Usage: i18n.G("Manage remote LXD servers."),
Description: i18n.G(`Manage remote LXD servers.
lxc remote add <name> <url> [--accept-certificate] [--password=PASSWORD] [--public] Add the remote <name> at <url>.
lxc remote remove <name> Remove the remote <name>.
lxc remote list List all remotes.
lxc remote rename <old> <new> Rename remote <old> to <new>.
lxc remote set-url <name> <url> Update <name>'s url to <url>.
lxc remote set-default <name> Set the default remote.
lxc remote get-default Print the default remote.`),
Subcommands: []cli.Command{
cli.Command{
Name: "add",
ArgsUsage: "<name> <url> [--accept-certificate] [--password=PASSWORD] [--public]",
Usage: i18n.G("Add the remote <name> at <url>."),
Flags: append(commandStandardFlags,
cli.BoolFlag{
Name: "accept-certificate",
Usage: i18n.G("Accept certificate."),
},
cli.StringFlag{
Name: "password",
Usage: i18n.G("Remote admin password."),
},
cli.BoolFlag{
Name: "public",
Usage: i18n.G("Public image server."),
},
),
Action: commandPrintAction,
},
cli.Command{
Name: "list",
ArgsUsage: "",
Usage: i18n.G("List all remotes."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "rename",
ArgsUsage: "<old> <new>",
Usage: i18n.G("Rename remote <old> to <new>."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "set-url",
ArgsUsage: "<name> <url>",
Usage: i18n.G("Update <name>'s url to <url>."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "set-default",
ArgsUsage: "<name>",
Usage: i18n.G("Set the default remote."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "get-default",
ArgsUsage: "",
Usage: i18n.G("Print the default remote."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
},
}
func main() {
app := cli.NewApp()
app.Name = "lxc"
app.Version = "0.0.1"
app.Usage = "LXD is pronounced lex-dee."
// app.Flags = commandStandardFlags
app.Commands = []cli.Command{
commandConfig,
cli.Command{
Name: "copy",
Usage: i18n.G("Copy containers within or in between lxd instances."),
ArgsUsage: "[remote:]<source container> [remote:]<destination container>",
Flags: append(commandStandardFlags, cli.BoolFlag{
Name: "ephemeral, e",
Usage: i18n.G("Create a ephemeral copy."),
}),
Action: commandPrintAction,
},
cli.Command{
Name: "delete",
Usage: i18n.G("Delete containers or container snapshots."),
ArgsUsage: "[remote:]<container>[/<snapshot>] [remote:][<container>[/<snapshot>]...]",
Description: i18n.G("Destroy containers or snapshots with any attached data (configuration, snapshots, ...)."),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "exec",
Usage: i18n.G("Execute the specified command in a container."),
ArgsUsage: "[remote:]container [--env EDITOR=/usr/bin/vim]... <command>",
Flags: append(commandStandardFlags, cli.StringFlag{
Name: "env",
Usage: i18n.G("An environment variable of the form HOME=/home/foo."),
Value: "",
}),
Action: commandPrintAction,
},
commandFile,
// TODO: Make this a "hidden" command.
cli.Command{
Name: "finger",
Usage: i18n.G("Fingers the LXD instance to check if it is up and working."),
ArgsUsage: "<remote>",
Flags: commandStandardFlags,
Action: commandPrintAction,
},
commandImage,
cli.Command{
Name: "info",
Usage: i18n.G("List information on containers."),
ArgsUsage: "[<remote>:]container [--show-log]",
Description: i18n.G("This will support remotes and images as well, but only containers for now."),
Flags: append(commandStandardFlags, cli.BoolFlag{
Name: "show-log",
Usage: i18n.G("Also show the log."),
}),
Action: commandPrintAction,
},
// TODO: Make this a "hidden" command.
cli.Command{
Name: "init",
Usage: i18n.G("Initializes a container using the specified image and name."),
ArgsUsage: "[remote:]<image> [remote:][<name>] [--ephemeral|-e] [--profile|-p <profile>...]",
Flags: append(commandStandardFlags,
cli.BoolFlag{
Name: "ephemeral, e",
Usage: i18n.G("Ephemeral container."),
},
cli.StringSliceFlag{
Name: "profile, p",
Usage: i18n.G("Profile to apply to the new container."),
},
),
Action: commandPrintAction,
},
cli.Command{
Name: "launch",
Usage: i18n.G("Launch a container from a particular image."),
ArgsUsage: "[remote:]<image> [remote:][<name>] [--ephemeral|-e] [--profile|-p <profile>...]",
Description: i18n.G(`Launches a container using the specified image and name.
Not specifying -p will result in the default profile.
Specifying "-p" with no argument will result in no profile.`),
Flags: append(commandStandardFlags,
cli.BoolFlag{
Name: "ephemeral, e",
Usage: i18n.G("Ephemeral."),
},
cli.StringSliceFlag{
Name: "profile, p",
Usage: i18n.G("Profile to apply to the new container."),
},
),
Action: commandPrintAction,
},
cli.Command{
Name: "list",
Usage: i18n.G("Lists the available resources."),
ArgsUsage: "[resource] [filters]",
Description: i18n.G(`The filters are:
* A single keyword like "web" which will list any container with "web" in its name.
* A key/value pair referring to a configuration item. For those, the namespace can be abreviated to the smallest unambiguous identifier:
* "user.blah=abc" will list all containers with the "blah" user property set to "abc"
* "u.blah=abc" will do the same
* "security.privileged=1" will list all privileged containers
* "s.privileged=1" will do the same.`),
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "move",
Usage: i18n.G("Move containers within or in between lxd instances."),
ArgsUsage: "[remote:]<source container> [remote:]<destination container>",
Flags: commandStandardFlags,
Action: commandPrintAction,
},
commandProfile,
cli.Command{
Name: "publish",
Usage: i18n.G("Publish containers as images."),
ArgsUsage: "[remote:]container [remote:] [--alias=ALIAS]... [prop-key=prop-value]...",
Flags: append(commandStandardFlags,
cli.BoolFlag{
Name: "force-local",
Usage: i18n.G("Force using the local unix socket."),
},
cli.StringSliceFlag{
Name: "alias",
Usage: i18n.G("New alias to define at target."),
},
cli.BoolFlag{
Name: "public",
Usage: i18n.G("Make the image public."),
},
),
Action: commandPrintAction,
},
commandRemote,
cli.Command{
Name: "restart",
Usage: i18n.G("Changes one or more containers state to restart."),
ArgsUsage: "<name> [<name>...]",
Flags: append(commandStandardFlags,
cli.BoolFlag{
Name: "force",
Usage: i18n.G("Force the container to shutdown."),
},
cli.IntFlag{
Name: "timeout",
Usage: i18n.G("Time to wait for the container before killing it."),
},
),
Action: commandPrintAction,
},
cli.Command{
Name: "restore",
Usage: i18n.G("Set the current state of a resource back to what it was when it was snapshotted."),
ArgsUsage: "[remote:]<resource> <snapshot name> [--stateful]",
Flags: append(commandStandardFlags,
cli.BoolFlag{
Name: "stateful",
Usage: i18n.G("Whether or not to restore the container's running state from snapshot (if available)."),
},
),
Action: commandPrintAction,
},
cli.Command{
Name: "snapshot",
Usage: i18n.G("Create a read-only snapshot of a container."),
ArgsUsage: "[remote:]<source> <snapshot name> [--stateful]",
Flags: append(commandStandardFlags,
cli.BoolFlag{
Name: "stateful",
Usage: i18n.G("Whether or not to snapshot the container's running state."),
},
),
Action: commandPrintAction,
},
cli.Command{
Name: "start",
Usage: i18n.G("Changes one or more containers state to start."),
ArgsUsage: "<name> [<name>...]",
Flags: commandStandardFlags,
Action: commandPrintAction,
},
cli.Command{
Name: "stop",
Usage: i18n.G("Changes one or more containers state to stop."),
ArgsUsage: "<name> [<name>...]",
Flags: append(commandStandardFlags,
cli.BoolFlag{
Name: "force",
Usage: i18n.G("Force the container to shutdown."),
},
cli.IntFlag{
Name: "timeout",
Usage: i18n.G("Time to wait for the container before killing it."),
},
),
Action: commandPrintAction,
},
cli.Command{
Name: "version",
Usage: i18n.G("Prints the version number of LXD."),
ArgsUsage: "",
Flags: commandStandardFlags,
Action: commandPrintAction,
},
}
app.Run(os.Args)
}
@Jyotikumari2018
Copy link

Which computer language is this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment