diff --git a/builder/xenserver/common/common_config.go b/builder/xenserver/common/common_config.go index 26ee51ff..6e323a1d 100644 --- a/builder/xenserver/common/common_config.go +++ b/builder/xenserver/common/common_config.go @@ -1,8 +1,10 @@ package common import ( + "crypto/tls" "errors" "fmt" + "net/http" "os" "time" @@ -10,13 +12,17 @@ import ( "github.com/mitchellh/packer/common" commonssh "github.com/mitchellh/packer/common/ssh" "github.com/mitchellh/packer/template/interpolate" + + xmlrpc "github.com/nilshell/xmlrpc" xsclient "github.com/xenserver/go-xenserver-client" ) type CommonConfig struct { - Username string `mapstructure:"remote_username"` - Password string `mapstructure:"remote_password"` - HostIp string `mapstructure:"remote_host"` + Username string `mapstructure:"remote_username"` + Password string `mapstructure:"remote_password"` + HostIp string `mapstructure:"remote_host"` + ApiUrl string `mapstructure:"remote_url"` + ApiTlsSkipVerify bool `mapstructure:"remote_url_tls_skip_verify"` VMName string `mapstructure:"vm_name"` VMDescription string `mapstructure:"vm_description"` @@ -147,6 +153,10 @@ func (c *CommonConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig errs = append(errs, errors.New("remote_host must be specified.")) } + if c.ApiUrl == "" { + c.ApiUrl = "http://" + c.HostIp + } + if c.HostPortMin > c.HostPortMax { errs = append(errs, errors.New("the host min port must be less than the max")) } @@ -245,3 +255,28 @@ func (config CommonConfig) GetSR(client xsclient.XenAPIClient) (*xsclient.SR, er return srs[0], nil } } + +func (config CommonConfig) GetXenAPIClient() (*xsclient.XenAPIClient, error) { + tlsConfig := tls.Config{} + if config.ApiTlsSkipVerify { + tlsConfig.InsecureSkipVerify = true + } + http_transport := http.Transport{ + TLSClientConfig: &tlsConfig, + } + rpc, err := xmlrpc.NewClient(config.ApiUrl, &http_transport) + + if err != nil { + return nil, err + } + + client := xsclient.XenAPIClient{ + Username: config.Username, + Password: config.Password, + Host: config.HostIp, + Url: config.ApiUrl, + RPC: rpc, + } + + return &client, nil +} diff --git a/builder/xenserver/iso/builder.go b/builder/xenserver/iso/builder.go index 03b230d2..3de49c65 100644 --- a/builder/xenserver/iso/builder.go +++ b/builder/xenserver/iso/builder.go @@ -178,9 +178,13 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, retErr error func (self *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { //Setup XAPI client - client := xsclient.NewXenAPIClient(self.config.HostIp, self.config.Username, self.config.Password) + _client, err := self.config.CommonConfig.GetXenAPIClient() + if err != nil { + return nil, err.(error) + } + client := *_client - err := client.Login() + err = client.Login() if err != nil { return nil, err.(error) } diff --git a/builder/xenserver/xva/builder.go b/builder/xenserver/xva/builder.go index 278bef15..ff1f7f0b 100644 --- a/builder/xenserver/xva/builder.go +++ b/builder/xenserver/xva/builder.go @@ -86,9 +86,13 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, retErr error func (self *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { //Setup XAPI client - client := xsclient.NewXenAPIClient(self.config.HostIp, self.config.Username, self.config.Password) + _client, err := self.config.CommonConfig.GetXenAPIClient() + if err != nil { + return nil, err.(error) + } + client := *_client - err := client.Login() + err = client.Login() if err != nil { return nil, err.(error) }