Passed
Pull Request — master (#3556)
by Lakshmi
05:19 queued 45s
created

SetupVirtualEnvironmentAction   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 16 2
F __init__() 0 34 11
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
18
from st2common.runners.base_action import Action
19
from st2common.util.virtualenvs import setup_pack_virtualenv
20
21
__all__ = [
22
    'SetupVirtualEnvironmentAction'
23
]
24
25
26
class SetupVirtualEnvironmentAction(Action):
27
    """
28
    Action which sets up virtual environment for the provided packs.
29
30
    Setup consists of the following step:
31
32
    1. Create virtual environment for the pack
33
    2. Install base requirements which are common to all the packs
34
    3. Install pack-specific requirements (if any)
35
36
    If the 'update' parameter is set to True, the setup skips the deletion and
37
    creation of the virtual environment and performs an update of the
38
    current dependencies as well as an installation of new dependencies
39
    """
40
    def __init__(self, config=None, action_service=None):
41
        super(SetupVirtualEnvironmentAction, self).__init__(
42
            config=config,
43
            action_service=action_service)
44
45
        self.https_proxy = os.environ.get('https_proxy', self.config.get('https_proxy', None))
46
        self.http_proxy = os.environ.get('http_proxy', self.config.get('http_proxy', None))
47
        self.proxy_ca_bundle_path = os.environ.get(
48
            'proxy_ca_bundle_path',
49
            self.config.get('proxy_ca_bundle_path', None)
50
        )
51
        self.no_proxy = os.environ.get('no_proxy', self.config.get('no_proxy', None))
52
53
        self.proxy_config = None
54
55
        if self.http_proxy or self.https_proxy:
56
            self.proxy_config = {
57
                'https_proxy': self.https_proxy,
58
                'http_proxy': self.http_proxy,
59
                'proxy_ca_bundle_path': self.proxy_ca_bundle_path,
60
                'no_proxy': self.no_proxy
61
            }
62
63
        if self.https_proxy and not os.environ.get('https_proxy', None):
64
            os.environ['https_proxy'] = self.https_proxy
65
66
        if self.http_proxy and not os.environ.get('http_proxy', None):
67
            os.environ['http_proxy'] = self.http_proxy
68
69
        if self.no_proxy and not os.environ.get('no_proxy', None):
70
            os.environ['no_proxy'] = self.no_proxy
71
72
        if self.proxy_ca_bundle_path and not os.environ.get('proxy_ca_bundle_path', None):
73
            os.environ['no_proxy'] = self.no_proxy
74
75
    def run(self, packs, update=False):
76
        """
77
        :param packs: A list of packs to create the environment for.
78
        :type: packs: ``list``
79
80
        :param update: True to update dependencies inside the virtual environment.
81
        :type update: ``bool``
82
        """
83
84
        for pack_name in packs:
85
            setup_pack_virtualenv(pack_name=pack_name, update=update, logger=self.logger,
86
                                  proxy_config=self.proxy_config)
87
88
        message = ('Successfuly set up virtualenv for the following packs: %s' %
89
                   (', '.join(packs)))
90
        return message
91