@@ -394,9 +394,8 @@ def main_init(name, args):
394394 )
395395 return (None , [])
396396 except sources .DataSourceNotFoundException :
397- # In the case of 'cloud-init init' without '--local' it is a bit
398- # more likely that the user would consider it failure if nothing was
399- # found.
397+ # In the case of 'cloud-init --stage=init' it is a bit more likely that
398+ # the user would consider it failure if nothing was found.
400399 if mode == sources .DSMODE_LOCAL :
401400 LOG .debug ("No local datasource found" )
402401 else :
@@ -845,6 +844,12 @@ def _maybe_set_hostname(init, stage, retry_stage):
845844def main_features (name , args ):
846845 sys .stdout .write ("\n " .join (sorted (version .FEATURES )) + "\n " )
847846
847+ STAGE_FROM_NAME = {
848+ "local" : main_init ,
849+ "network" : main_init ,
850+ "config" : main_modules ,
851+ "final" : main_modules ,
852+ }
848853
849854def main (sysv_args = None ):
850855 configure_root_logger ()
@@ -885,40 +890,18 @@ def main(sysv_args=None):
885890 dest = "force" ,
886891 default = False ,
887892 )
893+ parser .add_argument (
894+ "--stage" ,
895+ action = "store" ,
896+ help = "Cloud-init stage to start" ,
897+ choices = ("init" , "network" , "config" , "final" ),
898+ default = None ,
899+ )
888900
889901 parser .set_defaults (reporter = None )
890902 subparsers = parser .add_subparsers (title = "Subcommands" , dest = "subcommand" )
891903 subparsers .required = True
892904
893- # Each action and its sub-options (if any)
894- parser_init = subparsers .add_parser (
895- "init" , help = "Initialize cloud-init and perform initial modules."
896- )
897- parser_init .add_argument (
898- "--local" ,
899- "-l" ,
900- action = "store_true" ,
901- help = "Start in local mode (default: %(default)s)." ,
902- default = False ,
903- )
904- # This is used so that we can know which action is selected +
905- # the functor to use to run this subcommand
906- parser_init .set_defaults (action = ("init" , main_init ))
907-
908- # These settings are used for the 'config' and 'final' stages
909- parser_mod = subparsers .add_parser (
910- "modules" , help = "Activate modules using a given configuration key."
911- )
912- parser_mod .add_argument (
913- "--mode" ,
914- "-m" ,
915- action = "store" ,
916- help = "Module configuration name to use (default: %(default)s)." ,
917- default = "config" ,
918- choices = ("init" , "config" , "final" ),
919- )
920- parser_mod .set_defaults (action = ("modules" , main_modules ))
921-
922905 # This subcommand allows you to run a single module
923906 parser_single = subparsers .add_parser (
924907 "single" , help = "Run a single module."
@@ -1042,9 +1025,7 @@ def main(sysv_args=None):
10421025 parser_status .set_defaults (action = ("status" , handle_status_args ))
10431026
10441027 args = parser .parse_args (args = sysv_args )
1045-
1046- # Subparsers.required = True and each subparser sets action=(name, functor)
1047- (name , functor ) = args .action
1028+ name = args .stage
10481029
10491030 # Setup basic logging to start (until reinitialized)
10501031 # iff in debug mode.
@@ -1053,23 +1034,10 @@ def main(sysv_args=None):
10531034
10541035 # Setup signal handlers before running
10551036 signal_handler .attach_handlers ()
1056-
1057- # Write boot stage data to write status.json and result.json
1058- # Exclude modules --mode=init, since it is not a real boot stage and
1059- # should not be written into status.json
1060- if "init" == name or ("modules" == name and "init" != args .mode ):
1061- functor = status_wrapper
1062-
1063- rname = None
10641037 report_on = True
1065- if name == "init" :
1066- if args .local :
1067- rname , rdesc = ("init-local" , "searching for local datasources" )
1068- else :
1069- rname , rdesc = (
1070- "init-network" ,
1071- "searching for network datasources" ,
1072- )
1038+ if name in ["local" , "network" , "config" , "final" ]:
1039+ rname , rdesc = name , f"running in stage { name } "
1040+ args .action = (name , STAGE_FROM_NAME [args .stage ])
10731041 elif name == "modules" :
10741042 rname , rdesc = (
10751043 "modules-%s" % args .mode ,
@@ -1095,7 +1063,7 @@ def main(sysv_args=None):
10951063 logfunc = LOG .debug ,
10961064 msg = "cloud-init mode '%s'" % name ,
10971065 get_uptime = True ,
1098- func = functor ,
1066+ func = status_wrapper ,
10991067 args = (name , args ),
11001068 )
11011069 reporting .flush_events ()
0 commit comments