2020#include "opal/util/argv.h"
2121#include "opal/util/basename.h"
2222#include "opal/util/opal_environ.h"
23+ #include "opal/util/os_dirpath.h"
24+ #include "opal/util/path.h"
2325
2426#include "orte/runtime/orte_globals.h"
2527#include "orte/util/name_fns.h"
2628#include "orte/mca/schizo/base/base.h"
2729
2830#include "schizo_singularity.h"
2931
32+ static int setup_app (char * * personality ,
33+ orte_app_context_t * context );
3034static int setup_fork (orte_job_t * jdata ,
3135 orte_app_context_t * context );
3236
3337orte_schizo_base_module_t orte_schizo_singularity_module = {
38+ .setup_app = setup_app ,
3439 .setup_fork = setup_fork
3540};
3641
37- static int setup_fork ( orte_job_t * jdata ,
38- orte_app_context_t * app )
42+ static int setup_app ( char * * personality ,
43+ orte_app_context_t * app )
3944{
4045 int i ;
41- char * newenv ;
46+ char * newenv , * pth ;
4247 bool takeus = false;
43- char * cmd , * tmp = NULL , * p , * t2 ;
48+ char * p , * t2 ;
4449
4550 /* see if we are included */
46- for (i = 0 ; NULL != jdata -> personality [i ]; i ++ ) {
47- if (0 == strcmp (jdata -> personality [i ], "singularity" )) {
51+ for (i = 0 ; NULL != personality [i ]; i ++ ) {
52+ if (0 == strcmp (personality [i ], "singularity" )) {
4853 takeus = true;
4954 break ;
5055 }
@@ -64,55 +69,104 @@ static int setup_fork(orte_job_t *jdata,
6469 "%s schizo:singularity: checking app %s" ,
6570 ORTE_NAME_PRINT (ORTE_PROC_MY_NAME ), app -> argv [0 ]);
6671
72+ if (0 < strlen (OPAL_SINGULARITY_PATH )) {
73+ asprintf (& pth , "%s/singularity" , OPAL_SINGULARITY_PATH );
74+ } else {
75+ /* since we allow for detecting singularity's presence, it
76+ * is possible that we found it in the PATH, but not in a
77+ * standard location. Check for that here */
78+ pth = opal_path_findv ("singularity" , X_OK , app -> env , NULL );
79+ if (NULL == pth ) {
80+ /* cannot execute */
81+ return ORTE_ERR_TAKE_NEXT_OPTION ;
82+ }
83+ }
6784 /* find the path and prepend it with the path to Singularity */
6885 for (i = 0 ; NULL != app -> env && NULL != app -> env [i ]; ++ i ) {
6986 /* add to PATH */
7087 if (0 == strncmp ("PATH=" , app -> env [i ], 5 )) {
71- asprintf (& newenv , "%s:%s" , OPAL_SINGULARITY_PATH , app -> env [i ] + 5 );
88+ t2 = opal_dirname (pth );
89+ asprintf (& newenv , "%s:%s" , t2 , app -> env [i ] + 5 );
7290 opal_setenv ("PATH" , newenv , true, & app -> env );
7391 free (newenv );
92+ free (t2 );
7493 break ;
7594 }
7695 }
7796
78- /* flag that the app is in a container */
79- opal_setenv ("OPAL_PROC_CONTAINER" , "1" , true, & app -> env );
80-
8197 /* ensure that we use "singularity run" to execute this app */
8298 if (0 != strcmp (app -> app , "singularity" )) {
8399 opal_output_verbose (1 , orte_schizo_base_framework .framework_output ,
84- "%s schizo:singularity: adding singularity cmds at %s" ,
85- ORTE_NAME_PRINT (ORTE_PROC_MY_NAME ), OPAL_SINGULARITY_PATH );
100+ "%s schizo:singularity: adding singularity cmd %s" ,
101+ ORTE_NAME_PRINT (ORTE_PROC_MY_NAME ), pth );
86102 /* change the app to the "singularity" command */
87103 free (app -> app );
88- if (0 < strlen (OPAL_SINGULARITY_PATH )) {
89- asprintf (& app -> app , "%s/singularity" , OPAL_SINGULARITY_PATH );
90- } else {
91- app -> app = strdup ("singularity" );
92- }
93- /* if the app contains .sapp, then we need to strip that
94- * extension so singularity doesn't bark at us */
95- if (NULL != (p = strstr (app -> argv [0 ], ".sapp" ))) {
96- tmp = strdup (app -> argv [0 ]);
97- t2 = opal_basename (app -> argv [0 ]);
98- p = strstr (t2 , ".sapp" );
99- * p = '\0' ; // strip the extension
100- free (app -> argv [0 ]);
101- app -> argv [0 ] = t2 ;
104+ app -> app = pth ;
105+ } else {
106+ free (pth );
107+ }
108+
109+ /* if the app contains .sapp, then we need to strip that
110+ * extension so singularity doesn't bark at us */
111+ if (NULL != (p = strstr (app -> argv [0 ], ".sapp" ))) {
112+ t2 = opal_basename (app -> argv [0 ]);
113+ p = strstr (t2 , ".sapp" );
114+ * p = '\0' ; // strip the extension
115+ free (app -> argv [0 ]);
116+ app -> argv [0 ] = t2 ;
117+ }
118+ opal_argv_prepend_nosize (& app -> argv , "run" );
119+ opal_argv_prepend_nosize (& app -> argv , "singularity" );
120+
121+ /* export an envar to permit shared memory operations */
122+ opal_setenv ("SINGULARITY_NO_NAMESPACE_PID" , "1" , true, & app -> env );
123+
124+ return ORTE_SUCCESS ;
125+ }
126+
127+ static int setup_fork (orte_job_t * jdata ,
128+ orte_app_context_t * app )
129+ {
130+ int i ;
131+ bool takeus = false;
132+ char * p ;
133+ char dir [MAXPATHLEN ];
134+
135+ /* see if we are included */
136+ for (i = 0 ; NULL != jdata -> personality [i ]; i ++ ) {
137+ if (0 == strcmp (jdata -> personality [i ], "singularity" )) {
138+ takeus = true;
139+ break ;
102140 }
103- opal_argv_prepend_nosize (& app -> argv , "run" );
104- opal_argv_prepend_nosize (& app -> argv , "singularity" );
105141 }
106- /* ensure this application has been "installed" */
107- if ( NULL != tmp ) {
108- opal_output_verbose ( 1 , orte_schizo_base_framework . framework_output ,
109- "%s schizo: singularity: installing container %s" ,
110- ORTE_NAME_PRINT ( ORTE_PROC_MY_NAME ), tmp );
111- ( void ) asprintf ( & cmd , "singularity install %s >> /dev/null" , tmp );
112- system ( cmd );
113- free ( cmd ) ;
114- free ( tmp );
142+ if (! takeus ) {
143+ /* even if they didn't specify, check to see if
144+ * this involves a singularity container */
145+ if ( 0 != strcmp ( app -> argv [ 0 ], " singularity" ) &&
146+ 0 != strcmp ( app -> argv [ 0 ], "sapprun" ) &&
147+ NULL == strstr ( app -> argv [ 0 ], ".sapp" )) {
148+ /* guess not! */
149+ return ORTE_ERR_TAKE_NEXT_OPTION ;
150+ }
115151 }
116152
153+ /* save our current directory */
154+ getcwd (dir , sizeof (dir ));
155+
156+ /* change to the working directory for this context */
157+ chdir (app -> cwd );
158+
159+ /* ensure the app is installed */
160+ opal_output_verbose (1 , orte_schizo_base_framework .framework_output ,
161+ "%s schizo:singularity: installing app %s" ,
162+ ORTE_NAME_PRINT (ORTE_PROC_MY_NAME ), app -> argv [2 ]);
163+ (void )asprintf (& p , "%s install %s.sapp &> /dev/null" , app -> app , app -> argv [2 ]);
164+ system (p );
165+ free (p );
166+
167+ /* return to the original directory */
168+ chdir (dir );
169+
117170 return ORTE_SUCCESS ;
118171}
172+
0 commit comments