Test Failed
Pull Request — master (#4194)
by Arma
04:07
created

UninstallPackAction.run()   B

Complexity

Conditions 7

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
dl 0
loc 22
rs 7.952
c 0
b 0
f 0
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
import os
17
import shutil
18
19
from oslo_config import cfg
20
21
from st2common.runners.base_action import Action
22
from st2common.constants.pack import SYSTEM_PACK_NAMES
23
from st2common.util.shell import quote_unix
24
25
BLOCKED_PACKS = frozenset(SYSTEM_PACK_NAMES)
26
27
28
class UninstallPackAction(Action):
29
    def __init__(self, config=None, action_service=None):
30
        super(UninstallPackAction, self).__init__(config=config, action_service=action_service)
31
        self._base_virtualenvs_path = os.path.join(cfg.CONF.system.base_path,
32
                                                   'virtualenvs/')
33
34
    def run(self, packs, abs_repo_base):
35
        intersection = BLOCKED_PACKS & frozenset(packs)
36
        if len(intersection) > 0:
37
            names = ', '.join(list(intersection))
38
            raise ValueError('Uninstall includes an uninstallable pack - %s.' % (names))
39
40
        # 1. Delete pack content
41
        for fp in os.listdir(abs_repo_base):
42
            abs_fp = os.path.join(abs_repo_base, fp)
43
            if fp in packs and os.path.isdir(abs_fp):
44
                self.logger.debug('Deleting pack directory "%s"' % (abs_fp))
45
                shutil.rmtree(abs_fp)
46
47
        # 2. Delete pack virtual environment
48
        for pack_name in packs:
49
            pack_name = quote_unix(pack_name)
50
            virtualenv_path = os.path.join(self._base_virtualenvs_path, pack_name)
51
52
            if os.path.isdir(virtualenv_path):
53
                self.logger.debug('Deleting virtualenv "%s" for pack "%s"' %
54
                                  (virtualenv_path, pack_name))
55
                shutil.rmtree(virtualenv_path)
56