support shellscript

This commit is contained in:
Roman Mingazeev 2020-11-23 17:15:12 +03:00
parent 0101eb1bb1
commit 2b8fcead23
2 changed files with 24 additions and 3 deletions

View File

@ -5,10 +5,15 @@ import (
"fmt"
"mime/multipart"
"net/textproto"
"strings"
)
const (
defaultContentType = "text/cloud-config"
shellContentType = "text/x-shellscript"
)
const (
cloudInitIPv6Config = `#cloud-config
bootcmd:
- [ sh, -c, '/usr/bin/env dhclient -6 -D LL -nw -pf /run/dhclient_ipv6.eth0.pid -lf /var/lib/dhcp/dhclient_ipv6.eth0.leases eth0' ]
@ -30,8 +35,8 @@ func MergeCloudUserMetaData(usersData ...string) (string, error) {
for i, userData := range usersData {
w, err := data.CreatePart(textproto.MIMEHeader{
"Content-Disposition": {fmt.Sprintf("attachment; filename=\"user-data-%d.yaml\"", i)},
"Content-Type": {defaultContentType},
"Content-Disposition": {fmt.Sprintf("attachment; filename=\"user-data-%d\"", i)},
"Content-Type": {detectContentType(userData)},
})
if err != nil {
return "", err
@ -43,3 +48,14 @@ func MergeCloudUserMetaData(usersData ...string) (string, error) {
}
return buff.String(), nil
}
func detectContentType(content string) string {
switch {
case strings.HasPrefix(content, "#!"):
return shellContentType
case strings.HasPrefix(content, "#cloud-config"):
return defaultContentType
}
return defaultContentType
}

View File

@ -19,12 +19,15 @@ runcmd:
- touch "cmd3"
- cmd4
`
data3 = `#!/bin/bash
touch /test`
)
func TestCloudInitMerge(t *testing.T) {
merged, err := MergeCloudUserMetaData(
data1,
data2,
data3,
)
require.NoError(t, err)
@ -35,4 +38,6 @@ func TestCloudInitMerge(t *testing.T) {
require.Contains(t, merged, "\"cmd3\"")
require.Contains(t, merged, "cmd4")
require.Contains(t, merged, "text/cloud-config")
require.Contains(t, merged, "text/x-shellscript")
}