本文整理匯總了Golang中github.com/lxc/lxd.NewClient函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewClient函數的具體用法?Golang NewClient怎麽用?Golang NewClient使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewClient函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: activateIfNeeded
func activateIfNeeded() error {
// Don't start a full daemon, we just need DB access
d := &Daemon{
IsMock: false,
imagesDownloading: map[string]chan bool{},
imagesDownloadingLock: sync.RWMutex{},
}
err := initializeDbObject(d, shared.VarPath("lxd.db"))
if err != nil {
return err
}
// Look for network socket
value, err := d.ConfigValueGet("core.https_address")
if err != nil {
return err
}
if value != "" {
shared.Debugf("Daemon has core.https_address set, activating...")
_, err := lxd.NewClient(&lxd.DefaultConfig, "local")
return err
}
// Look for auto-started or previously started containers
d.IdmapSet, err = shared.DefaultIdmapSet()
if err != nil {
return err
}
result, err := dbContainersList(d.db, cTypeRegular)
if err != nil {
return err
}
for _, name := range result {
c, err := containerLoadByName(d, name)
if err != nil {
return err
}
config := c.ExpandedConfig()
lastState := config["volatile.last_state.power"]
autoStart := config["boot.autostart"]
if lastState == "RUNNING" || lastState == "Running" || autoStart == "true" {
shared.Debugf("Daemon has auto-started containers, activating...")
_, err := lxd.NewClient(&lxd.DefaultConfig, "local")
return err
}
}
shared.Debugf("No need to start the daemon now.")
return nil
}
示例2: doImageAlias
func doImageAlias(config *lxd.Config, args []string) error {
var remote string
switch args[1] {
case "list":
/* alias list [<remote>:] */
if len(args) > 2 {
remote, _ = config.ParseRemoteAndContainer(args[2])
} else {
remote, _ = config.ParseRemoteAndContainer("")
}
d, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
resp, err := d.ListAliases()
if err != nil {
return err
}
showAliases(resp)
return nil
case "create":
/* alias create [<remote>:]<alias> <target> */
if len(args) < 4 {
return errArgs
}
remote, alias := config.ParseRemoteAndContainer(args[2])
target := args[3]
d, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
/* TODO - what about description? */
err = d.PostAlias(alias, alias, target)
return err
case "delete":
/* alias delete [<remote>:]<alias> */
if len(args) < 3 {
return errArgs
}
remote, alias := config.ParseRemoteAndContainer(args[2])
d, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
err = d.DeleteAlias(alias)
return err
}
return errArgs
}
示例3: waitReady
func waitReady() error {
if *timeout < 0 {
*timeout = 15
}
finger := make(chan error, 1)
go func() {
for {
c, err := lxd.NewClient(&lxd.DefaultConfig, "local")
if err != nil {
time.Sleep(500 * 1e6 * time.Nanosecond)
continue
}
err = c.Finger()
if err != nil {
time.Sleep(500 * 1e6 * time.Nanosecond)
continue
}
finger <- nil
return
}
}()
select {
case <-finger:
break
case <-time.After(time.Second * time.Duration(*timeout)):
return fmt.Errorf("LXD still not running after %ds timeout.", *timeout)
}
return nil
}
示例4: run
func (c *monitorCmd) run(config *lxd.Config, args []string) error {
var remote string
if len(args) > 1 {
return errArgs
}
if len(args) == 0 {
remote, _ = config.ParseRemoteAndContainer("")
} else {
remote, _ = config.ParseRemoteAndContainer(args[0])
}
d, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
handler := func(message interface{}) {
render, err := yaml.Marshal(&message)
if err != nil {
return
}
fmt.Printf("%s\n\n", render)
}
return d.Monitor(typeArgs, handler)
}
示例5: run
func (c *snapshotCmd) run(config *lxd.Config, args []string) error {
if len(args) < 1 {
return errArgs
}
var snapname string
if len(args) < 2 {
snapname = ""
} else {
snapname = args[1]
}
remote, name := config.ParseRemoteAndContainer(args[0])
d, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
// we don't allow '/' in snapshot names
if shared.IsSnapshot(snapname) {
return fmt.Errorf(gettext.Gettext("'/' not allowed in snapshot name\n"))
}
resp, err := d.Snapshot(name, snapname, c.stateful)
if err != nil {
return err
}
return d.WaitForSuccess(resp.Operation)
}
示例6: run
func run(args []string) error {
// Parse command line
gnuflag.Parse(true)
if len(os.Args) == 1 || !shared.StringInSlice(os.Args[1], []string{"spawn", "delete"}) {
fmt.Printf("Usage: %s spawn [--count=COUNT] [--image=IMAGE] [--privileged=BOOL] [--parallel=COUNT]\n", os.Args[0])
fmt.Printf(" %s delete [--parallel=COUNT]\n\n", os.Args[0])
gnuflag.Usage()
fmt.Printf("\n")
return fmt.Errorf("An action (spawn or delete) must be passed.")
}
// Connect to LXD
c, err := lxd.NewClient(&lxd.DefaultConfig, "local")
if err != nil {
return err
}
switch os.Args[1] {
case "spawn":
return spawnContainers(c, *argCount, *argImage, *argPrivileged)
case "delete":
return deleteContainers(c)
}
return nil
}
示例7: waitReady
func waitReady() error {
var timeout int
if *argTimeout == -1 {
timeout = 15
} else {
timeout = *argTimeout
}
finger := make(chan error, 1)
go func() {
for {
_, err := lxd.NewClient(&lxd.DefaultConfig, "local")
if err != nil {
time.Sleep(500 * time.Millisecond)
continue
}
finger <- nil
return
}
}()
select {
case <-finger:
break
case <-time.After(time.Second * time.Duration(timeout)):
return fmt.Errorf("LXD still not running after %ds timeout.", timeout)
}
return nil
}
示例8: doSet
func doSet(config *lxd.Config, args []string) error {
if len(args) != 4 {
return errArgs
}
// [[lxc config]] set dakara:c1 limits.memory 200000
remote, container := config.ParseRemoteAndContainer(args[1])
d, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
key := args[2]
value := args[3]
if !terminal.IsTerminal(int(syscall.Stdin)) && value == "-" {
buf, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("Can't read from stdin: %s", err)
}
value = string(buf[:])
}
return d.SetContainerConfig(container, key, value)
}
示例9: run
func (c *moveCmd) run(config *lxd.Config, args []string) error {
if len(args) != 2 {
return errArgs
}
sourceRemote, sourceName := config.ParseRemoteAndContainer(args[0])
destRemote, destName := config.ParseRemoteAndContainer(args[1])
// As an optimization, if the source an destination are the same, do
// this via a simple rename. This only works for containers that aren't
// running, containers that are running should be live migrated (of
// course, this changing of hostname isn't supported right now, so this
// simply won't work).
if sourceRemote == destRemote {
source, err := lxd.NewClient(config, sourceRemote)
if err != nil {
return err
}
rename, err := source.Rename(sourceName, destName)
if err != nil {
return err
}
return source.WaitForSuccess(rename.Operation)
}
// A move is just a copy followed by a delete; however, we want to
// keep the volatile entries around since we are moving the container.
if err := copyContainer(config, args[0], args[1], true, -1); err != nil {
return err
}
return commands["delete"].run(config, args[:1])
}
示例10: deviceAdd
func deviceAdd(config *lxd.Config, which string, args []string) error {
if len(args) < 5 {
return errArgs
}
remote, name := config.ParseRemoteAndContainer(args[2])
client, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
devname := args[3]
devtype := args[4]
var props []string
if len(args) > 5 {
props = args[5:]
} else {
props = []string{}
}
var resp *lxd.Response
if which == "profile" {
resp, err = client.ProfileDeviceAdd(name, devname, devtype, props)
} else {
resp, err = client.ContainerDeviceAdd(name, devname, devtype, props)
}
if err != nil {
return err
}
fmt.Printf(gettext.Gettext("Device %s added to %s\n"), devname, name)
if which == "profile" {
return nil
}
return client.WaitForSuccess(resp.Operation)
}
示例11: deviceRm
func deviceRm(config *lxd.Config, which string, args []string) error {
if len(args) < 4 {
return errArgs
}
remote, name := config.ParseRemoteAndContainer(args[2])
client, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
devname := args[3]
var resp *lxd.Response
if which == "profile" {
resp, err = client.ProfileDeviceDelete(name, devname)
} else {
resp, err = client.ContainerDeviceDelete(name, devname)
}
if err != nil {
return err
}
fmt.Printf(gettext.Gettext("Device %s removed from %s\n"), devname, name)
if which == "profile" {
return nil
}
return client.WaitForSuccess(resp.Operation)
}
示例12: run
func (c *actionCmd) run(config *lxd.Config, args []string) error {
if len(args) == 0 {
return errArgs
}
for _, nameArg := range args {
remote, name := config.ParseRemoteAndContainer(nameArg)
d, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
resp, err := d.Action(name, c.action, timeout, force)
if err != nil {
return err
}
if resp.Type != lxd.Async {
return fmt.Errorf(gettext.Gettext("bad result type from action"))
}
if err := d.WaitForSuccess(resp.Operation); err != nil {
return fmt.Errorf("%s\n"+gettext.Gettext("Try `lxc info --show-log %s` for more info"), err, name)
}
}
return nil
}
示例13: run
func (c *deleteCmd) run(config *lxd.Config, args []string) error {
if len(args) == 0 {
return errArgs
}
for _, nameArg := range args {
remote, name := config.ParseRemoteAndContainer(nameArg)
d, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
if c.interactive {
err := c.promptDelete(name)
if err != nil {
return err
}
}
if shared.IsSnapshot(name) {
return c.doDelete(d, name)
}
ct, err := d.ContainerInfo(name)
if err != nil {
return err
}
if ct.StatusCode != 0 && ct.StatusCode != shared.Stopped {
if !c.force {
return fmt.Errorf(i18n.G("The container is currently running, stop it first or pass --force."))
}
resp, err := d.Action(name, shared.Stop, -1, true, false)
if err != nil {
return err
}
op, err := d.WaitFor(resp.Operation)
if err != nil {
return err
}
if op.StatusCode == shared.Failure {
return fmt.Errorf(i18n.G("Stopping container failed!"))
}
if ct.Ephemeral == true {
return nil
}
}
if err := c.doDelete(d, name); err != nil {
return err
}
}
return nil
}
示例14: run
func (c *execCmd) run(config *lxd.Config, args []string) error {
if len(args) < 2 {
return errArgs
}
remote, name := config.ParseRemoteAndContainer(args[0])
d, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
env := map[string]string{"HOME": "/root", "USER": "root"}
myEnv := os.Environ()
for _, ent := range myEnv {
if strings.HasPrefix(ent, "TERM=") {
env["TERM"] = ent[len("TERM="):]
}
}
for _, arg := range envArgs {
pieces := strings.SplitN(arg, "=", 2)
value := ""
if len(pieces) > 1 {
value = pieces[1]
}
env[pieces[0]] = value
}
cfd := syscall.Stdout
var oldttystate *terminal.State
if terminal.IsTerminal(cfd) {
oldttystate, err = terminal.MakeRaw(cfd)
if err != nil {
return err
}
defer terminal.Restore(cfd, oldttystate)
}
ret, err := d.Exec(name, args[1:], env, os.Stdin, os.Stdout, os.Stderr)
if err != nil {
return err
}
if oldttystate != nil {
/* A bit of a special case here: we want to exit with the same code as
* the process inside the container, so we explicitly exit here
* instead of returning an error.
*
* Additionally, since os.Exit() exits without running deferred
* functions, we restore the terminal explicitly.
*/
terminal.Restore(cfd, oldttystate)
}
/* we get the result of waitpid() here so we need to transform it */
os.Exit(ret >> 8)
return fmt.Errorf(gettext.Gettext("unreachable return reached"))
}
示例15: deviceSet
func (c *configCmd) deviceSet(config *lxd.Config, which string, args []string) error {
if len(args) < 6 {
return errArgs
}
remote, name := config.ParseRemoteAndContainer(args[2])
client, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
devname := args[3]
key := args[4]
value := args[5]
if which == "profile" {
st, err := client.ProfileConfig(name)
if err != nil {
return err
}
dev, ok := st.Devices[devname]
if !ok {
return fmt.Errorf(i18n.G("The device doesn't exist"))
}
dev[key] = value
st.Devices[devname] = dev
err = client.PutProfile(name, *st)
if err != nil {
return err
}
} else {
st, err := client.ContainerInfo(name)
if err != nil {
return err
}
dev, ok := st.Devices[devname]
if !ok {
return fmt.Errorf(i18n.G("The device doesn't exist"))
}
dev[key] = value
st.Devices[devname] = dev
err = client.UpdateContainerConfig(name, st.Brief())
if err != nil {
return err
}
}
return err
}