| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2016, Ryan Scott Brown <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: serverless |
| 11 |
short_description: Manages a Serverless Framework project |
| 12 |
description: |
| 13 |
- Provides support for managing Serverless Framework (U(https://serverless.com/)) project deployments and stacks. |
| 14 |
extends_documentation_fragment: |
| 15 |
- community.general._attributes |
| 16 |
attributes: |
| 17 |
check_mode: |
| 18 |
support: none |
| 19 |
diff_mode: |
| 20 |
support: none |
| 21 |
options: |
| 22 |
state: |
| 23 |
description: |
| 24 |
- Goal state of given stage/project. |
| 25 |
type: str |
| 26 |
choices: [absent, present] |
| 27 |
default: present |
| 28 |
serverless_bin_path: |
| 29 |
description: |
| 30 |
- The path of a serverless framework binary relative to the O(service_path), for example V(node_module/.bin/serverless). |
| 31 |
type: path |
| 32 |
service_path: |
| 33 |
description: |
| 34 |
- The path to the root of the Serverless Service to be operated on. |
| 35 |
type: path |
| 36 |
required: true |
| 37 |
stage: |
| 38 |
description: |
| 39 |
- The name of the serverless framework project stage to deploy to. |
| 40 |
- This uses the serverless framework default "dev". |
| 41 |
type: str |
| 42 |
default: '' |
| 43 |
region: |
| 44 |
description: |
| 45 |
- AWS region to deploy the service to. |
| 46 |
- This parameter defaults to V(us-east-1). |
| 47 |
type: str |
| 48 |
default: '' |
| 49 |
deploy: |
| 50 |
description: |
| 51 |
- Whether or not to deploy artifacts after building them. |
| 52 |
- When this option is V(false) all the functions are built, but no stack update is run to send them out. |
| 53 |
- This is mostly useful for generating artifacts to be stored/deployed elsewhere. |
| 54 |
type: bool |
| 55 |
default: true |
| 56 |
force: |
| 57 |
description: |
| 58 |
- Whether or not to force full deployment, equivalent to serverless C(--force) option. |
| 59 |
type: bool |
| 60 |
default: false |
| 61 |
verbose: |
| 62 |
description: |
| 63 |
- Shows all stack events during deployment, and display any Stack Output. |
| 64 |
type: bool |
| 65 |
default: false |
| 66 |
notes: |
| 67 |
- Currently, the C(serverless) command must be in the path of the node executing the task. In the future this may be a flag. |
| 68 |
requirements: |
| 69 |
- serverless |
| 70 |
- PyYAML |
| 71 |
author: |
| 72 |
- Ryan Scott Brown (@ryansb) |
| 73 |
""" |
| 74 |
|
| 75 |
EXAMPLES = r""" |
| 76 |
- name: Basic deploy of a service |
| 77 |
community.general.serverless: |
| 78 |
service_path: '{{ project_dir }}' |
| 79 |
state: present |
| 80 |
|
| 81 |
- name: Deploy a project, then pull its resource list back into Ansible |
| 82 |
community.general.serverless: |
| 83 |
stage: dev |
| 84 |
region: us-east-1 |
| 85 |
service_path: '{{ project_dir }}' |
| 86 |
register: sls |
| 87 |
|
| 88 |
# The cloudformation stack is always named the same as the full service, so the |
| 89 |
# cloudformation_info module can get a full list of the stack resources, as |
| 90 |
# well as stack events and outputs |
| 91 |
- cloudformation_info: |
| 92 |
region: us-east-1 |
| 93 |
stack_name: '{{ sls.service_name }}' |
| 94 |
stack_resources: true |
| 95 |
|
| 96 |
- name: Deploy a project using a locally installed serverless binary |
| 97 |
community.general.serverless: |
| 98 |
stage: dev |
| 99 |
region: us-east-1 |
| 100 |
service_path: '{{ project_dir }}' |
| 101 |
serverless_bin_path: node_modules/.bin/serverless |
| 102 |
""" |
| 103 |
|
| 104 |
RETURN = r""" |
| 105 |
service_name: |
| 106 |
type: str |
| 107 |
description: The service name specified in the serverless.yml that was just deployed. |
| 108 |
returned: always |
| 109 |
sample: my-fancy-service-dev |
| 110 |
state: |
| 111 |
type: str |
| 112 |
description: Whether the stack for the serverless project is present/absent. |
| 113 |
returned: always |
| 114 |
command: |
| 115 |
type: str |
| 116 |
description: Full C(serverless) command run by this module, in case you want to re-run the command outside the module. |
| 117 |
returned: always |
| 118 |
sample: serverless deploy --stage production |
| 119 |
""" |
| 120 |
|
| 121 |
import os |
| 122 |
|
| 123 |
try: |
| 124 |
import yaml |
| 125 |
|
| 126 |
HAS_YAML = True |
| 127 |
except ImportError: |
| 128 |
HAS_YAML = False |
| 129 |
|
| 130 |
from ansible.module_utils.basic import AnsibleModule |
| 131 |
|
| 132 |
|
| 133 |
def read_serverless_config(module): |
| 134 |
path = module.params.get("service_path") |
| 135 |
full_path = os.path.join(path, "serverless.yml") |
| 136 |
|
| 137 |
try: |
| 138 |
with open(full_path) as sls_config: |
| 139 |
config = yaml.safe_load(sls_config.read()) |
| 140 |
return config |
| 141 |
except OSError as e: |
| 142 |
module.fail_json(msg=f"Could not open serverless.yml in {full_path}. err: {e}") |
| 143 |
|
| 144 |
|
| 145 |
def get_service_name(module, stage): |
| 146 |
config = read_serverless_config(module) |
| 147 |
if config.get("service") is None: |
| 148 |
module.fail_json(msg="Could not read `service` key from serverless.yml file") |
| 149 |
|
| 150 |
if stage: |
| 151 |
return f"{config['service']}-{stage}" |
| 152 |
|
| 153 |
return f"{config['service']}-{config.get('stage', 'dev')}" |
| 154 |
|
| 155 |
|
| 156 |
def main(): |
| 157 |
module = AnsibleModule( |
| 158 |
argument_spec=dict( |
| 159 |
service_path=dict(type="path", required=True), |
| 160 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 161 |
region=dict(type="str", default=""), |
| 162 |
stage=dict(type="str", default=""), |
| 163 |
deploy=dict(type="bool", default=True), |
| 164 |
serverless_bin_path=dict(type="path"), |
| 165 |
force=dict(type="bool", default=False), |
| 166 |
verbose=dict(type="bool", default=False), |
| 167 |
), |
| 168 |
) |
| 169 |
|
| 170 |
if not HAS_YAML: |
| 171 |
module.fail_json(msg="yaml is required for this module") |
| 172 |
|
| 173 |
service_path = module.params.get("service_path") |
| 174 |
state = module.params.get("state") |
| 175 |
region = module.params.get("region") |
| 176 |
stage = module.params.get("stage") |
| 177 |
deploy = module.params.get("deploy", True) |
| 178 |
force = module.params.get("force", False) |
| 179 |
verbose = module.params.get("verbose", False) |
| 180 |
serverless_bin_path = module.params.get("serverless_bin_path") |
| 181 |
|
| 182 |
if serverless_bin_path is not None: |
| 183 |
command = f"{serverless_bin_path} " |
| 184 |
else: |
| 185 |
command = f"{module.get_bin_path('serverless')} " |
| 186 |
|
| 187 |
if state == "present": |
| 188 |
command += "deploy " |
| 189 |
elif state == "absent": |
| 190 |
command += "remove " |
| 191 |
else: |
| 192 |
module.fail_json(msg=f"State must either be 'present' or 'absent'. Received: {state}") |
| 193 |
|
| 194 |
if state == "present": |
| 195 |
if not deploy: |
| 196 |
command += "--noDeploy " |
| 197 |
elif force: |
| 198 |
command += "--force " |
| 199 |
|
| 200 |
if region: |
| 201 |
command += f"--region {region} " |
| 202 |
if stage: |
| 203 |
command += f"--stage {stage} " |
| 204 |
if verbose: |
| 205 |
command += "--verbose " |
| 206 |
|
| 207 |
rc, out, err = module.run_command(command, cwd=service_path) |
| 208 |
if rc != 0: |
| 209 |
if state == "absent" and f"-{stage}' does not exist" in out: |
| 210 |
module.exit_json( |
| 211 |
changed=False, state="absent", command=command, out=out, service_name=get_service_name(module, stage) |
| 212 |
) |
| 213 |
|
| 214 |
module.fail_json(msg=f"Failure when executing Serverless command. Exited {rc}.\nstdout: {out}\nstderr: {err}") |
| 215 |
|
| 216 |
# gather some facts about the deployment |
| 217 |
module.exit_json( |
| 218 |
changed=True, state="present", out=out, command=command, service_name=get_service_name(module, stage) |
| 219 |
) |
| 220 |
|
| 221 |
|
| 222 |
if __name__ == "__main__": |
| 223 |
main() |