Skip to content
Permalink
Browse files

Add configdrive support

  • Loading branch information...
subuk committed Aug 30, 2019
1 parent 46a5650 commit 404f7e7df0e0a9b1f1880e16b009054a3c043814
@@ -22,7 +22,7 @@ func Web(configFilename string) {
logger := zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}).With().Timestamp().Logger()

connectionPool := libvirt.NewConnectionPool(cfg.LibvirtUri, logger.With().Str("component", "libvirt-connection-pool").Logger())
machineRepo := libvirt.NewVirtualMachineRepository(connectionPool)
machineRepo := libvirt.NewVirtualMachineRepository(connectionPool, cfg.LibvirtConfigDrivePool, cfg.LibvirtConfigDriveSuffix, logger.With().Str("component", "vm-repository").Logger())
volumeRepo := libvirt.NewVolumeRepository(connectionPool)
hostInfoRepo := libvirt.NewHostInfoRepository(connectionPool)
keyRepo := filesystem.NewKeyRepository(cfg.KeyFile, logger.With().Str("component", "key-repository").Logger())
@@ -29,9 +29,9 @@ func (service *Service) VirtualMachineDetail(id string) (*VirtualMachine, error)
}

type VirtualMachineCreateParamsConfig struct {
MetaData map[string]interface{}
UserData string
NetworkConfig string
Hostname string
UserData string
KeyFingerprints []string
}

type VirtualMachineCreateParamsVolume struct {
@@ -64,6 +64,7 @@ type VirtualMachineCreateContext struct {
Images []*Volume
Pools []*VolumePool
Networks []*Network
Keys []*Key
Arches []Arch
}

@@ -91,6 +92,12 @@ func (service *Service) VirtualMachineCreateContext() (VirtualMachineCreateConte
}
context.Pools = pools

keys, err := service.key.List()
if err != nil {
return context, util.NewError(err, "cannot list keys")
}
context.Keys = keys

networks, err := service.net.List()
if err != nil {
return context, util.NewError(err, "cannot list networks")
@@ -143,8 +150,19 @@ func (service *Service) VirtualMachineCreate(params VirtualMachineCreateParams)
}
interfaces = append(interfaces, iface)
}
config := &VirtualMachineConfig{
Hostname: params.Config.Hostname,
Userdata: []byte(params.Config.UserData),
}
for _, fingerprint := range params.Config.KeyFingerprints {
key, err := service.key.Get(fingerprint)
if err != nil {
return nil, util.NewError(err, "cannot load key")
}
config.Keys = append(config.Keys, key)
}

vm, err := service.virt.Create(params.Id, NewArch(params.Arch), params.VCpus, params.MemoryKb, volumes, interfaces)
vm, err := service.virt.Create(params.Id, NewArch(params.Arch), params.VCpus, params.MemoryKb, volumes, interfaces, config)
if err != nil {
return nil, util.NewError(err, "cannot create virtual machine")
}
@@ -3,7 +3,7 @@ package compute
type VirtualMachineRepository interface {
List() ([]*VirtualMachine, error)
Get(id string) (*VirtualMachine, error)
Create(id string, arch Arch, vcpus int, memoryKb uint, volumes []*VirtualMachineAttachedVolume, interfaces []*VirtualMachineAttachedInterface) (*VirtualMachine, error)
Create(id string, arch Arch, vcpus int, memoryKb uint, volumes []*VirtualMachineAttachedVolume, interfaces []*VirtualMachineAttachedInterface, config *VirtualMachineConfig) (*VirtualMachine, error)
Delete(id string) error
Poweroff(id string) error
Reboot(id string) error
@@ -29,6 +29,12 @@ func (state VirtualMachineState) String() string {
}
}

type VirtualMachineConfig struct {
Hostname string
Keys []*Key
Userdata []byte
}

