summaryrefslogtreecommitdiffstats
path: root/ansible_collections/community/general/plugins/modules/webfaction_app.py
blob: 81bfc8b686d4737ed8887cce1ae25b8894f9ea2c (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (c) 2015, Quentin Stafford-Fraser, with contributions gratefully acknowledged from:
#     * Andy Baker
#     * Federico Tarantini
#
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

# Create a Webfaction application using Ansible and the Webfaction API
#
# Valid application types can be found by looking here:
# https://docs.webfaction.com/xmlrpc-api/apps.html#application-types

from __future__ import absolute_import, division, print_function
__metaclass__ = type


DOCUMENTATION = '''
---

deprecated:
  removed_in: 9.0.0
  why: the endpoints this module relies on do not exist any more and do not resolve to IPs in DNS.
  alternative: no known alternative at this point

module: webfaction_app
short_description: Add or remove applications on a Webfaction host
description:
    - Add or remove applications on a Webfaction host. Further documentation at U(https://github.com/quentinsf/ansible-webfaction).
author: Quentin Stafford-Fraser (@quentinsf)
notes:
    - >
      You can run playbooks that use this on a local machine, or on a Webfaction host, or elsewhere, since the scripts use the remote webfaction API.
      The location is not important. However, running them on multiple hosts I(simultaneously) is best avoided. If you do not specify C(localhost) as
      your host, you may want to add C(serial=1) to the plays.
    - See L(the webfaction API, https://docs.webfaction.com/xmlrpc-api/) for more info.

extends_documentation_fragment:
    - community.general.attributes

attributes:
    check_mode:
        support: full
    diff_mode:
        support: none

options:
    name:
        description:
            - The name of the application
        required: true
        type: str

    state:
        description:
            - Whether the application should exist
        choices: ['present', 'absent']
        default: "present"
        type: str

    type:
        description:
            - The type of application to create. See the Webfaction docs at U(https://docs.webfaction.com/xmlrpc-api/apps.html) for a list.
        required: true
        type: str

    autostart:
        description:
            - Whether the app should restart with an C(autostart.cgi) script
        type: bool
        default: false

    extra_info:
        description:
            - Any extra parameters required by the app
        default: ''
        type: str

    port_open:
        description:
            - IF the port should be opened
        type: bool
        default: false

    login_name:
        description:
            - The webfaction account to use
        required: true
        type: str

    login_password:
        description:
            - The webfaction password to use
        required: true
        type: str

    machine:
        description:
            - The machine name to use (optional for accounts with only one machine)
        type: str

'''

EXAMPLES = '''
  - name: Create a test app
    community.general.webfaction_app:
      name: "my_wsgi_app1"
      state: present
      type: mod_wsgi35-python27
      login_name: "{{webfaction_user}}"
      login_password: "{{webfaction_passwd}}"
      machine: "{{webfaction_machine}}"
'''

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six.moves import xmlrpc_client


webfaction = xmlrpc_client.ServerProxy('https://api.webfaction.com/')


def main():

    module = AnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            state=dict(choices=['present', 'absent'], default='present'),
            type=dict(required=True),
            autostart=dict(type='bool', default=False),
            extra_info=dict(default=""),
            port_open=dict(type='bool', default=False),
            login_name=dict(required=True),
            login_password=dict(required=True, no_log=True),
            machine=dict(),
        ),
        supports_check_mode=True
    )
    app_name = module.params['name']
    app_type = module.params['type']
    app_state = module.params['state']

    if module.params['machine']:
        session_id, account = webfaction.login(
            module.params['login_name'],
            module.params['login_password'],
            module.params['machine']
        )
    else:
        session_id, account = webfaction.login(
            module.params['login_name'],
            module.params['login_password']
        )

    app_list = webfaction.list_apps(session_id)
    app_map = dict([(i['name'], i) for i in app_list])
    existing_app = app_map.get(app_name)

    result = {}

    # Here's where the real stuff happens

    if app_state == 'present':

        # Does an app with this name already exist?
        if existing_app:
            if existing_app['type'] != app_type:
                module.fail_json(msg="App already exists with different type. Please fix by hand.")

            # If it exists with the right type, we don't change it
            # Should check other parameters.
            module.exit_json(
                changed=False,
                result=existing_app,
            )

        if not module.check_mode:
            # If this isn't a dry run, create the app
            result.update(
                webfaction.create_app(
                    session_id, app_name, app_type,
                    module.boolean(module.params['autostart']),
                    module.params['extra_info'],
                    module.boolean(module.params['port_open'])
                )
            )

    elif app_state == 'absent':

        # If the app's already not there, nothing changed.
        if not existing_app:
            module.exit_json(
                changed=False,
            )

        if not module.check_mode:
            # If this isn't a dry run, delete the app
            result.update(
                webfaction.delete_app(session_id, app_name)
            )

    else:
        module.fail_json(msg="Unknown state specified: {0}".format(app_state))

    module.exit_json(
        changed=True,
        result=result
    )


if __name__ == '__main__':
    main()