Skip to content

Commit 7d8a838

Browse files
authored
Merge pull request #1635 from grycap/virtualenv
Virtualenv
2 parents 057f105 + 3ce577d commit 7d8a838

18 files changed

+71
-46
lines changed

.github/workflows/main.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Set up Python 3.
1717
uses: actions/setup-python@v5
1818
with:
19-
python-version: '3.11'
19+
python-version: '3.12'
2020

2121
- name: Install dependencies
2222
run: python -m pip install tox

IM/ConfManager.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
from IM.recipe import Recipe
5757
from IM.config import Config
5858
from radl.radl import system, contextualize_item
59+
from IM.CtxtAgentBase import CtxtAgentBase
5960

6061

6162
class ConfManager(LoggerMixin, threading.Thread):
@@ -397,7 +398,8 @@ def launch_ctxt_agent(self, vm, tasks):
397398
vault_password = vm.info.systems[0].getValue("vault.password")
398399
if vault_password:
399400
vault_export = "export VAULT_PASS='%s' && " % vault_password
400-
(pid, _, _) = ssh.execute("nohup sh -c \"" + vault_export + "python3 " + Config.REMOTE_CONF_DIR +
401+
(pid, _, _) = ssh.execute("nohup sh -c \"" + vault_export + CtxtAgentBase.VENV_DIR +
402+
"/bin/python3 " + Config.REMOTE_CONF_DIR +
401403
"/" + str(self.inf.id) + "/" + ctxt_agent_command +
402404
Config.REMOTE_CONF_DIR + "/" + str(self.inf.id) + "/" +
403405
"/general_info.cfg " + remote_dir + "/" + os.path.basename(conf_file) +

IM/CtxtAgentBase.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class CtxtAgentBase:
3737
# the ConfManager
3838
PLAYBOOK_RETRIES = 1
3939
INTERNAL_PLAYBOOK_RETRIES = 1
40+
VENV_DIR = "/var/tmp/.ansible" # nosec
4041

4142
def __init__(self, conf_data_filename):
4243
self.logger = None
@@ -508,7 +509,7 @@ def install_ansible_roles(self, general_conf_data, playbook):
508509
task["name"] = "Create YAML file to install the collections with ansible-galaxy"
509510
yaml_data[0]['tasks'].append(task)
510511

511-
task = {"command": "ansible-galaxy collection install -c -r %s" % filename}
512+
task = {"command": self.VENV_DIR + "/bin/ansible-galaxy collection install -c -r %s" % filename}
512513
task["name"] = "Install galaxy collections"
513514
task["become"] = "yes"
514515
task["register"] = "collections_install"
@@ -562,7 +563,7 @@ def install_ansible_roles(self, general_conf_data, playbook):
562563
task["name"] = "Create YAML file to install the roles with ansible-galaxy"
563564
yaml_data[0]['tasks'].append(task)
564565

565-
task = {"command": "ansible-galaxy install -c -r %s" % filename}
566+
task = {"command": self.VENV_DIR + "/bin/ansible-galaxy install -c -r %s" % filename}
566567
task["name"] = "Install galaxy roles"
567568
task["become"] = "yes"
568569
task["register"] = "roles_install"

IM/connectors/OSCAR.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def _get_service_json(radl_system):
134134
if radl_system.getValue("name"):
135135
service["name"] = radl_system.getValue("name")
136136
if radl_system.getValue("memory.size"):
137-
service["memory"] = "%dMi" % radl_system.getFeature('memory.size').getValue('M')
137+
service["memory"] = "%dMi" % radl_system.getFeature('memory.size').getValue('Mi')
138138
if radl_system.getValue("cpu.count"):
139139
service["cpu"] = "%g" % radl_system.getValue("cpu.count")
140140
if radl_system.getValue("gpu.count"):
@@ -260,7 +260,7 @@ def update_system_info_from_service_info(self, system, service_info):
260260
conflict="other", missing="other")
261261
if "memory" in service_info and service_info["memory"]:
262262
memory = self.convert_memory_unit(service_info["memory"], "Mi")
263-
system.addFeature(Feature("memory.size", "=", memory, "M"),
263+
system.addFeature(Feature("memory.size", "=", memory, "Mi"),
264264
conflict="other", missing="other")
265265
if "script" in service_info and service_info["script"]:
266266
system.addFeature(Feature("script", "=", service_info["script"]),

contextualization/ansible_install.sh

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ distribution_id() {
3939
echo ${RETVAL}
4040
}
4141

42+
# Create a symbolic link to python3 in case of not venv created
43+
ls /var/tmp/.ansible/bin/ || mkdir -p /var/tmp/.ansible/bin/
44+
ls /var/tmp/.ansible/bin/python3 || ln -s /usr/bin/python3 /var/tmp/.ansible/bin/python3
45+
4246
if [ $(which ansible-playbook) ]; then
4347
echo "Ansible installed. Do not install."
4448
else

contextualization/conf-ansible.yml

+35-18
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
vars:
77
# Ansible specific Version or "latest"
88
ANSIBLE_VERSION: 4.10.0
9+
VENV_PATH: /var/tmp/.ansible
910
tasks:
1011
############## To avoid some issues with cloud-init and unattended upgrades ###############
1112
- name: Avoid unattended upgrades
@@ -99,15 +100,15 @@
99100
################### Install Ansible/pip requisites #########################
100101

101102
- name: Debian/Ubuntu install requisites with apt
102-
apt: name=python3-pip,wget,python3-setuptools,python3-psutil,sshpass,openssh-client,unzip install_recommends=no
103+
apt: name=python3-pip,wget,python3-setuptools,sshpass,openssh-client,unzip install_recommends=no
103104
when: ansible_os_family == "Debian"
104105

105106
- name: Yum install requisites RH 7/8 or Fedora
106-
command: yum install -y python3-pip python3-setuptools python3-psutil sshpass openssh-clients
107+
command: yum install -y python3-pip python3-setuptools sshpass openssh-clients
107108
when: ansible_os_family == "RedHat"
108109

109110
- name: Zypper install requirements Suse
110-
zypper: name=python3-pip,python3-setuptools,python3-psutil,wget,python3-cryptography state=present
111+
zypper: name=python3-pip,python3-setuptools,wget,python3-cryptography state=present
111112
when: ansible_os_family == "Suse"
112113

113114
- name: Install python-setuptools
@@ -116,49 +117,65 @@
116117

117118
######################################### Use pip to enable to set the version #############################################
118119

120+
- name: Set extra_args var
121+
set_fact:
122+
extra_args: ''
123+
124+
- name: Set extra_args var in py3.11
125+
set_fact:
126+
extra_args: --break-system-packages
127+
when: ansible_python_version is version('3.11', '>=')
128+
129+
- name: Install virtualenv with pip
130+
pip:
131+
name: virtualenv
132+
executable: pip3
133+
extra_args: "{{ extra_args }}"
134+
119135
# Version over 21 does not work with python 3.6 or older
120136
- name: Upgrade pip in py3.6-
121137
pip:
122138
name: pip>18.0,<21.0
123-
executable: pip3
139+
virtualenv: "{{ VENV_PATH }}"
124140
# in some old distros we need to trust in the pypi to avoid SSL errors
125141
extra_args: --trusted-host files.pythonhosted.org --trusted-host pypi.org --trusted-host pypi.python.org
126142
when: ansible_python_version is version('3.7', '<')
127143

128144
- name: Upgrade pip in py3.7-py3.8
129145
pip:
130146
name: pip>20.0
131-
executable: pip3
147+
virtualenv: "{{ VENV_PATH }}"
132148
when: ansible_python_version is version('3.7', '>=') and ansible_python_version is version('3.9', '<')
133149

134150
- name: Upgrade pip in py3.9-py3.10
135151
pip:
136152
name: pip>=22.0
137-
executable: pip3
153+
virtualenv: "{{ VENV_PATH }}"
138154
when: ansible_python_version is version('3.9', '>=') and ansible_python_version is version('3.11', '<')
139155

140156
# Version 66 (#2497) fails
141157
- name: Upgrade setuptools with Pip in py3.11-
142158
pip:
143159
name: setuptools<66.0.0
144-
executable: pip3
160+
virtualenv: "{{ VENV_PATH }}"
145161
when: ansible_python_version is version('3.11', '<')
146162

147163
- name: Set extra_args var
148164
set_fact:
149165
extra_args: --prefer-binary
150166

151-
- name: Set extra_args var in py3.11
152-
set_fact:
153-
extra_args: --prefer-binary --break-system-packages
154-
when: ansible_python_version is version('3.11', '>=')
167+
- name: Install psutil
168+
pip:
169+
name: psutil
170+
virtualenv: "{{ VENV_PATH }}"
171+
extra_args: "{{ extra_args }}"
155172

156173
- name: Install cryptography & pyOpenSSL in py3.11-
157174
pip:
158175
name:
159176
- cryptography>36.0.0,<39.0.0
160177
- pyOpenSSL>20.0,<22.1.0
161-
executable: pip3
178+
virtualenv: "{{ VENV_PATH }}"
162179
extra_args: "{{ extra_args }}"
163180
when: ansible_python_version is version('3.11', '<')
164181

@@ -167,7 +184,7 @@
167184
name:
168185
- cryptography>36.0.0
169186
- pyOpenSSL>20.0
170-
executable: pip3
187+
virtualenv: "{{ VENV_PATH }}"
171188
extra_args: "{{ extra_args }}"
172189
when: ansible_python_version is version('3.11', '>=')
173190

@@ -178,7 +195,7 @@
178195
- pyyaml
179196
- paramiko>=2.9.5
180197
- packaging
181-
executable: pip3
198+
virtualenv: "{{ VENV_PATH }}"
182199
extra_args: "{{ extra_args }}"
183200

184201
- name: Set Ansible newer version for python 3.8+
@@ -201,14 +218,14 @@
201218
pip:
202219
name: ansible
203220
version: "{{ ANSIBLE_VERSION }}"
204-
executable: pip3
221+
virtualenv: "{{ VENV_PATH }}"
205222
extra_args: "{{ extra_args }}"
206223
when: ANSIBLE_VERSION != "latest"
207224

208225
- name: Install latest ansible version with Pip
209226
pip:
210227
name: ansible
211-
executable: pip3
228+
virtualenv: "{{ VENV_PATH }}"
212229
extra_args: "{{ extra_args }}"
213230
when: ANSIBLE_VERSION == "latest"
214231

@@ -219,13 +236,13 @@
219236
name:
220237
- jmespath
221238
- scp
222-
executable: pip3
239+
virtualenv: "{{ VENV_PATH }}"
223240
extra_args: "{{ extra_args }}"
224241

225242
- name: Install pywinrm with Pip
226243
pip:
227244
name: pywinrm
228-
executable: pip3
245+
virtualenv: "{{ VENV_PATH }}"
229246
extra_args: "{{ extra_args }}"
230247
ignore_errors: yes
231248

contextualization/ctxt_agent_dist.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ def LaunchRemoteAgent(self, vm, vault_pass, pk_file, changed_pass_ok):
162162
vm_dir = os.path.abspath(os.path.dirname(self.vm_conf_data_filename))
163163
remote_dir = os.path.abspath(os.path.dirname(self.conf_data_filename))
164164
try:
165-
(pid, _, _) = ssh_client.execute(vault_export + "nohup python3 " + remote_dir + "/ctxt_agent_dist.py " +
165+
(pid, _, _) = ssh_client.execute(vault_export + "nohup " + CtxtAgentBase.VENV_DIR + "/bin/python3 " +
166+
remote_dir + "/ctxt_agent_dist.py " +
166167
self.conf_data_filename + " " + self.vm_conf_data_filename +
167168
" 1 > " + vm_dir + "/stdout 2> " + vm_dir +
168169
"/stderr < /dev/null & echo -n $!")

requirements-tests.txt

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
ansible >= 2.4
2-
ansible-base
1+
ansible == 8.7.0
32
paramiko >= 1.14
43
PyYAML
54
cheroot
65
boto3
76
apache-libcloud >= 3.3.1
8-
RADL >= 1.3.3
7+
RADL >= 1.3.4
98
flask
109
werkzeug
1110
netaddr

test/integration/TestIM.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def test_11_create(self):
154154
self.__class__.inf_id = inf_id
155155

156156
all_configured = self.wait_inf_state(
157-
inf_id, VirtualMachine.CONFIGURED, 2400)
157+
inf_id, VirtualMachine.CONFIGURED, 2700)
158158
self.assertTrue(
159159
all_configured, msg="ERROR waiting the infrastructure to be configured (timeout).")
160160

test/unit/REST.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ def test_CreateInfrastructure(self, get_infrastructure, CreateInfrastructure):
228228
data=read_file_as_bytes("../files/test_simple.json"))
229229
self.assertEqual(res.json, {"one": {"cloudType": "OpenNebula",
230230
"cloudEndpoint": "http://ramses.i3m.upv.es:2633",
231-
"compute": [{"cpuCores": 1, "memoryInMegabytes": 1024},
232-
{"cpuCores": 1, "memoryInMegabytes": 1024}], "storage": []}})
231+
"compute": [{"cpuCores": 1, "memoryInMegabytes": 1074},
232+
{"cpuCores": 1, "memoryInMegabytes": 1074}], "storage": []}})
233233

234234
headers["Content-Type"] = "application/json"
235235
CreateInfrastructure.side_effect = InvaliddUserException()

test/unit/connectors/Fogbow.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def test_30_updateVMInfo(self, sleep, requests):
300300
self.assertNotIn("ERROR", self.log.getvalue(), msg="ERROR found in log: %s" % self.log.getvalue())
301301
self.assertEqual(vm.info.systems[0].getValue("net_interface.1.ip"), "10.0.0.1")
302302
self.assertEqual(vm.info.systems[0].getValue("net_interface.0.ip"), "8.8.8.8")
303-
self.assertEqual(vm.info.systems[0].getValue("memory.size"), 1073741824)
303+
self.assertEqual(vm.info.systems[0].getValue("memory.size"), 1024000000)
304304
self.assertEqual(vm.info.systems[0].getValue("disk.1.device"), "/dev/sdb")
305305

306306
data = json.loads(requests.call_args_list[1][1]["data"])

test/unit/connectors/GCE.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def test_10_concrete(self, get_driver):
129129

130130
concrete = gce_cloud.concreteSystem(radl_system, auth)
131131
self.assertEqual(len(concrete), 1)
132-
self.assertEqual(concrete[0].getValue("memory.size"), 2147483648)
132+
self.assertEqual(concrete[0].getValue("memory.size"), 2048000000)
133133
self.assertNotIn("ERROR", self.log.getvalue(), msg="ERROR found in log: %s" % self.log.getvalue())
134134