type VirtualMachine struct {
Id string
VCpus int
@@ -37,6 +43,7 @@ type VirtualMachine struct {
Memory uint // KiB
Interfaces []*VirtualMachineAttachedInterface
Volumes []*VirtualMachineAttachedVolume
Config *VirtualMachineConfig
}

func (vm *VirtualMachine) IsRunning() bool {
@@ -26,16 +26,20 @@ type WebConfig struct {
}

type Config struct {
LibvirtUri string `hcl:"libvirt_uri"`
Bridges []string `hcl:"bridges"`
KeyFile string `hcl:"key_file"`
Web WebConfig `hcl:"web"`
LibvirtUri string `hcl:"libvirt_uri"`
LibvirtConfigDriveSuffix string `hcl:"libvirt_config_drive_suffix"`
LibvirtConfigDrivePool string `hcl:"libvirt_config_drive_pool"`
Bridges []string `hcl:"bridges"`
KeyFile string `hcl:"key_file"`
Web WebConfig `hcl:"web"`
}

func Default() Config {
return Config{
LibvirtUri: "qemu:///system",
KeyFile: "~/.vmango/authorized_keys",
LibvirtUri: "qemu:///system",
LibvirtConfigDrivePool: "default",
LibvirtConfigDriveSuffix: "_config.iso",
KeyFile: "~/.vmango/authorized_keys",
Web: WebConfig{
Listen: ":8080",
Debug: false,
2 go.mod
@@ -5,6 +5,7 @@ go 1.12
require (
github.com/akamensky/argparse v0.0.0-20190309155458-28b0496b54cb
github.com/go-bindata/go-bindata v3.1.2+incompatible
github.com/google/uuid v1.1.1
github.com/gorilla/csrf v1.6.0
github.com/gorilla/mux v1.7.3
github.com/gorilla/sessions v1.2.0
@@ -14,4 +15,5 @@ require (
github.com/rs/zerolog v1.15.0
github.com/unrolled/render v1.0.1
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
gopkg.in/yaml.v2 v2.2.2
)
6 go.sum
@@ -7,6 +7,8 @@ github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 h1:clC1lXBpe2kTj2VHdaI
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385/go.mod h1:0vRUJqYpeSZifjYj7uP3BG/gKcuzL9xWVV/Y+cK33KM=
github.com/go-bindata/go-bindata v3.1.2+incompatible h1:5vjJMVhowQdPzjE1LdxyFF7YFTXg5IgGVW4gBr5IbvE=
github.com/go-bindata/go-bindata v3.1.2+incompatible/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/csrf v1.6.0 h1:60oN1cFdncCE8tjwQ3QEkFND5k37lQPcRjnlvm7CIJ0=
github.com/gorilla/csrf v1.6.0/go.mod h1:7tSf8kmjNYr7IWDCYhd3U8Ck34iQ/Yw5CJu7bAkHEGI=
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
@@ -39,3 +41,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
@@ -0,0 +1,20 @@
package libvirt

import (
"gopkg.in/yaml.v2"
)

type CloudInitMetadata struct {
InstanceId string `yaml:"instance-id"`
Hostname string `yaml:"hostname"`
LocalHostname string `yaml:"local-hostname"`
PublicKeys []string `yaml:"public-keys"`
}

func (md *CloudInitMetadata) Unmarshal(in []byte) error {
return yaml.Unmarshal(in, md)
}

func (md *CloudInitMetadata) Marshal() ([]byte, error) {
return yaml.Marshal(md)
}
@@ -107,9 +107,6 @@ func VirtualMachineFromDomainConfig(domainConfig *libvirtxml.Domain, domainInfo

for _, diskConfig := range domainConfig.Devices.Disks {
volume := VirtualMachineAttachedVolumeFromDomainDiskConfig(diskConfig)
if volume == nil {
continue
}
vm.Volumes = append(vm.Volumes, volume)
}
return vm, nil

0 comments on commit 404f7e7

Please sign in to comment.
You can’t perform that action at this time.