Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | import logging |
Oleh Hryhorov | 49c4b49 | 2018-05-03 16:07:39 +0300 | [diff] [blame] | 3 | import time |
Vasyl Saienko | 0291d9c | 2018-05-31 12:44:07 +0300 | [diff] [blame] | 4 | import functools |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 5 | |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 6 | LOG = logging.getLogger(__name__) |
| 7 | |
| 8 | # Import third party libs |
| 9 | HAS_CINDER = False |
| 10 | try: |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 11 | from cinderclient.client import Client |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 12 | HAS_CINDER = True |
| 13 | except ImportError: |
| 14 | pass |
| 15 | |
| 16 | __opts__ = {} |
| 17 | |
| 18 | |
| 19 | def __virtual__(): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 20 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 21 | Only load this module if cinder |
| 22 | is installed on this minion. |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 23 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 24 | if HAS_CINDER: |
| 25 | return 'cinderng' |
| 26 | return False |
| 27 | |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 28 | |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 29 | def _authng(profile=None): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 30 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 31 | Set up cinder credentials |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 32 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 33 | credentials = { |
| 34 | 'username': profile['user'], |
| 35 | 'password': profile['password'], |
| 36 | 'project_id': profile['project_id'], |
Oleg Iurchenko | 6fe8e5d | 2018-02-20 14:26:04 +0200 | [diff] [blame] | 37 | 'auth_url': "{}://{}:{}/{}".format( |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 38 | profile['protocol'], |
| 39 | profile['host'], |
Oleg Iurchenko | 6fe8e5d | 2018-02-20 14:26:04 +0200 | [diff] [blame] | 40 | profile['port'], |
| 41 | profile.get('api_version', 'v3') |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 42 | ), |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 43 | 'endpoint_type': profile['endpoint_type'], |
| 44 | 'certificate': profile['certificate'], |
Oleg Iurchenko | 86bf9e4 | 2018-01-18 20:58:21 +0200 | [diff] [blame] | 45 | 'region_name': profile['region_name'], |
| 46 | 'retries': profile.get('retries', 5) |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 47 | } |
| 48 | return credentials |
| 49 | |
Oleh Hryhorov | 49c4b49 | 2018-05-03 16:07:39 +0300 | [diff] [blame] | 50 | def retries(func): |
Vasyl Saienko | 0291d9c | 2018-05-31 12:44:07 +0300 | [diff] [blame] | 51 | @functools.wraps(func) |
Oleh Hryhorov | 49c4b49 | 2018-05-03 16:07:39 +0300 | [diff] [blame] | 52 | def func_wrapper(*args, **kwargs): |
Oleh Hryhorov | c4398b2 | 2018-05-29 13:12:08 +0300 | [diff] [blame] | 53 | retries = kwargs.get('retries', 5) |
Oleh Hryhorov | 49c4b49 | 2018-05-03 16:07:39 +0300 | [diff] [blame] | 54 | timeout = kwargs.get('timeout', 5) |
| 55 | res = None |
| 56 | for i in range(retries): |
| 57 | try: |
Vasyl Saienko | 0291d9c | 2018-05-31 12:44:07 +0300 | [diff] [blame] | 58 | return func(*args, **kwargs) |
Oleh Hryhorov | 49c4b49 | 2018-05-03 16:07:39 +0300 | [diff] [blame] | 59 | except Exception as e: |
| 60 | if i == retries - 1: |
| 61 | raise e |
Oleh Hryhorov | 49c4b49 | 2018-05-03 16:07:39 +0300 | [diff] [blame] | 62 | else: |
Vasyl Saienko | 0291d9c | 2018-05-31 12:44:07 +0300 | [diff] [blame] | 63 | time.sleep(timeout) |
Oleh Hryhorov | 49c4b49 | 2018-05-03 16:07:39 +0300 | [diff] [blame] | 64 | return func_wrapper |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 65 | |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 66 | def create_conn(cred=None): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 67 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 68 | create connection |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 69 | """ |
| 70 | nt = Client( |
| 71 | '2', |
| 72 | username=cred['username'], |
| 73 | api_key=cred['password'], |
| 74 | project_id=cred['project_id'], |
| 75 | auth_url=cred['auth_url'], |
| 76 | endpoint_type=cred['endpoint_type'], |
| 77 | cacert=cred['certificate'], |
Oleg Iurchenko | 86bf9e4 | 2018-01-18 20:58:21 +0200 | [diff] [blame] | 78 | region_name=cred['region_name'], |
| 79 | retries=cred['retries'] |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 80 | ) |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 81 | return nt |
| 82 | |
Oleh Hryhorov | 49c4b49 | 2018-05-03 16:07:39 +0300 | [diff] [blame] | 83 | @retries |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 84 | def list_volumes(profile=None, **kwargs): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 85 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 86 | Return list of cinder volumes. |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 87 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 88 | cred = _authng(profile) |
| 89 | nt = create_conn(cred) |
| 90 | return nt.volumes.list() |
| 91 | |
Oleh Hryhorov | 49c4b49 | 2018-05-03 16:07:39 +0300 | [diff] [blame] | 92 | @retries |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 93 | def list_volume_type(profile=None, **kwargs): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 94 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 95 | Return list of volume types |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 96 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 97 | cred = _authng(profile) |
| 98 | nt = create_conn(cred) |
| 99 | return nt.volume_types.list() |
| 100 | |
Oleh Hryhorov | 49c4b49 | 2018-05-03 16:07:39 +0300 | [diff] [blame] | 101 | @retries |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 102 | def get_volume_type(type_name, profile=None, **kwargs): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 103 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 104 | Returns id of the specified volume type name |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 105 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 106 | vt_id = None |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 107 | vt_list = list_volume_type(profile) |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 108 | for vt in vt_list: |
| 109 | if vt.name == type_name: |
| 110 | vt_id = vt.id |
| 111 | |
| 112 | if vt_id: |
| 113 | cred = _authng(profile) |
| 114 | nt = create_conn(cred) |
| 115 | try: |
| 116 | vt = nt.volume_types.get(vt_id) |
| 117 | return vt |
| 118 | except: |
| 119 | return |
| 120 | else: |
| 121 | return |
| 122 | |
Oleh Hryhorov | 49c4b49 | 2018-05-03 16:07:39 +0300 | [diff] [blame] | 123 | @retries |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 124 | def create_volume_type(type_name, profile=None, **kwargs): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 125 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 126 | Create cinder volume type |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 127 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 128 | vt = get_volume_type(type_name, profile) |
| 129 | if not vt: |
| 130 | cred = _authng(profile) |
| 131 | nt = create_conn(cred) |
| 132 | try: |
| 133 | nt.volume_types.create(type_name) |
| 134 | return 'created' |
| 135 | except: |
| 136 | return 'failed' |
| 137 | else: |
| 138 | return 'exists' |
| 139 | |
Oleh Hryhorov | 49c4b49 | 2018-05-03 16:07:39 +0300 | [diff] [blame] | 140 | @retries |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 141 | def get_keys_volume_type(type_name, profile=None, **kwargs): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 142 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 143 | Return extra specs of the specified volume type. |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 144 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 145 | |
| 146 | vt = get_volume_type(type_name, profile) |
| 147 | if vt: |
| 148 | try: |
| 149 | return vt.get_keys() |
| 150 | except: |
| 151 | return 'failed' |
| 152 | else: |
| 153 | return |
| 154 | |
Oleh Hryhorov | 49c4b49 | 2018-05-03 16:07:39 +0300 | [diff] [blame] | 155 | @retries |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 156 | def set_keys_volume_type(type_name, keys={}, profile=None, **kwargs): |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 157 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 158 | Set extra specs of the specified volume type. |
Denis V. Meltsaykin | 6b1fa43 | 2017-09-18 16:49:53 +0300 | [diff] [blame] | 159 | """ |
Jiri Broulik | 47aa6b3 | 2017-07-10 18:39:15 +0200 | [diff] [blame] | 160 | set_keys = False |
| 161 | vt = get_volume_type(type_name, profile) |
| 162 | if vt: |
| 163 | k = get_keys_volume_type(type_name, profile) |
| 164 | if not k: |
| 165 | set_keys = True |
| 166 | elif k: |
| 167 | for key in keys: |
| 168 | if k.get(key) != keys[key]: |
| 169 | set_keys = True |
| 170 | elif len(k) != len(keys): |
| 171 | set_keys = True |
| 172 | else: |
| 173 | return |
| 174 | |
| 175 | if set_keys: |
| 176 | try: |
| 177 | vt.set_keys(keys) |
| 178 | return 'updated' |
| 179 | except: |
| 180 | return 'failed' |
| 181 | else: |
| 182 | return 'exist' |
| 183 | else: |
| 184 | return 'not found' |