-
Notifications
You must be signed in to change notification settings - Fork 5
/
flamingo.go
154 lines (134 loc) · 3.1 KB
/
flamingo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"flag"
"time"
"github.com/tmrts/flamingo/pkg/datasrc"
"github.com/tmrts/flamingo/pkg/distro"
"github.com/tmrts/flamingo/pkg/flog"
"github.com/tmrts/flamingo/pkg/sys"
)
var flags struct {
cloudConfig string
configDrive string
metadata string
}
func init() {
flag.StringVar(&flags.cloudConfig, "cloud-config", "", "user-data configuration file")
flag.StringVar(&flags.configDrive, "config-drive", "", "config drive mount path")
flag.StringVar(&flags.metadata, "meta-data", "", "meta-data file")
}
func InitializeContextualization() error {
// TODO(tmrts): Add plug-in hooks
flog.Info("Initializing contextualization",
flog.Fields{
Event: "main.InitializeContextualization",
},
)
return nil
}
func FinalizeContextualization() error {
// TODO(tmrts): Add plug-in hooks
flog.Info("Finalizing contextualization",
flog.Fields{
Event: "main.FinalizeContextualization",
},
)
return nil
}
func HasRootPrivileges() (bool, error) {
// TODO(tmrts): Move privilege check to sys.nss package
ent, err := sys.Execute("getent", "gshadow", "root")
if err != nil {
return false, err
}
return ent != "", nil
}
func main() {
// TODO(tmrts): Add command-line flags
// TODO(tmrts): Build a logger hierarchy
hasRoot, err := HasRootPrivileges()
if err != nil {
flog.Fatal("Failed checking user privileges",
flog.Fields{
Event: "main.HasRootPrivileges",
Error: err,
},
)
}
if !hasRoot {
flog.Fatal("current user doesn't have root privileges")
}
centOS := distro.CentOS(sys.DefaultExecutor)
if err := InitializeContextualization(); err != nil {
flog.Fatal("Failed to start contextualization",
flog.Fields{
Event: "main.InitializeContextualization",
Error: err,
},
)
}
providers := datasrc.SupportedProviders()
p, err := datasrc.FindProvider(providers, 5*time.Second)
if err != nil {
flog.Fatal("Failed to start contextualization",
flog.Fields{
Event: "datasrc.FindProvider",
Error: err,
},
)
}
m, err := p.FetchMetadata()
if err != nil {
flog.Fatal("Failed to fetch meta-data from provider",
flog.Fields{
Event: "datasrc.Provider.FetchMetadata",
Error: err,
},
flog.Details{
"provider": p,
},
)
}
u, err := p.FetchUserdata()
if err != nil {
flog.Fatal("Failed to fetch user-data from provider",
flog.Fields{
Event: "datasrc.Provider.FetchUserdata",
Error: err,
},
flog.Details{
"provider": p,
},
)
}
if err := centOS.ConsumeMetadata(m); err != nil {
flog.Fatal("Failed to consume meta-data",
flog.Fields{
Event: "distro.Implementation.ConsumeMetadata",
Error: err,
},
flog.Details{
"distribution": "CentOS",
},
)
}
if err := centOS.ConsumeUserdata(u); err != nil {
flog.Fatal("Failed to consume user-data",
flog.Fields{
Event: "distro.Implementation.ConsumeMetadata",
Error: err,
},
flog.Details{
"distribution": "CentOS",
},
)
}
if err := FinalizeContextualization(); err != nil {
flog.Fatal("Failed to finalize contextualization",
flog.Fields{
Event: "main.FinalizeContextualization",
Error: err,
},
)
}
}