1414# limitations under the License.
1515
1616import sys
17+ from os import listdir
1718
1819from oslo_config import cfg
1920
2021from st2common import config
2122from st2common import log as logging
2223from st2common .config import do_register_cli_opts
2324from st2common .script_setup import setup as common_setup
25+ from st2common .util .pack import get_pack_metadata
2426from st2common .util .pack_management import download_pack
2527from st2common .util .pack_management import get_and_set_proxy_config
2628from st2common .util .virtualenvs import setup_pack_virtualenv
29+ from st2common .content .utils import get_pack_base_path , get_packs_base_paths
2730
2831__all__ = ["main" ]
2932
@@ -53,10 +56,77 @@ def _register_cli_opts():
5356 help = "True to force pack installation and ignore install "
5457 "lock file if it exists." ,
5558 ),
59+ cfg .BoolOpt (
60+ "get-dependencies" ,
61+ default = False ,
62+ help = "True to install pack dependencies" ,
63+ ),
5664 ]
5765 do_register_cli_opts (cli_opts )
5866
5967
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+
60130def main (argv ):
61131 _register_cli_opts ()
62132
@@ -71,40 +141,10 @@ def main(argv):
71141 packs = cfg .CONF .pack
72142 verify_ssl = cfg .CONF .verify_ssl
73143 force = cfg .CONF .force
144+ dependencies = cfg .CONF .get_dependencies
74145
75146 proxy_config = get_and_set_proxy_config ()
76147
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 )
109149
110150 return 0
0 commit comments