From 079c2f27b135aa9c75b2e9eb40ea36af63b1e0c6 Mon Sep 17 00:00:00 2001 From: duyiwei Date: Fri, 31 Mar 2023 18:15:54 +0800 Subject: [PATCH] add new type [rpm-ostree] and support /etc/systemd/system --- deploy/tools/helper.sh | 8 +- deploy/tools/install_controller.sh | 24 +++++- deploy/tools/loadbalancer.sh | 8 +- deploy/tools/node.sh | 16 +++- .../binary/cleanupcluster/cleanupetcd.go | 1 + .../cleanupcluster/cleanuploadbalance.go | 2 +- .../binary/cleanupcluster/cleanupnode.go | 5 ++ .../binary/commontools/systemdservices.go | 15 +++- .../binary/coredns/binarycoredns.go | 2 +- .../binary/etcdcluster/etcdcluster.go | 12 +++ pkg/utils/dependency/dependency.go | 80 ++++++++++++++++++- pkg/utils/dependency/install.go | 14 ++-- pkg/utils/runner/runner.go | 2 +- 13 files changed, 169 insertions(+), 20 deletions(-) diff --git a/deploy/tools/helper.sh b/deploy/tools/helper.sh index f34f52b..526b954 100755 --- a/deploy/tools/helper.sh +++ b/deploy/tools/helper.sh @@ -276,7 +276,13 @@ function deploy_coredns() { loadbalance } EOF - cat >/usr/lib/systemd/system/coredns.service < /dev/null; then + file_path="/etc/systemd/system/coredns.service" +else + file_path="/usr/lib/systemd/system/coredns.service" +fi + + cat >"$file_path"</usr/lib/systemd/system/kube-apiserver.service < /dev/null; then + file_path="/etc/systemd/system/kube-apiserver.service" +else + file_path="/usr/lib/systemd/system/kube-apiserver.service" +fi + + cat > "$file_path" </usr/lib/systemd/system/kube-controller-manager.service < /dev/null; then + file_path="/etc/systemd/system/kube-controller-manager.service" +else + file_path="/usr/lib/systemd/system/kube-controller-manager.service" +fi + + cat > "$file_path" </usr/lib/systemd/system/kube-scheduler.service < /dev/null; then + file_path="/etc/systemd/system/kube-scheduler.service" +else + file_path="/usr/lib/systemd/system/kube-scheduler.service" +fi + + cat > "$file_path" </usr/lib/systemd/system/nginx.service < /dev/null; then + file_path="/etc/systemd/system/nginx.service" +else + file_path="/usr/lib/systemd/system/nginx.service" +fi + + cat > "$file_path" </usr/lib/systemd/system/kubelet.service < /dev/null; then + file_path="/etc/systemd/system/kubelet.service" +else + file_path="/usr/lib/systemd/system/kubelet.service" +fi + + cat > "$file_path" </usr/lib/systemd/system/kube-proxy.service < /dev/null; then + file_path="/etc/systemd/system/kube-proxy.service" +else + file_path="/usr/lib/systemd/system/kube-proxy.service" +fi + + cat > "$file_path" < /dev/null; then + file_path="/etc/systemd/system" +else + file_path="/usr/lib/systemd/system" +fi + {{- if .content }} -rm -f /usr/lib/systemd/system/{{ .name }}.service -echo {{ .content }} | base64 -d > /usr/lib/systemd/system/{{ .name }}.service + +rm -f "$file_path"/{{ .name }}.service +echo {{ .content }} | base64 -d > "$file_path"/{{ .name }}.service systemctl daemon-reload # disable success when service file exist systemctl disable {{ .name }} {{- end }} which chcon if [ $? -eq 0 ]; then - chcon -v -t systemd_unit_file_t -u system_u /usr/lib/systemd/system/{{ .name }}.service - for conf in $(cat /usr/lib/systemd/system/{{ .name }}.service | grep EnvironmentFile | awk -F '=' '{print $2}'); do + chcon -v -t systemd_unit_file_t -u system_u "$file_path"/{{ .name }}.service + for conf in $(cat $file_path/{{ .name }}.service | grep EnvironmentFile | awk -F '=' '{print $2}'); do split=$(echo $conf | awk -F '-' '{print $2}') if [ "x$split" == "x" ]; then split=$conf diff --git a/pkg/clusterdeployment/binary/coredns/binarycoredns.go b/pkg/clusterdeployment/binary/coredns/binarycoredns.go index aa3fada..5d0955a 100644 --- a/pkg/clusterdeployment/binary/coredns/binarycoredns.go +++ b/pkg/clusterdeployment/binary/coredns/binarycoredns.go @@ -333,7 +333,7 @@ fi systemctl stop coredns systemctl disable coredns -rm -f /etc/dns/Corefile /usr/lib/systemd/system/coredns.service +rm -f /etc/dns/Corefile /usr/lib/systemd/system/coredns.service /etc/systemd/system/coredns.service exit 0 ` datastore := make(map[string]interface{}) diff --git a/pkg/clusterdeployment/binary/etcdcluster/etcdcluster.go b/pkg/clusterdeployment/binary/etcdcluster/etcdcluster.go index d7d2f41..c015242 100644 --- a/pkg/clusterdeployment/binary/etcdcluster/etcdcluster.go +++ b/pkg/clusterdeployment/binary/etcdcluster/etcdcluster.go @@ -28,6 +28,7 @@ import ( "isula.org/eggo/pkg/clusterdeployment/binary/commontools" "isula.org/eggo/pkg/constants" "isula.org/eggo/pkg/utils" + "isula.org/eggo/pkg/utils/dependency" "isula.org/eggo/pkg/utils/nodemanager" "isula.org/eggo/pkg/utils/runner" "isula.org/eggo/pkg/utils/task" @@ -221,6 +222,17 @@ func prepareEtcdConfigs(ccfg *api.ClusterConfig, r runner.Runner, hostConfig *ap hostConfig.Address, err, output) } + //add /etc/systemd/system/ support + output, err := r.RunCommand(fmt.Sprintf("sudo -E /bin/sh -c \"%s\"", dependency.PrmTest)) + if err != nil { + logrus.Errorf("package repo manager test failed: %v", err) + return err + } + + if strings.Contains(output, "rpm-ostree") { + servicePath = "/etc/systemd/system/etcd.service" + } + base64Str = base64.StdEncoding.EncodeToString([]byte(createEtcdService())) cmd = fmt.Sprintf("echo %v | base64 -d > %v", base64Str, servicePath) if output, err := r.RunCommand(utils.AddSudo(cmd)); err != nil { diff --git a/pkg/utils/dependency/dependency.go b/pkg/utils/dependency/dependency.go index 3250579..8588371 100644 --- a/pkg/utils/dependency/dependency.go +++ b/pkg/utils/dependency/dependency.go @@ -27,7 +27,7 @@ import ( ) const ( - PrmTest = "if [ x != x$(which apt 2>/dev/null) ]; then echo apt ; elif [ x != x$(which yum 2>/dev/null) ]; then echo yum ; fi" + PrmTest = "if [ x != x$(which apt 2>/dev/null) ]; then echo apt ;fi; if [ x != x$(which yum 2>/dev/null) ]; then echo yum ;fi; if [ x != x$(which rpm-ostree 2>/dev/null) ]; then echo rpm-ostree ; fi" PmTest = "if [ x != x$(which dpkg 2>/dev/null) ]; then echo dpkg ; elif [ x != x$(which rpm 2>/dev/null) ]; then echo rpm ; fi" ) @@ -46,6 +46,10 @@ func getPackageRepoManager(r runner.Runner) (*managerCommand, error) { installCommand: "yum install -y", removeCommand: "yum remove -y", }, + "rpm-ostree": { + installCommand: "rpm-ostree rebase --experimental --bypass-driver", + removeCommand: "rpm-ostree rollback", + }, } output, err := r.RunCommand(fmt.Sprintf("sudo -E /bin/sh -c \"%s\"", PrmTest)) @@ -60,6 +64,9 @@ func getPackageRepoManager(r runner.Runner) (*managerCommand, error) { if strings.Contains(output, "yum") { return packageRepoManagerCommand["yum"], nil } + if strings.Contains(output, "rpm-ostree") { + return packageRepoManagerCommand["rpm-ostree"], nil + } return nil, fmt.Errorf("invalid package repo manager %s", output) } @@ -97,6 +104,69 @@ type dependency interface { Remove(r runner.Runner) error } +type dependencyRpmOstree struct { + software []*api.PackageConfig +} + +func (dr *dependencyRpmOstree) Install(r runner.Runner) error { + if len(dr.software) == 0 { + return nil + } + + prManager, err := getPackageRepoManager(r) + if err != nil { + return err + } + + join := "" + for _, s := range dr.software { + join += s.Name + " " + } + + if _, err := r.RunCommand(fmt.Sprintf("sudo -E /bin/sh -c \"%s %s && systemctl reboot\"", prManager.installCommand, join)); err != nil { + return fmt.Errorf("%s failed: %v", prManager.installCommand, err) + } + for { + if err := r.Reconnect(); err == nil { + break + } + } + + return nil +} + +func (dr *dependencyRpmOstree) Remove(r runner.Runner) error { + if len(dr.software) == 0 { + return nil + } + + prManager, err := getPackageRepoManager(r) + if err != nil { + return err + } + + join := "" + for _, s := range dr.software { + join += s.Name + " " + } + + if _, err := r.RunCommand(fmt.Sprintf("sudo -E /bin/sh -c \"%s && systemctl reboot\"", prManager.removeCommand)); err != nil { + return fmt.Errorf("%s failed: %v", prManager.removeCommand, err) + } + + for { + if err := r.Reconnect(); err == nil { + break + } + } + + if _, err := r.RunCommand(fmt.Sprintf("sudo -E /bin/sh -c \"%s \"", "rpm-ostree cleanup -r -m")); err != nil { + return fmt.Errorf("%s failed: %v", "rpm-ostree cleanup", err) + } + + return nil +} + type dependencyRepo struct { software []*api.PackageConfig } @@ -111,6 +181,10 @@ func (dr *dependencyRepo) Install(r runner.Runner) error { return err } + if strings.Contains(prManager.installCommand, "rpm-ostree") { + return nil + } + join := "" for _, s := range dr.software { join += s.Name + " " @@ -132,6 +206,10 @@ func (dr *dependencyRepo) Remove(r runner.Runner) error { return err } + if strings.Contains(prManager.removeCommand, "rpm-ostree") { + return nil + } + join := "" for _, s := range dr.software { join += s.Name + " " diff --git a/pkg/utils/dependency/install.go b/pkg/utils/dependency/install.go index 62437b7..b301677 100644 --- a/pkg/utils/dependency/install.go +++ b/pkg/utils/dependency/install.go @@ -33,11 +33,12 @@ import ( func newBaseDependency(roleInfra *api.RoleInfra, packagePath string) map[string]dependency { packages := map[string][]*api.PackageConfig{ - "repo": {}, - "pkg": {}, - "bin": {}, - "file": {}, - "dir": {}, + "repo": {}, + "pkg": {}, + "bin": {}, + "file": {}, + "dir": {}, + "rpm-ostree": {}, } for _, p := range roleInfra.Softwares { @@ -48,6 +49,9 @@ func newBaseDependency(roleInfra *api.RoleInfra, packagePath string) map[string] } baseDependency := map[string]dependency{ + "rpm-ostree": &dependencyRpmOstree{ + software: packages["rpm-ostree"], + }, "repo": &dependencyRepo{ software: packages["repo"], }, diff --git a/pkg/utils/runner/runner.go b/pkg/utils/runner/runner.go index 7b54730..c2cdc44 100644 --- a/pkg/utils/runner/runner.go +++ b/pkg/utils/runner/runner.go @@ -153,7 +153,7 @@ func (ssh *SSHRunner) Close() { func (ssh *SSHRunner) Reconnect() error { conn, err := connect(ssh.Host) if err != nil { - return nil + return err } ssh.Conn = conn return nil -- Gitee