blob: 45cff84fa725d639a4ebe810b05dee7326db8452 [file] [log] [blame]
kairat_kushaev66852622018-06-07 17:27:35 +04001# Import Python libs
2from __future__ import absolute_import, print_function, unicode_literals
3import logging
4import time
5
6LOG = logging.getLogger(__name__)
7
8
9def __virtual__():
10 return 'heatv1'
11
12
13def _heat_call(fname, *args, **kwargs):
14 return __salt__['heatv1.{}'.format(fname)](*args, **kwargs)
15
16
17def _poll_for_complete(stack_name, cloud_name=None, action=None,
18 poll_period=5, timeout=60):
19 if action:
20 stop_status = ('{0}_FAILED'.format(action), '{0}_COMPLETE'.format(action))
21 stop_check = lambda a: a in stop_status
22 else:
23 stop_check = lambda a: a.endswith('_COMPLETE') or a.endswith('_FAILED')
24 timeout_sec = timeout * 60
25 msg_template = '\n Stack %(name)s %(status)s \n'
26 while True:
27 stack = _heat_call('stack_show',
28 name=stack_name,
29 cloud_name=cloud_name)
30 if not stack["result"]:
Oleksiy Petrenko5e6ebc92018-08-09 15:03:30 +030031 if action == "DELETE" and stack['status_code'] == 404:
32 stack_status = 'DELETE COMPLETE'
33 msg = msg_template % dict(name=stack_name, status=stack_status)
34 return 'DELETE_COMPLETE', msg
kairat_kushaev66852622018-06-07 17:27:35 +040035 raise Exception("request for stack failed")
36
37 stack = stack["body"]["stack"]
38 stack_status = stack["stack_status"]
39 msg = msg_template % dict(
40 name=stack_name, status=stack_status)
41 if stop_check(stack_status):
42 return stack_status, msg
43
44 time.sleep(poll_period)
45 timeout_sec -= poll_period
46 if timeout_sec <= 0:
47 stack_status = '{0}_FAILED'.format(action)
48 msg = 'Timeout expired'
49 return stack_status, msg
50
51
52def stack_present(name, cloud_name, template=None,
53 environment=None, params=None, poll=5, rollback=False,
54 timeout=60, profile=None, **connection_args):
55 LOG.debug('Deployed with(' +
56 '{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8} {9})'
57 .format(name, cloud_name, template, environment, params,
58 poll, rollback, timeout, profile, connection_args))
59 ret = {'name': None,
60 'comment': '',
61 'changes': {},
62 'result': True}
63
64 if not name:
65 ret['result'] = False
66 ret['comment'] = 'Name is not valid'
67 return ret
68
69 ret['name'] = name,
70
71 existing_stack = _heat_call('stack_show', name=name,
72 cloud_name=cloud_name)
73
74 if existing_stack['result']:
75 _heat_call('stack_update', name=name,
76 template=template,
77 cloud_name=cloud_name,
78 environment=environment,
79 parameters=params,
80 disable_rollback=not rollback,
81 timeout=timeout)
82 ret['changes']['comment'] = 'Updated stack'
83 status, res = _poll_for_complete(stack_name=name,
84 cloud_name=cloud_name,
85 action="UPDATE", timeout=timeout)
86 ret["result"] = status == "UPDATE_COMPLETE"
87 ret['comment'] = res
88 else:
89 _heat_call('stack_create',
90 name=name,
91 template=template,
92 cloud_name=cloud_name,
93 environment=environment,
94 parameters=params,
95 disable_rollback=not rollback,
96 timeout=timeout)
97 status, res = _poll_for_complete(stack_name=name,
98 cloud_name=cloud_name,
99 action="CREATE", timeout=timeout)
100 ret["result"] = status == "CREATE_COMPLETE"
101 ret['comment'] = res
102 ret['changes']['stack_name'] = name
103 return ret
104
105
106def stack_absent(name, cloud_name, poll=5, timeout=60):
107 LOG.debug('Absent with(' +
108 '{0}, {1}, {2})'.format(name, poll, cloud_name))
109 ret = {'name': None,
110 'comment': '',
111 'changes': {},
112 'result': True}
113 if not name:
114 ret['result'] = False
115 ret['comment'] = 'Name is not valid'
116 return ret
117
118 ret['name'] = name,
119
120 existing_stack = _heat_call('stack_show',
121 name=name, cloud_name=cloud_name)
122
123 if not existing_stack['result']:
124 ret['result'] = True
125 ret['comment'] = 'Stack does not exist'
126 return ret
127
128 _heat_call('stack_delete', name=name, cloud_name=cloud_name)
129 status, comment = _poll_for_complete(stack_name=name,
130 cloud_name=cloud_name,
131 action="DELETE", timeout=timeout)
132 ret['result'] = status == "DELETE_COMPLETE"
133 ret['comment'] = comment
134 ret['changes']['stack_name'] = name
135 return ret