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"
|
|
|
|
"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 (
|
|
|
|
ubuntuDockerfileTemplate = template.Must(template.New("ubuntu.Dockerfile").Parse(ubuntuDockerfile))
|
|
|
|
debianDockerfileTemplate = template.Must(template.New("debian.Dockerfile").Parse(debianDockerfile))
|
|
|
|
alpineDockerfileTemplate = template.Must(template.New("alpine.Dockerfile").Parse(alpineDockerfile))
|
|
|
|
centOSDockerfileTemplate = template.Must(template.New("centos.Dockerfile").Parse(centOSDockerfile))
|
|
|
|
)
|
|
|
|
|
2022-08-08 15:58:49 +00:00
|
|
|
type NetworkManager string
|
|
|
|
|
|
|
|
const (
|
|
|
|
NetworkManagerNone NetworkManager = "none"
|
|
|
|
NetworkManagerIfupdown2 NetworkManager = "ifupdown"
|
|
|
|
NetworkManagerNetplan NetworkManager = "netplan"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
tmpl *template.Template
|
2022-04-19 12:01:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d Dockerfile) Render(w io.Writer) error {
|
|
|
|
return d.tmpl.Execute(w, d)
|
|
|
|
}
|
|
|
|
|
2022-08-08 15:58:49 +00:00
|
|
|
func NewDockerfile(release OSRelease, img, password string, networkManager NetworkManager) (Dockerfile, error) {
|
2022-04-19 12:01:08 +00:00
|
|
|
if password == "" {
|
|
|
|
password = "root"
|
|
|
|
}
|
2022-08-08 15:58:49 +00:00
|
|
|
d := Dockerfile{Release: release, Image: img, Password: password, NetworkManager: networkManager}
|
|
|
|
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-04-19 12:01:08 +00:00
|
|
|
case ReleaseUbuntu:
|
|
|
|
d.tmpl = ubuntuDockerfileTemplate
|
2022-08-08 15:58:49 +00:00
|
|
|
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-04-21 19:31:57 +00:00
|
|
|
case ReleaseCentOS:
|
2022-04-19 12:01:08 +00:00
|
|
|
d.tmpl = centOSDockerfileTemplate
|
|
|
|
default:
|
|
|
|
return Dockerfile{}, fmt.Errorf("unsupported distribution: %s", release.ID)
|
|
|
|
}
|
2022-08-08 15:58:49 +00:00
|
|
|
if d.NetworkManager == "" {
|
|
|
|
logrus.Warnf("no network manager specified, using distribution defaults: %s", net)
|
|
|
|
d.NetworkManager = net
|
|
|
|
}
|
2022-04-19 12:01:08 +00:00
|
|
|
return d, nil
|
|
|
|
}
|