本文整理匯總了Golang中github.com/lxc/lxd/shared.OperationWrap函數的典型用法代碼示例。如果您正苦於以下問題:Golang OperationWrap函數的具體用法?Golang OperationWrap怎麽用?Golang OperationWrap使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了OperationWrap函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: createFromNone
func createFromNone(d *Daemon, req *containerPostReq) Response {
args := DbCreateContainerArgs{
d: d,
name: req.Name,
ctype: cTypeRegular,
config: req.Config,
profiles: req.Profiles,
ephem: req.Ephemeral,
}
_, err := dbCreateContainer(args)
if err != nil {
return SmartError(err)
}
run := shared.OperationWrap(func() error {
c, err := newLxdContainer(req.Name, d)
if err != nil {
return err
}
err = templateApply(c, "create")
if err != nil {
return err
}
return nil
})
resources := make(map[string][]string)
resources["containers"] = []string{req.Name}
return &asyncResponse{run: run, resources: resources}
}
示例2: containerPut
/*
* Update configuration, or, if 'restore:snapshot-name' is present, restore
* the named snapshot
*/
func containerPut(d *Daemon, r *http.Request) Response {
name := mux.Vars(r)["name"]
c, err := newLxdContainer(name, d)
if err != nil {
return NotFound
}
configRaw := containerConfigReq{}
if err := json.NewDecoder(r.Body).Decode(&configRaw); err != nil {
return BadRequest(err)
}
var do = func() error { return nil }
if configRaw.Restore == "" {
// Update container configuration
do = func() error {
return containerReplaceConfig(d, c, name, configRaw)
}
} else {
// Snapshot Restore
do = func() error {
return containerSnapRestore(d, name, configRaw.Restore)
}
}
return AsyncResponse(shared.OperationWrap(do), nil)
}
示例3: containerPost
func containerPost(d *Daemon, r *http.Request) Response {
name := mux.Vars(r)["name"]
c, err := containerLXDLoad(d, name)
if err != nil {
return SmartError(err)
}
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
return InternalError(err)
}
body := containerPostBody{}
if err := json.Unmarshal(buf, &body); err != nil {
return BadRequest(err)
}
if body.Migration {
ws, err := NewMigrationSource(c)
if err != nil {
return InternalError(err)
}
return AsyncResponseWithWs(ws, nil)
}
run := func(id string) error {
return c.Rename(body.Name)
}
return AsyncResponse(shared.OperationWrap(run), nil)
}
示例4: snapshotPost
func snapshotPost(r *http.Request, c *lxdContainer, oldName string) Response {
raw := shared.Jmap{}
if err := json.NewDecoder(r.Body).Decode(&raw); err != nil {
return BadRequest(err)
}
newName, err := raw.GetString("name")
if err != nil {
return BadRequest(err)
}
oldDir := snapshotDir(c, oldName)
newDir := snapshotDir(c, newName)
_, err = os.Stat(newDir)
if !os.IsNotExist(err) {
return InternalError(err)
} else if err == nil {
return Conflict
}
/*
* TODO: do we need to do something more intelligent here? We probably
* shouldn't do anything for stateful snapshots, since changing the fs
* out from under criu will cause it to fail, but it may be useful to
* do something for stateless ones.
*/
rename := func() error { return os.Rename(oldDir, newDir) }
return AsyncResponse(shared.OperationWrap(rename), nil)
}
示例5: containerPost
func containerPost(d *Daemon, r *http.Request) Response {
name := mux.Vars(r)["name"]
c, err := newLxdContainer(name, d)
if err != nil {
return SmartError(err)
}
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
return InternalError(err)
}
body := containerPostBody{}
if err := json.Unmarshal(buf, &body); err != nil {
return BadRequest(err)
}
if body.Migration {
ws, err := migration.NewMigrationSource(c.c)
if err != nil {
return InternalError(err)
}
return AsyncResponseWithWs(ws, nil)
} else {
if c.c.Running() {
return BadRequest(fmt.Errorf("renaming of running container not allowed"))
}
args := DbCreateContainerArgs{
d: d,
name: body.Name,
ctype: cTypeRegular,
config: c.config,
profiles: c.profiles,
ephem: c.ephemeral,
baseImage: c.config["volatile.baseImage"],
architecture: c.architecture,
}
_, err := dbCreateContainer(args)
if err != nil {
return SmartError(err)
}
run := func() error {
oldPath := fmt.Sprintf("%s/", shared.VarPath("lxc", c.name))
newPath := fmt.Sprintf("%s/", shared.VarPath("lxc", body.Name))
if err := os.Rename(oldPath, newPath); err != nil {
return err
}
removeContainer(d, c.name)
return nil
}
return AsyncResponse(shared.OperationWrap(run), nil)
}
}
示例6: containerSnapshotsPost
func containerSnapshotsPost(d *Daemon, r *http.Request) Response {
name := mux.Vars(r)["name"]
/*
* snapshot is a three step operation:
* 1. choose a new name
* 2. copy the database info over
* 3. copy over the rootfs
*/
c, err := containerLXDLoad(d, name)
if err != nil {
return SmartError(err)
}
raw := shared.Jmap{}
if err := json.NewDecoder(r.Body).Decode(&raw); err != nil {
return BadRequest(err)
}
snapshotName, err := raw.GetString("name")
if err != nil || snapshotName == "" {
// come up with a name
i := nextSnapshot(d, name)
snapshotName = fmt.Sprintf("snap%d", i)
}
stateful, err := raw.GetBool("stateful")
if err != nil {
return BadRequest(err)
}
fullName := name +
shared.SnapshotDelimiter +
snapshotName
snapshot := func(id string) error {
config := c.Config()
args := containerLXDArgs{
Ctype: cTypeSnapshot,
Config: config,
Profiles: c.Profiles(),
Ephemeral: c.IsEphemeral(),
BaseImage: config["volatile.base_image"],
Architecture: c.Architecture(),
Devices: c.Devices(),
}
_, err := containerLXDCreateAsSnapshot(d, fullName, args, c, stateful)
if err != nil {
return err
}
return nil
}
return AsyncResponse(shared.OperationWrap(snapshot), nil)
}
示例7: createFromImage
func createFromImage(d *Daemon, req *containerPostReq) Response {
var hash string
var err error
var run func() shared.OperationResult
if req.Source.Alias != "" {
if req.Source.Mode == "pull" && req.Source.Server != "" {
hash, err = remoteGetImageFingerprint(d, req.Source.Server, req.Source.Alias)
if err != nil {
return InternalError(err)
}
} else {
hash, err = dbImageAliasGet(d.db, req.Source.Alias)
if err != nil {
return InternalError(err)
}
}
} else if req.Source.Fingerprint != "" {
hash = req.Source.Fingerprint
} else {
return BadRequest(fmt.Errorf("must specify one of alias or fingerprint for init from image"))
}
if req.Source.Server != "" {
err := d.ImageDownload(req.Source.Server, hash, req.Source.Secret, true)
if err != nil {
return InternalError(err)
}
}
imgInfo, err := dbImageGet(d.db, hash, false, false)
if err != nil {
return SmartError(err)
}
hash = imgInfo.Fingerprint
args := containerLXDArgs{
Ctype: cTypeRegular,
Config: req.Config,
Profiles: req.Profiles,
Ephemeral: req.Ephemeral,
BaseImage: hash,
Architecture: imgInfo.Architecture,
}
run = shared.OperationWrap(func() error {
_, err := containerLXDCreateFromImage(d, req.Name, args, hash)
return err
})
resources := make(map[string][]string)
resources["containers"] = []string{req.Name}
return &asyncResponse{run: run, resources: resources}
}
示例8: containerPut
/*
* Update configuration, or, if 'restore:snapshot-name' is present, restore
* the named snapshot
*/
func containerPut(d *Daemon, r *http.Request) Response {
name := mux.Vars(r)["name"]
c, err := newLxdContainer(name, d)
if err != nil {
return NotFound
}
configRaw := containerConfigReq{}
if err := json.NewDecoder(r.Body).Decode(&configRaw); err != nil {
return BadRequest(err)
}
var do = func() error { return nil }
if configRaw.Restore == "" {
// Update container configuration
do = func() error {
preDevList, err := dbDevicesGet(d.db, name, false)
if err != nil {
return err
}
if err := validateConfig(c, configRaw.Devices); err != nil {
return err
}
tx, err := containerReplaceConfig(d, c, name, configRaw)
if err != nil {
return err
}
if !c.c.Running() {
return txCommit(tx)
}
// apply new devices
postDevList := configRaw.Devices
if err != nil {
tx.Rollback()
return err
}
if err := devicesApplyDeltaLive(tx, c, preDevList, postDevList); err != nil {
tx.Rollback()
return err
}
return txCommit(tx)
}
} else {
// Snapshot Restore
do = func() error {
return containerSnapRestore(d, name, configRaw.Restore)
}
}
return AsyncResponse(shared.OperationWrap(do), nil)
}
示例9: containerStatePut
func containerStatePut(d *Daemon, r *http.Request) Response {
name := mux.Vars(r)["name"]
raw := containerStatePutReq{}
// We default to -1 (i.e. no timeout) here instead of 0 (instant
// timeout).
raw.Timeout = -1
if err := json.NewDecoder(r.Body).Decode(&raw); err != nil {
return BadRequest(err)
}
c, err := containerLXDLoad(d, name)
if err != nil {
return SmartError(err)
}
var do func() error
switch shared.ContainerAction(raw.Action) {
case shared.Start:
do = func() error {
if err = c.Start(); err != nil {
return err
}
return nil
}
case shared.Stop:
if raw.Timeout == 0 || raw.Force {
do = func() error {
if err = c.Stop(); err != nil {
return err
}
return nil
}
} else {
do = func() error {
if err = c.Shutdown(time.Duration(raw.Timeout) * time.Second); err != nil {
return err
}
return nil
}
}
case shared.Restart:
do = c.Reboot
case shared.Freeze:
do = c.Freeze
case shared.Unfreeze:
do = c.Unfreeze
default:
return BadRequest(fmt.Errorf("unknown action %s", raw.Action))
}
return AsyncResponse(shared.OperationWrap(do), nil)
}
示例10: containerDelete
func containerDelete(d *Daemon, r *http.Request) Response {
name := mux.Vars(r)["name"]
c, err := containerLXDLoad(d, name)
if err != nil {
return SmartError(err)
}
rmct := func() error {
return c.Delete()
}
return AsyncResponse(shared.OperationWrap(rmct), nil)
}
示例11: containerDelete
func containerDelete(d *Daemon, r *http.Request) Response {
name := mux.Vars(r)["name"]
_, err := dbGetContainerId(d.db, name)
if err != nil {
return SmartError(err)
}
rmct := func() error {
return removeContainer(d, name)
}
return AsyncResponse(shared.OperationWrap(rmct), nil)
}
示例12: snapshotPost
func snapshotPost(r *http.Request, sc container, containerName string) Response {
raw := shared.Jmap{}
if err := json.NewDecoder(r.Body).Decode(&raw); err != nil {
return BadRequest(err)
}
newName, err := raw.GetString("name")
if err != nil {
return BadRequest(err)
}
rename := func(id string) error {
return sc.Rename(containerName + shared.SnapshotDelimiter + newName)
}
return AsyncResponse(shared.OperationWrap(rename), nil)
}
示例13: containerDelete
func containerDelete(d *Daemon, r *http.Request) Response {
name := mux.Vars(r)["name"]
c, err := containerLXDLoad(d, name)
if err != nil {
return SmartError(err)
}
if c.IsRunning() {
return BadRequest(fmt.Errorf("container is running"))
}
rmct := func(id string) error {
return c.Delete()
}
return AsyncResponse(shared.OperationWrap(rmct), nil)
}
示例14: createFromNone
func createFromNone(d *Daemon, req *containerPostReq) Response {
args := containerLXDArgs{
Ctype: cTypeRegular,
Config: req.Config,
Profiles: req.Profiles,
Ephemeral: req.Ephemeral,
}
run := shared.OperationWrap(func() error {
_, err := containerLXDCreateAsEmpty(d, req.Name, args)
return err
})
resources := make(map[string][]string)
resources["containers"] = []string{req.Name}
return &asyncResponse{run: run, resources: resources}
}
示例15: containerDelete
func containerDelete(d *Daemon, r *http.Request) Response {
name := mux.Vars(r)["name"]
_, err := dbContainerIDGet(d.db, name)
if err != nil {
return SmartError(err)
}
// TODO: i have added this not sure its a good idea (pcdummy)
c, err := newLxdContainer(name, d)
if err != nil {
return SmartError(err)
}
rmct := func() error {
return removeContainer(d, c)
}
return AsyncResponse(shared.OperationWrap(rmct), nil)
}