2022-04-19 12:01:08 +00:00
|
|
|
// Copyright 2022 Linka Cloud All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2022-04-21 16:28:50 +00:00
|
|
|
package d2vm
|
2022-04-19 12:01:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
_ "embed"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2024-07-09 16:32:19 +00:00
|
|
|
"strconv"
|
2022-04-19 12:01:08 +00:00
|
|
|
"text/template"
|
2022-08-08 15:58:49 +00:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2022-04-19 12:01:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed templates/ubuntu.Dockerfile
|
|
|
|
var ubuntuDockerfile string
|
|
|
|
|
|
|
|
//go:embed templates/debian.Dockerfile
|
|
|
|
var debianDockerfile string
|
|
|
|
|
|
|
|
//go:embed templates/alpine.Dockerfile
|
|
|
|
var alpineDockerfile string
|
|
|
|
|
|
|
|
//go:embed templates/centos.Dockerfile
|
|
|
|
var centOSDockerfile string
|
|
|
|
|
|
|
|
var (
|
2024-08-29 09:29:58 +00:00
|
|
|
ubuntuDockerfileTemplate = template.Must(template.New("ubuntu.Dockerfile").Funcs(tplFuncs).Parse(ubuntuDockerfile))
|
|
|
|
debianDockerfileTemplate = template.Must(template.New("debian.Dockerfile").Funcs(tplFuncs).Parse(debianDockerfile))
|
|
|
|
alpineDockerfileTemplate = template.Must(template.New("alpine.Dockerfile").Funcs(tplFuncs).Parse(alpineDockerfile))
|
|
|
|
centOSDockerfileTemplate = template.Must(template.New("centos.Dockerfile").Funcs(tplFuncs).Parse(centOSDockerfile))
|
2022-04-19 12:01:08 +00:00
|
|
|
)
|
|
|
|
|
2022-08-08 15:58:49 +00:00
|
|
|
type NetworkManager string
|
|
|
|
|
|
|
|
const (
|
|
|
|
NetworkManagerNone NetworkManager = "none"
|
|
|
|
NetworkManagerIfupdown2 NetworkManager = "ifupdown"
|
|
|
|
NetworkManagerNetplan NetworkManager = "netplan"
|
|
|
|
)
|
|
|
|
|
2022-08-09 12:30:03 +00:00
|
|
|
func (n NetworkManager) Validate() error {
|
|
|
|
switch n {
|
|
|
|
case NetworkManagerNone, NetworkManagerIfupdown2, NetworkManagerNetplan:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported network manager: %s", n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 12:01:08 +00:00
|
|
|
type Dockerfile struct {
|
2022-08-08 15:58:49 +00:00
|
|
|
Image string
|
|
|
|
Password string
|
|
|
|
Release OSRelease
|
|
|
|
NetworkManager NetworkManager
|
2023-02-23 14:02:12 +00:00
|
|
|
Luks bool
|
2023-09-12 11:59:11 +00:00
|
|
|
GrubBIOS bool
|
|
|
|
GrubEFI bool
|
2022-08-08 15:58:49 +00:00
|
|
|
tmpl *template.Template
|
2022-04-19 12:01:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-12 11:59:11 +00:00
|
|
|
func (d Dockerfile) Grub() bool {
|
|
|
|
return d.GrubBIOS || d.GrubEFI
|
|
|
|
}
|
|
|
|
|
2022-04-19 12:01:08 +00:00
|
|
|
func (d Dockerfile) Render(w io.Writer) error {
|
|
|
|
return d.tmpl.Execute(w, d)
|
|
|
|
}
|
|
|
|
|
2023-09-12 11:59:11 +00:00
|
|
|
func NewDockerfile(release OSRelease, img, password string, networkManager NetworkManager, luks, grubBIOS, grubEFI bool) (Dockerfile, error) {
|
|
|
|
d := Dockerfile{Release: release, Image: img, Password: password, NetworkManager: networkManager, Luks: luks, GrubBIOS: grubBIOS, GrubEFI: grubEFI}
|
2022-08-08 15:58:49 +00:00
|
|
|
var net NetworkManager
|
2022-04-19 12:01:08 +00:00
|
|
|
switch release.ID {
|
|
|
|
case ReleaseDebian:
|
|
|
|
d.tmpl = debianDockerfileTemplate
|
2022-08-08 15:58:49 +00:00
|
|
|
net = NetworkManagerIfupdown2
|
2022-09-18 21:02:25 +00:00
|
|
|
case ReleaseKali:
|
|
|
|
d.tmpl = debianDockerfileTemplate
|
|
|
|
net = NetworkManagerIfupdown2
|
2022-04-19 12:01:08 +00:00
|
|
|
case ReleaseUbuntu:
|
2024-08-29 09:29:58 +00:00
|
|
|
d.tmpl = ubuntuDockerfileTemplate
|
2024-08-23 12:57:10 +00:00
|
|
|
if release.VersionID < "18.04" {
|
|
|
|
net = NetworkManagerIfupdown2
|
|
|
|
} else {
|
|
|
|
net = NetworkManagerNetplan
|
|
|
|
}
|
2022-04-19 12:01:08 +00:00
|
|
|
case ReleaseAlpine:
|
|
|
|
d.tmpl = alpineDockerfileTemplate
|
2022-08-08 15:58:49 +00:00
|
|
|
net = NetworkManagerIfupdown2
|
2022-08-09 12:30:03 +00:00
|
|
|
if networkManager == NetworkManagerNetplan {
|
|
|
|
return d, fmt.Errorf("netplan is not supported on alpine")
|
|
|
|
}
|
2022-04-21 19:31:57 +00:00
|
|
|
case ReleaseCentOS:
|
2022-04-19 12:01:08 +00:00
|
|
|
d.tmpl = centOSDockerfileTemplate
|
2022-08-09 12:30:03 +00:00
|
|
|
net = NetworkManagerNone
|
|
|
|
if networkManager != "" && networkManager != NetworkManagerNone {
|
|
|
|
return Dockerfile{}, fmt.Errorf("network manager is not supported on centos")
|
|
|
|
}
|
2022-04-19 12:01:08 +00:00
|
|
|
default:
|
|
|
|
return Dockerfile{}, fmt.Errorf("unsupported distribution: %s", release.ID)
|
|
|
|
}
|
2022-08-08 15:58:49 +00:00
|
|
|
if d.NetworkManager == "" {
|
2022-08-09 12:30:03 +00:00
|
|
|
if release.ID != ReleaseCentOS {
|
|
|
|
logrus.Warnf("no network manager specified, using distribution defaults: %s", net)
|
|
|
|
}
|
2022-08-08 15:58:49 +00:00
|
|
|
d.NetworkManager = net
|
|
|
|
}
|
2022-08-09 12:30:03 +00:00
|
|
|
if err := d.NetworkManager.Validate(); err != nil {
|
|
|
|
return Dockerfile{}, err
|
|
|
|
}
|
2022-04-19 12:01:08 +00:00
|
|
|
return d, nil
|
|
|
|
}
|
2024-07-09 16:32:19 +00:00
|
|
|
|
|
|
|
var tplFuncs = template.FuncMap{
|
|
|
|
"atoi": strconv.Atoi,
|
|
|
|
}
|