forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapper.go
161 lines (133 loc) · 3.54 KB
/
mapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package schema
import (
gsunit "github.com/coreos/fleet/Godeps/_workspace/src/github.com/coreos/go-systemd/unit"
"github.com/coreos/fleet/job"
"github.com/coreos/fleet/machine"
"github.com/coreos/fleet/unit"
)
func MapUnitFileToSchemaUnitOptions(uf *unit.UnitFile) []*UnitOption {
sopts := make([]*UnitOption, len(uf.Options))
for i, opt := range uf.Options {
sopts[i] = &UnitOption{
Section: opt.Section,
Name: opt.Name,
Value: opt.Value,
}
}
return sopts
}
func MapSchemaUnitOptionsToUnitFile(sopts []*UnitOption) *unit.UnitFile {
opts := make([]*gsunit.UnitOption, len(sopts))
for i, sopt := range sopts {
opts[i] = &gsunit.UnitOption{
Section: sopt.Section,
Name: sopt.Name,
Value: sopt.Value,
}
}
return unit.NewUnitFromOptions(opts)
}
func MapSchemaUnitToUnit(entity *Unit) *job.Unit {
uf := MapSchemaUnitOptionsToUnitFile(entity.Options)
j := job.Unit{
Name: entity.Name,
Unit: *uf,
}
return &j
}
func MapUnitToSchemaUnit(u *job.Unit, su *job.ScheduledUnit) *Unit {
s := Unit{
Name: u.Name,
Options: MapUnitFileToSchemaUnitOptions(&(u.Unit)),
DesiredState: string(u.TargetState),
}
if su != nil {
s.MachineID = su.TargetMachineID
if su.State != nil {
s.CurrentState = string(*su.State)
}
}
return &s
}
func MapSchemaUnitsToUnits(entities []*Unit) []job.Unit {
units := make([]job.Unit, len(entities))
for i, _ := range entities {
entity := entities[i]
units[i] = *(MapSchemaUnitToUnit(entity))
}
return units
}
func MapMachineStateToSchema(ms *machine.MachineState) *Machine {
sm := Machine{
Id: ms.ID,
PrimaryIP: ms.PublicIP,
}
sm.Metadata = make(map[string]string, len(ms.Metadata))
for k, v := range ms.Metadata {
sm.Metadata[k] = v
}
return &sm
}
func MapSchemaToMachineStates(entities []*Machine) []machine.MachineState {
machines := make([]machine.MachineState, len(entities))
for i, _ := range entities {
me := entities[i]
ms := machine.MachineState{
ID: me.Id,
PublicIP: me.PrimaryIP,
}
ms.Metadata = make(map[string]string, len(me.Metadata))
for k, v := range me.Metadata {
ms.Metadata[k] = v
}
machines[i] = ms
}
return machines
}
func MapUnitStatesToSchemaUnitStates(entities []*unit.UnitState) []*UnitState {
sus := make([]*UnitState, len(entities))
for i, e := range entities {
sus[i] = MapUnitStateToSchemaUnitState(e)
}
return sus
}
func MapUnitStateToSchemaUnitState(entity *unit.UnitState) *UnitState {
us := UnitState{
Name: entity.UnitName,
Hash: entity.UnitHash,
MachineID: entity.MachineID,
SystemdLoadState: entity.LoadState,
SystemdActiveState: entity.ActiveState,
SystemdSubState: entity.SubState,
}
return &us
}
func MapSchemaUnitStatesToUnitStates(entities []*UnitState) []*unit.UnitState {
us := make([]*unit.UnitState, len(entities))
for i, e := range entities {
us[i] = &unit.UnitState{
UnitName: e.Name,
UnitHash: e.Hash,
MachineID: e.MachineID,
LoadState: e.SystemdLoadState,
ActiveState: e.SystemdActiveState,
SubState: e.SystemdSubState,
}
}
return us
}
func MapSchemaUnitToScheduledUnit(entity *Unit) *job.ScheduledUnit {
cs := job.JobState(entity.CurrentState)
return &job.ScheduledUnit{
Name: entity.Name,
State: &cs,
TargetMachineID: entity.MachineID,
}
}
func MapSchemaUnitsToScheduledUnits(entities []*Unit) []job.ScheduledUnit {
su := make([]job.ScheduledUnit, len(entities))
for i, e := range entities {
su[i] = *MapSchemaUnitToScheduledUnit(e)
}
return su
}