Bootstrap Cloudify
[oom.git] / TOSCA / cloudify-environment-setup / scripts / manager / tasks.py
1 # ============LICENSE_START==========================================
2 # ===================================================================
3 # Copyright (c) 2018 AT&T
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #         http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #============LICENSE_END============================================
17
18 from tempfile import NamedTemporaryFile
19 from fabric.api import get, sudo, run
20 from cloudify import ctx
21 from cloudify.exceptions import NonRecoverableError, RecoverableError
22
23 CONFIG_PATH = '/etc/cloudify/config.yaml'
24
25
26 def install_rpm(_rpm):
27     try:
28         sudo("rpm -i {0}".format(_rpm))
29     except Exception as e:
30         raise NonRecoverableError(str(e))
31     return True
32
33
34 def install_requirements():
35     try:
36         sudo("sudo yum install -y python-backports-ssl_match_hostname "
37              "python-setuptools python-backports")
38     except Exception as e:
39         raise NonRecoverableError(str(e))
40     return True
41
42
43 def update_config(private, public, _config_path):
44     SED = "sed -i 's|{0}|{1}|g' {2}"
45
46     old_private_ip = "  private_ip: \\x27\\x27"
47     new_private_ip = "  private_ip: \\x27{0}\\x27".format(private)
48
49     try:
50         sudo(SED.format(old_private_ip, new_private_ip, _config_path))
51     except Exception as e:
52         raise NonRecoverableError(str(e))
53
54     old_public_ip = "  public_ip: \\x27\\x27"
55     new_public_ip = "  public_ip: \\x27{0}\\x27".format(public)
56
57     try:
58         sudo(SED.format(old_public_ip, new_public_ip, _config_path))
59     except Exception as e:
60         raise NonRecoverableError(str(e))
61
62     old_networks = "  networks: {}"
63     new_networks = "  networks: {{ \\x27default\\x27: \\x27{0}\\x27, \\x27external\\x27: \\x27{1}\\x27 }}".format(
64         private, public)
65
66     try:
67         sudo(SED.format(old_networks, new_networks, _config_path))
68         sudo("chmod 775 {0}".format(_config_path))
69     except Exception as e:
70         raise NonRecoverableError(str(e))
71     return True
72
73
74 def cfy_install(password, old=False):
75     sudo("chmod 777 {0}".format(CONFIG_PATH))
76
77     install_string = 'cfy_manager install'
78
79     if password:
80         install_string = \
81             install_string + ' ' + '--admin-password {0}'.format(
82                 password)
83     if old:
84         install_string = install_string + '  --clean-db'
85     elif not old:
86         try:
87             sudo("sudo yum install -y openssl-1.0.2k")
88         except Exception as e:
89             raise NonRecoverableError(str(e))
90
91     try:
92         run(install_string)
93     except Exception as e:
94         ctx.logger.error(str(e))
95         return False
96
97     sudo("chmod 775 {0}".format(CONFIG_PATH))
98
99     return True
100
101
102 def plugins_upload():
103     try:
104         run("cfy plugins bundle-upload")
105     except Exception as e:
106         raise NonRecoverableError(str(e))
107
108     return True
109
110
111 def secrets_create(secret_key, secret_value):
112     try:
113         run("cfy secrets create {0} -s \"{1}\"".format(
114             secret_key, secret_value))
115     except Exception as e:
116         raise NonRecoverableError(str(e))
117
118     return True
119
120
121 def blueprints_upload(file, name, url):
122     try:
123         run("cfy blueprints upload -n {0} -b {1} {2}".format(file, name, url))
124     except Exception as e:
125         raise NonRecoverableError(str(e))
126
127     return True
128
129
130 def create(private_ip,
131            public_ip,
132            rpm,
133            secrets,
134            blueprints,
135            config_path=CONFIG_PATH,
136            password=None,
137            **_):
138     ctx.logger.info("Installing Cloudify Manager components.")
139
140     try:
141         run("echo Hello")
142     except Exception as e:
143         raise RecoverableError(str(e))
144
145     if not ctx.instance.runtime_properties.get('installed_rpm'):
146         install_requirements()
147         ctx.instance.runtime_properties['installed_rpm'] = install_rpm(rpm)
148
149     if not ctx.instance.runtime_properties.get('updated_config'):
150         ctx.instance.runtime_properties['updated_config'] = \
151             update_config(private_ip, public_ip, config_path)
152
153     if 'cfy_installed' not in ctx.instance.runtime_properties:
154         cfy_install_output = cfy_install(password)
155     else:
156         cfy_install_output = cfy_install(password, old=True)
157
158     ctx.instance.runtime_properties['cfy_installed'] = cfy_install_output
159     if not cfy_install_output:
160         raise RecoverableError('cfy install failed.')
161
162     if not ctx.instance.runtime_properties.get('plugins_uploaded'):
163         try:
164             run(
165                 "cfy plugins upload https://nexus.onap.org/content/sites/raw/org.onap.ccsdk.platform.plugins/plugins/helm-3.0.0-py27-none-linux_x86_64.wgn -y https://nexus.onap.org/content/sites/raw/org.onap.ccsdk.platform.plugins/type_files/helm/1.1.0/helm-type.yaml")
166         except Exception as e:
167             raise NonRecoverableError(str(e))
168         ctx.instance.runtime_properties['plugins_uploaded'] = plugins_upload()
169
170     more_secrets = [
171         {'key': 'cfy_user', 'value': 'admin'},
172         {'key': 'kubernetes_master_port', 'value': 'kubernetes_master_port'},
173         {'key': 'kubernetes-admin_client_certificate_data',
174          'value': 'kubernetes-admin_client_certificate_data'},
175         {'key': 'kubernetes_master_ip', 'value': 'kubernetes_master_ip'},
176         {'key': 'kubernetes_certificate_authority_data',
177          'value': 'kubernetes_certificate_authority_data'},
178         {'key': 'kubernetes-admin_client_key_data',
179          'value': 'kubernetes-admin_client_key_data'},
180         {'key': 'cfy_password', 'value': password or 'cfy_password'},
181         {'key': 'cfy_tenant', 'value': 'default_tenant'},
182         {'key': 'kubernetes_token', 'value': 'kubernetes_token'}
183     ]
184     for ms in more_secrets:
185         secrets.append(ms)
186
187     for secret in secrets:
188         secrets_create(
189             secret.get('key'),
190             secret.get('value'))
191
192     for blueprint in blueprints:
193         blueprints_upload(
194             blueprint.get('file'),
195             blueprint.get('name'),
196             blueprint.get('url'))
197
198     ctx.logger.info(
199         "Initialize your CLI profile: "
200         "`cfy profiles use "
201         "{0} -u admin -p {1} -t default_tenant`".format(public_ip,
202                                                         password or "_"))
203     if not password:
204         ctx.logger.info(
205             "Since you did not provide a password, scroll up though "
206             "the execution log and search for \"Manager password is\".")