providres/docker: cache client
This commit is contained in:
parent
7c253155c1
commit
09333e5e76
|
@ -6,14 +6,11 @@ import (
|
||||||
dc "github.com/fsouza/go-dockerclient"
|
dc "github.com/fsouza/go-dockerclient"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Config is the structure that stores the configuration to talk to a
|
||||||
|
// Docker API compatible host.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Host string
|
Host string
|
||||||
CertPath string
|
CertPath string
|
||||||
SkipPull bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type Data struct {
|
|
||||||
DockerImages map[string]*dc.APIImages
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient() returns a new Docker client.
|
// NewClient() returns a new Docker client.
|
||||||
|
@ -30,9 +27,7 @@ func (c *Config) NewClient() (*dc.Client, error) {
|
||||||
return dc.NewTLSClient(c.Host, cert, key, ca)
|
return dc.NewTLSClient(c.Host, cert, key, ca)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewData() returns a new data struct.
|
// Data ia structure for holding data that we fetch from Docker.
|
||||||
func (c *Config) NewData() *Data {
|
type Data struct {
|
||||||
return &Data{
|
DockerImages map[string]*dc.APIImages
|
||||||
DockerImages: map[string]*dc.APIImages{},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,5 +38,5 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
|
||||||
CertPath: d.Get("cert_path").(string),
|
CertPath: d.Get("cert_path").(string),
|
||||||
}
|
}
|
||||||
|
|
||||||
return &config, nil
|
return config.NewClient()
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,16 +11,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) error {
|
func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) error {
|
||||||
config := meta.(*Config)
|
var err error
|
||||||
|
client := meta.(*dc.Client)
|
||||||
|
|
||||||
client, err := config.NewClient()
|
var data Data
|
||||||
if err != nil {
|
if err := fetchLocalImages(&data, client); err != nil {
|
||||||
return fmt.Errorf("Unable to connect to Docker: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
data := config.NewData()
|
|
||||||
|
|
||||||
if err := fetchLocalImages(data, client); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,12 +111,7 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err
|
||||||
}
|
}
|
||||||
|
|
||||||
func resourceDockerContainerRead(d *schema.ResourceData, meta interface{}) error {
|
func resourceDockerContainerRead(d *schema.ResourceData, meta interface{}) error {
|
||||||
config := meta.(*Config)
|
client := meta.(*dc.Client)
|
||||||
|
|
||||||
client, err := config.NewClient()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Unable to connect to Docker: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
apiContainer, err := fetchDockerContainer(d.Get("name").(string), client)
|
apiContainer, err := fetchDockerContainer(d.Get("name").(string), client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -152,12 +142,7 @@ func resourceDockerContainerUpdate(d *schema.ResourceData, meta interface{}) err
|
||||||
}
|
}
|
||||||
|
|
||||||
func resourceDockerContainerDelete(d *schema.ResourceData, meta interface{}) error {
|
func resourceDockerContainerDelete(d *schema.ResourceData, meta interface{}) error {
|
||||||
config := meta.(*Config)
|
client := meta.(*dc.Client)
|
||||||
|
|
||||||
client, err := config.NewClient()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Unable to connect to Docker: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
removeOpts := dc.RemoveContainerOptions{
|
removeOpts := dc.RemoveContainerOptions{
|
||||||
ID: d.Id(),
|
ID: d.Id(),
|
||||||
|
|
|
@ -9,9 +9,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func resourceDockerImageCreate(d *schema.ResourceData, meta interface{}) error {
|
func resourceDockerImageCreate(d *schema.ResourceData, meta interface{}) error {
|
||||||
config := meta.(*Config)
|
client := meta.(*dc.Client)
|
||||||
|
apiImage, err := findImage(d, client)
|
||||||
apiImage, err := findImage(d, config)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Unable to read Docker image into resource: %s", err)
|
return fmt.Errorf("Unable to read Docker image into resource: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -23,9 +22,8 @@ func resourceDockerImageCreate(d *schema.ResourceData, meta interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func resourceDockerImageRead(d *schema.ResourceData, meta interface{}) error {
|
func resourceDockerImageRead(d *schema.ResourceData, meta interface{}) error {
|
||||||
config := meta.(*Config)
|
client := meta.(*dc.Client)
|
||||||
|
apiImage, err := findImage(d, client)
|
||||||
apiImage, err := findImage(d, config)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Unable to read Docker image into resource: %s", err)
|
return fmt.Errorf("Unable to read Docker image into resource: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -53,6 +51,10 @@ func fetchLocalImages(data *Data, client *dc.Client) error {
|
||||||
return fmt.Errorf("Unable to list Docker images: %s", err)
|
return fmt.Errorf("Unable to list Docker images: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if data.DockerImages == nil {
|
||||||
|
data.DockerImages = make(map[string]*dc.APIImages)
|
||||||
|
}
|
||||||
|
|
||||||
// Docker uses different nomenclatures in different places...sometimes a short
|
// Docker uses different nomenclatures in different places...sometimes a short
|
||||||
// ID, sometimes long, etc. So we store both in the map so we can always find
|
// ID, sometimes long, etc. So we store both in the map so we can always find
|
||||||
// the same image object. We store the tags, too.
|
// the same image object. We store the tags, too.
|
||||||
|
@ -132,15 +134,9 @@ func getImageTag(image string) string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func findImage(d *schema.ResourceData, config *Config) (*dc.APIImages, error) {
|
func findImage(d *schema.ResourceData, client *dc.Client) (*dc.APIImages, error) {
|
||||||
client, err := config.NewClient()
|
var data Data
|
||||||
if err != nil {
|
if err := fetchLocalImages(&data, client); err != nil {
|
||||||
return nil, fmt.Errorf("Unable to connect to Docker: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
data := config.NewData()
|
|
||||||
|
|
||||||
if err := fetchLocalImages(data, client); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -163,7 +159,7 @@ func findImage(d *schema.ResourceData, config *Config) (*dc.APIImages, error) {
|
||||||
foundImage := searchLocal()
|
foundImage := searchLocal()
|
||||||
|
|
||||||
if d.Get("keep_updated").(bool) || foundImage == nil {
|
if d.Get("keep_updated").(bool) || foundImage == nil {
|
||||||
if err := pullImage(data, client, imageName); err != nil {
|
if err := pullImage(&data, client, imageName); err != nil {
|
||||||
return nil, fmt.Errorf("Unable to pull image %s: %s", imageName, err)
|
return nil, fmt.Errorf("Unable to pull image %s: %s", imageName, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue