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