talos

Форк
0
/
controller.go 
144 строки · 2.5 Кб
1
// This Source Code Form is subject to the terms of the Mozilla Public
2
// License, v. 2.0. If a copy of the MPL was not distributed with this
3
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4

5
package qemu
6

7
import (
8
	"sync"
9

10
	"github.com/siderolabs/talos/pkg/provision/providers/vm"
11
)
12

13
// PowerState is current VM power state.
14
type PowerState string
15

16
// Virtual machine power state.
17
const (
18
	PoweredOn  PowerState = "on"
19
	PoweredOff PowerState = "off"
20
)
21

22
// VMCommand is a translated VM command.
23
type VMCommand string
24

25
// Virtual machine commands.
26
const (
27
	VMCommandStart VMCommand = "start"
28
	VMCommandStop  VMCommand = "stop"
29
)
30

31
// Controller supports IPMI-like machine control.
32
type Controller struct {
33
	mu    sync.Mutex
34
	state PowerState
35

36
	forcePXEBoot bool
37

38
	commandsCh chan VMCommand
39
}
40

41
// NewController initializes controller in "powered on" state.
42
func NewController() *Controller {
43
	return &Controller{
44
		state:      PoweredOn,
45
		commandsCh: make(chan VMCommand),
46
	}
47
}
48

49
// PowerOn implements vm.Controller interface.
50
func (c *Controller) PowerOn() error {
51
	c.mu.Lock()
52

53
	if c.state == PoweredOn {
54
		c.mu.Unlock()
55

56
		return nil
57
	}
58

59
	c.state = PoweredOn
60
	c.mu.Unlock()
61

62
	c.commandsCh <- VMCommandStart
63

64
	return nil
65
}
66

67
// PowerOff implements vm.Controller interface.
68
func (c *Controller) PowerOff() error {
69
	c.mu.Lock()
70

71
	if c.state == PoweredOff {
72
		c.mu.Unlock()
73

74
		return nil
75
	}
76

77
	c.state = PoweredOff
78
	c.mu.Unlock()
79

80
	c.commandsCh <- VMCommandStop
81

82
	return nil
83
}
84

85
// Reboot implements vm.Controller interface.
86
func (c *Controller) Reboot() error {
87
	c.mu.Lock()
88

89
	if c.state == PoweredOff {
90
		c.mu.Unlock()
91

92
		return nil
93
	}
94

95
	c.mu.Unlock()
96

97
	c.commandsCh <- VMCommandStop
98

99
	return nil
100
}
101

102
// PXEBootOnce implements vm.Controller interface.
103
func (c *Controller) PXEBootOnce() error {
104
	c.mu.Lock()
105
	defer c.mu.Unlock()
106

107
	c.forcePXEBoot = true
108

109
	return nil
110
}
111

112
// Status implements vm.Controller interface.
113
func (c *Controller) Status() vm.Status {
114
	return vm.Status{
115
		PoweredOn: c.PowerState() == PoweredOn,
116
	}
117
}
118

119
// PowerState returns current power state.
120
func (c *Controller) PowerState() PowerState {
121
	c.mu.Lock()
122
	defer c.mu.Unlock()
123

124
	return c.state
125
}
126

127
// ForcePXEBoot returns whether next boot should be PXE boot.
128
func (c *Controller) ForcePXEBoot() bool {
129
	c.mu.Lock()
130
	defer c.mu.Unlock()
131

132
	if c.forcePXEBoot {
133
		c.forcePXEBoot = false
134

135
		return true
136
	}
137

138
	return false
139
}
140

141
// CommandsCh returns channel with commands.
142
func (c *Controller) CommandsCh() <-chan VMCommand {
143
	return c.commandsCh
144
}
145

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.