Ilya Kharin | aa03d4e | 2017-04-18 17:25:53 +0400 | [diff] [blame] | 1 | import logging |
| 2 | |
| 3 | import salt.exceptions |
| 4 | |
| 5 | import requests |
| 6 | from requests.compat import urljoin |
| 7 | |
| 8 | LOG = logging.getLogger(__name__) |
| 9 | |
| 10 | |
| 11 | def get_project(name): |
| 12 | session, make_url = get_session() |
| 13 | resp = session.get(make_url("/api/18/project/{}".format(name))) |
| 14 | status_code = resp.status_code |
| 15 | if status_code == 200: |
| 16 | return resp.json() |
| 17 | elif status_code == 404: |
| 18 | return None |
| 19 | raise salt.exceptions.SaltInvocationError( |
| 20 | "Could not retrieve information about project {} from Rundeck {}: {}" |
| 21 | .format(name, make_url.base_url, status_code)) |
| 22 | |
| 23 | |
| 24 | def create_project(name, params): |
| 25 | session, make_url = get_session() |
| 26 | config = create_project_config(name, params) |
| 27 | LOG.debug("create_project: %s", name) |
| 28 | LOG.warning("create_project.config: %s/%s", name, config) |
| 29 | resp = session.post( |
| 30 | make_url("/api/18/projects"), |
| 31 | json={ |
| 32 | 'name': name, |
| 33 | 'config': config, |
| 34 | }, |
| 35 | allow_redirects=False, |
| 36 | ) |
| 37 | if resp.status_code == 201: |
| 38 | return resp.json() |
| 39 | LOG.debug("create_project: %s", name) |
| 40 | |
| 41 | |
| 42 | def create_project_config(name, params, config=None): |
| 43 | config = dict(config) if config else {} |
| 44 | if params['description']: |
| 45 | config['project.description'] = params['description'] |
| 46 | else: |
| 47 | config.pop('project.description', None) |
| 48 | config.update({ |
| 49 | 'resources.source.1.config.file': |
| 50 | "/var/rundeck/projects/{}/etc/resources.yaml".format(name), |
| 51 | 'resources.source.1.config.format': 'resourceyaml', |
| 52 | 'resources.source.1.config.generateFileAutomatically': 'true', |
| 53 | 'resources.source.1.config.includeServerNode': 'false', |
| 54 | 'resources.source.1.config.requireFileExists': 'false', |
| 55 | 'project.ssh-keypath': '/var/rundeck/.ssh/id_rsa', |
| 56 | 'resources.source.1.type': 'file', |
| 57 | }) |
| 58 | return config |
| 59 | |
| 60 | |
| 61 | def update_project_config(name, project, config): |
| 62 | session, make_url = get_session() |
| 63 | resp = session.put( |
| 64 | make_url("/api/18/project/{}/config".format(name)), |
| 65 | json=config, |
| 66 | allow_redirects=False, |
| 67 | ) |
| 68 | if resp.status_code == 201: |
| 69 | return resp.json() |
| 70 | LOG.debug("update_project: %s", name) |
| 71 | |
| 72 | |
| 73 | def delete_project(name): |
| 74 | session, make_url = get_session() |
| 75 | resp = session.delete(make_url("/api/18/project/{}".format(name))) |
| 76 | status_code = resp.status_code |
| 77 | if status_code != 204: |
| 78 | raise salt.exceptions.SaltInvocationError( |
| 79 | "Could not remove project {} from Rundeck {}: {}" |
| 80 | .format(name, make_url.base_url, status_code)) |
| 81 | |
| 82 | |
| 83 | def get_session(): |
| 84 | def make_url(url): |
| 85 | return urljoin(make_url.base_url, url) |
| 86 | |
| 87 | rundeck_url = __salt__['config.get']('rundeck.url') |
| 88 | make_url.base_url = rundeck_url |
| 89 | |
| 90 | api_token = __salt__['config.get']('rundeck.api_token') |
| 91 | username = __salt__['config.get']('rundeck.username') |
| 92 | password = __salt__['config.get']('rundeck.password') |
| 93 | |
| 94 | session = requests.Session() |
| 95 | |
| 96 | if api_token: |
| 97 | session.headers.update({ |
| 98 | 'Content-Type': 'application/json', |
| 99 | 'X-Rundeck-Auth-Token': api_token, |
| 100 | }) |
| 101 | else: |
| 102 | resp = session.post(make_url('/j_security_check'), |
| 103 | data={ |
| 104 | 'j_username': username, |
| 105 | 'j_password': password, |
| 106 | }, |
| 107 | ) |
| 108 | if (resp.status_code != 200 or |
| 109 | '/user/error' in resp.url or |
| 110 | '/user/login' in resp.url): |
| 111 | raise salt.exceptions.SaltInvocationError( |
| 112 | "Username/passowrd authorization failed in Rundeck {} for " |
| 113 | "user {}".format(rundeck_url, username)) |
| 114 | session.params.update({ |
| 115 | 'format': 'json', |
| 116 | }) |
| 117 | return session, make_url |