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" "fmt"
"mime/multipart" "mime/multipart"
"net/textproto" "net/textproto"
"strings"
)
const (
defaultContentType = "text/cloud-config"
shellContentType = "text/x-shellscript"
) )
const ( const (
defaultContentType = "text/cloud-config"
cloudInitIPv6Config = `#cloud-config cloudInitIPv6Config = `#cloud-config
bootcmd: 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' ] - [ 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 { for i, userData := range usersData {
w, err := data.CreatePart(textproto.MIMEHeader{ w, err := data.CreatePart(textproto.MIMEHeader{
"Content-Disposition": {fmt.Sprintf("attachment; filename=\"user-data-%d.yaml\"", i)}, "Content-Disposition": {fmt.Sprintf("attachment; filename=\"user-data-%d\"", i)},
"Content-Type": {defaultContentType}, "Content-Type": {detectContentType(userData)},
}) })
if err != nil { if err != nil {
return "", err return "", err
@ -43,3 +48,14 @@ func MergeCloudUserMetaData(usersData ...string) (string, error) {
} }
return buff.String(), nil 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" - touch "cmd3"
- cmd4 - cmd4
` `
data3 = `#!/bin/bash
touch /test`
) )
func TestCloudInitMerge(t *testing.T) { func TestCloudInitMerge(t *testing.T) {
merged, err := MergeCloudUserMetaData( merged, err := MergeCloudUserMetaData(
data1, data1,
data2, data2,
data3,
) )
require.NoError(t, err) require.NoError(t, err)
@ -35,4 +38,6 @@ func TestCloudInitMerge(t *testing.T) {
require.Contains(t, merged, "\"cmd3\"") require.Contains(t, merged, "\"cmd3\"")
require.Contains(t, merged, "cmd4") require.Contains(t, merged, "cmd4")
require.Contains(t, merged, "text/cloud-config")
require.Contains(t, merged, "text/x-shellscript")
} }