@@ -36,6 +36,10 @@ type CloudConfigUser struct {
36
36
Sudo string `yaml:"sudo"`
37
37
}
38
38
39
+ type MetaData struct {
40
+ LocalHostname string `yaml:"local-hostname"`
41
+ }
42
+
39
43
const (
40
44
ConfigDriveLabel = "cidata"
41
45
UserDataFile = "user-data"
@@ -44,27 +48,70 @@ const (
44
48
45
49
func main () {
46
50
log .Print ("Starting rancher-flatcar-cloudinit" )
47
- err := process ()
51
+
52
+ log .Printf ("Mounting config drive with LABEL = %s" , ConfigDriveLabel )
53
+ configDriveDir , err := mountConfigDrive ()
54
+ if err != nil {
55
+ log .Printf ("ERROR: %s" , err )
56
+ os .Exit (1 )
57
+ }
58
+
59
+ log .Print ("Processing user-data" )
60
+ err = processMetaData (configDriveDir )
61
+ if err != nil {
62
+ log .Printf ("ERROR: %s" , err )
63
+ os .Exit (1 )
64
+ }
65
+
66
+ log .Print ("Processing user-data" )
67
+ err = processUserData (configDriveDir )
48
68
if err != nil {
49
69
log .Printf ("ERROR: %s" , err )
50
70
os .Exit (1 )
51
71
}
52
72
}
53
73
54
- func process () error {
74
+ func mountConfigDrive () ( string , error ) {
55
75
// mount config drive
56
76
configDriveDir , err := os .MkdirTemp ("" , "configdrive" )
57
77
if err != nil {
58
- log . Fatal ( err )
78
+ return "" , err
59
79
}
60
80
defer os .RemoveAll (configDriveDir )
61
81
62
82
output , err := exec .Command ("mount" , "-L" , ConfigDriveLabel , configDriveDir ).CombinedOutput ()
63
83
if err != nil {
64
- return fmt .Errorf ("could not mount config drive with label '%s': %s\n %s" , ConfigDriveLabel , err , output )
84
+ return "" , fmt .Errorf ("could not mount config drive with label '%s': %s\n %s" , ConfigDriveLabel , err , output )
65
85
}
66
86
defer exec .Command ("umount" , configDriveDir )
67
87
88
+ return configDriveDir , nil
89
+ }
90
+
91
+ func processMetaData (configDriveDir string ) error {
92
+ // parse meta data
93
+ metaData , err := os .ReadFile (configDriveDir + "/" + MetaDataFile )
94
+ if err != nil {
95
+ return fmt .Errorf ("could not read user-data file: %s" , err )
96
+ }
97
+
98
+ var md MetaData
99
+ err = yaml .Unmarshal (metaData , md )
100
+ if err != nil {
101
+ return fmt .Errorf ("could not parse meta-data file as YAML: %s" , err )
102
+ }
103
+
104
+ if md .LocalHostname != "" {
105
+ output , err := exec .Command ("hostnamectl" , "set-hostname" , md .LocalHostname ).CombinedOutput ()
106
+ if err != nil {
107
+ log .Printf ("Error setting hostname '%s': %s\n %s" , md .LocalHostname , err , output )
108
+ }
109
+ }
110
+
111
+ return nil
112
+ }
113
+
114
+ func processUserData (configDriveDir string ) error {
68
115
// parse user data
69
116
userData , err := os .ReadFile (configDriveDir + "/" + UserDataFile )
70
117
if err != nil {
0 commit comments