Newer
Older
"bazil.org/fuse"
"bazil.org/fuse/fs"
_ "bazil.org/fuse/fs/fstestutil"
var client *http.Client
var password string
var classes map[float64]Puppetclass
password = os.Args[1:][0]
classes = map[float64]Puppetclass{}
tr := &http.Transport {
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
// request, err := http.NewRequest("GET", "https://chapman.lysator.liu.se/api/hosts", nil)
request, err := http.NewRequest("GET", "http://localhost:8000/hosts.json", nil)
if err != nil {
log.Fatal(err);
}
request.SetBasicAuth("hugo", password);
resp, err := client.Do(request)
if err != nil {
log.Fatal(err);
}
// echo curl --user "hugo:$(pass lysator/hugo)" -k https://chapman.lysator.liu.se/api/hosts
decoder := json.NewDecoder(resp.Body);
var m Message
err = decoder.Decode(&m)
if err != nil {
log.Fatal(err);
}
for _, record := range m.Results {
if record.Owner_name == "Hugo Hörnquist" {
fmt.Printf("%v\n", record.Name)
}
}
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
c, err := fuse.Mount(
"mnt",
fuse.FSName("Foreman"),
fuse.Subtype("foreman"),
)
if err != nil {
log.Fatal(err)
}
// defer c.Unmount()
defer c.Close()
err = fs.Serve(c, FS{})
if err != nil {
log.Fatal(err)
}
}
type ResultRecord struct {
Ip string // "130.236.254.139",
Ip6 string // "2001:6b0:17:f0a0::8b",
Environment_id float64 // 1,
Environment_name string // "production",
Last_report string // "2020-10-28 19:10:57 UTC",
Mac string // "1c:c1:de:03:e7:b6",
/*
"realm_id": null,
"realm_name": null,
"sp_mac": null,
"sp_ip": null,
"sp_name": null,
*/
Domain_id float64 // 1,
Domain_name string // "lysator.liu.se",
Architecture_id float64 // 1,
Architecture_name string // "x86_64",
Operatingsystem_id float64 // 6,
Operatingsystem_name string // "CentOS Linux 7.8.2003",
/*
"subnet_id": null,
"subnet_name": null,
"subnet6_id": null,
"subnet6_name": null,
"sp_subnet_id": null,
"ptable_id": null,
"ptable_name": null,
"medium_id": null,
"medium_name": null,
"pxe_loader": null,
"build": false,
*/
Comment string // "",
/*
"disk": null,
"installed_at": null,
*/
Model_id float64 // 3,
Hostgroup_id float64 // 6,
Owner_id float64 // 5,
Owner_name string // "Henrik Henriksson",
Owner_type string // "User",
Enabled bool // true,
Managed bool // false,
/*
"use_image": null,
*/
Image_file string // "",
/*
"uuid": null,
"compute_resource_id": null,
"compute_resource_name": null,
"compute_profile_id": null,
"compute_profile_name": null,
*/
// capabilities []interface{} // [ "build" ],
Provision_method string // "build",
Certname string // "analysator-system.lysator.liu.se",
/*
"image_id": null,
"image_name": null,
*/
Created_at string // "2020-08-16 19:51:59 UTC",
Updated_at string // "2020-10-28 19:11:26 UTC",
/*
"last_compile": null,
*/
Global_status float64 // 0,
Global_status_label string // "OK",
Uptime_seconds float64 // 20857465,
Organization_id float64 // 1,
Organization_name string // "Lysator",
Location_id float64 // 2,
Location_name string // "Foo",
Puppet_status float64 // 0,
Model_name string // "ProLiant DL180 G6",
Configuration_status float64 // 0,
Configuration_status_label string // "No changes",
Name string // "analysator-system.lysator.liu.se",
Id float64 // 13,
Puppet_proxy_id float64 // 1,
Puppet_proxy_name string // "chapman.lysator.liu.se",
Puppet_ca_proxy_id float64 // 1,
Puppet_ca_proxy_name string // "chapman.lysator.liu.se",
/*
"puppet_proxy": {
"name": "chapman.lysator.liu.se",
"id": 1,
"url": "https://chapman.lysator.liu.se:8443"
},
"puppet_ca_proxy": {
"name": "chapman.lysator.liu.se",
"id": 1,
"url": "https://chapman.lysator.liu.se:8443"
},
*/
Hostgroup_name string // "System",
Hostgroup_title string // "Analysator/System"
}
type Message struct {
Total, Subtotal, Page/*, Per_page*/ float64
Results []ResultRecord
// search
// sort { by, order null }
}
type FS struct{
// data []ResultRecord
}
func (fs FS) Root() (fs.Node, error) {
}
func (FS) GenerateInode(parentInode uint64, name string) uint64 {
sum := uint64(0)
sum += parentInode
for _, c := range []byte(name) {
sum += uint64(c)
}
log.Printf("Generating inode for %v (inode = %v, parent = %v)",
name, sum, parentInode)
/* dir hashable? */
/* TODO does two Dir object with same value hash become the same
* object? */
}
func (Dir) Attr(ctx context.Context, a *fuse.Attr) error {
a.Mode = os.ModeDir | 0o555
return nil
}
func (d Dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
log.Printf("Lookup '%v' in '%v'", name, d.name)
if name == "hosts" {
return Dir{ d.name + "/" + name }, nil
}
for _, item := range data {
if item.Name == name {
log.Printf("Found %v", name)
}
}
log.Printf("File %v missing", name)
return nil, syscall.ENOENT
}
type File struct {
}
func (d Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
var dirents []fuse.Dirent
dirents = append(dirents, fuse.Dirent{Inode: 0, Name: "hosts", Type: fuse.DT_Dir})
/*
for _, item := range data {
dirents = append(dirents, fuse.Dirent{Inode: 0, Name: item.Name, Type: fuse.DT_File})
}
return dirents, nil
}
func (f File) Attr(ctx context.Context, a *fuse.Attr) error {
a.Inode = 0
a.Mode = 0o444
a.Size = uint64(len(fmt.Sprintf("%+v\n", f.record)))
// a.Size = uint64(len(greeting))
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
type Puppetclass struct {
Profiles []struct {
Id float64
Name string
Created_at string
Updated_at string
}
/*
[{"id":433,"name":"profiles::elvisp","created_at":"2020-10-27T07:57:47.102Z","updated_at":"2020-10-27T07:57:47.102Z"},
{"id":243,"name":"profiles::service","created_at":"2020-08-16T15:07:23.431Z","updated_at":"2020-08-16T15:07:23.431Z"},
{"id":434,"name":"profiles::vitellary","created_at":"2020 -10-27T07:57:47.130Z","updated_at":"2020-10-27T07:57:47.130Z"}]}
*/
/*
{
"total": 3,
"subtotal": 3,
"page": 1,
"per_page": 50,
"search": null,
"sort": {
"by": null,
"order": null
},
"results": {"profiles":[{"id":433,"name":"profiles::elvisp","created_at":"2020-10-27T07:57:47.102Z","updated_at":"2020-10-27T07:57:47.102Z"},{"id":243,"name"
:"profiles::service","created_at":"2020-08-16T15:07:23.431Z","updated_at":"2020-08-16T15:07:23.431Z"},{"id":434,"name":"profiles::vitellary","created_at":"2020
-10-27T07:57:47.130Z","updated_at":"2020-10-27T07:57:47.130Z"}]}
}
*/
}
type PuppetclassMessage struct {
Total, Subtotal, Page/*, Per_page*/ float64
Results Puppetclass
}
func (f File) ReadAll(ctx context.Context) ([]byte, error) {
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
var record Puppetclass
if val, ok := classes[f.record.Id]; ok {
record = val
} else {
url := fmt.Sprintf("https://chapman.lysator.liu.se/api/hosts/%d/puppetclasses", int(f.record.Id));
request, err := http.NewRequest("GET",url, nil)
if err != nil {
log.Fatal(err);
}
request.SetBasicAuth("hugo", password);
resp, err := client.Do(request)
if err != nil {
log.Fatal(err);
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body);
var m PuppetclassMessage
err = decoder.Decode(&m)
if err != nil {
log.Fatal(err);
}
classes[f.record.Id] = m.Results
record = m.Results
}
str := ""
for _, r := range record.Profiles {
str += r.Name + "\n"
}
//return []byte(fmt.Sprintf("%+v\n", f.record)), nil
return []byte(str), nil