本文整理汇总了Golang中github.com/lxc/lxd.Client.ProfileConfig方法的典型用法代码示例。如果您正苦于以下问题:Golang Client.ProfileConfig方法的具体用法?Golang Client.ProfileConfig怎么用?Golang Client.ProfileConfig使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/lxc/lxd.Client
的用法示例。
在下文中一共展示了Client.ProfileConfig方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: doProfileShow
func doProfileShow(client *lxd.Client, p string) error {
profile, err := client.ProfileConfig(p)
if err != nil {
return err
}
data, err := yaml.Marshal(&profile)
fmt.Printf("%s", data)
return nil
}
示例2: verifyDefaultProfileBridgeConfig
// verifyDefaultProfileBridgeConfig takes a LXD API client and extracts the
// network bridge configured on the "default" profile. Additionally, if the
// default bridge bridge is used, its configuration in LXDBridgeFile is also
// inspected to make sure it has a chance to work.
func verifyDefaultProfileBridgeConfig(client *lxd.Client) (string, error) {
const (
defaultProfileName = "default"
configTypeKey = "type"
configTypeNic = "nic"
configNicTypeKey = "nictype"
configBridged = "bridged"
configEth0 = "eth0"
configParentKey = "parent"
)
config, err := client.ProfileConfig(defaultProfileName)
if err != nil {
return "", errors.Trace(err)
}
// If the default profile doesn't have eth0 in it, then the user has messed
// with it, so let's just use whatever they set up.
eth0, ok := config.Devices[configEth0]
if !ok {
return "", errors.Errorf("unexpected LXD %q profile config without eth0: %+v", defaultProfileName, config)
}
// If eth0 is there, but not with the expected attributes, likewise fail
// early.
if eth0[configTypeKey] != configTypeNic || eth0[configNicTypeKey] != configBridged {
return "", errors.Errorf("unexpected LXD %q profile config: %+v", defaultProfileName, config)
}
bridgeName := eth0[configParentKey]
logger.Infof(`LXD "default" profile uses network bridge %q`, bridgeName)
if bridgeName != network.DefaultLXDBridge {
// When the user changed which bridge to use, just return its name and
// check no further.
return bridgeName, nil
}
bridgeConfig, err := ioutil.ReadFile(LXDBridgeFile)
if os.IsNotExist(err) {
return "", bridgeConfigError("lxdbr0 configured but no config file found at " + LXDBridgeFile)
} else if err != nil {
return "", errors.Trace(err)
}
if err := checkLXDBridgeConfiguration(string(bridgeConfig)); err != nil {
return "", errors.Trace(err)
}
return bridgeName, nil
}
示例3: doNetworkDetachProfile
func (c *networkCmd) doNetworkDetachProfile(client *lxd.Client, name string, args []string) error {
if len(args) < 1 || len(args) > 2 {
return errArgs
}
profileName := args[0]
devName := ""
if len(args) > 1 {
devName = args[1]
}
profile, err := client.ProfileConfig(profileName)
if err != nil {
return err
}
if devName == "" {
for n, d := range profile.Devices {
if d["type"] == "nic" && d["parent"] == name {
if devName != "" {
return fmt.Errorf(i18n.G("More than one device matches, specify the device name."))
}
devName = n
}
}
}
if devName == "" {
return fmt.Errorf(i18n.G("No device found for this network"))
}
device, ok := profile.Devices[devName]
if !ok {
return fmt.Errorf(i18n.G("The specified device doesn't exist"))
}
if device["type"] != "nic" || device["parent"] != name {
return fmt.Errorf(i18n.G("The specified device doesn't match the network"))
}
_, err = client.ProfileDeviceDelete(profileName, devName)
return err
}
示例4: doProfileEdit
func doProfileEdit(client *lxd.Client, p string) error {
if !terminal.IsTerminal(syscall.Stdin) {
contents, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return err
}
newdata := shared.ProfileConfig{}
err = yaml.Unmarshal(contents, &newdata)
if err != nil {
return err
}
newdata.Name = p
return client.PutProfile(p, newdata)
}
profile, err := client.ProfileConfig(p)
if err != nil {
return err
}
editor := os.Getenv("VISUAL")
if editor == "" {
editor = os.Getenv("EDITOR")
if editor == "" {
editor = "vi"
}
}
data, err := yaml.Marshal(&profile)
f, err := ioutil.TempFile("", "lxd_lxc_profile_")
if err != nil {
return err
}
fname := f.Name()
if err = f.Chmod(0600); err != nil {
f.Close()
os.Remove(fname)
return err
}
f.Write([]byte(profileEditHelp + "\n"))
f.Write(data)
f.Close()
defer os.Remove(fname)
for {
var err error
cmdParts := strings.Fields(editor)
cmd := exec.Command(cmdParts[0], append(cmdParts[1:], fname)...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return err
}
contents, err := ioutil.ReadFile(fname)
if err != nil {
return err
}
newdata := shared.ProfileConfig{}
err = yaml.Unmarshal(contents, &newdata)
if err == nil {
err = client.PutProfile(p, newdata)
}
if err != nil {
fmt.Fprintf(os.Stderr, gettext.Gettext("Config parsing error: %s")+"\n", err)
fmt.Println(gettext.Gettext("Press enter to open the editor again"))
_, err := os.Stdin.Read(make([]byte, 1))
if err != nil {
return err
}
continue
}
break
}
return nil
}
示例5: doProfileEdit
func doProfileEdit(client *lxd.Client, p string) error {
// If stdin isn't a terminal, read text from it
if !terminal.IsTerminal(int(syscall.Stdin)) {
contents, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return err
}
newdata := shared.ProfileConfig{}
err = yaml.Unmarshal(contents, &newdata)
if err != nil {
return err
}
return client.PutProfile(p, newdata)
}
// Extract the current value
profile, err := client.ProfileConfig(p)
if err != nil {
return err
}
data, err := yaml.Marshal(&profile)
if err != nil {
return err
}
// Spawn the editor
content, err := shared.TextEditor("", []byte(profileEditHelp+"\n\n"+string(data)))
if err != nil {
return err
}
for {
// Parse the text received from the editor
newdata := shared.ProfileConfig{}
err = yaml.Unmarshal(content, &newdata)
if err == nil {
err = client.PutProfile(p, newdata)
}
// Respawn the editor
if err != nil {
fmt.Fprintf(os.Stderr, i18n.G("Config parsing error: %s")+"\n", err)
fmt.Println(i18n.G("Press enter to open the editor again"))
_, err := os.Stdin.Read(make([]byte, 1))
if err != nil {
return err
}
content, err = shared.TextEditor("", content)
if err != nil {
return err
}
continue
}
break
}
return nil
}
示例6: verifyDefaultProfileBridgeConfig
// verifyDefaultProfileBridgeConfig takes a LXD API client and extracts the
// network bridge configured on the "default" profile. Additionally, if the
// default bridge bridge is used, its configuration in LXDBridgeFile is also
// inspected to make sure it has a chance to work.
func verifyDefaultProfileBridgeConfig(client *lxd.Client, networkAPISupported bool) (string, error) {
const (
defaultProfileName = "default"
configTypeKey = "type"
configTypeNic = "nic"
configNicTypeKey = "nictype"
configBridged = "bridged"
configEth0 = "eth0"
configParentKey = "parent"
)
config, err := client.ProfileConfig(defaultProfileName)
if err != nil {
return "", errors.Trace(err)
}
eth0, ok := config.Devices[configEth0]
if !ok {
/* on lxd >= 2.3, there is nothing in the default profile
* w.r.t. eth0, because there is no lxdbr0 by default. Let's
* handle this case and configure one now.
*/
if networkAPISupported {
if err := CreateDefaultBridgeInDefaultProfile(client); err != nil {
return "", errors.Annotate(err, "couldn't create default bridge")
}
return network.DefaultLXDBridge, nil
}
return "", errors.Errorf("unexpected LXD %q profile config without eth0: %+v", defaultProfileName, config)
} else if networkAPISupported {
if err := checkBridgeConfig(client, eth0[configParentKey]); err != nil {
return "", err
}
}
// If eth0 is there, but not with the expected attributes, likewise fail
// early.
if eth0[configTypeKey] != configTypeNic || eth0[configNicTypeKey] != configBridged {
return "", errors.Errorf("unexpected LXD %q profile config: %+v", defaultProfileName, config)
}
bridgeName := eth0[configParentKey]
logger.Infof(`LXD "default" profile uses network bridge %q`, bridgeName)
if bridgeName != network.DefaultLXDBridge {
// When the user changed which bridge to use, just return its name and
// check no further.
return bridgeName, nil
}
/* if the network API is supported, that means the lxd-bridge config
* file has been obsoleted so we don't need to check it for correctness
*/
if networkAPISupported {
return bridgeName, nil
}
bridgeConfig, err := ioutil.ReadFile(LXDBridgeFile)
if os.IsNotExist(err) {
return "", bridgeConfigError("lxdbr0 configured but no config file found at " + LXDBridgeFile)
} else if err != nil {
return "", errors.Trace(err)
}
if err := checkLXDBridgeConfiguration(string(bridgeConfig)); err != nil {
return "", errors.Trace(err)
}
return bridgeName, nil
}
示例7: doProfileEdit
func doProfileEdit(client *lxd.Client, p string) error {
if !terminal.IsTerminal(syscall.Stdin) {
contents, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return err
}
newdata := shared.ProfileConfig{}
err = yaml.Unmarshal(contents, &newdata)
if err != nil {
return err
}
return client.PutProfile(p, newdata)
}
profile, err := client.ProfileConfig(p)
if err != nil {
return err
}
editor := os.Getenv("VISUAL")
if editor == "" {
editor = os.Getenv("EDITOR")
if editor == "" {
editor = "vi"
}
}
data, err := yaml.Marshal(&profile)
f, err := ioutil.TempFile("", "lxd_lxc_profile_")
if err != nil {
return err
}
fname := f.Name()
if err = f.Chmod(0600); err != nil {
f.Close()
os.Remove(fname)
return err
}
f.Write([]byte(profileEditHelp))
f.Write(data)
f.Close()
defer os.Remove(fname)
for {
cmd := exec.Command(editor, fname)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return err
}
contents, err := ioutil.ReadFile(fname)
if err != nil {
return err
}
newdata := shared.ProfileConfig{}
err = yaml.Unmarshal(contents, &newdata)
if err != nil {
fmt.Fprintf(os.Stderr, gettext.Gettext("YAML parse error %v\n"), err)
fmt.Printf("Press enter to play again ")
_, err := os.Stdin.Read(make([]byte, 1))
if err != nil {
return err
}
continue
}
err = client.PutProfile(p, newdata)
break
}
return err
}
示例8: cmdCreate
func cmdCreate(c *lxd.Client, args []string) error {
var wgBatch sync.WaitGroup
// A path must be provided
if len(args) < 1 {
return fmt.Errorf("A path must be passed to create.")
}
// Load the simulation
routersMap, err := importFromCSV(args[0])
if err != nil {
return err
}
routers := []*Router{}
for _, v := range routersMap {
if v.Tier < 1 {
continue
}
routers = append(routers, v)
}
// Clear any existing images
fp := c.GetAlias("internet-router")
if fp != "" {
logf("Deleting the existing router image: %s", fp)
err = c.DeleteImage(fp)
if err != nil {
return err
}
}
// Load the image
logf("Importing the router image")
_, err = c.PostImage("image/image-meta.tar.xz", "image/image-rootfs.tar.xz", nil, false, []string{"internet-router"}, nil)
if err != nil {
return err
}
logf("New router image imported: %s", fp)
// Create the profile
_, err = c.ProfileConfig("internet-base")
if err != nil {
logf("Creating the profile")
err := c.ProfileCreate("internet-base")
if err != nil {
return err
}
}
// Helper function
createContainer := func(router *Router) {
defer wgBatch.Done()
var interfaces string
var bgpd string
// Configuration
config := map[string]string{}
devices := map[string]map[string]string{}
config["user.internet.type"] = "router"
config["user.internet.organization"] = router.Organization
config["user.internet.priority"] = fmt.Sprintf("%d", router.Priority)
config["user.internet.tier"] = fmt.Sprintf("%d", router.Tier)
config["user.internet.location"] = router.Location
for i, r := range router.DNS {
config[fmt.Sprintf("user.internet.dns.%d", i)] = r
}
config["user.internet.router.fqdn"] = router.Configuration.FQDN
config["user.internet.router.asn"] = fmt.Sprintf("%d", router.Configuration.ASN)
config["user.internet.router.password.login"] = router.Configuration.PasswordLogin
config["user.internet.router.password.enable"] = router.Configuration.PasswordEnable
if router.Configuration.RouterID != nil {
config["user.internet.router.routerid"] = router.Configuration.RouterID.String()
}
if router.Internal {
config["user.internet.internal"] = "true"
} else {
config["user.internet.internal"] = "false"
}
if router.Tier >= 1 && router.Tier <= 3 {
interfaces = fmt.Sprintf(`auto lo
iface lo inet loopback
pre-up echo 0 > /proc/sys/net/ipv6/conf/all/accept_dad || true
post-up echo 1 > /proc/sys/net/ipv6/conf/all/forwarding || true
auto local
iface local inet6 manual
pre-up ip link add local type dummy || true
pre-up ip link set local up || true
`)
}
for i, r := range router.Configuration.Loopback.Addresses {
//.........这里部分代码省略.........