a446ee45e648926d80e2f9c162ee67f7078a564d
[integration/test.git] / tools / deployment / openstack_ha / libraries / SystemUtils.robot
1 *** Settings ***
2 Documentation       Library to use common Linux Commands and also some configuration on MySQL,Rabbit etc.
3
4 Library             Collections
5 Library             SSHLibrary
6 Library             OperatingSystem
7 Resource            SSHKeywords.robot
8
9
10 *** Keywords ***
11 Install Rpm Package
12     [Documentation]    Install packages in a node
13     [Arguments]    ${os_node_cxn}    ${package}
14     Switch Connection    ${os_node_cxn}
15     ${output}    ${rc}=    Execute Command    sudo yum install -y ${package}    return_rc=True    return_stdout=True
16     Log    ${output}
17     Should Not Be True    ${rc}
18
19 Local Install Rpm Package
20     [Documentation]    Install packages in local VM
21     [Arguments]    ${package}
22     ${rc}    ${output}=    Run And Return Rc And Output    sudo yum install -y ${package}
23     Log    ${output}
24     Should Not Be True    ${rc}
25
26 Install Local Rpm Package
27     [Documentation]    Install local rpm packages
28     [Arguments]    ${os_node_cxn}    ${package}
29     Switch Connection    ${os_node_cxn}
30     Put File    /tmp/${ODL_RPM}    /tmp/
31     ${output}    ${rc}=    Execute Command
32     ...    sudo yum localinstall -y /tmp/${package}
33     ...    return_rc=True
34     ...    return_stdout=True
35     Log    ${output}
36     Should Not Be True    ${rc}
37
38 Crudini Edit
39     [Documentation]    Crudini edit on a configuration file
40     [Arguments]    ${os_node_cxn}    ${conf_file}    ${section}    ${key}    ${value}
41     Switch Connection    ${os_node_cxn}
42     ${output}    ${rc}=    Execute Command
43     ...    sudo crudini --verbose --set --inplace ${conf_file} ${section} ${key} ${value}
44     ...    return_rc=True
45     ...    return_stdout=True
46     Log    ${output}
47     Should Not Be True    ${rc}
48     RETURN    ${output}
49
50 Crudini Delete
51     [Documentation]    Crudini edit on a configuration file
52     [Arguments]    ${os_node_cxn}    ${conf_file}    ${section}    ${key}
53     Switch Connection    ${os_node_cxn}
54     ${output}    ${rc}=    Execute Command
55     ...    sudo crudini --verbose --del --inplace ${conf_file} ${section} ${key}
56     ...    return_rc=True
57     ...    return_stdout=True
58     Log    ${output}
59     Should Not Be True    ${rc}
60     RETURN    ${output}
61
62 Update Packages
63     [Documentation]    yum update to the latest versions in the repo
64     [Arguments]    ${os_node_cxn}
65     Switch Connection    ${os_node_cxn}
66     ${output}    ${rc}=    Execute Command    sudo yum update -y    return_rc=True    return_stdout=True
67     Log    ${output}
68     Should Not Be True    ${rc}
69     RETURN    ${output}
70
71 Start Service
72     [Documentation]    Start a service in CentOs
73     [Arguments]    ${os_node_cxn}    ${service}
74     Switch Connection    ${os_node_cxn}
75     ${output}    ${rc}=    Execute Command    sudo systemctl start ${service}    return_rc=True    return_stdout=True
76     Log    ${output}
77     Should Not Be True    ${rc}
78     RETURN    ${output}
79
80 Enable Service
81     [Documentation]    Enable a service in CentOs
82     [Arguments]    ${os_node_cxn}    ${service}
83     Switch Connection    ${os_node_cxn}
84     ${output}    ${rc}=    Execute Command    sudo systemctl enable ${service}    return_rc=True    return_stdout=True
85     Log    ${output}
86     Should Not Be True    ${rc}
87     RETURN    ${output}
88
89 Stop Service
90     [Documentation]    stop a service in CentOs
91     [Arguments]    ${os_node_cxn}    ${service}
92     Switch Connection    ${os_node_cxn}
93     ${output}    ${rc}=    Execute Command    sudo systemctl stop ${service}    return_rc=True    return_stdout=True
94     Log    ${output}
95     Should Not Be True    ${rc}
96     RETURN    ${output}
97
98 Daemon Reload
99     [Documentation]    daemon reload
100     [Arguments]    ${os_node_cxn}
101     Switch Connection    ${os_node_cxn}
102     ${output}    ${rc}=    Execute Command    sudo systemctl daemon-reload    return_rc=True    return_stdout=True
103     Log    ${output}
104     Should Not Be True    ${rc}
105     RETURN    ${output}
106
107 Restart Service
108     [Documentation]    Restart a service in CentOs
109     [Arguments]    ${os_node_cxn}    ${service}
110     Switch Connection    ${os_node_cxn}
111     ${output}    ${rc}=    Execute Command    sudo systemctl restart ${service}    return_rc=True    return_stdout=True
112     Log    ${output}
113     Should Not Be True    ${rc}
114     RETURN    ${output}
115
116 Stop And Disable Firewall
117     [Documentation]    Disable/stop firewalld and iptables for testing
118     [Arguments]    ${os_node_cxn}
119     Switch Connection    ${os_node_cxn}
120     ${output}    ${rc}=    Execute Command    sudo systemctl stop firewalld    return_rc=True    return_stdout=True
121     Log    ${output}
122     Log    ${rc}
123     Should Not Be True    ${rc}
124     ${output}    ${rc}=    Execute Command    sudo systemctl disable firewalld    return_rc=True    return_stdout=True
125     Log    ${output}
126     Log    ${rc}
127     Should Not Be True    ${rc}
128     ${output}    ${rc}=    Execute Command    sudo systemctl stop iptables    return_rc=True    return_stdout=True
129     Log    ${output}
130     Log    ${rc}
131     ${output}    ${rc}=    Execute Command    sudo systemctl disable iptables    return_rc=True    return_stdout=True
132     Log    ${output}
133     Log    ${rc}
134
135 Chmod File
136     [Documentation]    Chmod on any file in server
137     [Arguments]    ${os_node_cxn}    ${file_or_path}    ${perm_value}
138     Switch Connection    ${os_node_cxn}
139     ${output}    ${rc}=    Execute Command
140     ...    sudo chmod ${perm_value} ${file_or_path}
141     ...    return_rc=True
142     ...    return_stdout=True
143     Log    ${output}
144     Should Not Be True    ${rc}
145     RETURN    ${output}
146
147 Chown File
148     [Documentation]    Chown on any file in server
149     [Arguments]    ${os_node_cxn}    ${file_or_path}    ${user}    ${group}
150     Switch Connection    ${os_node_cxn}
151     ${output}    ${rc}=    Execute Command
152     ...    sudo chown -R ${user}:${group} ${file_or_path}
153     ...    return_rc=True
154     ...    return_stdout=True
155     Log    ${output}
156     Should Not Be True    ${rc}
157     RETURN    ${output}
158
159 Copy File
160     [Documentation]    Copy file in server from src to dest
161     [Arguments]    ${os_node_cxn}    ${file_src}    ${file_dst}
162     Switch Connection    ${os_node_cxn}
163     ${output}    ${rc}=    Execute Command
164     ...    sudo cp -f ${file_src} ${file_dst}
165     ...    return_rc=True
166     ...    return_stdout=True
167     Log    ${output}
168     Should Not Be True    ${rc}
169     RETURN    ${output}
170
171 Move File
172     [Documentation]    Move or rename a file in server
173     [Arguments]    ${os_node_cxn}    ${file_src}    ${file_dst}
174     Switch Connection    ${os_node_cxn}
175     ${output}    ${rc}=    Execute Command
176     ...    sudo mv -v -f ${file_src} ${file_dst}
177     ...    return_rc=True
178     ...    return_stdout=True
179     Log    ${output}
180     RETURN    ${output}
181
182 Touch File
183     [Documentation]    Execute touch and create a file in server
184     [Arguments]    ${os_node_cxn}    ${file_name}
185     Switch Connection    ${os_node_cxn}
186     ${output}    ${rc}=    Execute Command    sudo touch ${file_name}    return_rc=True    return_stdout=True
187     Log    ${output}
188     Should Not Be True    ${rc}
189     RETURN    ${output}
190
191 Run Command As User
192     [Documentation]    Run a command as a differnt user
193     [Arguments]    ${os_node_cxn}    ${command}    ${run_as_user}
194     Switch Connection    ${os_node_cxn}
195     ${output}    ${rc}=    Execute Command
196     ...    sudo su -s /bin/sh -c ${command} ${run_as_user}
197     ...    return_rc=True
198     ...    return_stdout=True
199     Log    ${output}
200     Should Not Be True    ${rc}
201     RETURN    ${output}
202
203 Run Command
204     [Documentation]    Run a command as a differnt user
205     [Arguments]    ${os_node_cxn}    ${command}
206     Switch Connection    ${os_node_cxn}
207     ${output}    ${rc}=    Execute Command    ${command}    return_rc=True    return_stdout=True
208     Log    ${output}
209     Should Not Be True    ${rc}
210     RETURN    ${output}
211
212 Create User Pass For Mysql
213     [Documentation]    Create an user with password to access Mysql DB
214     [Arguments]    ${os_node_cxn}    ${mysql_user}    ${mysql_pass}
215     Switch Connection    ${os_node_cxn}
216     ${output}    ${rc}=    Execute Command
217     ...    sudo mysqladmin -u ${mysql_user} password ${mysql_pass}
218     ...    return_rc=True
219     ...    return_stdout=True
220     Log    ${output}
221     Should Not Be True    ${rc}
222     RETURN    ${output}
223
224 Create Database for Mysql
225     [Documentation]    Create a database on MySQL
226     [Arguments]    ${os_node_cxn}    ${mysql_user}    ${mysql_pass}    ${db_name}
227     Switch Connection    ${os_node_cxn}
228     Write Commands Until Expected Prompt    sudo mysql -u${mysql_user} -p${mysql_pass}    >    30s
229     ${output}=    Write Commands Until Expected Prompt
230     ...    CREATE DATABASE ${db_name} CHARACTER SET utf8; exit;
231     ...    Bye
232     ...    30s
233     Log    ${output}
234     RETURN    ${output}
235
236 Rsync Directory
237     [Arguments]    ${os_node_cxn}    ${dst_node_ip}    ${src_dir}    ${dst_dir}
238     Switch Connection    ${os_node_cxn}
239     Write Commands Until Expected Prompt
240     ...    sudo rsync -e "ssh -o StrictHostKeyChecking=no" -avz ${src_dir} ${dst_node_ip}:${dst_dir}
241     ...    d:
242     ...    30s
243     Write Commands Until Expected Prompt    ${OS_USER_PASSWORD}    ${OS_NODE_PROMPT}    30s
244
245 Grant Privileges To Mysql Database
246     [Documentation]    Grant Privileges on a database in MySQL
247     [Arguments]    ${os_node_cxn}    ${mysql_user}    ${mysql_pass}    ${db_name}    ${db_user}    ${host_name}
248     ...    ${db_pass}
249     Switch Connection    ${os_node_cxn}
250     Write Commands Until Expected Prompt    sudo mysql -u${mysql_user} -p${mysql_pass}    >    30s
251     ${output}=    Write Commands Until Expected Prompt
252     ...    GRANT ALL PRIVILEGES ON ${db_name} TO '${db_user}'@'${host_name}' identified by '${db_pass}'; exit;
253     ...    Bye
254     ...    30s
255     Log    ${output}
256     RETURN    ${output}
257
258 Grant Process To Mysql Database
259     [Documentation]    Grant Privileges on a database in MySQL
260     [Arguments]    ${os_node_cxn}    ${mysql_user}    ${mysql_pass}    ${db_name}    ${db_user}    ${host_name}
261     ...    ${db_pass}
262     Switch Connection    ${os_node_cxn}
263     Write Commands Until Expected Prompt    sudo mysql -u${mysql_user} -p${mysql_pass}    >    30s
264     ${output}=    Write Commands Until Expected Prompt
265     ...    GRANT PROCESS ON ${db_name} TO '${db_user}'@'${host_name}' identified by '${db_pass}'; FLUSH PRIVILEGES; exit;
266     ...    Bye
267     ...    30s
268     Log    ${output}
269     RETURN    ${output}
270
271 Execute MySQL STATUS Query
272     [Arguments]    ${os_node_cxn}    ${mysql_user}    ${mysql_pass}    ${attribute}
273     Switch Connection    ${os_node_cxn}
274     Write Commands Until Expected Prompt    sudo mysql -u${mysql_user} -p${mysql_pass}    >    30s
275     ${output}=    Write Commands Until Expected Prompt    show STATUS LIKE '${attribute}';exit;    Bye    30s
276     Log    ${output}
277     RETURN    ${output}
278
279 Write To File
280     [Documentation]    Write to file which require sudo access
281     [Arguments]    ${os_node_cxn}    ${file_name}    ${buffer}
282     Switch Connection    ${os_node_cxn}
283     ${output}    ${rc}=    Execute Command
284     ...    echo ${buffer} | sudo tee ${file_name}
285     ...    return_rc=True
286     ...    return_stdout=True
287     Log    ${output}
288     Should Not Be True    ${rc}
289     RETURN    ${output}
290
291 Append To File
292     [Documentation]    Append to file which require sudo access
293     [Arguments]    ${os_node_cxn}    ${file_name}    ${buffer}
294     Switch Connection    ${os_node_cxn}
295     ${output}    ${rc}=    Execute Command
296     ...    echo ${buffer} | sudo tee --append ${file_name}
297     ...    return_rc=True
298     ...    return_stdout=True
299     Log    ${output}
300     Should Not Be True    ${rc}
301     RETURN    ${output}
302
303 Create Softlink
304     [Documentation]    Create Soft Link
305     [Arguments]    ${os_node_cxn}    ${src_file_name}    ${link_path}
306     Switch Connection    ${os_node_cxn}
307     ${output}    ${rc}=    Execute Command
308     ...    sudo ln -s ${src_file_name} ${link_path}
309     ...    return_rc=True
310     ...    return_stdout=True
311     Log    ${output}
312     Should Not Be True    ${rc}
313     RETURN    ${output}
314
315 Unlink File
316     [Documentation]    Unlink a wrong link created
317     [Arguments]    ${os_node_cxn}    ${link_path}
318     Switch Connection    ${os_node_cxn}
319     ${output}    ${rc}=    Execute Command    sudo unlink ${link_path}    return_rc=True    return_stdout=True
320     Log    ${output}
321     Should Not Be True    ${rc}
322     RETURN    ${output}
323
324 Create LocalFile
325     [Documentation]    Touch a local file
326     [Arguments]    ${src_file_name}
327     ${rc}    ${output}=    Run And Return Rc And Output    sudo touch ${src_file_name}
328     Log    ${output}
329     Should Not Be True    ${rc}
330
331 Write To Local File
332     [Documentation]    AddEntry to Local File
333     [Arguments]    ${src_file_name}    ${buffer}
334     ${rc}    ${output}=    Run And Return Rc And Output    echo ${buffer} | sudo tee ${src_file_name}
335     Log    ${output}
336     Should Not Be True    ${rc}
337
338 Append To Local File
339     [Documentation]    AddEntry to Local File
340     [Arguments]    ${src_file_name}    ${buffer}
341     ${rc}    ${output}=    Run And Return Rc And Output    echo ${buffer} | sudo tee -a ${src_file_name}
342     Log    ${output}
343     Should Not Be True    ${rc}
344
345 Source Local File
346     [Documentation]    Export Varaibles to Env
347     [Arguments]    ${src_file_name}
348     ${rc}    ${output}=    Run And Return Rc And Output    source ${src_file_name}
349     Log    ${output}
350     Should Not Be True    ${rc}
351
352 Run Command In Local Node
353     [Arguments]    ${command}
354     ${rc}    ${output}=    Run And Return Rc And Output    ${command}
355     Log    ${output}
356     Should Not Be True    ${rc}
357
358 Generic HAProxy Entry
359     [Documentation]    Add an entry in haproxy.cfg for the service
360     [Arguments]    ${os_node_cxn}    ${haproxy_ip}    ${port_to_listen}    ${proxy_entry}
361     Append To File    ${os_node_cxn}    /etc/haproxy/haproxy.cfg    ' '
362     Append To File    ${os_node_cxn}    /etc/haproxy/haproxy.cfg    frontend vip-${proxy_entry}
363     Append To File    ${os_node_cxn}    /etc/haproxy/haproxy.cfg    ' 'bind *:${port_to_listen}
364     Append To File    ${os_node_cxn}    /etc/haproxy/haproxy.cfg    ' 'timeout client 90s
365     Append To File    ${os_node_cxn}    /etc/haproxy/haproxy.cfg    ' 'default_backend ${proxy_entry}
366     Append To File    ${os_node_cxn}    /etc/haproxy/haproxy.cfg    ' '
367     Append To File    ${os_node_cxn}    /etc/haproxy/haproxy.cfg    backend ${proxy_entry}
368     Append To File    ${os_node_cxn}    /etc/haproxy/haproxy.cfg    ' 'balance roundrobin
369     Append To File
370     ...    ${os_node_cxn}
371     ...    /etc/haproxy/haproxy.cfg
372     ...    ' 'server ${proxy_entry}_controller1 ${OS_CONTROL_1_IP}:${port_to_listen} check inter 1s
373     Append To File
374     ...    ${os_node_cxn}
375     ...    /etc/haproxy/haproxy.cfg
376     ...    ' 'server ${proxy_entry}_controller2 ${OS_CONTROL_2_IP}:${port_to_listen} check inter 1s
377     Append To File
378     ...    ${os_node_cxn}
379     ...    /etc/haproxy/haproxy.cfg
380     ...    ' 'server ${proxy_entry}_controller3 ${OS_CONTROL_3_IP}:${port_to_listen} check inter 1s
381     IF    3 < ${NUM_CONTROL_NODES}
382         Append To File
383         ...    ${os_node_cxn}
384         ...    /etc/haproxy/haproxy.cfg
385         ...    ' 'server ${proxy_entry}_controller4 ${OS_CONTROL_4_IP}:${port_to_listen} check inter 1s
386     END
387     IF    4 < ${NUM_CONTROL_NODES}
388         Append To File
389         ...    ${os_node_cxn}
390         ...    /etc/haproxy/haproxy.cfg
391         ...    ' 'server ${proxy_entry}_controller5 ${OS_CONTROL_5_IP}:${port_to_listen} check inter 1s
392     END
393     Restart Service    ${os_node_cxn}    haproxy
394
395 Cat File
396     [Documentation]    Read a file for logging
397     [Arguments]    ${os_node_cxn}    ${file_name}
398     Switch Connection    ${os_node_cxn}
399     ${output}    ${rc}=    Execute Command    cat ${file_name}    return_rc=True    return_stdout=True
400     Log    ${output}
401     Should Not Be True    ${rc}
402
403 Disable SeLinux Tempororily
404     [Documentation]    Disable SELinux from command
405     [Arguments]    ${os_node_cxn}
406     Switch Connection    ${os_node_cxn}
407     ${output}    ${rc}=    Execute Command    sudo setenforce 0    return_rc=True    return_stdout=True
408     Log    ${output}
409
410 Get Ssh Connection
411     [Arguments]    ${os_ip}    ${os_user}    ${os_password}    ${prompt}
412     ${conn_id}=    SSHLibrary.Open Connection    ${os_ip}    prompt=${prompt}    timeout=1 hour    alias=${os_ip}
413     SSHKeywords.Flexible SSH Login    ${os_user}    password=${os_password}
414     SSHLibrary.Set Client Configuration    timeout=1 hour
415     RETURN    ${conn_id}