mirror of
https://github.com/woodchen-ink/certimate.git
synced 2025-07-18 17:31:55 +08:00
feat: pre command for ssh deploy
This commit is contained in:
parent
38d975a3bb
commit
9acdd15c1e
@ -24,6 +24,7 @@ type sshAccess struct {
|
||||
Password string `json:"password"`
|
||||
Key string `json:"key"`
|
||||
Port string `json:"port"`
|
||||
PreCommand string `json:"preCommand"`
|
||||
Command string `json:"command"`
|
||||
CertPath string `json:"certPath"`
|
||||
KeyPath string `json:"keyPath"`
|
||||
@ -56,6 +57,7 @@ func (s *ssh) Deploy(ctx context.Context) error {
|
||||
access.CertPath = strings.ReplaceAll(access.CertPath, key, v)
|
||||
access.KeyPath = strings.ReplaceAll(access.KeyPath, key, v)
|
||||
access.Command = strings.ReplaceAll(access.Command, key, v)
|
||||
access.PreCommand = strings.ReplaceAll(access.PreCommand, key, v)
|
||||
}
|
||||
|
||||
// 连接
|
||||
@ -67,14 +69,24 @@ func (s *ssh) Deploy(ctx context.Context) error {
|
||||
|
||||
s.infos = append(s.infos, toStr("ssh连接成功", nil))
|
||||
|
||||
// 上传
|
||||
// 执行前置命令
|
||||
if access.PreCommand != "" {
|
||||
session, err := client.NewSession()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create session: %w", err)
|
||||
}
|
||||
defer session.Close()
|
||||
var stdoutBuf bytes.Buffer
|
||||
session.Stdout = &stdoutBuf
|
||||
var stderrBuf bytes.Buffer
|
||||
session.Stderr = &stderrBuf
|
||||
|
||||
s.infos = append(s.infos, toStr("ssh创建session成功", nil))
|
||||
s.infos = append(s.infos, toStr("ssh为前置命令创建session成功", nil))
|
||||
|
||||
if err := session.Run(access.PreCommand); err != nil {
|
||||
return fmt.Errorf("failed to run pre-command: %w, stdout: %s, stderr: %s", err, stdoutBuf.String(), stderrBuf.String())
|
||||
}
|
||||
}
|
||||
|
||||
// 上传证书
|
||||
if err := s.upload(client, s.option.Certificate.Certificate, access.CertPath); err != nil {
|
||||
@ -91,11 +103,16 @@ func (s *ssh) Deploy(ctx context.Context) error {
|
||||
s.infos = append(s.infos, toStr("ssh上传私钥成功", nil))
|
||||
|
||||
// 执行命令
|
||||
session, err := client.NewSession()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create session: %w", err)
|
||||
}
|
||||
defer session.Close()
|
||||
var stdoutBuf bytes.Buffer
|
||||
session.Stdout = &stdoutBuf
|
||||
var stderrBuf bytes.Buffer
|
||||
session.Stderr = &stderrBuf
|
||||
|
||||
s.infos = append(s.infos, toStr("ssh创建session成功", nil))
|
||||
if err := session.Run(access.Command); err != nil {
|
||||
return fmt.Errorf("failed to run command: %w, stdout: %s, stderr: %s", err, stdoutBuf.String(), stderrBuf.String())
|
||||
}
|
||||
|
@ -4,7 +4,7 @@
|
||||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"dev": "vite --host",
|
||||
"build": "tsc -b && vite build",
|
||||
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
|
||||
"preview": "vite preview"
|
||||
|
@ -79,6 +79,7 @@ const AccessSSHForm = ({
|
||||
key: z.string().min(0).max(20480),
|
||||
keyFile: z.any().optional(),
|
||||
command: z.string().min(1).max(2048),
|
||||
preCommand: z.string().min(0).max(2048).optional(),
|
||||
certPath: z.string().min(0).max(2048),
|
||||
keyPath: z.string().min(0).max(2048),
|
||||
});
|
||||
@ -90,6 +91,7 @@ const AccessSSHForm = ({
|
||||
password: "",
|
||||
key: "",
|
||||
keyFile: "",
|
||||
preCommand: "",
|
||||
command: "sudo service nginx restart",
|
||||
certPath: "/etc/nginx/ssl/certificate.crt",
|
||||
keyPath: "/etc/nginx/ssl/private.key",
|
||||
@ -112,6 +114,7 @@ const AccessSSHForm = ({
|
||||
certPath: config.certPath,
|
||||
keyPath: config.keyPath,
|
||||
command: config.command,
|
||||
preCommand: config.preCommand,
|
||||
},
|
||||
});
|
||||
|
||||
@ -133,6 +136,7 @@ const AccessSSHForm = ({
|
||||
password: data.password,
|
||||
key: data.key,
|
||||
command: data.command,
|
||||
preCommand: data.preCommand,
|
||||
certPath: data.certPath,
|
||||
keyPath: data.keyPath,
|
||||
},
|
||||
@ -468,6 +472,21 @@ const AccessSSHForm = ({
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="preCommand"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>前置 Command</FormLabel>
|
||||
<FormControl>
|
||||
<Textarea placeholder="请输入要在部署证书前执行的前置命令" {...field} />
|
||||
</FormControl>
|
||||
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="command"
|
||||
|
@ -86,6 +86,7 @@ export type GodaddyConfig = {
|
||||
export type SSHConfig = {
|
||||
host: string;
|
||||
port: string;
|
||||
preCommand?: string;
|
||||
command: string;
|
||||
username: string;
|
||||
password?: string;
|
||||
|
Loading…
x
Reference in New Issue
Block a user