Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions builder/xenserver/common/common_config.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package common

import (
"crypto/tls"
"errors"
"fmt"
"net/http"
"os"
"time"

"github.com/mitchellh/multistep"
"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"`
Expand Down Expand Up @@ -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"))
}
Expand Down Expand Up @@ -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
}
8 changes: 6 additions & 2 deletions builder/xenserver/iso/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 6 additions & 2 deletions builder/xenserver/xva/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down