Add ability to upload executable files (#55)

* Add ability to upload executable files
This commit is contained in:
Sid Verma 2018-04-20 15:00:45 +05:30 committed by Manuel Vogel
parent 46d1fc59eb
commit 97599ecb3d
4 changed files with 27 additions and 2 deletions

View file

@ -405,6 +405,12 @@ func resourceDockerContainer() *schema.Resource {
Required: true,
ForceNew: true,
},
"executable": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Default: false,
},
},
},
},

View file

@ -210,15 +210,23 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err
}
if v, ok := d.GetOk("upload"); ok {
var mode int64
for _, upload := range v.(*schema.Set).List() {
content := upload.(map[string]interface{})["content"].(string)
file := upload.(map[string]interface{})["file"].(string)
executable := upload.(map[string]interface{})["executable"].(bool)
buf := new(bytes.Buffer)
tw := tar.NewWriter(buf)
if executable {
mode = 0744
} else {
mode = 0644
}
hdr := &tar.Header{
Name: file,
Mode: 0644,
Mode: mode,
Size: int64(len(content)),
}
if err := tw.WriteHeader(hdr); err != nil {

View file

@ -5,6 +5,8 @@ import (
"bytes"
"fmt"
"os"
"strconv"
"strings"
"testing"
dc "github.com/fsouza/go-dockerclient"
@ -245,8 +247,13 @@ func TestAccDockerContainer_upload(t *testing.T) {
r := bytes.NewReader(buf.Bytes())
tr := tar.NewReader(r)
if _, err := tr.Next(); err != nil {
if header, err := tr.Next(); err != nil {
return fmt.Errorf("Unable to read content of tar archive: %s", err)
} else {
mode := strconv.FormatInt(header.Mode, 8)
if !strings.HasSuffix(mode, "744") {
return fmt.Errorf("File permissions are incorrect: %s", mode)
}
}
fbuf := new(bytes.Buffer)
@ -479,6 +486,7 @@ resource "docker_container" "foo" {
upload {
content = "foo"
file = "/terraform/test.txt"
executable = true
}
}
`

View file

@ -161,6 +161,9 @@ Each `upload` supports the following
* `content` - (Required, string) A content of a file to upload.
* `file` - (Required, string) path to a file in the container.
* `executable` - (Optional, bool) If true, the file will be uploaded with user
executable permission.
Defaults to false.
<a id="devices"></a>
### Devices