Skip to content

Commit 0571efd

Browse files
committed
feat(proxmoxve): initial commit
1 parent a7e36b7 commit 0571efd

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Copyright 2019 Red Hat, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// The OpenStack provider fetches configurations from the userdata available in
16+
// both the config-drive as well as the network metadata service. Whichever
17+
// responds first is the config that is used.
18+
// NOTE: This provider is still EXPERIMENTAL.
19+
20+
package proxmoxve
21+
22+
import (
23+
"context"
24+
"fmt"
25+
"os"
26+
"os/exec"
27+
"path/filepath"
28+
"time"
29+
30+
"github.com/coreos/ignition/v2/config/v3_5_experimental/types"
31+
"github.com/coreos/ignition/v2/internal/distro"
32+
"github.com/coreos/ignition/v2/internal/log"
33+
"github.com/coreos/ignition/v2/internal/platform"
34+
"github.com/coreos/ignition/v2/internal/providers/util"
35+
"github.com/coreos/ignition/v2/internal/resource"
36+
ut "github.com/coreos/ignition/v2/internal/util"
37+
38+
"github.com/coreos/vcontext/report"
39+
)
40+
41+
const (
42+
cidataPath = "/user-data"
43+
deviceLabel = "cidata"
44+
)
45+
46+
func init() {
47+
platform.Register(
48+
platform.Provider{
49+
Name: "proxmoxve",
50+
Fetch: fetchConfig,
51+
},
52+
)
53+
}
54+
55+
func fetchConfig(f *resource.Fetcher) (types.Config, report.Report, error) {
56+
var data []byte
57+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
58+
59+
dispatch := func(name string, fn func() ([]byte, error)) {
60+
raw, err := fn()
61+
if err != nil {
62+
switch err {
63+
case context.Canceled:
64+
case context.DeadlineExceeded:
65+
f.Logger.Err("timed out while fetching config from %s", name)
66+
default:
67+
f.Logger.Err("failed to fetch config from %s: %v", name, err)
68+
}
69+
return
70+
}
71+
72+
data = raw
73+
cancel()
74+
}
75+
76+
go dispatch(
77+
"config drive (cidata)", func() ([]byte, error) {
78+
return fetchConfigFromDevice(f.Logger, ctx, filepath.Join(distro.DiskByLabelDir(), deviceLabel))
79+
},
80+
)
81+
82+
<-ctx.Done()
83+
if ctx.Err() == context.DeadlineExceeded {
84+
f.Logger.Info("cidata drive was not available in time. Continuing without a config...")
85+
}
86+
87+
return util.ParseConfig(f.Logger, data)
88+
}
89+
90+
func fileExists(path string) bool {
91+
_, err := os.Stat(path)
92+
return (err == nil)
93+
}
94+
95+
func fetchConfigFromDevice(logger *log.Logger, ctx context.Context, path string) ([]byte, error) {
96+
for !fileExists(path) {
97+
logger.Debug("config drive (%q) not found. Waiting...", path)
98+
select {
99+
case <-time.After(time.Second):
100+
case <-ctx.Done():
101+
return nil, ctx.Err()
102+
}
103+
}
104+
105+
logger.Debug("creating temporary mount point")
106+
mnt, err := os.MkdirTemp("", "ignition-configdrive")
107+
if err != nil {
108+
return nil, fmt.Errorf("failed to create temp directory: %v", err)
109+
}
110+
defer os.Remove(mnt)
111+
112+
cmd := exec.Command(distro.MountCmd(), "-o", "ro", "-t", "auto", path, mnt)
113+
if _, err := logger.LogCmd(cmd, "mounting config drive"); err != nil {
114+
return nil, err
115+
}
116+
defer func() {
117+
_ = logger.LogOp(
118+
func() error {
119+
return ut.UmountPath(mnt)
120+
},
121+
"unmounting %q at %q", path, mnt,
122+
)
123+
}()
124+
125+
if !fileExists(filepath.Join(mnt, cidataPath)) {
126+
return nil, nil
127+
}
128+
129+
return os.ReadFile(filepath.Join(mnt, cidataPath))
130+
}

0 commit comments

Comments
 (0)