135135
@patch('libcloud.compute.drivers.gce.GCENodeDriver')

test/unit/connectors/Kubernetes.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def test_20_launch(self, save_data, requests):
188188
'labels': {'name': 'test-1'}},
189189
"spec": {
190190
"accessModes": ["ReadWriteOnce"],
191-
"resources": {"requests": {"storage": 10737418240}},
191+
"resources": {"requests": {"storage": 10000000000}},
192192
},
193193
}
194194
self.assertEqual(requests.call_args_list[1][0][1],
@@ -237,8 +237,8 @@ def test_20_launch(self, save_data, requests):
237237
"imagePullPolicy": "Always",
238238
"ports": [{"containerPort": 8080, "protocol": "TCP"}],
239239
"resources": {
240-
"limits": {"cpu": "1", "memory": "536870912"},
241-
"requests": {"cpu": "1", "memory": "536870912"},
240+
"limits": {"cpu": "1", "memory": "512000000"},
241+
"requests": {"cpu": "1", "memory": "512000000"},
242242
},
243243
"env": [{"name": "var", "value": "some_val"},
244244
{"name": "var2", "value": "some,val2"}],

test/unit/connectors/OCCI.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ def test_30_updateVMInfo(self, get_keystone_uri, requests):
327327
self.assertNotIn("ERROR", self.log.getvalue(), msg="ERROR found in log: %s" % self.log.getvalue())
328328

329329
memory = vm.info.systems[0].getValue("memory.size")
330-
self.assertEqual(memory, 1824522240)
330+
self.assertEqual(memory, 1740000000)
331331

332332
@patch('requests.request')
333333
@patch('IM.connectors.OCCI.KeyStoneAuth.get_keystone_uri')

test/unit/connectors/OSCAR.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def test_20_launch(self, save_data, requests):
124124
radl_data = """
125125
system test (
126126
name = 'plants' and
127-
memory.size = 2G and
127+
memory.size = 2GI and
128128
cpu.count = 1.0 and
129129
cpu.sgx = 1 and
130130
gpu.count = 1 and
@@ -288,7 +288,7 @@ def test_55_alter(self, requests):
288288
new_radl_data = """
289289
system test (
290290
cpu.count>=2 and
291-
memory.size>=4G
291+
memory.size>=4GI
292292
)"""
293293
new_radl = radl_parse.parse_radl(new_radl_data)
294294

@@ -306,7 +306,7 @@ def test_55_alter(self, requests):
306306
self.assertTrue(success, msg="ERROR: modifying VM info.")
307307
self.assertNotIn("ERROR", self.log.getvalue(), msg="ERROR found in log: %s" % self.log.getvalue())
308308
self.assertEqual(new_vm.info.systems[0].getValue("cpu.count"), 2)
309-
self.assertEqual(new_vm.info.systems[0].getFeature("memory.size").getValue("M"), 4096)
309+
self.assertEqual(new_vm.info.systems[0].getFeature("memory.size").getValue("M"), 4295)
310310
self.assertEqual(requests.call_args_list[0][0][0], "PUT")
311311
self.assertEqual(requests.call_args_list[0][0][1], "http://oscar.com:80/system/services/fname")
312312
self.assertEqual(json.loads(requests.call_args_list[0][1]['data']), {'memory': '4096Mi', 'cpu': '2'})

test/unit/connectors/OpenNebula.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def test_20_launch(self, save_data, getONEVersion, server_proxy):
144144
OS = [ ARCH = "x86_64" ]
145145
146146
DISK = [ IMAGE_ID = "1" ]
147-
DISK = [ SAVE = no, TYPE = fs , FORMAT = qcow2, SIZE = 1024, TARGET = hdb ]
147+
DISK = [ SAVE = no, TYPE = fs , FORMAT = qcow2, SIZE = 1000, TARGET = hdb ]
148148
149149
150150
SCHED_REQUIREMENTS = "CLUSTER_ID=\\"0\\""\n"""

test/unit/test_ctxt_agent.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,8 @@ def test_95_install_ansible_roles(self):
277277
copy_content = yaml_data[0]['tasks'][1]['copy'][pos + 9:-2]
278278
self.assertEqual(copy_content, "[{src: ansible_role}, {name: hadoop, src: "
279279
"'git+https://github.com/micafer/ansible-role-hadoop'}]")
280-
self.assertEqual(yaml_data[0]['tasks'][2]['command'][:47], "ansible-galaxy install -c -r /tmp/galaxy_roles_")
280+
self.assertEqual(yaml_data[0]['tasks'][2]['command'][:69],
281+
ctxt_agent.VENV_DIR + "/bin/ansible-galaxy install -c -r /tmp/galaxy_roles_")
281282

282283
os.unlink(res)
283284

@@ -300,8 +301,8 @@ def test_99_install_ansible_collections(self):
300301
pos = yaml_data[0]['tasks'][0]['copy'].find('content="')
301302
copy_content = yaml_data[0]['tasks'][0]['copy'][pos + 9:-2]
302303
self.assertEqual(copy_content, "{collections: [{name: ns.collection, version: '1.0'}]}")
303-
self.assertEqual(yaml_data[0]['tasks'][1]['command'][:64],
304-
"ansible-galaxy collection install -c -r /tmp/galaxy_collections_")
304+
self.assertEqual(yaml_data[0]['tasks'][1]['command'][:86],
305+
ctxt_agent.VENV_DIR + "/bin/ansible-galaxy collection install -c -r /tmp/galaxy_collections_")
305306

306307
os.unlink(res)
307308

test/unit/test_im_logic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1552,8 +1552,8 @@ def test_estimate_resources(self):
15521552
'cloud0': {
15531553
'cloudType': 'Dummy',
15541554
'cloudEndpoint': 'http://server.com:80/path',
1555-
'compute': [{'cpuCores': 2, 'memoryInMegabytes': 4096, 'diskSizeInGigabytes': 40},
1556-
{'cpuCores': 1, 'memoryInMegabytes': 2048, 'diskSizeInGigabytes': 10}],
1555+
'compute': [{'cpuCores': 2, 'memoryInMegabytes': 4000, 'diskSizeInGigabytes': 40},
1556+
{'cpuCores': 1, 'memoryInMegabytes': 2000, 'diskSizeInGigabytes': 10}],
15571557
'storage': [{'sizeInGigabytes': 100}]
15581558
}})
15591559

0 commit comments

Comments
 (0)