diff --git a/builder/yandex/cloud_init.go b/builder/yandex/cloud_init.go index 46ee4815c..f1e63b6ea 100644 --- a/builder/yandex/cloud_init.go +++ b/builder/yandex/cloud_init.go @@ -5,10 +5,15 @@ import ( "fmt" "mime/multipart" "net/textproto" + "strings" +) + +const ( + defaultContentType = "text/cloud-config" + shellContentType = "text/x-shellscript" ) const ( - defaultContentType = "text/cloud-config" 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 +} diff --git a/builder/yandex/cloud_init_test.go b/builder/yandex/cloud_init_test.go index 7b00b10e0..aa2d5fcb3 100644 --- a/builder/yandex/cloud_init_test.go +++ b/builder/yandex/cloud_init_test.go @@ -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") }