20
20
#include "opal/util/argv.h"
21
21
#include "opal/util/basename.h"
22
22
#include "opal/util/opal_environ.h"
23
+ #include "opal/util/os_dirpath.h"
24
+ #include "opal/util/path.h"
23
25
24
26
#include "orte/runtime/orte_globals.h"
25
27
#include "orte/util/name_fns.h"
26
28
#include "orte/mca/schizo/base/base.h"
27
29
28
30
#include "schizo_singularity.h"
29
31
32
+ static int setup_app (char * * personality ,
33
+ orte_app_context_t * context );
30
34
static int setup_fork (orte_job_t * jdata ,
31
35
orte_app_context_t * context );
32
36
33
37
orte_schizo_base_module_t orte_schizo_singularity_module = {
38
+ .setup_app = setup_app ,
34
39
.setup_fork = setup_fork
35
40
};
36
41
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 )
39
44
{
40
45
int i ;
41
- char * newenv ;
46
+ char * newenv , * pth ;
42
47
bool takeus = false;
43
- char * cmd , * tmp = NULL , * p , * t2 ;
48
+ char * p , * t2 ;
44
49
45
50
/* 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" )) {
48
53
takeus = true;
49
54
break ;
50
55
}
@@ -64,55 +69,104 @@ static int setup_fork(orte_job_t *jdata,
64
69
"%s schizo:singularity: checking app %s" ,
65
70
ORTE_NAME_PRINT (ORTE_PROC_MY_NAME ), app -> argv [0 ]);
66
71
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
+ }
67
84
/* find the path and prepend it with the path to Singularity */
68
85
for (i = 0 ; NULL != app -> env && NULL != app -> env [i ]; ++ i ) {
69
86
/* add to PATH */
70
87
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 );
72
90
opal_setenv ("PATH" , newenv , true, & app -> env );
73
91
free (newenv );
92
+ free (t2 );
74
93
break ;
75
94
}
76
95
}
77
96
78
- /* flag that the app is in a container */
79
- opal_setenv ("OPAL_PROC_CONTAINER" , "1" , true, & app -> env );
80
-
81
97
/* ensure that we use "singularity run" to execute this app */
82
98
if (0 != strcmp (app -> app , "singularity" )) {
83
99
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 );
86
102
/* change the app to the "singularity" command */
87
103
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 ;
102
140
}
103
- opal_argv_prepend_nosize (& app -> argv , "run" );
104
- opal_argv_prepend_nosize (& app -> argv , "singularity" );
105
141
}
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
+ }
115
151
}
116
152
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
+
117
170
return ORTE_SUCCESS ;
118
171
}
172
+
0 commit comments