mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-11-05 23:41:47 +08:00
86 lines
2.3 KiB
Go
86 lines
2.3 KiB
Go
// Copyright 2019 Yunion
|
|
//
|
|
// 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.
|
|
|
|
package fsdriver
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"yunion.io/x/pkg/errors"
|
|
|
|
"yunion.io/x/onecloud/pkg/util/procutils"
|
|
)
|
|
|
|
const (
|
|
unitDirPath = "/usr/lib/systemd/system"
|
|
enableDirPath = "/etc/systemd/system/multi-user.target.wants"
|
|
)
|
|
|
|
func (d *sLinuxRootFs) isSupportSystemd() bool {
|
|
return d.rootFs.Exists(unitDirPath, false) && d.rootFs.Exists(enableDirPath, false)
|
|
}
|
|
|
|
func (d *sLinuxRootFs) installInitScript(name, cmd string) error {
|
|
if d.isSupportSystemd() {
|
|
return d.installSystemd(name, cmd)
|
|
} else {
|
|
return d.installCrond(cmd)
|
|
}
|
|
}
|
|
|
|
func (d *sLinuxRootFs) installCrond(cmd string) error {
|
|
cronJob := fmt.Sprintf("@reboot %s", cmd)
|
|
if procutils.NewCommand("chroot", d.rootFs.GetMountPath(), "crontab", "-l", "|", "grep", cronJob).Run() == nil {
|
|
// if cronjob exist, return success
|
|
return nil
|
|
}
|
|
output, err := procutils.NewCommand("chroot", d.rootFs.GetMountPath(), "sh", "-c",
|
|
fmt.Sprintf("(crontab -l 2>/dev/null; echo '%s') |crontab -", cronJob),
|
|
).Output()
|
|
if err != nil {
|
|
return errors.Wrapf(err, "add crontab %s", output)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (d *sLinuxRootFs) installSystemd(name, cmd string) error {
|
|
var serviceName = fmt.Sprintf("%s.service", name)
|
|
var unitPath = fmt.Sprintf("%s/%s", unitDirPath, serviceName)
|
|
var enablePath = fmt.Sprintf("%s/%s", enableDirPath, serviceName)
|
|
var unitContent = fmt.Sprintf(`[Unit]
|
|
Description=Run once
|
|
After=local-fs.target
|
|
After=network.target
|
|
|
|
[Service]
|
|
ExecStart=%s
|
|
RemainAfterExit=true
|
|
Type=oneshot
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
`, cmd)
|
|
err := d.rootFs.FilePutContents(unitPath, unitContent, false, false)
|
|
if err != nil {
|
|
return errors.Wrap(err, "save user_data unit fail")
|
|
}
|
|
|
|
err = d.rootFs.Symlink(unitPath, enablePath, false)
|
|
if err != nil {
|
|
return errors.Wrap(err, "create user_data symlink fail")
|
|
}
|
|
|
|
return nil
|
|
}
|