2020-11-23 05:19:50 -05:00
|
|
|
package yandex
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"mime/multipart"
|
|
|
|
"net/textproto"
|
2020-11-23 09:15:12 -05:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-12-14 05:04:27 -05:00
|
|
|
defaultContentType = "text/cloud-config"
|
|
|
|
shellContentType = "text/x-shellscript"
|
|
|
|
multipartContentType = "multipart/mixed"
|
2020-11-23 05:19:50 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-12-14 05:04:27 -05:00
|
|
|
cloudInitIPv6Config = `#!/usr/bin/env bash
|
|
|
|
dhclient -6 -D LL -nw -pf /run/dhclient_ipv6.eth0.pid -lf /var/lib/dhcp/dhclient_ipv6.eth0.leases eth0
|
2020-11-23 05:19:50 -05:00
|
|
|
`
|
|
|
|
)
|
|
|
|
|
|
|
|
// MergeCloudUserMetaData allow merge some user-data sections
|
|
|
|
func MergeCloudUserMetaData(usersData ...string) (string, error) {
|
|
|
|
buff := new(bytes.Buffer)
|
|
|
|
data := multipart.NewWriter(buff)
|
2020-12-14 05:04:27 -05:00
|
|
|
_, err := buff.WriteString(fmt.Sprintf("Content-Type: %s; boundary=\"%s\"\r\n", multipartContentType, data.Boundary()))
|
2020-11-23 05:19:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
_, err = buff.WriteString("MIME-Version: 1.0\r\n\r\n")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, userData := range usersData {
|
|
|
|
w, err := data.CreatePart(textproto.MIMEHeader{
|
2020-11-23 09:15:12 -05:00
|
|
|
"Content-Disposition": {fmt.Sprintf("attachment; filename=\"user-data-%d\"", i)},
|
|
|
|
"Content-Type": {detectContentType(userData)},
|
2020-11-23 05:19:50 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
_, err = w.Write([]byte(userData))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buff.String(), nil
|
|
|
|
}
|
2020-11-23 09:15:12 -05:00
|
|
|
|
|
|
|
func detectContentType(content string) string {
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(content, "#!"):
|
|
|
|
return shellContentType
|
|
|
|
case strings.HasPrefix(content, "#cloud-config"):
|
|
|
|
return defaultContentType
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultContentType
|
|
|
|
}
|