Debian Codegraph @

ansible_collections/community/hrobot/plugins/modules/reset.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2019 Felix Fontein <[email protected]>
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8 from __future__ import absolute_import, division, print_function
9 __metaclass__ = type
10
11
12 DOCUMENTATION = r"""
13 module: reset
14 short_description: Reset a dedicated server
15 version_added: 1.2.0
16 author:
17 - Felix Fontein (@felixfontein)
18 description:
19 - Reset a dedicated server with a software or hardware reset, or by requesting a manual reset.
20 seealso:
21 - module: community.hrobot.reset_info
22 description: Retrieve information on resetter.
23 extends_documentation_fragment:
24 - community.hrobot.robot
25 - community.hrobot.attributes
26 - community.hrobot.attributes.actiongroup_robot
27
28 attributes:
29 action_group:
30 version_added: 1.6.0
31 check_mode:
32 support: full
33 diff_mode:
34 support: none
35 idempotent:
36 support: none
37 details:
38 - This module performs an action on every invocation.
39
40 options:
41 server_number:
42 description:
43 - The server number of the server to reset.
44 type: int
45 required: true
46 reset_type:
47 description:
48 - How to reset the server.
49 - V(software) is a software reset. This should be similar to pressing Ctrl+Alt+Del on the keyboard.
50 - V(power) is a hardware reset similar to pressing the Power button. An ACPI signal is sent, and if the server is configured
51 correctly, this will trigger a regular shutdown.
52 - V(hardware) is a hardware reset similar to pressing the Restart button. The power is cycled for the server.
53 - V(manual) is a manual reset. This requests a technician to manually do the shutdown while looking at the screen output.
54 B(Be careful) and only use this when really necessary!
55 - "Note that not every server supports every reset method! You can query the supported reset methods by using the
56 RV(community.hrobot.reset_info#module:reset.type) return value of the M(community.hrobot.reset_info) module."
57 type: str
58 required: true
59 choices:
60 - software
61 - hardware
62 - power
63 - manual
64 """
65
66 EXAMPLES = r"""
67 ---
68 - name: Send ACPI signal to server to request controlled shutdown
69 community.hrobot.reset:
70 hetzner_user: foo
71 hetzner_password: bar
72 server_number: 1234
73 state: power
74
75 - name: Make sure that the server supports manual reset
76 community.hrobot.reset:
77 hetzner_user: foo
78 hetzner_password: bar
79 server_number: 1234
80 reset_type: manual
81 check_mode: true
82
83 - name: Request a manual reset (by a technican)
84 community.hrobot.reset:
85 hetzner_user: foo
86 hetzner_password: bar
87 server_number: 1234
88 reset_type: manual
89 """
90
91 RETURN = r"""#"""
92
93 from ansible.module_utils.basic import AnsibleModule
94
95 from ansible_collections.community.hrobot.plugins.module_utils.robot import (
96 BASE_URL,
97 ROBOT_DEFAULT_ARGUMENT_SPEC,
98 fetch_url_json,
99 )
100
101 try:
102 from urllib.parse import urlencode
103 except ImportError:
104 # Python 2.x fallback:
105 from urllib import urlencode
106
107
108 def main():
109 argument_spec = dict(
110 server_number=dict(type='int', required=True),
111 reset_type=dict(type='str', required=True, choices=['software', 'hardware', 'power', 'manual']),
112 )
113 argument_spec.update(ROBOT_DEFAULT_ARGUMENT_SPEC)
114 module = AnsibleModule(
115 argument_spec=argument_spec,
116 supports_check_mode=True,
117 )
118
119 server_number = module.params['server_number']
120 reset_type = {
121 'software': 'sw',
122 'hardware': 'hw',
123 'power': 'power',
124 'manual': 'man',
125 }[module.params['reset_type']]
126
127 if module.check_mode:
128 url = "{0}/reset/{1}".format(BASE_URL, server_number)
129 result, error = fetch_url_json(module, url, accept_errors=['SERVER_NOT_FOUND', 'RESET_NOT_AVAILABLE'])
130 if not error and reset_type not in result['reset']['type']:
131 module.fail_json(msg='The chosen reset method is not supported for this server')
132 else:
133 headers = {"Content-type": "application/x-www-form-urlencoded"}
134 data = dict(
135 type=reset_type,
136 )
137 url = "{0}/reset/{1}".format(BASE_URL, server_number)
138 result, error = fetch_url_json(
139 module,
140 url,
141 data=urlencode(data),
142 headers=headers,
143 method='POST',
144 accept_errors=['INVALID_INPUT', 'SERVER_NOT_FOUND', 'RESET_NOT_AVAILABLE', 'RESET_MANUAL_ACTIVE', 'RESET_FAILED'],
145 )
146 if error and error == 'INVALID_INPUT':
147 module.fail_json(msg='The chosen reset method is not supported for this server')
148 if error:
149 if error == 'SERVER_NOT_FOUND':
150 module.fail_json(msg='This server does not exist, or you do not have access rights for it')
151 if error == 'RESET_NOT_AVAILABLE':
152 module.fail_json(msg='The server has no reset option available')
153 if error == 'RESET_MANUAL_ACTIVE':
154 module.fail_json(msg='A manual reset is already running')
155 if error == 'RESET_FAILED':
156 module.fail_json(msg='The reset failed due to an internal error at Hetzner')
157 raise AssertionError('Unexpected error {0}'.format(error)) # pragma: no cover
158
159 module.exit_json(changed=True)
160
161
162 if __name__ == '__main__': # pragma: no cover
163 main() # pragma: no cover