~nskaggs/+junk/xenial-test

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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2014 Canonical Ltd.
// Copyright 2014 Cloudbase Solutions
// Licensed under the AGPLv3, see LICENCE file for details.

package common

import (
	"github.com/juju/errors"
	"gopkg.in/juju/names.v2"

	"github.com/juju/juju/apiserver/params"
	"github.com/juju/juju/state"
)

// RebootRequester implements the RequestReboot API method
type RebootRequester struct {
	st   state.EntityFinder
	auth GetAuthFunc
}

func NewRebootRequester(st state.EntityFinder, auth GetAuthFunc) *RebootRequester {
	return &RebootRequester{
		st:   st,
		auth: auth,
	}
}

func (r *RebootRequester) oneRequest(tag names.Tag) error {
	entity0, err := r.st.FindEntity(tag)
	if err != nil {
		return err
	}
	entity, ok := entity0.(state.RebootFlagSetter)
	if !ok {
		return NotSupportedError(tag, "request reboot")
	}
	return entity.SetRebootFlag(true)
}

// RequestReboot sets the reboot flag on the provided machines
func (r *RebootRequester) RequestReboot(args params.Entities) (params.ErrorResults, error) {
	result := params.ErrorResults{
		Results: make([]params.ErrorResult, len(args.Entities)),
	}
	if len(args.Entities) == 0 {
		return result, nil
	}
	auth, err := r.auth()
	if err != nil {
		return params.ErrorResults{}, errors.Trace(err)
	}
	for i, entity := range args.Entities {
		tag, err := names.ParseTag(entity.Tag)
		if err != nil {
			result.Results[i].Error = ServerError(ErrPerm)
			continue
		}
		err = ErrPerm
		if auth(tag) {
			err = r.oneRequest(tag)
		}
		result.Results[i].Error = ServerError(err)
	}
	return result, nil
}

// RebootActionGetter implements the GetRebootAction API method
type RebootActionGetter struct {
	st   state.EntityFinder
	auth GetAuthFunc
}

func NewRebootActionGetter(st state.EntityFinder, auth GetAuthFunc) *RebootActionGetter {
	return &RebootActionGetter{
		st:   st,
		auth: auth,
	}
}

func (r *RebootActionGetter) getOneAction(tag names.Tag) (params.RebootAction, error) {
	entity0, err := r.st.FindEntity(tag)
	if err != nil {
		return "", err
	}
	entity, ok := entity0.(state.RebootActionGetter)
	if !ok {
		return "", NotSupportedError(tag, "request reboot")
	}
	rAction, err := entity.ShouldRebootOrShutdown()
	if err != nil {
		return params.ShouldDoNothing, err
	}
	return params.RebootAction(rAction), nil
}

// GetRebootAction returns the action a machine agent should take.
// If a reboot flag is set on the machine, then that machine is
// expected to reboot (params.ShouldReboot).
// a reboot flag set on the machine parent or grandparent, will
// cause the machine to shutdown (params.ShouldShutdown).
// If no reboot flag is set, the machine should do nothing (params.ShouldDoNothing).
func (r *RebootActionGetter) GetRebootAction(args params.Entities) (params.RebootActionResults, error) {
	result := params.RebootActionResults{
		Results: make([]params.RebootActionResult, len(args.Entities)),
	}
	if len(args.Entities) == 0 {
		return result, nil
	}
	auth, err := r.auth()
	if err != nil {
		return params.RebootActionResults{}, errors.Trace(err)
	}
	for i, entity := range args.Entities {
		tag, err := names.ParseTag(entity.Tag)
		if err != nil {
			result.Results[i].Error = ServerError(ErrPerm)
			continue
		}
		err = ErrPerm
		if auth(tag) {
			result.Results[i].Result, err = r.getOneAction(tag)
		}
		result.Results[i].Error = ServerError(err)
	}
	return result, nil
}

// RebootFlagClearer implements the ClearReboot API call
type RebootFlagClearer struct {
	st   state.EntityFinder
	auth GetAuthFunc
}

func NewRebootFlagClearer(st state.EntityFinder, auth GetAuthFunc) *RebootFlagClearer {
	return &RebootFlagClearer{
		st:   st,
		auth: auth,
	}
}

func (r *RebootFlagClearer) clearOneFlag(tag names.Tag) error {
	entity0, err := r.st.FindEntity(tag)
	if err != nil {
		return err
	}
	entity, ok := entity0.(state.RebootFlagSetter)
	if !ok {
		return NotSupportedError(tag, "clear reboot flag")
	}
	return entity.SetRebootFlag(false)
}

// ClearReboot will clear the reboot flag on provided machines, if it exists.
func (r *RebootFlagClearer) ClearReboot(args params.Entities) (params.ErrorResults, error) {
	result := params.ErrorResults{
		Results: make([]params.ErrorResult, len(args.Entities)),
	}
	if len(args.Entities) == 0 {
		return result, nil
	}
	auth, err := r.auth()
	if err != nil {
		return params.ErrorResults{}, errors.Trace(err)
	}
	for i, entity := range args.Entities {
		tag, err := names.ParseTag(entity.Tag)
		if err != nil {
			result.Results[i].Error = ServerError(ErrPerm)
			continue
		}
		err = ErrPerm
		if auth(tag) {
			err = r.clearOneFlag(tag)
		}
		result.Results[i].Error = ServerError(err)
	}
	return result, nil
}