14
14
# limitations under the License.
15
15
16
16
import sys
17
+ from os import listdir
17
18
18
19
from oslo_config import cfg
19
20
20
21
from st2common import config
21
22
from st2common import log as logging
22
23
from st2common .config import do_register_cli_opts
23
24
from st2common .script_setup import setup as common_setup
25
+ from st2common .util .pack import get_pack_metadata
24
26
from st2common .util .pack_management import download_pack
25
27
from st2common .util .pack_management import get_and_set_proxy_config
26
28
from st2common .util .virtualenvs import setup_pack_virtualenv
29
+ from st2common .content .utils import get_pack_base_path , get_packs_base_paths
27
30
28
31
__all__ = ["main" ]
29
32
@@ -53,10 +56,77 @@ def _register_cli_opts():
53
56
help = "True to force pack installation and ignore install "
54
57
"lock file if it exists." ,
55
58
),
59
+ cfg .BoolOpt (
60
+ "get-dependencies" ,
61
+ default = False ,
62
+ help = "True to install pack dependencies" ,
63
+ ),
56
64
]
57
65
do_register_cli_opts (cli_opts )
58
66
59
67
68
+ def get_pack_dependencies (pack , verify_ssl , force , dependencies , proxy_config ):
69
+ pack_path = get_pack_base_path (pack )
70
+
71
+ try :
72
+ pack_metadata = get_pack_metadata (pack_dir = pack_path )
73
+ result = pack_metadata .get ("dependencies" , None )
74
+ if result :
75
+ LOG .info ('Getting pack dependencies for pack "%s"' % (pack ))
76
+ download_packs (result , verify_ssl , force , dependencies , proxy_config )
77
+ LOG .info ('Successfully got pack dependencies for pack "%s"' % (pack ))
78
+ except IOError :
79
+ LOG .error ("Could not open pack.yaml at location %s" % pack_path )
80
+ result = None
81
+
82
+
83
+ def download_packs (packs , verify_ssl , force , dependencies , proxy_config ):
84
+ packs_base_paths = get_packs_base_paths ()
85
+
86
+ for pack in packs :
87
+ for pack_dir in packs_base_paths :
88
+ if pack in listdir (pack_dir ):
89
+ LOG .info ('Pack (%s) already installed in "%s"' % (pack , pack_dir ))
90
+ break
91
+ else :
92
+ # 1. Download the pack
93
+ LOG .info ('Installing pack "%s"' % (pack ))
94
+ result = download_pack (
95
+ pack = pack ,
96
+ verify_ssl = verify_ssl ,
97
+ force = force ,
98
+ proxy_config = proxy_config ,
99
+ force_permissions = True ,
100
+ )
101
+
102
+ # Raw pack name excluding the version
103
+ pack_name = result [1 ]
104
+ success = result [2 ][0 ]
105
+
106
+ if success :
107
+ LOG .info ('Successfully installed pack "%s"' % (pack_name ))
108
+ else :
109
+ error = result [2 ][1 ]
110
+ LOG .error ('Failed to install pack "%s": %s' % (pack_name , error ))
111
+ sys .exit (2 )
112
+
113
+ # 2. Setup pack virtual environment
114
+ LOG .info ('Setting up virtualenv for pack "%s"' % (pack_name ))
115
+ setup_pack_virtualenv (
116
+ pack_name = pack_name ,
117
+ update = False ,
118
+ logger = LOG ,
119
+ proxy_config = proxy_config ,
120
+ no_download = True ,
121
+ )
122
+ LOG .info ('Successfully set up virtualenv for pack "%s"' % (pack_name ))
123
+
124
+ if dependencies :
125
+ get_pack_dependencies (
126
+ pack_name , verify_ssl , force , dependencies , proxy_config
127
+ )
128
+
129
+
60
130
def main (argv ):
61
131
_register_cli_opts ()
62
132
@@ -71,40 +141,10 @@ def main(argv):
71
141
packs = cfg .CONF .pack
72
142
verify_ssl = cfg .CONF .verify_ssl
73
143
force = cfg .CONF .force
144
+ dependencies = cfg .CONF .get_dependencies
74
145
75
146
proxy_config = get_and_set_proxy_config ()
76
147
77
- for pack in packs :
78
- # 1. Download the pack
79
- LOG .info ('Installing pack "%s"' % (pack ))
80
- result = download_pack (
81
- pack = pack ,
82
- verify_ssl = verify_ssl ,
83
- force = force ,
84
- proxy_config = proxy_config ,
85
- force_permissions = True ,
86
- )
87
-
88
- # Raw pack name excluding the version
89
- pack_name = result [1 ]
90
- success = result [2 ][0 ]
91
-
92
- if success :
93
- LOG .info ('Successfully installed pack "%s"' % (pack_name ))
94
- else :
95
- error = result [2 ][1 ]
96
- LOG .error ('Failed to install pack "%s": %s' % (pack_name , error ))
97
- sys .exit (2 )
98
-
99
- # 2. Setup pack virtual environment
100
- LOG .info ('Setting up virtualenv for pack "%s"' % (pack_name ))
101
- setup_pack_virtualenv (
102
- pack_name = pack_name ,
103
- update = False ,
104
- logger = LOG ,
105
- proxy_config = proxy_config ,
106
- no_download = True ,
107
- )
108
- LOG .info ('Successfully set up virtualenv for pack "%s"' % (pack_name ))
148
+ download_packs (packs , verify_ssl , force , dependencies , proxy_config )
109
149
110
150
return 0
0 commit comments